|
| 1 | +$ProgressPreference = 'Continue' |
| 2 | +$VerbosePreference = 'Continue' |
| 3 | +$ErrorActionPreference = 'Stop' |
| 4 | +Set-StrictMode -Version 2.0 |
| 5 | + |
| 6 | +[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor 'Tls12' |
| 7 | + |
| 8 | +New-Variable -Name curdir -Option Constant -Value $PSScriptRoot |
| 9 | +Write-Host "[INFO] curdir: $curdir" |
| 10 | + |
| 11 | +New-Variable -Name ci_dir -Option Constant -Value (Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath '.ci') |
| 12 | + |
| 13 | +New-Variable -Name certs_dir -Option Constant -Value (Join-Path -Path $ci_dir -ChildPath 'certs') |
| 14 | + |
| 15 | +New-Variable -Name ci_windows_dir -Option Constant -Value (Join-Path -Path $ci_dir -ChildPath 'windows') |
| 16 | + |
| 17 | +New-Variable -Name ca_certificate_file -Option Constant -Value ` |
| 18 | + (Resolve-Path -LiteralPath (Join-Path -Path $certs_dir -ChildPath 'ca_certificate.pem')) |
| 19 | + |
| 20 | +New-Variable -Name enabled_plugins_file -Option Constant -Value ` |
| 21 | + (Resolve-Path -LiteralPath (Join-Path -Path $ci_windows_dir -ChildPath 'enabled_plugins')) |
| 22 | + |
| 23 | +Write-Host "[INFO] importing CA cert from '$ca_certificate_file'" |
| 24 | +Import-Certificate -Verbose -CertStoreLocation Cert:\LocalMachine\Root -FilePath $ca_certificate_file |
| 25 | + |
| 26 | +New-Variable -Name versions_path -Option Constant -Value ` |
| 27 | + (Resolve-Path -LiteralPath (Join-Path -Path $ci_windows_dir -ChildPath 'versions.json')) |
| 28 | +$versions = Get-Content $versions_path | ConvertFrom-Json |
| 29 | +Write-Host "[INFO] versions: $versions" |
| 30 | +$erlang_ver = $versions.erlang |
| 31 | +$rabbitmq_ver = $versions.rabbitmq |
| 32 | + |
| 33 | +$base_installers_dir = Join-Path -Path $HOME -ChildPath 'installers' |
| 34 | +if (-Not (Test-Path $base_installers_dir)) |
| 35 | +{ |
| 36 | + New-Item -Verbose -ItemType Directory $base_installers_dir |
| 37 | +} |
| 38 | + |
| 39 | +$erlang_download_url = "https://github.com/erlang/otp/releases/download/OTP-$erlang_ver/otp_win64_$erlang_ver.exe" |
| 40 | +$erlang_installer_path = Join-Path -Path $base_installers_dir -ChildPath "otp_win64_$erlang_ver.exe" |
| 41 | +$erlang_install_dir = Join-Path -Path $HOME -ChildPath 'erlang' |
| 42 | + |
| 43 | +Write-Host '[INFO] Downloading Erlang...' |
| 44 | + |
| 45 | +if (-Not (Test-Path -LiteralPath $erlang_installer_path)) |
| 46 | +{ |
| 47 | + Invoke-WebRequest -UseBasicParsing -Uri $erlang_download_url -OutFile $erlang_installer_path |
| 48 | +} |
| 49 | +else |
| 50 | +{ |
| 51 | + Write-Host "[INFO] Found '$erlang_installer_path' in cache!" |
| 52 | +} |
| 53 | + |
| 54 | +Write-Host "[INFO] Installing Erlang to $erlang_install_dir..." |
| 55 | +& $erlang_installer_path '/S' "/D=$erlang_install_dir" | Out-Null |
| 56 | + |
| 57 | +# https://github.com/rabbitmq/rabbitmq-server/releases/download/v4.0.0-beta.5/rabbitmq-server-windows-4.0.0-beta.5.zip |
| 58 | +$rabbitmq_installer_download_url = "https://github.com/rabbitmq/rabbitmq-server/releases/download/v$rabbitmq_ver/rabbitmq-server-$rabbitmq_ver.exe" |
| 59 | +$rabbitmq_installer_path = Join-Path -Path $base_installers_dir -ChildPath "rabbitmq-server-$rabbitmq_ver.exe" |
| 60 | +Write-Host "[INFO] rabbitmq installer path $rabbitmq_installer_path" |
| 61 | + |
| 62 | +if (Test-Path -LiteralPath 'HKLM:\SOFTWARE\WOW6432Node\') |
| 63 | +{ |
| 64 | + New-Variable -Name erlangRegKeyPath -Option Constant ` |
| 65 | + -Value 'HKLM:\SOFTWARE\WOW6432Node\Ericsson\Erlang' |
| 66 | +} |
| 67 | +else |
| 68 | +{ |
| 69 | + New-Variable -Name erlangRegKeyPath -Option Constant ` |
| 70 | + -Value 'HKLM:\SOFTWARE\Ericsson\Erlang' |
| 71 | +} |
| 72 | + |
| 73 | +New-Variable -Name erlangRegKey -Option Constant ` |
| 74 | + -Value (Get-ChildItem $erlangRegKeyPath) |
| 75 | + |
| 76 | +if ($erlangRegKey -eq $null) { |
| 77 | + Write-Error "Could not find Erlang installation registry key at $erlangRegKeyPath" |
| 78 | +} |
| 79 | + |
| 80 | +New-Variable -Name erlangErtsVersion -Option Constant ` |
| 81 | + -Value (Select-Object -InputObject $erlangRegKey -Last 1).PSChildName |
| 82 | +Write-Verbose "erlangErtsVersion: $erlangErtsVersion" |
| 83 | + |
| 84 | +New-Variable -Name erlangErtsRegKeyPath -Option Constant ` |
| 85 | + -Value "HKLM:\SOFTWARE\WOW6432Node\Ericsson\Erlang\$erlangErtsVersion" |
| 86 | + |
| 87 | +New-Variable -Name erlangErtsRegKey -Option Constant ` |
| 88 | + -Value (Get-ItemProperty -LiteralPath HKLM:\SOFTWARE\WOW6432Node\Ericsson\Erlang\$erlangErtsVersion) |
| 89 | + |
| 90 | +if ($erlangErtsRegKey -eq $null) { |
| 91 | + Write-Error "Could not find Erlang erts registry key at $erlangErtsRegKeyPath" |
| 92 | +} |
| 93 | + |
| 94 | +New-Variable -Name erlangProgramFilesPath -Option Constant ` |
| 95 | + -Value ($erlangErtsRegKey.'(default)') |
| 96 | + |
| 97 | +if (Test-Path -LiteralPath $erlangProgramFilesPath) { |
| 98 | + Write-Verbose "Erlang installation directory: '$erlangProgramFilesPath'" |
| 99 | +} |
| 100 | +else { |
| 101 | + Write-Error 'Could not find Erlang installation directory!' |
| 102 | +} |
| 103 | + |
| 104 | +New-Variable -Name allowedExes -Option Constant -Value @('erl.exe', 'epmd.exe', 'werl.exe') |
| 105 | + |
| 106 | +New-Variable -Name exes -Option Constant -Value ` |
| 107 | + $(Get-ChildItem -Filter '*.exe' -Recurse -LiteralPath $erlangProgramFilesPath | Where-Object { $_.Name -in $allowedExes }) |
| 108 | + |
| 109 | +foreach ($exe in $exes) { |
| 110 | + $fwRuleName = "rabbitmq-allow-$($exe.Name)-$(Get-Random)" |
| 111 | + Write-Verbose "Updating or creating firewall rule for '$exe' - fwRuleName: $fwRuleName" |
| 112 | + if (!(Get-NetFirewallRule -ErrorAction 'SilentlyContinue' -Name $fwRuleName)) { |
| 113 | + New-NetFirewallRule -Enabled True -Name $fwRuleName -DisplayName $fwRuleName -Direction In -Program $exe -Profile Any -Action Allow |
| 114 | + } |
| 115 | + else { |
| 116 | + Set-NetFirewallRule -Enabled True -Name $fwRuleName -DisplayName $fwRuleName -Direction In -Program $exe -Profile Any -Action Allow |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +Write-Host "[INFO] Setting ERLANG_HOME to '$erlangProgramFilesPath'..." |
| 121 | +$env:ERLANG_HOME = $erlangProgramFilesPath |
| 122 | +[Environment]::SetEnvironmentVariable('ERLANG_HOME', $erlangProgramFilesPath, 'Machine') |
| 123 | +Add-Content -Verbose -LiteralPath $env:GITHUB_ENV -Value "ERLANG_HOME=$erlangProgramFilesPath" |
| 124 | + |
| 125 | +Write-Host "[INFO] Setting RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS..." |
| 126 | +$env:RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS = '-rabbitmq_stream advertised_host localhost' |
| 127 | +[Environment]::SetEnvironmentVariable('RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS', '-rabbitmq_stream advertised_host localhost', 'Machine') |
| 128 | + |
| 129 | +Write-Host '[INFO] Downloading RabbitMQ...' |
| 130 | + |
| 131 | +if (-Not (Test-Path -LiteralPath $rabbitmq_installer_path)) |
| 132 | +{ |
| 133 | + Invoke-WebRequest -UseBasicParsing -Uri $rabbitmq_installer_download_url -OutFile $rabbitmq_installer_path |
| 134 | +} |
| 135 | +else |
| 136 | +{ |
| 137 | + Write-Host "[INFO] Found '$rabbitmq_installer_path' in cache!" |
| 138 | +} |
| 139 | + |
| 140 | +Write-Host "[INFO] Installer dir '$base_installers_dir' contents:" |
| 141 | +Get-ChildItem -Verbose -LiteralPath $base_installers_dir |
| 142 | + |
| 143 | +$rabbitmq_conf_in_file = Join-Path -Path $ci_windows_dir -ChildPath 'rabbitmq.conf.in' |
| 144 | +$rabbitmq_appdata_dir = Join-Path -Path $env:AppData -ChildPath 'RabbitMQ' |
| 145 | +New-Item -Path $rabbitmq_appdata_dir -ItemType Directory |
| 146 | +$rabbitmq_conf_file = Join-Path -Path $rabbitmq_appdata_dir -ChildPath 'rabbitmq.conf' |
| 147 | +$rabbitmq_enabled_plugins_file = Join-Path -Path $rabbitmq_appdata_dir -ChildPath 'enabled_plugins' |
| 148 | + |
| 149 | +Write-Host "[INFO] Creating RabbitMQ configuration file in '$rabbitmq_appdata_dir'" |
| 150 | +Get-Content $rabbitmq_conf_in_file | %{ $_ -replace '@@CERTS_DIR@@', $certs_dir } | %{ $_ -replace '\\', '/' } | Set-Content -LiteralPath $rabbitmq_conf_file |
| 151 | +Get-Content $rabbitmq_conf_file |
| 152 | + |
| 153 | +Write-Host "[INFO] Copying '$enabled_plugins_file' to '$rabbitmq_enabled_plugins_file'" |
| 154 | +Copy-Item -Verbose -Force -LiteralPath $enabled_plugins_file -Destination $rabbitmq_enabled_plugins_file |
| 155 | + |
| 156 | +Write-Host '[INFO] Creating Erlang cookie files...' |
| 157 | + |
| 158 | +function Set-ErlangCookie |
| 159 | +{ |
| 160 | + Param($Path, $Value = 'RABBITMQ-COOKIE') |
| 161 | + Remove-Item -Force $Path -ErrorAction SilentlyContinue |
| 162 | + [System.IO.File]::WriteAllText($Path, $Value, [System.Text.Encoding]::ASCII) |
| 163 | +} |
| 164 | + |
| 165 | +$erlang_cookie_user = Join-Path -Path $HOME -ChildPath '.erlang.cookie' |
| 166 | +$erlang_cookie_system = Join-Path -Path $env:SystemRoot -ChildPath 'System32\config\systemprofile\.erlang.cookie' |
| 167 | + |
| 168 | +Set-ErlangCookie -Path $erlang_cookie_user |
| 169 | +Set-ErlangCookie -Path $erlang_cookie_system |
| 170 | + |
| 171 | +Write-Host '[INFO] Installing and starting RabbitMQ...' |
| 172 | + |
| 173 | +& $rabbitmq_installer_path '/S' | Out-Null |
| 174 | +(Get-Service -Name RabbitMQ).Status |
| 175 | + |
| 176 | +$rabbitmq_base_path = (Get-ItemProperty -Name Install_Dir -LiteralPath 'HKLM:\SOFTWARE\WOW6432Node\VMware, Inc.\RabbitMQ Server').Install_Dir |
| 177 | +$regPath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\RabbitMQ' |
| 178 | +if (Test-Path -LiteralPath 'HKLM:\SOFTWARE\WOW6432Node\') |
| 179 | +{ |
| 180 | + $regPath = 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\RabbitMQ' |
| 181 | +} |
| 182 | +$rabbitmq_version = (Get-ItemProperty $regPath 'DisplayVersion').DisplayVersion |
| 183 | +Write-Host "[INFO] RabbitMQ version path: $rabbitmq_base_path and version: $rabbitmq_version" |
| 184 | + |
| 185 | +$rabbitmq_home = Join-Path -Path $rabbitmq_base_path -ChildPath "rabbitmq_server-$rabbitmq_version" |
| 186 | +Write-Host "[INFO] Setting RABBITMQ_HOME to '$rabbitmq_home'..." |
| 187 | +[Environment]::SetEnvironmentVariable('RABBITMQ_HOME', $rabbitmq_home, 'Machine') |
| 188 | +$env:RABBITMQ_HOME = $rabbitmq_home |
| 189 | + |
| 190 | +$rabbitmqctl_path = Join-Path -Path $rabbitmq_base_path -ChildPath "rabbitmq_server-$rabbitmq_version" | Join-Path -ChildPath 'sbin' | Join-Path -ChildPath 'rabbitmqctl.bat' |
| 191 | +$rabbitmq_plugins_path = Join-Path -Path $rabbitmq_base_path -ChildPath "rabbitmq_server-$rabbitmq_version" | Join-Path -ChildPath 'sbin' | Join-Path -ChildPath 'rabbitmq-plugins.bat' |
| 192 | + |
| 193 | +$epmd_running = $false |
| 194 | +[int]$count = 1 |
| 195 | + |
| 196 | +$epmd_exe = Join-Path -Path $erlangProgramFilesPath -ChildPath "erts-$erlangErtsVersion" | Join-Path -ChildPath 'bin' | Join-Path -ChildPath 'epmd.exe' |
| 197 | + |
| 198 | +Write-Host "[INFO] Waiting for epmd ($epmd_exe) to report that RabbitMQ has started..." |
| 199 | + |
| 200 | +Do { |
| 201 | + $epmd_running = & $epmd_exe -names | Select-String -CaseSensitive -SimpleMatch -Quiet -Pattern 'name rabbit at port' |
| 202 | + if ($epmd_running -eq $true) { |
| 203 | + Write-Host '[INFO] epmd reports that RabbitMQ is running!' |
| 204 | + break |
| 205 | + } |
| 206 | + |
| 207 | + if ($count -gt 60) { |
| 208 | + throw '[ERROR] too many tries waiting for epmd to report RabbitMQ running!' |
| 209 | + } |
| 210 | + |
| 211 | + Write-Host "[INFO] epmd NOT reporting yet that RabbitMQ is running, count: '$count'..." |
| 212 | + $count = $count + 1 |
| 213 | + Start-Sleep -Seconds 5 |
| 214 | + |
| 215 | +} While ($true) |
| 216 | + |
| 217 | +[int]$count = 1 |
| 218 | + |
| 219 | +Do { |
| 220 | + $proc_id = (Get-Process -Name erl).Id |
| 221 | + if (-Not ($proc_id -is [array])) { |
| 222 | + & $rabbitmqctl_path await_startup |
| 223 | + if ($LASTEXITCODE -ne 0) { |
| 224 | + throw "[ERROR] 'rabbitmqctl await_startup' returned error: $LASTEXITCODE" |
| 225 | + } |
| 226 | + break |
| 227 | + } |
| 228 | + |
| 229 | + if ($count -gt 120) { |
| 230 | + throw '[ERROR] too many tries waiting for just one erl process to be running!' |
| 231 | + } |
| 232 | + |
| 233 | + Write-Host '[INFO] multiple erl instances running still...' |
| 234 | + $count = $count + 1 |
| 235 | + Start-Sleep -Seconds 5 |
| 236 | + |
| 237 | +} While ($true) |
| 238 | + |
| 239 | +$ErrorActionPreference = 'Continue' |
| 240 | +Write-Host '[INFO] Getting RabbitMQ status...' |
| 241 | +& $rabbitmqctl_path status |
| 242 | + |
| 243 | +echo Q | openssl s_client -connect localhost:5671 -CAfile "$certs_dir/ca_certificate.pem" -cert "$certs_dir/client_localhost_certificate.pem" -key "$certs_dir/client_localhost_key.pem" -pass pass:grapefruit |
| 244 | +if ($LASTEXITCODE -ne 0) |
| 245 | +{ |
| 246 | + throw "[ERROR] 'openssl s_client' returned error: $LASTEXITCODE" |
| 247 | +} |
0 commit comments