installer.nsi 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. !include "MUI2.nsh"
  2. !include "nsDialogs.nsh"
  3. !include "FileFunc.nsh"
  4. Name "Netdata"
  5. Outfile "netdata-installer-x64.exe"
  6. InstallDir "$PROGRAMFILES\Netdata"
  7. RequestExecutionLevel admin
  8. !define MUI_ICON "NetdataWhite.ico"
  9. !define MUI_UNICON "NetdataWhite.ico"
  10. !define ND_UININSTALL_REG "Software\Microsoft\Windows\CurrentVersion\Uninstall\Netdata"
  11. !define MUI_ABORTWARNING
  12. !define MUI_UNABORTWARNING
  13. !insertmacro MUI_PAGE_WELCOME
  14. !insertmacro MUI_PAGE_LICENSE "C:\msys64\cloud.txt"
  15. !insertmacro MUI_PAGE_LICENSE "C:\msys64\gpl-3.0.txt"
  16. !insertmacro MUI_PAGE_DIRECTORY
  17. Page Custom NetdataConfigPage NetdataConfigLeave
  18. !insertmacro MUI_PAGE_INSTFILES
  19. !insertmacro MUI_PAGE_FINISH
  20. !insertmacro MUI_UNPAGE_CONFIRM
  21. !insertmacro MUI_UNPAGE_INSTFILES
  22. !insertmacro MUI_UNPAGE_FINISH
  23. !insertmacro MUI_LANGUAGE "English"
  24. !define INSTALLERLOCKFILEGUID "f787d5ef-5c41-4dc0-a115-a1fb654fad1c"
  25. # https://nsis.sourceforge.io/Allow_only_one_installer_instance
  26. !macro SingleInstanceFile
  27. !if "${NSIS_PTR_SIZE}" > 4
  28. !include "Util.nsh"
  29. !else ifndef IntPtrCmp
  30. !define IntPtrCmp IntCmp
  31. !endif
  32. !ifndef NSIS_PTR_SIZE & SYSTYPE_PTR
  33. !define SYSTYPE_PTR i ; NSIS v2.x
  34. !else
  35. !define /ifndef SYSTYPE_PTR p ; NSIS v3.0+
  36. !endif
  37. !if "${NSIS_CHAR_SIZE}" < 2
  38. Push "$TEMP\${INSTALLERLOCKFILEGUID}.lock"
  39. !else
  40. Push "$APPDATA\${INSTALLERLOCKFILEGUID}.lock"
  41. !endif
  42. System::Call 'KERNEL32::CreateFile(ts,i0x40000000,i0,${SYSTYPE_PTR}0,i4,i0x04000000,${SYSTYPE_PTR}0)${SYSTYPE_PTR}.r0'
  43. ${IntPtrCmp} $0 -1 "" launch launch
  44. System::Call 'kernel32::AttachConsole(i -1)i.r0'
  45. ${If} $0 != 0
  46. System::Call 'kernel32::GetStdHandle(i -11)i.r0'
  47. FileWrite $0 "The installer is already running.$\r$\n"
  48. ${EndIf}
  49. Quit
  50. launch:
  51. !macroend
  52. var hCtrlButton
  53. var hStartMsys
  54. var startMsys
  55. var hCloudURL
  56. var cloudURL
  57. var hCloudToken
  58. var cloudToken
  59. var hCloudRooms
  60. var cloudRooms
  61. var hProxy
  62. var proxy
  63. var hInsecure
  64. var insecure
  65. var accepted
  66. var avoidClaim
  67. Function .onInit
  68. !insertmacro SingleInstanceFile
  69. nsExec::ExecToLog '$SYSDIR\sc.exe stop Netdata'
  70. pop $0
  71. ${If} $0 == 0
  72. nsExec::ExecToLog '$SYSDIR\sc.exe delete Netdata'
  73. pop $0
  74. ${EndIf}
  75. StrCpy $startMsys ${BST_UNCHECKED}
  76. StrCpy $insecure ${BST_UNCHECKED}
  77. StrCpy $avoidClaim ${BST_UNCHECKED}
  78. StrCpy $accepted ${BST_UNCHECKED}
  79. ${GetParameters} $R0
  80. ${GetOptions} $R0 "/s" $0
  81. IfErrors +2 0
  82. SetSilent silent
  83. ClearErrors
  84. ${GetOptions} $R0 "/t" $0
  85. IfErrors +2 0
  86. StrCpy $startMsys ${BST_CHECKED}
  87. ClearErrors
  88. ${GetOptions} $R0 "/i" $0
  89. IfErrors +2 0
  90. StrCpy $insecure ${BST_CHECKED}
  91. ClearErrors
  92. ${GetOptions} $R0 "/a" $0
  93. IfErrors +2 0
  94. StrCpy $accepted ${BST_CHECKED}
  95. ClearErrors
  96. ${GetOptions} $R0 "/token=" $0
  97. IfErrors +2 0
  98. StrCpy $cloudToken $0
  99. ClearErrors
  100. ${GetOptions} $R0 "/rooms=" $0
  101. IfErrors +2 0
  102. StrCpy $cloudRooms $0
  103. ClearErrors
  104. ${GetOptions} $R0 "/proxy=" $0
  105. IfErrors +2 0
  106. StrCpy $proxy $0
  107. ClearErrors
  108. IfSilent checklicense goahead
  109. checklicense:
  110. ${If} $accepted == ${BST_UNCHECKED}
  111. System::Call 'kernel32::AttachConsole(i -1)i.r0'
  112. ${If} $0 != 0
  113. System::Call 'kernel32::GetStdHandle(i -11)i.r0'
  114. FileWrite $0 "You must accept the licenses (/A) to continue.$\r$\n"
  115. ${EndIf}
  116. Quit
  117. ${EndIf}
  118. goahead:
  119. FunctionEnd
  120. Function un.onInit
  121. !insertmacro SingleInstanceFile
  122. FunctionEnd
  123. Function ShowHelp
  124. Pop $0
  125. MessageBox MB_ICONQUESTION|MB_OK "$\"Cloud URL$\" The Netdata Cloud base URL.$\n$\n$\"Proxy URL$\" set the proxy server address to use if your network requires one.$\n$\n$\"Insecure connection$\" disable verification of the server's certificate chain and host name.$\n$\n$\"Open Terminal$\" open MSYS2 terminal to run additional commands after installation." IDOK endHelp
  126. endHelp:
  127. FunctionEnd
  128. Function NetdataConfigPage
  129. !insertmacro MUI_HEADER_TEXT "Netdata configuration" "Connect your Agent to your Netdata Cloud Space"
  130. nsDialogs::Create 1018
  131. Pop $0
  132. ${If} $0 == error
  133. Abort
  134. ${EndIf}
  135. IfFileExists "$INSTDIR\etc\netdata\claim.conf" NotNeeded
  136. ${NSD_CreateLabel} 0 0 100% 12u "Enter your Space's Claim Token and the Room IDs where you want to add the Agent."
  137. ${NSD_CreateLabel} 0 12% 100% 12u "If no Room IDs are specified, the Agent will be added to the $\"All nodes$\" Room."
  138. ${NSD_CreateLabel} 0 30% 20% 10% "Claim Token"
  139. Pop $0
  140. ${NSD_CreateText} 21% 30% 79% 10% ""
  141. Pop $hCloudToken
  142. ${NSD_CreateLabel} 0 45% 20% 10% "Room ID(s)"
  143. Pop $0
  144. ${NSD_CreateText} 21% 45% 79% 10% ""
  145. Pop $hCloudRooms
  146. ${NSD_CreateLabel} 0 60% 20% 10% "Proxy URL"
  147. Pop $0
  148. ${NSD_CreateText} 21% 60% 79% 10% ""
  149. Pop $hProxy
  150. ${NSD_CreateLabel} 0 75% 20% 10% "Cloud URL"
  151. Pop $0
  152. ${NSD_CreateText} 21% 75% 79% 10% "https://app.netdata.cloud"
  153. Pop $hCloudURL
  154. ${NSD_CreateCheckbox} 0 92% 25% 10u "Insecure connection"
  155. Pop $hInsecure
  156. ${NSD_CreateCheckbox} 50% 92% 25% 10u "Open terminal"
  157. Pop $hStartMsys
  158. ${NSD_CreateButton} 90% 90% 30u 15u "&Help"
  159. Pop $hCtrlButton
  160. ${NSD_OnClick} $hCtrlButton ShowHelp
  161. Goto EndDialogDraw
  162. NotNeeded:
  163. StrCpy $avoidClaim ${BST_CHECKED}
  164. ${NSD_CreateLabel} 0 0 100% 12u "Your host has already been claimed. You can proceed with the update."
  165. EndDialogDraw:
  166. nsDialogs::Show
  167. FunctionEnd
  168. Function NetdataConfigLeave
  169. ${If} $avoidClaim == ${BST_UNCHECKED}
  170. ${NSD_GetText} $hCloudToken $cloudToken
  171. ${NSD_GetText} $hCloudURL $cloudURL
  172. ${NSD_GetText} $hCloudRooms $cloudRooms
  173. ${NSD_GetText} $hProxy $proxy
  174. ${NSD_GetState} $hStartMsys $startMsys
  175. ${NSD_GetState} $hInsecure $insecure
  176. ${EndIf}
  177. FunctionEnd
  178. Function NetdataUninstallRegistry
  179. ClearErrors
  180. WriteRegStr HKLM "${ND_UININSTALL_REG}" \
  181. "DisplayName" "Netdata - Real-time system monitoring."
  182. WriteRegStr HKLM "${ND_UININSTALL_REG}" \
  183. "DisplayIcon" "$INSTDIR\Uninstall.exe,0"
  184. WriteRegStr HKLM "${ND_UININSTALL_REG}" \
  185. "UninstallString" "$INSTDIR\Uninstall.exe"
  186. WriteRegStr HKLM "${ND_UININSTALL_REG}" \
  187. "RegOwner" "Netdata Inc."
  188. WriteRegStr HKLM "${ND_UININSTALL_REG}" \
  189. "RegCompany" "Netdata Inc."
  190. WriteRegStr HKLM "${ND_UININSTALL_REG}" \
  191. "Publisher" "Netdata Inc."
  192. WriteRegStr HKLM "${ND_UININSTALL_REG}" \
  193. "HelpLink" "https://learn.netdata.cloud/"
  194. WriteRegStr HKLM "${ND_UININSTALL_REG}" \
  195. "URLInfoAbout" "https://www.netdata.cloud/"
  196. WriteRegStr HKLM "${ND_UININSTALL_REG}" \
  197. "DisplayVersion" "${CURRVERSION}"
  198. WriteRegStr HKLM "${ND_UININSTALL_REG}" \
  199. "VersionMajor" "${MAJORVERSION}"
  200. WriteRegStr HKLM "${ND_UININSTALL_REG}" \
  201. "VersionMinor" "${MINORVERSION}"
  202. IfErrors 0 +2
  203. MessageBox MB_ICONEXCLAMATION|MB_OK "Unable to create an entry in the Control Panel!" IDOK end
  204. ClearErrors
  205. ${GetSize} "$INSTDIR" "/S=0K" $0 $1 $2
  206. IntFmt $0 "0x%08X" $0
  207. WriteRegDWORD HKLM "${ND_UININSTALL_REG}" "EstimatedSize" "$0"
  208. IfErrors 0 +2
  209. MessageBox MB_ICONEXCLAMATION|MB_OK "Cannot estimate the installation size." IDOK end
  210. end:
  211. FunctionEnd
  212. Function InstallDLL
  213. ; Check if certutil is available
  214. nsExec::ExecToStack 'where certutil'
  215. Pop $R0
  216. StrCmp $R0 "" NoCertUtil FoundCertUtil
  217. NoCertUtil:
  218. DetailPrint "certutil not found, assuming files are different."
  219. Goto CopyDLL
  220. FoundCertUtil:
  221. ; Calculate hash of the existing DLL
  222. nsExec::ExecToStack 'certutil -hashfile "$SYSDIR\wevt_netdata.dll" MD5'
  223. Pop $R0
  224. ; Calculate hash of the new DLL
  225. nsExec::ExecToStack 'certutil -hashfile "$INSTDIR\usr\bin\wevt_netdata.dll" MD5'
  226. Pop $R1
  227. StrCmp $R0 $R1 SetPermissions
  228. CopyDLL:
  229. ClearErrors
  230. CopyFiles /SILENT "$INSTDIR\usr\bin\wevt_netdata.dll" "$SYSDIR"
  231. IfErrors RetryPrompt SetPermissions
  232. RetryPrompt:
  233. MessageBox MB_RETRYCANCEL|MB_ICONEXCLAMATION "Failed to copy wevt_netdata.dll probably because it is in use. Please close the Event Viewer (or other Event Log applications) and press Retry."
  234. StrCmp $R0 IDRETRY CopyDLL
  235. StrCmp $R0 IDCANCEL ExitInstall
  236. Goto End
  237. SetPermissions:
  238. nsExec::ExecToLog 'icacls "$SYSDIR\wevt_netdata.dll" /grant "NT SERVICE\EventLog":R'
  239. Goto End
  240. ExitInstall:
  241. Abort
  242. End:
  243. FunctionEnd
  244. Function InstallManifest
  245. IfFileExists "$INSTDIR\usr\bin\wevt_netdata_manifest.xml" CopyManifest End
  246. CopyManifest:
  247. ClearErrors
  248. CopyFiles /SILENT "$INSTDIR\usr\bin\wevt_netdata_manifest.xml" "$SYSDIR"
  249. IfErrors RetryPrompt InstallManifest
  250. RetryPrompt:
  251. MessageBox MB_RETRYCANCEL|MB_ICONEXCLAMATION "Failed to copy wevt_netdata_manifest.xml."
  252. StrCmp $R0 IDRETRY CopyManifest
  253. StrCmp $R0 IDCANCEL ExitInstall
  254. InstallManifest:
  255. nsExec::ExecToLog 'wevtutil im "$SYSDIR\wevt_netdata_manifest.xml" "/mf:$SYSDIR\wevt_netdata.dll" "/rf:$SYSDIR\wevt_netdata.dll"'
  256. Goto End
  257. ExitInstall:
  258. Abort
  259. End:
  260. FunctionEnd
  261. Section "Install Netdata"
  262. SetOutPath $INSTDIR
  263. SetCompress off
  264. File /r "C:\msys64\opt\netdata\*.*"
  265. ClearErrors
  266. nsExec::ExecToLog '$SYSDIR\sc.exe create Netdata binPath= "$INSTDIR\usr\bin\netdata.exe" start= delayed-auto'
  267. pop $0
  268. ${If} $0 != 0
  269. DetailPrint "Warning: Failed to create Netdata service."
  270. ${EndIf}
  271. ClearErrors
  272. nsExec::ExecToLog '$SYSDIR\sc.exe description Netdata "Real-time system monitoring service"'
  273. pop $0
  274. ${If} $0 != 0
  275. DetailPrint "Warning: Failed to add Netdata service description."
  276. ${EndIf}
  277. WriteUninstaller "$INSTDIR\Uninstall.exe"
  278. Call NetdataUninstallRegistry
  279. Call InstallDLL
  280. Call InstallManifest
  281. StrLen $0 $cloudToken
  282. StrLen $1 $cloudRooms
  283. ${If} $0 == 0
  284. ${OrIf} $1 == 0
  285. Goto runCmds
  286. ${EndIf}
  287. ${If} $0 == 135
  288. ${AndIf} $1 >= 36
  289. nsExec::ExecToLog '$INSTDIR\usr\bin\NetdataClaim.exe /T $cloudToken /R $cloudRooms /P $proxy /I $insecure /U $cloudURL'
  290. pop $0
  291. ${Else}
  292. MessageBox MB_OK "The Cloud information does not have the expected length."
  293. ${EndIf}
  294. runCmds:
  295. ClearErrors
  296. nsExec::ExecToLog '$SYSDIR\sc.exe start Netdata'
  297. pop $0
  298. ${If} $0 != 0
  299. MessageBox MB_OK "Warning: Failed to start Netdata service."
  300. ${EndIf}
  301. ${If} $startMsys == ${BST_CHECKED}
  302. nsExec::ExecToLog '$INSTDIR\msys2.exe'
  303. pop $0
  304. ${EndIf}
  305. SectionEnd
  306. Section "Uninstall"
  307. ClearErrors
  308. nsExec::ExecToLog '$SYSDIR\sc.exe stop Netdata'
  309. pop $0
  310. ${If} $0 != 0
  311. DetailPrint "Warning: Failed to stop Netdata service."
  312. ${EndIf}
  313. ClearErrors
  314. nsExec::ExecToLog '$SYSDIR\sc.exe delete Netdata'
  315. pop $0
  316. ${If} $0 != 0
  317. DetailPrint "Warning: Failed to delete Netdata service."
  318. ${EndIf}
  319. ; Check if the manifest exists before uninstalling it
  320. IfFileExists "$SYSDIR\wevt_netdata_manifest.xml" ManifestExistsForUninstall ManifestNotExistsForUninstall
  321. ManifestExistsForUninstall:
  322. nsExec::ExecToLog 'wevtutil um "$SYSDIR\wevt_netdata_manifest.xml"'
  323. pop $0
  324. ${If} $0 != 0
  325. DetailPrint "Warning: Failed to uninstall the event manifest."
  326. ${EndIf}
  327. Delete "$SYSDIR\wevt_netdata_manifest.xml"
  328. Delete "$SYSDIR\wevt_netdata.dll"
  329. Goto DoneUninstall
  330. ManifestNotExistsForUninstall:
  331. DetailPrint "Manifest not found, skipping manifest uninstall."
  332. DoneUninstall:
  333. ; Remove files
  334. SetOutPath "$PROGRAMFILES"
  335. RMDir /r /REBOOTOK "$INSTDIR"
  336. DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Netdata"
  337. SectionEnd