From 31f118f3f5021c8396c37e68d5957a928e9eb712 Mon Sep 17 00:00:00 2001 From: Daijiro Fukuda Date: Wed, 26 Jun 2024 19:33:38 +0900 Subject: [PATCH] msi: prevent launching Fluentd wrongly if the service is running Before: We can launch Fluentd by fluentd.bat even though fluentdwinsvc is running. Launching multiple Fluentd with the same config may cause inconsistency of the buffers or the pos files. After: We can't launch Fluentd by fluent.bat with the default config path if fluentdwinsvc is running. If some options are specified, we can execute fluentd.bat as before. Inspired by @kenhys's PR #622. Signed-off-by: Daijiro Fukuda Co-authored-by: Kentaro Hayashi --- fluent-package/msi/assets/fluentd.bat | 29 +++++++++++++++++++++- fluent-package/msi/install-test.ps1 | 35 +++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/fluent-package/msi/assets/fluentd.bat b/fluent-package/msi/assets/fluentd.bat index 061497626..d109115bf 100644 --- a/fluent-package/msi/assets/fluentd.bat +++ b/fluent-package/msi/assets/fluentd.bat @@ -13,13 +13,40 @@ set PATH=%FLUENT_PACKAGE_TOPDIR%;%PATH% set "FLUENT_CONF=%FLUENT_PACKAGE_TOPDIR%etc/fluent/fluentd.conf" set "FLUENT_PLUGIN=%FLUENT_PACKAGE_TOPDIR%etc/fluent/plugin" -setlocal +setlocal enabledelayedexpansion set "FLUENT_PACKAGE_VERSION=%FLUENT_PACKAGE_TOPDIR%bin/fluent-package-version.rb" +set NEED_DUPLICATE_LAUNCH_CHECK=1 +set HAS_SHORT_VERBOSE_OPTION=0 + for %%p in (%*) do ( if "%%p"=="--version" ( ruby "%FLUENT_PACKAGE_VERSION%" goto last ) + if "%%p"=="-c" set NEED_DUPLICATE_LAUNCH_CHECK=0 + if "%%p"=="--config" set NEED_DUPLICATE_LAUNCH_CHECK=0 + if "%%p"=="--dry-run" set NEED_DUPLICATE_LAUNCH_CHECK=0 + if "%%p"=="--reg-winsvc" set NEED_DUPLICATE_LAUNCH_CHECK=0 + if "%%p"=="--reg-winsvc-fluentdopt" set NEED_DUPLICATE_LAUNCH_CHECK=0 + if "%%p"=="-v" set HAS_SHORT_VERBOSE_OPTION=1 +) + +@rem Abort if the fluentdwinsvc service is running and the config path is not specified. +if %NEED_DUPLICATE_LAUNCH_CHECK% equ 1 ( + sc query fluentdwinsvc | findstr RUNNING > nul 2>&1 + if !ERRORLEVEL! equ 0 ( + echo Error: Can't start duplicate Fluentd instance with the default config. + if %HAS_SHORT_VERBOSE_OPTION% equ 1 ( + echo. + echo To take the version, please use '--version', not '-v' ^('--verbose'^). + ) + echo. + echo To start Fluentd, please do one of the following: + echo ^(Caution: Please be careful not to start multiple instances with the same config.^) + echo - Stop the Fluentd Windows service 'fluentdwinsvc'. + echo - Specify the config path explicitly by '-c' ^('--config'^). + exit /b 2 + ) ) endlocal diff --git a/fluent-package/msi/install-test.ps1 b/fluent-package/msi/install-test.ps1 index e5cb99a78..250edf133 100644 --- a/fluent-package/msi/install-test.ps1 +++ b/fluent-package/msi/install-test.ps1 @@ -41,6 +41,41 @@ Get-ChildItem "C:\\opt\\fluent\\*.log" | %{ } } +# Test: fluentd.bat +Start-Service fluentdwinsvc + +$proc = Start-Process "C:\\opt\\fluent\\fluentd.bat" -Wait -NoNewWindow -PassThru +if ($proc.ExitCode -ne 2) { + Write-Host "Failed to abort when already fluentdwinsvc service is running" + [Environment]::Exit(1) +} +Write-Host "Succeeded to abort when already fluentdwinsvc service is running" + +$proc = Start-Process "C:\\opt\\fluent\\fluentd.bat" -ArgumentList "--version" -Wait -NoNewWindow -PassThru +if ($proc.ExitCode -ne 0) { + Write-Host "Failed to take the version" + [Environment]::Exit(1) +} +Write-Host "Succeeded to take the version" + +$proc = Start-Process "C:\\opt\\fluent\\fluentd.bat" -ArgumentList "--dry-run" -Wait -NoNewWindow -PassThru +if ($proc.ExitCode -ne 0) { + Write-Host "Failed to dry-run" + [Environment]::Exit(1) +} +Write-Host "Succeeded to dry-run" + +$fluentdopt = "-c 'C:\opt\fluent\etc\fluent\fluentd.conf' -o 'C:\opt\fluent\fluentd.log' -v" +$proc = Start-Process "C:\\opt\\fluent\\fluentd.bat" -ArgumentList "--reg-winsvc-fluentdopt ""$fluentdopt""" -Wait -NoNewWindow -PassThru +$fluentdoptResult = Get-ItemPropertyValue -Path HKLM:\SYSTEM\CurrentControlSet\Services\fluentdwinsvc -Name fluentdopt +if ($proc.ExitCode -ne 0 -or $fluentdopt -ne $fluentdoptResult) { + Write-Host "Failed to register fluentdopt" + [Environment]::Exit(1) +} +Write-Host "Succeeded to register fluentdopt" + +# Test: Uninstall +Stop-Service fluentdwinsvc $msi -Match "fluent-package-([0-9\.]+)-.+\.msi" $name = "Fluent Package v" + $matches[1] Write-Host "Uninstalling ...${name}"