summaryrefslogtreecommitdiffstats
path: root/packaging/windows/installer.nsi
blob: a462d49970694676becd08394eefb4ef32134ecc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
!include "MUI2.nsh"
!include "nsDialogs.nsh"
!include "FileFunc.nsh"

Name "Netdata"
Outfile "netdata-installer-x64.exe"
InstallDir "$PROGRAMFILES\Netdata"
RequestExecutionLevel admin

!define MUI_ICON "NetdataWhite.ico"
!define MUI_UNICON "NetdataWhite.ico"

!define ND_UININSTALL_REG "Software\Microsoft\Windows\CurrentVersion\Uninstall\Netdata"

!define MUI_ABORTWARNING
!define MUI_UNABORTWARNING

!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE "C:\msys64\cloud.txt"
!insertmacro MUI_PAGE_LICENSE "C:\msys64\gpl-3.0.txt"
!insertmacro MUI_PAGE_DIRECTORY
Page Custom NetdataConfigPage NetdataConfigLeave
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_UNPAGE_FINISH

!insertmacro MUI_LANGUAGE "English"

!define INSTALLERLOCKFILEGUID "f787d5ef-5c41-4dc0-a115-a1fb654fad1c"

# https://nsis.sourceforge.io/Allow_only_one_installer_instance
!macro SingleInstanceFile
    !if "${NSIS_PTR_SIZE}" > 4
    !include "Util.nsh"
    !else ifndef IntPtrCmp
    !define IntPtrCmp IntCmp
    !endif

    !ifndef NSIS_PTR_SIZE & SYSTYPE_PTR
    !define SYSTYPE_PTR i ; NSIS v2.x
    !else
    !define /ifndef SYSTYPE_PTR p ; NSIS v3.0+
    !endif

    !if "${NSIS_CHAR_SIZE}" < 2
    Push "$TEMP\${INSTALLERLOCKFILEGUID}.lock"
    !else
    Push "$APPDATA\${INSTALLERLOCKFILEGUID}.lock"
    !endif

    System::Call 'KERNEL32::CreateFile(ts,i0x40000000,i0,${SYSTYPE_PTR}0,i4,i0x04000000,${SYSTYPE_PTR}0)${SYSTYPE_PTR}.r0'
    ${IntPtrCmp} $0 -1 "" launch launch
        System::Call 'kernel32::AttachConsole(i -1)i.r0'
        ${If} $0 != 0
            System::Call 'kernel32::GetStdHandle(i -11)i.r0'
            FileWrite $0 "The installer is already running.$\r$\n"
        ${EndIf}
	Quit
    launch:
!macroend

var hCtrlButton
var hStartMsys
var startMsys

var hCloudURL
var cloudURL
var hCloudToken
var cloudToken
var hCloudRooms
var cloudRooms
var hProxy
var proxy
var hInsecure
var insecure
var accepted

var avoidClaim

Function .onInit
        !insertmacro SingleInstanceFile

        nsExec::ExecToLog '$SYSDIR\sc.exe stop Netdata'
        pop $0
        ${If} $0 == 0
            nsExec::ExecToLog '$SYSDIR\sc.exe delete Netdata'
            pop $0
        ${EndIf}

        StrCpy $startMsys ${BST_UNCHECKED}
        StrCpy $insecure ${BST_UNCHECKED}
        StrCpy $avoidClaim ${BST_UNCHECKED}
        StrCpy $accepted ${BST_UNCHECKED}
        
        ${GetParameters} $R0
        ${GetOptions} $R0 "/s" $0
        IfErrors +2 0
            SetSilent silent
        ClearErrors

        ${GetOptions} $R0 "/t" $0
        IfErrors +2 0
            StrCpy $startMsys ${BST_CHECKED}
        ClearErrors

        ${GetOptions} $R0 "/i" $0
        IfErrors +2 0
            StrCpy $insecure ${BST_CHECKED}
        ClearErrors

        ${GetOptions} $R0 "/a" $0
        IfErrors +2 0
            StrCpy $accepted ${BST_CHECKED}
        ClearErrors

        ${GetOptions} $R0 "/token=" $0
        IfErrors +2 0
            StrCpy $cloudToken $0
        ClearErrors

        ${GetOptions} $R0 "/rooms=" $0
        IfErrors +2 0
            StrCpy $cloudRooms $0
        ClearErrors

        ${GetOptions} $R0 "/proxy=" $0
        IfErrors +2 0
            StrCpy $proxy $0
        ClearErrors

        IfSilent checklicense goahead
        checklicense:
                ${If} $accepted == ${BST_UNCHECKED}
                    System::Call 'kernel32::AttachConsole(i -1)i.r0'
                    ${If} $0 != 0
                        System::Call 'kernel32::GetStdHandle(i -11)i.r0'
                        FileWrite $0 "You must accept the licenses (/A) to continue.$\r$\n"
                    ${EndIf}
                    Quit
                ${EndIf}
        goahead:
