forked from puppetlabs/bolt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ci.ps1
149 lines (126 loc) · 4.43 KB
/
ci.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
$InformationPreference = 'Continue'
$ErrorActionPreference = 'Stop'
function Set-CACert
{
$uri = 'https://curl.haxx.se/ca/cacert.pem'
$CACertFile = Join-Path -Path $ENV:AppData -ChildPath 'RubyCACert.pem'
$retryArgs = @{
SuccessMessage = "Succeeded in downloading CA bundle from $uri"
FailMessage = "Failed to download CA bundle from $uri"
Retries = 5
Timeout = 1
Script = {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $uri -UseBasicParsing -OutFile $CACertFile | Out-Null
}
}
# only download CA file if not present - throw on failures
If (-Not (Test-Path -Path $CACertFile)) { Invoke-ScriptBlockWithRetry @retryArgs }
Write-Information "Setting CA Certificate store set to $CACertFile.."
$ENV:SSL_CERT_FILE = $CACertFile
[System.Environment]::SetEnvironmentVariable('SSL_CERT_FILE', $CACertFile, [System.EnvironmentVariableTarget]::Machine)
}
function Install-Puppetfile
{
Set-CACert
# Forge connections may fail intermittently
$retryArgs = @{
SuccessMessage = 'Succeeded in installing Puppetfile'
FailMessage = 'Failed to install required modules from Forge'
Retries = 10
Timeout = 2
Script = { bundle exec r10k puppetfile install }
}
Invoke-ScriptBlockWithRetry @retryArgs
}
function New-RandomPassword
{
Add-Type -AssemblyName System.Web
"&aA4" + [System.Web.Security.Membership]::GeneratePassword(10, 3)
}
function New-LocalAdmin($userName, $password)
{
$userArgs = @{
Name = $userName
Password = (ConvertTo-SecureString -String $password -Force -AsPlainText)
}
$user = New-LocalUser @userArgs
Write-Information ($user | Format-List | Out-String)
Add-LocalGroupMember -Group 'Remote Management Users' -Member $user
Add-LocalGroupMember -Group Administrators -Member $user
}
function Install-Certificate($path, $password)
{
$importArgs = @{
FilePath = $path
CertStoreLocation = 'cert:\\LocalMachine\\My'
Password = (ConvertTo-SecureString -String $password -Force -AsPlainText)
}
return (Import-PfxCertificate @importArgs)
}
function Grant-WinRMHttpsAccess($certThumbprint)
{
$winRMArgs = @{
ResourceURI = 'winrm/config/Listener'
SelectorSet = @{ Address = '*'; Transport = 'HTTPS' }
ValueSet = @{ Hostname = 'localhost'; CertificateThumbprint = $certThumbprint }
}
$instance = New-WSManInstance @winRMArgs
Write-Information ($instance | Format-List | Out-String)
}
function Set-WinRMHostConfiguration
{
# configure WinRM to use resources/cert.pfx for SSL
$cert = Install-Certificate -Path 'resources/cert.pfx' -Password 'bolt'
Write-Information ($cert | Format-List | Out-String)
Grant-WinRMHttpsAccess -CertThumbprint $cert.Thumbprint
}
function Invoke-ScriptBlockWithRetry([ScriptBlock]$script, $failMessage, $successMessage, $retries = 15, $timeout = 1)
{
$retried = 0
Do
{
try {
$script.Invoke()
Write-Information "$successMessage after $($retried + 1) attempt(s)"
return $true
}
catch
{
$retried++
Start-Sleep -Seconds $timeout
}
} While ($retried -lt $retries)
throw "ERROR: $failMessage in $retried retries`n$($Error[0])"
}
function Test-WinRMConfiguration($userName, $password, $retries = 15, $timeout = 1)
{
$retryArgs = @{
FailMessage = 'Failed to establish WinRM connection over SSL'
SuccessMessage = "Successfully established WinRM connection with $userName"
Retries = $retries
Timeout = $timeout
Script = {
$pass = ConvertTo-SecureString $password -AsPlainText -Force
$sessionArgs = @{
ComputerName = 'localhost'
Credential = New-Object System.Management.Automation.PSCredential ($userName, $pass)
UseSSL = $true
SessionOption = New-PSSessionOption -SkipRevocationCheck -SkipCACheck
}
if (New-PSSession @sessionArgs) { return $true }
}
}
Invoke-ScriptBlockWithRetry @retryArgs
}
# Ensure Puppet Ruby 5 / 6 takes precedence over system Ruby
function Set-ActiveRubyFromPuppet
{
# https://github.com/puppetlabs/puppet-specifications/blob/master/file_paths.md
$path = @(
"${ENV:ProgramFiles}\Puppet Labs\Puppet\sys\ruby\bin",
"${ENV:ProgramFiles}\Puppet Labs\Puppet\puppet\bin",
$ENV:Path
) -join ';'
[System.Environment]::SetEnvironmentVariable('Path', $path, [System.EnvironmentVariableTarget]::Machine)
}