From 68edd3f3d1cffa0e6f97c35f0d47d6ae28e95fc4 Mon Sep 17 00:00:00 2001 From: Lemon Lam Date: Sat, 17 Aug 2024 20:06:03 +0800 Subject: [PATCH 1/6] consul: Implement no service installation switch --- consul/consul.nuspec | 15 ++- consul/tools/chocolateyInstall.ps1 | 189 ++++++++++++++------------- consul/tools/chocolateyUninstall.ps1 | 14 +- 3 files changed, 116 insertions(+), 102 deletions(-) diff --git a/consul/consul.nuspec b/consul/consul.nuspec index 68f52523..22d333f3 100644 --- a/consul/consul.nuspec +++ b/consul/consul.nuspec @@ -18,9 +18,18 @@ Consul is a tool for service discovery and configuration. Consul is distributed, Uses NSSM as the service wrapper. More info at: https://nssm.cc/ #### Package Parameters -The package parameters can be set - https://www.consul.io/docs/agent/options.html -These parameters can be passed to the installer with the use of `-params`. -For example: `-params '-config-file="%PROGRAMDATA%\consul\dsc-config\default.json"'`. +The package parameters can be set - + + * `/noservice` - Disables installation of Windows service + +These parameters can be passed to the installer with the use of `--params`. +For example: `--params="'/noservice'"`. + +#### Installer Arguments +Consul agent runtime options can also be set via `--ia`: https://developer.hashicorp.com/consul/docs/agent/config/cli-flags + +For example: `--ia='-config-file ""%PROGRAMDATA%\consul\dsc-config\default.json/"" -client 10.0.0.1'` +Or better, drop a `config.hcl` into `%PROGRAMDATA%\consul\config` with your configuration. &1 | Out-Null + + SchTasks.exe /Delete /F /TN "ConsulLogrotate" 2>&1 | Out-Null -Write-Host "Removing C:\ProgramData\consul\ ..." -takeown /f "C:\ProgramData\consul\" /a /r /d Y | Out-Null -icacls "C:\ProgramData\consul" /grant administrators:F /t | Out-Null -Remove-Item -Path "C:\ProgramData\consul\" -Force -Recurse -ErrorAction SilentlyContinue | Out-Null + Write-Host "Removing C:\ProgramData\consul\ ..." + takeown /f "C:\ProgramData\consul\" /a /r /d Y | Out-Null + icacls "C:\ProgramData\consul" /grant administrators:F /t | Out-Null + Remove-Item -Path "C:\ProgramData\consul\" -Force -Recurse -ErrorAction SilentlyContinue | Out-Null +} From 5ca15eb7cea56145838609e11fef2d2acc5e1e4d Mon Sep 17 00:00:00 2001 From: Lemon Lam Date: Sat, 17 Aug 2024 20:29:25 +0800 Subject: [PATCH 2/6] consul: Implement option to set CONSUL_HTTP_ADDR --- consul/consul.nuspec | 3 ++- consul/tools/chocolateyInstall.ps1 | 4 ++++ consul/tools/chocolateyUninstall.ps1 | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/consul/consul.nuspec b/consul/consul.nuspec index 22d333f3..ad91af48 100644 --- a/consul/consul.nuspec +++ b/consul/consul.nuspec @@ -21,9 +21,10 @@ Uses NSSM as the service wrapper. More info at: https://nssm.cc/ The package parameters can be set - * `/noservice` - Disables installation of Windows service + * `/apiaddr` - Sets environment variable `CONSUL_HTTP_ADDRESS` for easy execute commands on remote agent These parameters can be passed to the installer with the use of `--params`. -For example: `--params="'/noservice'"`. +For example: `--params="'/noservice /apiaddr=http://10.0.0.1:8500'"`. #### Installer Arguments Consul agent runtime options can also be set via `--ia`: https://developer.hashicorp.com/consul/docs/agent/config/cli-flags diff --git a/consul/tools/chocolateyInstall.ps1 b/consul/tools/chocolateyInstall.ps1 index 1bedd738..c4a5bc5b 100644 --- a/consul/tools/chocolateyInstall.ps1 +++ b/consul/tools/chocolateyInstall.ps1 @@ -12,6 +12,10 @@ Get-ChocolateyUnzip ` -fileFullPath64 $(Join-Path $binariesPath "consul_$($Env:ChocolateyPackageVersion)_windows_amd64.zip") ` -destination "$toolsPath" +if ($packageParameters["apiaddr"] -ne "") { + Install-ChocolateyEnvironmentVariable "CONSUL_HTTP_ADDR" $packageParameters["apiaddr"] +} + # Default to install with Windows service if (-not ($packageParameters["noservice"])) { $serviceName = "consul" diff --git a/consul/tools/chocolateyUninstall.ps1 b/consul/tools/chocolateyUninstall.ps1 index a5c24f43..c931e530 100644 --- a/consul/tools/chocolateyUninstall.ps1 +++ b/consul/tools/chocolateyUninstall.ps1 @@ -1,3 +1,7 @@ +if ($Env:CONSUL_HTTP_ADDR -ne "") { + Uninstall-ChocolateyEnvironmentVariable "CONSUL_HTTP_ADDR" +} + $service = Get-Service "consul" -ErrorAction SilentlyContinue if ($service) { From 033185f734189193052e44952a3d8256062f7a40 Mon Sep 17 00:00:00 2001 From: Lemon Lam Date: Sat, 17 Aug 2024 21:09:11 +0800 Subject: [PATCH 3/6] consul: Remove binaries from the package ...and enable AU to auto update package sources. --- consul/tools/chocolateyInstall.ps1 | 23 +++++++++++++---------- consul/update.ps1 | 13 ++++++++++--- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/consul/tools/chocolateyInstall.ps1 b/consul/tools/chocolateyInstall.ps1 index c4a5bc5b..d6381fd9 100644 --- a/consul/tools/chocolateyInstall.ps1 +++ b/consul/tools/chocolateyInstall.ps1 @@ -1,17 +1,20 @@ -if (Test-Path Function:\au_GetLatest) { - return -} - -# Defaults +# Defaults $toolsPath = Split-Path -Parent $MyInvocation.MyCommand.Definition $packageParameters = Get-PackageParameters -# Unzip and move Consul -Get-ChocolateyUnzip ` - -fileFullPath $(Join-Path $binariesPath "consul_$($Env:ChocolateyPackageVersion)_windows_386.zip") ` - -fileFullPath64 $(Join-Path $binariesPath "consul_$($Env:ChocolateyPackageVersion)_windows_amd64.zip") ` - -destination "$toolsPath" +# Download and unzip consul= +$package = @{ + PackageName = 'consul' + Url = 'https://releases.hashicorp.com/consul/1.19.1/consul_1.19.1_windows_386.zip' + Url64bit = 'https://releases.hashicorp.com/consul/1.19.1/consul_1.19.1_windows_amd64.zip' + UnzipLocation = $toolsPath + Checksum = '5e6cc24d3219c1c331f9b39ade2961b9948c86e254a214751b921b4027f168a5' + Checksum64 = 'a33bed52d6004c956b5b9a1fa6659477a32db14a07d37425f9ed96a6b1eaeae2' + ChecksumType = 'sha256' +} +Install-ChocolateyZipPackage @package +# Install Env CONSUL_HTTP_ADDR if specified if ($packageParameters["apiaddr"] -ne "") { Install-ChocolateyEnvironmentVariable "CONSUL_HTTP_ADDR" $packageParameters["apiaddr"] } diff --git a/consul/update.ps1 b/consul/update.ps1 index f97f7d93..d5a09c51 100644 --- a/consul/update.ps1 +++ b/consul/update.ps1 @@ -1,13 +1,20 @@ Import-Module chocolatey-au function global:au_SearchReplace { - @{ } + @{ + 'tools\chocolateyinstall.ps1' = @{ + "(\t*Url\s*=\s*)('.*')" = "`$1'$($Latest.Url32)'" + "(\t*Url64bit\s*=\s*)('.*')" = "`$1'$($Latest.Url64)'" + "(\t*Checksum\s*=\s*)('.*')" = "`$1'$($Latest.Checksum32)'" + "(\t*Checksum64\s*=\s*)('.*')" = "`$1'$($Latest.Checksum64)'" + } + } } . ../_scripts/GitHub.ps1 function global:au_BeforeUpdate() { - Get-RemoteFiles -Purge -NoSuffix + } function global:au_GetLatest { @@ -37,4 +44,4 @@ function global:au_AfterUpdate ($Package) { Update-ReleaseNotes $Package } -update \ No newline at end of file +update From 5fc2db19810b81184c833f3603fea89d94c04b5a Mon Sep 17 00:00:00 2001 From: Lemon Lam Date: Sat, 17 Aug 2024 21:12:24 +0800 Subject: [PATCH 4/6] consul: Format --- consul/tools/chocolateyInstall.ps1 | 6 +++--- consul/update.ps1 | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/consul/tools/chocolateyInstall.ps1 b/consul/tools/chocolateyInstall.ps1 index d6381fd9..f15be31a 100644 --- a/consul/tools/chocolateyInstall.ps1 +++ b/consul/tools/chocolateyInstall.ps1 @@ -60,13 +60,13 @@ if (-not ($packageParameters["noservice"])) { $service = Get-Service $serviceName -ErrorAction SilentlyContinue if ($service) { Write-Host "Uninstalling existing service" - if($service.Status -ne "Stopped" -and $service.Status -ne "Stopping") { + if ($service.Status -ne "Stopped" -and $service.Status -ne "Stopping") { Write-Host "Stopping consul process ..." $service.Stop(); } $service.WaitForStatus("Stopped", (New-TimeSpan -Minutes 1)); - if($service.Status -ne "Stopped") { + if ($service.Status -ne "Stopped") { throw "$serviceName could not be stopped within the allotted timespan. Stop the service and try again." } @@ -105,7 +105,7 @@ if (-not ($packageParameters["noservice"])) { Write-Verbose "Stop service" # Let this call to Get-Service throw if the service does not exist $service = Get-Service $serviceName - if($service.Status -ne "Stopped" -and $service.Status -ne "Stopping") { + if ($service.Status -ne "Stopped" -and $service.Status -ne "Stopping") { $service.Stop() } diff --git a/consul/update.ps1 b/consul/update.ps1 index d5a09c51..3c5deea8 100644 --- a/consul/update.ps1 +++ b/consul/update.ps1 @@ -32,9 +32,9 @@ function global:au_GetLatest { } $Latest = @{ - Version = $version - Url32 = "https://releases.hashicorp.com/consul/$version/consul_$($version)_windows_386.zip" - Url64 = "https://releases.hashicorp.com/consul/$version/consul_$($version)_windows_amd64.zip" + Version = $version + Url32 = "https://releases.hashicorp.com/consul/$version/consul_$($version)_windows_386.zip" + Url64 = "https://releases.hashicorp.com/consul/$version/consul_$($version)_windows_amd64.zip" ReleaseNotes = $release.body.Replace("# ", "## ") # Increase heading levels } return $Latest From 7e1d7adafe4f89124a9b2e1665214161b04ea934 Mon Sep 17 00:00:00 2001 From: Lemon Lam Date: Sat, 17 Aug 2024 21:45:57 +0800 Subject: [PATCH 5/6] consul: Small fixes --- consul/consul.nuspec | 2 +- consul/tools/chocolateyInstall.ps1 | 4 ++-- consul/tools/chocolateyUninstall.ps1 | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/consul/consul.nuspec b/consul/consul.nuspec index ad91af48..3557b1a9 100644 --- a/consul/consul.nuspec +++ b/consul/consul.nuspec @@ -21,7 +21,7 @@ Uses NSSM as the service wrapper. More info at: https://nssm.cc/ The package parameters can be set - * `/noservice` - Disables installation of Windows service - * `/apiaddr` - Sets environment variable `CONSUL_HTTP_ADDRESS` for easy execute commands on remote agent + * `/apiaddr` - Sets environment variable `CONSUL_HTTP_ADDR` for easy execute commands on remote agent These parameters can be passed to the installer with the use of `--params`. For example: `--params="'/noservice /apiaddr=http://10.0.0.1:8500'"`. diff --git a/consul/tools/chocolateyInstall.ps1 b/consul/tools/chocolateyInstall.ps1 index f15be31a..c701a188 100644 --- a/consul/tools/chocolateyInstall.ps1 +++ b/consul/tools/chocolateyInstall.ps1 @@ -2,7 +2,7 @@ $toolsPath = Split-Path -Parent $MyInvocation.MyCommand.Definition $packageParameters = Get-PackageParameters -# Download and unzip consul= +# Download and unzip consul $package = @{ PackageName = 'consul' Url = 'https://releases.hashicorp.com/consul/1.19.1/consul_1.19.1_windows_386.zip' @@ -14,7 +14,7 @@ $package = @{ } Install-ChocolateyZipPackage @package -# Install Env CONSUL_HTTP_ADDR if specified +# Install Env CONSUL_HTTP_ADDR if ($packageParameters["apiaddr"] -ne "") { Install-ChocolateyEnvironmentVariable "CONSUL_HTTP_ADDR" $packageParameters["apiaddr"] } diff --git a/consul/tools/chocolateyUninstall.ps1 b/consul/tools/chocolateyUninstall.ps1 index c931e530..3f532067 100644 --- a/consul/tools/chocolateyUninstall.ps1 +++ b/consul/tools/chocolateyUninstall.ps1 @@ -1,4 +1,4 @@ -if ($Env:CONSUL_HTTP_ADDR -ne "") { +if (Test-Path env:CONSUL_HTTP_ADDR) { Uninstall-ChocolateyEnvironmentVariable "CONSUL_HTTP_ADDR" } From 3778d35fd0eecf1952071c824728cd9c55026cf0 Mon Sep 17 00:00:00 2001 From: David Gardiner Date: Sun, 18 Aug 2024 10:35:53 +0930 Subject: [PATCH 6/6] Remove empty function --- consul/update.ps1 | 4 ---- 1 file changed, 4 deletions(-) diff --git a/consul/update.ps1 b/consul/update.ps1 index 3c5deea8..0b866aed 100644 --- a/consul/update.ps1 +++ b/consul/update.ps1 @@ -13,10 +13,6 @@ function global:au_SearchReplace { . ../_scripts/GitHub.ps1 -function global:au_BeforeUpdate() { - -} - function global:au_GetLatest { # This repo has releases for the cli tool as well as VS Code vsix $release = Get-GitHubLatestRelease "hashicorp/consul"