FunctionEnd

Function un.onInit
!insertmacro SingleInstanceFile
FunctionEnd

Function ShowHelp
Pop $0
        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
        endHelp:
FunctionEnd

Function NetdataConfigPage
        !insertmacro MUI_HEADER_TEXT "Netdata configuration" "Connect your Agent to your Netdata Cloud Space"

        nsDialogs::Create 1018
        Pop $0
        ${If} $0 == error
            Abort
        ${EndIf}

        IfFileExists "$INSTDIR\etc\netdata\claim.conf" NotNeeded

        ${NSD_CreateLabel} 0 0 100% 12u "Enter your Space's Claim Token and the Room IDs where you want to add the Agent."
        ${NSD_CreateLabel} 0 12% 100% 12u "If no Room IDs are specified, the Agent will be added to the $\"All nodes$\" Room."

        ${NSD_CreateLabel} 0 30% 20% 10% "Claim Token"
        Pop $0
        ${NSD_CreateText} 21% 30% 79% 10% ""
        Pop $hCloudToken

        ${NSD_CreateLabel} 0 45% 20% 10% "Room ID(s)"
        Pop $0
        ${NSD_CreateText} 21% 45% 79% 10% ""
        Pop $hCloudRooms

        ${NSD_CreateLabel} 0 60% 20% 10% "Proxy URL"
        Pop $0
        ${NSD_CreateText} 21% 60% 79% 10% ""
        Pop $hProxy

        ${NSD_CreateLabel} 0 75% 20% 10% "Cloud URL"
        Pop $0
        ${NSD_CreateText} 21% 75% 79% 10% "https://app.netdata.cloud"
        Pop $hCloudURL

        ${NSD_CreateCheckbox} 0 92% 25% 10u "Insecure connection"
        Pop $hInsecure

        ${NSD_CreateCheckbox} 50% 92% 25% 10u "Open terminal"
        Pop $hStartMsys

        ${NSD_CreateButton} 90% 90% 30u 15u "&Help"
        Pop $hCtrlButton
        ${NSD_OnClick} $hCtrlButton ShowHelp

        Goto EndDialogDraw

        NotNeeded:
        StrCpy $avoidClaim ${BST_CHECKED}
        ${NSD_CreateLabel} 0 0 100% 12u "Your host has already been claimed. You can proceed with the update."

        EndDialogDraw:
        nsDialogs::Show
FunctionEnd

Function NetdataConfigLeave
        ${If} $avoidClaim == ${BST_UNCHECKED}
                ${NSD_GetText} $hCloudToken $cloudToken
                ${NSD_GetText} $hCloudURL $cloudURL
                ${NSD_GetText} $hCloudRooms $cloudRooms
                ${NSD_GetText} $hProxy $proxy
                ${NSD_GetState} $hStartMsys $startMsys
                ${NSD_GetState} $hInsecure $insecure
        ${EndIf}
FunctionEnd

