From 03a2cbc1c70a4320722bc19b8050becea3ae67b5 Mon Sep 17 00:00:00 2001 From: Marco Pfatschbacher Date: Wed, 28 Sep 2022 16:50:05 +0200 Subject: [PATCH 1/4] Add upgrade support and improve windows installer The installer can now also upgrade existing sidcar installations. It detects this by the presence of an existing sidecar.yml config. In upgrade mode it skip the configuration page. Additionally the installer will now also register the sidecar service automatically. Making this extra command line step obsolete. For debugging purposes, we write a installerlog.txt file now. Improve welcome message test to also show the version and mention the autmatic upgrade mode. Only replace the winlogbeat and filebeat collector binaries AFTER we stopped the sidecar. Otherwise the files might still be locked and the upgrade fails. --- dist/recipe.nsi | 70 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 13 deletions(-) diff --git a/dist/recipe.nsi b/dist/recipe.nsi index 42ae3ba..9ac27ec 100644 --- a/dist/recipe.nsi +++ b/dist/recipe.nsi @@ -31,8 +31,8 @@ !searchreplace SUFFIX '${VERSION_SUFFIX}' "-" "." OutFile "pkg/graylog_sidecar_installer_${VERSION}-${REVISION}${SUFFIX}.exe" RequestExecutionLevel admin ;Require admin rights - ShowInstDetails "nevershow" - ShowUninstDetails "nevershow" + ShowInstDetails "show" + ShowUninstDetails "show" ; Variables Var Params @@ -54,12 +54,19 @@ Var Dialog Var Label Var GraylogDir + Var IsUpgrade + Var LogFile ;-------------------------------- ;Modern UI Configuration !define MUI_ICON "graylog.ico" + !define MUI_WELCOMEPAGE_TITLE "Graylog Sidecar ${VERSION}-${REVISION}${SUFFIX} Installation / Upgrade" + !define MUI_WELCOMEPAGE_TEXT "This setup is gonna guide you through the installation / upgrade of the Graylog Sidecar.\r\n\r\n \ + If an already configured Sidecar is detected ('sidecar.yml' present), it will perform an upgrade.\r\n \r\n\ + Click Next to continue." + !insertmacro MUI_PAGE_WELCOME !insertmacro MUI_PAGE_LICENSE "../LICENSE" !insertmacro MUI_UNPAGE_WELCOME @@ -83,6 +90,12 @@ !insertmacro WordFind !insertmacro WordFind2X + !macro _LogWrite text + FileWrite $LogFile '${text}$\r$\n' + !macroend + !define LogWrite "!insertmacro _LogWrite" + + !macro Check_X64 ${If} ${RunningX64} SetRegView 64 @@ -92,8 +105,20 @@ Strcpy $GraylogDir "$PROGRAMFILES32\Graylog" ${EndIf} Strcpy $INSTDIR "$GraylogDir\sidecar" + CreateDirectory $INSTDIR !macroend + !macro Check_Upgrade + ${If} ${FileExists} "$INSTDIR\sidecar.yml" + Strcpy $IsUpgrade "true" + ${LogWrite} "Existing installation detected. Performing upgrade." + ${Else} + Strcpy $IsUpgrade "false" + ${LogWrite} "No previous installation detected. Running installation mode." + ${EndIf} + !macroend + + ;-------------------------------- ;Data @@ -109,14 +134,6 @@ Section "Install" CreateDirectory "$INSTDIR\module" SetOutPath "$INSTDIR" - ${If} ${RunningX64} - File "collectors/winlogbeat/windows/x86_64/winlogbeat.exe" - File "collectors/filebeat/windows/x86_64/filebeat.exe" - ${Else} - File "collectors/winlogbeat/windows/x86/winlogbeat.exe" - File "collectors/filebeat/windows/x86/filebeat.exe" - ${EndIf} - SetOverwrite off File /oname=sidecar.yml "../sidecar-windows-example.yml" SetOverwrite on @@ -128,7 +145,8 @@ Section "Install" !insertmacro _IfKeyExists HKLM "SYSTEM\CurrentControlSet\Services" "graylog-sidecar" Pop $R0 ${If} $R0 = 1 - ExecWait '"$INSTDIR\graylog-sidecar.exe" -service stop' + ExecWait '"$INSTDIR\graylog-sidecar.exe" -service stop' $0 + ${LogWrite} "Stopping existing Sidecar Service: $0" ${EndIf} ${If} ${RunningX64} @@ -137,9 +155,26 @@ Section "Install" File /oname=graylog-sidecar.exe "../build/${VERSION}/windows/386/graylog-sidecar.exe" ${EndIf} + ; Install beats collectors + ${If} ${RunningX64} + File "collectors/winlogbeat/windows/x86_64/winlogbeat.exe" + File "collectors/filebeat/windows/x86_64/filebeat.exe" + ${Else} + File "collectors/winlogbeat/windows/x86/winlogbeat.exe" + File "collectors/filebeat/windows/x86/filebeat.exe" + ${EndIf} + ;When we stop the Sidecar service we also turn it on again ${If} $R0 = 1 - ExecWait '"$INSTDIR\graylog-sidecar.exe" -service start' + ExecWait '"$INSTDIR\graylog-sidecar.exe" -service start' $0 + ${LogWrite} "Starting existing Sidecar Service: $0" + ${EndIf} + + ${If} $IsUpgrade != 'true' + ExecWait '"$INSTDIR\graylog-sidecar.exe" -service install' + ${LogWrite} "Installing new Sidecar Service: $0" + ExecWait '"$INSTDIR\graylog-sidecar.exe" -service start' $0 + ${LogWrite} "Starting new Sidecar Service: $0" ${EndIf} WriteUninstaller "$INSTDIR\uninstall.exe" @@ -226,6 +261,7 @@ Section "Post" !insertmacro _ReplaceInFile "$INSTDIR\sidecar.yml" "" $SendStatus !insertmacro _ReplaceInFile "$INSTDIR\sidecar.yml" "" $ApiToken + FileClose $LogFile SectionEnd ;-------------------------------- @@ -254,13 +290,17 @@ SectionEnd ;Functions Function .onInit + !insertmacro Check_X64 + + FileOpen $LogFile "$INSTDIR\installerlog.txt" w + ; check admin rights Call CheckAdmin ; check concurrent un/installations Call CheckConcurrent - !insertmacro Check_X64 + !insertmacro Check_Upgrade FunctionEnd Function un.oninit @@ -276,6 +316,10 @@ FunctionEnd Function nsDialogsPage + ${If} $IsUpgrade == 'true' + Abort + ${EndIf} + nsDialogs::Create 1018 From e2b68179c62d3fddf4c25eb8f83c3b3e001ed708 Mon Sep 17 00:00:00 2001 From: Marco Pfatschbacher Date: Wed, 28 Sep 2022 17:18:57 +0200 Subject: [PATCH 2/4] Suppport -NODEID param for silent installer Also fix default node-id file location for 32-bit installs --- dist/recipe.nsi | 11 +++++++++++ sidecar-windows-example.yml | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/dist/recipe.nsi b/dist/recipe.nsi index 9ac27ec..2512886 100644 --- a/dist/recipe.nsi +++ b/dist/recipe.nsi @@ -50,6 +50,8 @@ Var TlsSkipVerify Var ParamSendStatus Var ParamApiToken + Var ParamNodeId + Var NodeId Var SendStatus Var Dialog Var Label @@ -220,6 +222,7 @@ Section "Post" ${GetOptions} $Params " -TLS_SKIP_VERIFY=" $ParamTlsSkipVerify ${GetOptions} $Params " -SEND_STATUS=" $ParamSendStatus ${GetOptions} $Params " -APITOKEN=" $ParamApiToken + ${GetOptions} $Params " -NODEID=" $ParamNodeId ${If} $ParamServerUrl != "" StrCpy $ServerUrl $ParamServerUrl @@ -239,6 +242,9 @@ Section "Post" ${If} $ParamApiToken != "" StrCpy $ApiToken $ParamApiToken ${EndIf} + ${If} $ParamNodeId != "" + StrCpy $NodeId $ParamNodeId + ${EndIf} ; set defaults ${If} $ServerUrl == "" @@ -253,6 +259,10 @@ Section "Post" ${If} $SendStatus == "" StrCpy $SendStatus "true" ${EndIf} + ${If} $NodeId == "" + ;sidecar.yml needs double escapes + ${WordReplace} "file:$INSTDIR\node-id" "\" "\\" "+" $NodeId + ${EndIf} !insertmacro _ReplaceInFile "$INSTDIR\sidecar.yml" "" $ServerUrl !insertmacro _ReplaceInFile "$INSTDIR\sidecar.yml" "" $NodeName @@ -260,6 +270,7 @@ Section "Post" !insertmacro _ReplaceInFile "$INSTDIR\sidecar.yml" "" $TlsSkipVerify !insertmacro _ReplaceInFile "$INSTDIR\sidecar.yml" "" $SendStatus !insertmacro _ReplaceInFile "$INSTDIR\sidecar.yml" "" $ApiToken + !insertmacro _ReplaceInFile "$INSTDIR\sidecar.yml" "" $NodeId FileClose $LogFile SectionEnd diff --git a/sidecar-windows-example.yml b/sidecar-windows-example.yml index 263520f..7a7623a 100644 --- a/sidecar-windows-example.yml +++ b/sidecar-windows-example.yml @@ -16,7 +16,7 @@ server_api_token: "" # ATTENTION: Every sidecar instance needs a unique ID! # # Default: "file:C:\\Program Files\\Graylog\\sidecar\\node-id" -node_id: "file:C:\\Program Files\\Graylog\\sidecar\\node-id" +node_id: "" # The node name of the sidecar. If this is empty, the sidecar will use the # hostname of the host it is running on. From 002d201575e4b3032d32dfef24b962538a7d636c Mon Sep 17 00:00:00 2001 From: Marco Pfatschbacher Date: Thu, 29 Sep 2022 13:41:04 +0200 Subject: [PATCH 3/4] Fix service start after installation We need to start the service AFTER we've written the final sidecar.yml Also using nsExec allows us to see the output of the service command. --- dist/recipe.nsi | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/dist/recipe.nsi b/dist/recipe.nsi index 2512886..ef4ed5e 100644 --- a/dist/recipe.nsi +++ b/dist/recipe.nsi @@ -147,8 +147,10 @@ Section "Install" !insertmacro _IfKeyExists HKLM "SYSTEM\CurrentControlSet\Services" "graylog-sidecar" Pop $R0 ${If} $R0 = 1 - ExecWait '"$INSTDIR\graylog-sidecar.exe" -service stop' $0 - ${LogWrite} "Stopping existing Sidecar Service: $0" + nsExec::ExecToStack '"$INSTDIR\graylog-sidecar.exe" -service stop' + Pop $0 + Pop $1 + ${LogWrite} "Stopping existing Sidecar Service: [exit $0] Stdout: $1" ${EndIf} ${If} ${RunningX64} @@ -168,15 +170,10 @@ Section "Install" ;When we stop the Sidecar service we also turn it on again ${If} $R0 = 1 - ExecWait '"$INSTDIR\graylog-sidecar.exe" -service start' $0 - ${LogWrite} "Starting existing Sidecar Service: $0" - ${EndIf} - - ${If} $IsUpgrade != 'true' - ExecWait '"$INSTDIR\graylog-sidecar.exe" -service install' - ${LogWrite} "Installing new Sidecar Service: $0" - ExecWait '"$INSTDIR\graylog-sidecar.exe" -service start' $0 - ${LogWrite} "Starting new Sidecar Service: $0" + nsExec::ExecToStack '"$INSTDIR\graylog-sidecar.exe" -service start' + Pop $0 + Pop $1 + ${LogWrite} "Restarting existing Sidecar Service: [exit $0] Stdout: $1" ${EndIf} WriteUninstaller "$INSTDIR\uninstall.exe" @@ -272,6 +269,19 @@ Section "Post" !insertmacro _ReplaceInFile "$INSTDIR\sidecar.yml" "" $ApiToken !insertmacro _ReplaceInFile "$INSTDIR\sidecar.yml" "" $NodeId + ;Install sidecar service + ${If} $IsUpgrade == 'false' + nsExec::ExecToStack '"$INSTDIR\graylog-sidecar.exe" -service install' + Pop $0 + Pop $1 + ${LogWrite} "Installing new Sidecar Service: [exit $0] Stdout: $1" + + nsExec::ExecToStack '"$INSTDIR\graylog-sidecar.exe" -service start' + Pop $0 + Pop $1 + ${LogWrite} "Starting new Sidecar Service: [exit $0] Stdout: $1" + ${EndIf} + FileClose $LogFile SectionEnd From a92503d0cd8d4f1e41f57a473d1c00f390f45b1a Mon Sep 17 00:00:00 2001 From: Marco Pfatschbacher Date: Thu, 29 Sep 2022 15:29:28 +0200 Subject: [PATCH 4/4] Add console logging support --- dist/recipe.nsi | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/dist/recipe.nsi b/dist/recipe.nsi index ef4ed5e..3d96327 100644 --- a/dist/recipe.nsi +++ b/dist/recipe.nsi @@ -58,6 +58,7 @@ Var GraylogDir Var IsUpgrade Var LogFile + Var LogMsgText ;-------------------------------- @@ -91,9 +92,15 @@ !insertmacro MUI_LANGUAGE "English" !insertmacro WordFind !insertmacro WordFind2X + !insertmacro GetTime !macro _LogWrite text - FileWrite $LogFile '${text}$\r$\n' + StrCpy $LogMsgText "${text}" + ${GetTime} "" "L" $0 $1 $2 $3 $4 $5 $6 + FileWrite $LogFile '$2$1$0$4$5$6: $LogMsgText$\r$\n' + System::Call 'kernel32::GetStdHandle(i -11)i.r9' + System::Call 'kernel32::AttachConsole(i -1)' + FileWrite $9 "$LogMsgText$\r$\n" !macroend !define LogWrite "!insertmacro _LogWrite" @@ -282,6 +289,7 @@ Section "Post" ${LogWrite} "Starting new Sidecar Service: [exit $0] Stdout: $1" ${EndIf} + ${LogWrite} "Installer/Upgrader finished." FileClose $LogFile SectionEnd @@ -315,6 +323,9 @@ Function .onInit FileOpen $LogFile "$INSTDIR\installerlog.txt" w + ${LogWrite} "$\r$\n" ;Powershell seems to swallow the first line + ${LogWrite} "Starting Sidecar ${VERSION}-${REVISION}${SUFFIX} installer/upgrader." + ; check admin rights Call CheckAdmin