Skip to content

Commit

Permalink
msi: prevent launching Fluentd wrongly if the service is running
Browse files Browse the repository at this point in the history
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 `--config` (`-c`) or `--dry-run` are specified, we can execute
  fluentd.bat as before.

Inspired by @kenhys's PR fluent#622.

Signed-off-by: Daijiro Fukuda <[email protected]>
Co-authored-by: Kentaro Hayashi <[email protected]>
  • Loading branch information
daipom and kenhys committed Jun 25, 2024
1 parent ce3fbf3 commit e192963
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
26 changes: 26 additions & 0 deletions fluent-package/msi/assets/fluentd.bat
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,37 @@ set "FLUENT_PLUGIN=%FLUENT_PACKAGE_TOPDIR%etc/fluent/plugin"

setlocal
set "FLUENT_PACKAGE_VERSION=%FLUENT_PACKAGE_TOPDIR%bin/fluent-package-version.rb"
set HAS_DRY_RUN_OPTION=0
set HAS_CONFIG_OPTION=0
set HAS_SHORT_VERBOSE_OPTION=0

for %%p in (%*) do (
if "%%p"=="--version" (
ruby "%FLUENT_PACKAGE_VERSION%"
goto last
)
if "%%p"=="--dry-run" set HAS_DRY_RUN_OPTION=1
if "%%p"=="-c" set HAS_CONFIG_OPTION=1
if "%%p"=="--config" set HAS_CONFIG_OPTION=1
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 %HAS_DRY_RUN_OPTION% equ 0 if %HAS_CONFIG_OPTION% equ 0 (
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

Expand Down
23 changes: 23 additions & 0 deletions fluent-package/msi/install-test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ Get-ChildItem "C:\\opt\\fluent\\*.log" | %{
}
}

# Test: fluentd.bat
$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"

# Test: Uninstall
$msi -Match "fluent-package-([0-9\.]+)-.+\.msi"
$name = "Fluent Package v" + $matches[1]
Write-Host "Uninstalling ...${name}"
Expand Down

0 comments on commit e192963

Please sign in to comment.