Function NetdataUninstallRegistry
        ClearErrors
        WriteRegStr HKLM "${ND_UININSTALL_REG}" \
                         "DisplayName" "Netdata - Real-time system monitoring."
        WriteRegStr HKLM "${ND_UININSTALL_REG}" \
                         "DisplayIcon" "$INSTDIR\Uninstall.exe,0"
        WriteRegStr HKLM "${ND_UININSTALL_REG}" \
                         "UninstallString" "$INSTDIR\Uninstall.exe"
        WriteRegStr HKLM "${ND_UININSTALL_REG}" \
                         "RegOwner" "Netdata Inc."
        WriteRegStr HKLM "${ND_UININSTALL_REG}" \
                         "RegCompany" "Netdata Inc."
        WriteRegStr HKLM "${ND_UININSTALL_REG}" \
                         "Publisher" "Netdata Inc."
        WriteRegStr HKLM "${ND_UININSTALL_REG}" \
                         "HelpLink" "https://learn.netdata.cloud/"
        WriteRegStr HKLM "${ND_UININSTALL_REG}" \
                         "URLInfoAbout" "https://www.netdata.cloud/"
        WriteRegStr HKLM "${ND_UININSTALL_REG}" \
                         "DisplayVersion" "${CURRVERSION}"
        WriteRegStr HKLM "${ND_UININSTALL_REG}" \
                         "VersionMajor" "${MAJORVERSION}"
        WriteRegStr HKLM "${ND_UININSTALL_REG}" \
                         "VersionMinor" "${MINORVERSION}"

        IfErrors 0 +2
        MessageBox MB_ICONEXCLAMATION|MB_OK "Unable to create an entry in the Control Panel!" IDOK end

        ClearErrors
        ${GetSize} "$INSTDIR" "/S=0K" $0 $1 $2
        IntFmt $0 "0x%08X" $0
        WriteRegDWORD HKLM "${ND_UININSTALL_REG}" "EstimatedSize" "$0"

        IfErrors 0 +2
        MessageBox MB_ICONEXCLAMATION|MB_OK "Cannot estimate the installation size." IDOK end
        end:
FunctionEnd

Function InstallDLL
        ; Check if certutil is available
        nsExec::ExecToStack 'where certutil'
        Pop $R0
        StrCmp $R0 "" NoCertUtil FoundCertUtil

    NoCertUtil:
        DetailPrint "certutil not found, assuming files are different."
        Goto CopyDLL

    FoundCertUtil:
        ; Calculate hash of the existing DLL
        nsExec::ExecToStack 'certutil -hashfile "$SYSDIR\wevt_netdata.dll" MD5'
        Pop $R0

        ; Calculate hash of the new DLL
        nsExec::ExecToStack 'certutil -hashfile "$INSTDIR\usr\bin\wevt_netdata.dll" MD5'
        Pop $R1

        StrCmp $R0 $R1 SetPermissions

    CopyDLL:
        ClearErrors
        CopyFiles /SILENT "$INSTDIR\usr\bin\wevt_netdata.dll" "$SYSDIR"
        IfErrors RetryPrompt SetPermissions

    RetryPrompt:
        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."
        StrCmp $R0 IDRETRY CopyDLL
        StrCmp $R0 IDCANCEL ExitInstall

        Goto End

    SetPermissions:
        nsExec::ExecToLog 'icacls "$SYSDIR\wevt_netdata.dll" /grant "NT SERVICE\EventLog":R'
        Goto End

    ExitInstall:
        Abort

    End:
FunctionEnd

