diff --git a/tasks/check.json b/tasks/check.json index 3ce9072..249dd47 100644 --- a/tasks/check.json +++ b/tasks/check.json @@ -29,6 +29,10 @@ "openvox_bootstrap/lib/openvox_bootstrap/task.rb", "openvox_bootstrap/tasks/check.rb" ] + }, + { + "name": "check_windows.ps1", + "requirements": ["powershell"] } ] } diff --git a/tasks/check_windows.ps1 b/tasks/check_windows.ps1 new file mode 100644 index 0000000..03094bf --- /dev/null +++ b/tasks/check_windows.ps1 @@ -0,0 +1,16 @@ +$rootPath = 'HKLM:\SOFTWARE\Puppet Labs\Puppet' + +$reg = Get-ItemProperty -Path $rootPath -ErrorAction SilentlyContinue +if ($null -ne $reg) { + if ($null -ne $reg.RememberedInstallDir64) { + $loc = $reg.RememberedInstallDir64+'VERSION' + } elseif ($null -ne $reg.RememberedInstallDir) { + $loc = $reg.RememberedInstallDir+'VERSION' + } +} + +if ( ($null -ne $loc) -and (Test-Path -Path $loc) ) { + Write-Output "{`"version`":`"$(Get-Content -Path $loc -ErrorAction Stop)`",`"source`":`"$($loc.replace('\', '/'))`"}" +} else { + Write-Output '{"version":null,"source":null}' +} diff --git a/tasks/install.json b/tasks/install.json index 1fa9226..7195405 100644 --- a/tasks/install.json +++ b/tasks/install.json @@ -40,6 +40,10 @@ "facts/tasks/bash.sh", "openvox_bootstrap/files/common.sh" ] + }, + { + "name": "install_windows.ps1", + "requirements": ["powershell"] } ] } diff --git a/tasks/install_windows.ps1 b/tasks/install_windows.ps1 new file mode 100644 index 0000000..9fdfdc9 --- /dev/null +++ b/tasks/install_windows.ps1 @@ -0,0 +1,106 @@ +[CmdletBinding()] +param( + [Parameter(Mandatory = $false)] + [string]$package = "openvox-agent", + + [Parameter(Mandatory = $false)] + [string]$version = "latest", + + [Parameter(Mandatory = $false)] + [string]$collection = "openvox8", + + [Parameter(Mandatory = $false)] + [string]$apt_source = "https://apt.voxpupuli.org", + + [Parameter(Mandatory = $false)] + [string]$yum_source = "https://yum.voxpupuli.org", + + [Parameter(Mandatory = $false)] + [bool]$stop_service = $false +) + +$agent_package = "openvox-agent" + +$ErrorActionPreference = "Stop" + +function Write-Result($status, $message, $extra = @{}) { + $output = @{ + status = $status + message = $message + } + $extra + $output | ConvertTo-Json -Compress + if ($status -eq "failure") { exit 1 } else { exit 0 } +} + +# Exit if anything other than the agent is requested +if ($package -ne $agent_package) { + Write-Result "failure" "Unsupported package name '$package'. This task only supports '$agent_package'." +} + +try { + # Detect if already installed + $installed = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | + Where-Object { $_.DisplayName -like "$agent_package*" } | + Select-Object -First 1 + + if ($installed) { + $installedVersion = $installed.DisplayVersion + if ($installedVersion -eq $version) { + Write-Result "skipped" "$agent_package $version is already installed." @{ version = $installedVersion } + } + } + + # Service handling + $serviceName = "Puppet Agent" + $service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue + + if ($service) { + if ($service.Status -eq "Running") { + if ($stop_service) { + Write-Verbose "Stopping $serviceName service as requested." + Stop-Service -Name $serviceName -Force -ErrorAction Stop + } else { + Write-Result "failure" "The $serviceName service is running. Use stop_service=true to allow upgrade." + } + } + } + + # Resolve "latest" version if requested + if ($version -eq "latest") { + $version = '8.23.1' + } + + # Build download URL + $baseUrl = "https://artifacts.voxpupuli.org/downloads/windows/$collection" + $installer = "$($agent_package)-$($version)-x64.msi" + $url = "$baseUrl/$installer" + $installerPath = Join-Path $env:TEMP $installer + $installLog = Join-Path $env:TEMP "$($agent_package)-install.log" + + # Download installer + Write-Verbose "Downloading from $url" + Invoke-WebRequest -Uri $url -OutFile $installerPath + + # Install package silently + Start-Process "msiexec.exe" -ArgumentList "/i `"$installerPath`" /qn /norestart /log `"$installLog`"" -Wait + + # Cleanup downloaded artifacts + Remove-Item -Path $installerPath -ErrorAction SilentlyContinue + Remove-Item -Path $installLog -ErrorAction SilentlyContinue + + # Restart service if previously running + if ($service -and $stop_service) { + Write-Verbose "Restarting $serviceName service." + Start-Service -Name $serviceName -ErrorAction SilentlyContinue + } + + # Return success result + Write-Result "success" "$agent_package $version installed successfully." @{ + package = $agent_package + version = $newInstall.DisplayVersion + source = $url + } + +} catch { + Write-Result "failure" $_.Exception.Message +}