Function InstallManifest
    IfFileExists "$INSTDIR\usr\bin\wevt_netdata_manifest.xml" CopyManifest End

    CopyManifest:
        ClearErrors
        CopyFiles /SILENT "$INSTDIR\usr\bin\wevt_netdata_manifest.xml" "$SYSDIR"
        IfErrors RetryPrompt InstallManifest

    RetryPrompt:
        MessageBox MB_RETRYCANCEL|MB_ICONEXCLAMATION "Failed to copy wevt_netdata_manifest.xml."
        StrCmp $R0 IDRETRY CopyManifest
        StrCmp $R0 IDCANCEL ExitInstall

    InstallManifest:
        nsExec::ExecToLog 'wevtutil im "$SYSDIR\wevt_netdata_manifest.xml" "/mf:$SYSDIR\wevt_netdata.dll" "/rf:$SYSDIR\wevt_netdata.dll"'
        Goto End

    ExitInstall:
        Abort

    End:
FunctionEnd

Section "Install Netdata"
        SetOutPath $INSTDIR
        SetCompress off

        File /r "C:\msys64\opt\netdata\*.*"

        ClearErrors
        nsExec::ExecToLog '$SYSDIR\sc.exe create Netdata binPath= "$INSTDIR\usr\bin\netdata.exe" start= delayed-auto'
        pop $0
        ${If} $0 != 0
        DetailPrint "Warning: Failed to create Netdata service."
        ${EndIf}

        ClearErrors
        nsExec::ExecToLog '$SYSDIR\sc.exe description Netdata "Real-time system monitoring service"'
        pop $0
        ${If} $0 != 0
        DetailPrint "Warning: Failed to add Netdata service description."
        ${EndIf}

        WriteUninstaller "$INSTDIR\Uninstall.exe"

        Call NetdataUninstallRegistry
        Call InstallDLL
        Call InstallManifest

        StrLen $0 $cloudToken
        StrLen $1 $cloudRooms
        ${If} $0 == 0
        ${OrIf} $1 == 0
                Goto runCmds
        ${EndIf}

        ${If} $0 == 135
        ${AndIf} $1 >= 36
                nsExec::ExecToLog '$INSTDIR\usr\bin\NetdataClaim.exe /T $cloudToken /R $cloudRooms /P $proxy /I $insecure /U $cloudURL'
                pop $0
        ${Else}
                MessageBox MB_OK "The Cloud information does not have the expected length."
        ${EndIf}

        runCmds:
        ClearErrors
        nsExec::ExecToLog '$SYSDIR\sc.exe start Netdata'
        pop $0
        ${If} $0 != 0
	        MessageBox MB_OK "Warning: Failed to start Netdata service."
        ${EndIf}

        ${If} $startMsys == ${BST_CHECKED}
                nsExec::ExecToLog '$INSTDIR\msys2.exe'
                pop $0
        ${EndIf}
SectionEnd

Section "Uninstall"
	ClearErrors
        nsExec::ExecToLog '$SYSDIR\sc.exe stop Netdata'
        pop $0
        ${If} $0 != 0
	    DetailPrint "Warning: Failed to stop Netdata service."
        ${EndIf}

	ClearErrors
        nsExec::ExecToLog '$SYSDIR\sc.exe delete Netdata'
        pop $0
        ${If} $0 != 0
	    DetailPrint "Warning: Failed to delete Netdata service."
        ${EndIf}

        ; Check if the manifest exists before uninstalling it
        IfFileExists "$SYSDIR\wevt_netdata_manifest.xml" ManifestExistsForUninstall ManifestNotExistsForUninstall

ManifestExistsForUninstall:
        nsExec::ExecToLog 'wevtutil um "$SYSDIR\wevt_netdata_manifest.xml"'
        pop $0
        ${If} $0 != 0
            DetailPrint "Warning: Failed to uninstall the event manifest."
        ${EndIf}
        Delete "$SYSDIR\wevt_netdata_manifest.xml"
        Delete "$SYSDIR\wevt_netdata.dll"
        Goto DoneUninstall

ManifestNotExistsForUninstall:
        DetailPrint "Manifest not found, skipping manifest uninstall."

DoneUninstall:

        ; Remove files
        SetOutPath "$PROGRAMFILES"
        RMDir /r /REBOOTOK "$INSTDIR"

        DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Netdata"
SectionEnd