-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathTest-Tooling.ps1
More file actions
243 lines (228 loc) · 10 KB
/
Test-Tooling.ps1
File metadata and controls
243 lines (228 loc) · 10 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
function Test-Tooling {
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $false)]
[switch]$skipAlzModuleVersionCheck,
[Parameter(Mandatory = $false)]
[switch]$checkYamlModule
)
$checkResults = @()
$hasFailure = $false
# Check if PowerShell is the correct version
Write-Verbose "Checking PowerShell version"
$powerShellVersionTable = $PSVersionTable
$powerShellVersion = $powerShellVersionTable.PSVersion.ToString()
if ($powerShellVersionTable.PSVersion.Major -lt 7) {
$checkResults += @{
message = "PowerShell version $powerShellVersion is not supported. Please upgrade to PowerShell 7.4 or higher. Either switch to the `pwsh` prompt or follow the instructions here: https://aka.ms/install-powershell"
result = "Failure"
}
$hasFailure = $true
} elseif ($powerShellVersionTable.PSVersion.Major -eq 7 -and $powerShellVersionTable.PSVersion.Minor -lt 4) {
$checkResults += @{
message = "PowerShell version $powerShellVersion is not supported. Please upgrade to PowerShell 7.4 or higher. Either switch to the `pwsh` prompt or follow the instructions here: https://aka.ms/install-powershell"
result = "Failure"
}
$hasFailure = $true
} else {
$checkResults += @{
message = "PowerShell version $powerShellVersion is supported."
result = "Success"
}
}
# Check if Git is installed
Write-Verbose "Checking Git installation"
$gitPath = Get-Command git -ErrorAction SilentlyContinue
if ($gitPath) {
$checkResults += @{
message = "Git is installed."
result = "Success"
}
} else {
$checkResults += @{
message = "Git is not installed. Follow the instructions here: https://git-scm.com/downloads"
result = "Failure"
}
$hasFailure = $true
}
# Check if using Service Principal Auth
Write-Verbose "Checking Azure environment variables"
$nonAzCliEnvVars = @(
"ARM_CLIENT_ID",
"ARM_SUBSCRIPTION_ID",
"ARM_TENANT_ID"
)
$envVarsSet = $true
$envVarValid = $true
$envVarUnique = $true
$envVarAtLeastOneSet = $false
$envVarsWithValue = @()
$checkedEnvVars = @()
foreach($envVar in $nonAzCliEnvVars) {
$envVarValue = [System.Environment]::GetEnvironmentVariable($envVar)
if($envVarValue -eq $null -or $envVarValue -eq "" ) {
$envVarsSet = $false
continue
}
$envVarAtLeastOneSet = $true
$envVarsWithValue += $envVar
if($envVarValue -notmatch("^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$")) {
$envVarValid = $false
continue
}
if($checkedEnvVars -contains $envVarValue) {
$envVarUnique = $false
continue
}
$checkedEnvVars += $envVarValue
}
if($envVarsSet) {
Write-Verbose "Using Service Principal Authentication, skipping Azure CLI checks"
if($envVarValid -and $envVarUnique) {
$checkResults += @{
message = "Azure environment variables are set and are valid unique GUIDs."
result = "Success"
}
}
if(-not $envVarValid) {
$checkResults += @{
message = "Azure environment variables are set, but are not all valid GUIDs."
result = "Failure"
}
$hasFailure = $true
}
if (-not $envVarUnique) {
$envVarValidationOutput = ""
foreach($envVar in $nonAzCliEnvVars) {
$envVarValue = [System.Environment]::GetEnvironmentVariable($envVar)
$envVarValidationOutput += " $envVar ($envVarValue)"
}
$checkResults += @{
message = "Azure environment variables are set, but are not unique GUIDs. There is at least one duplicate:$envVarValidationOutput."
result = "Failure"
}
$hasFailure = $true
}
} else {
if($envVarAtLeastOneSet) {
$envVarValidationOutput = ""
foreach($envVar in $envVarsWithValue) {
$envVarValue = [System.Environment]::GetEnvironmentVariable($envVar)
$envVarValidationOutput += " $envVar ($envVarValue)"
}
$checkResults += @{
message = "At least one environment variable is set, but the other expected environment variables are not set. This could cause Terraform to fail in unexpected ways. Set environment variables:$envVarValidationOutput."
result = "Warning"
}
}
# Check if Azure CLI is installed
Write-Verbose "Checking Azure CLI installation"
$azCliPath = Get-Command az -ErrorAction SilentlyContinue
if ($azCliPath) {
$checkResults += @{
message = "Azure CLI is installed."
result = "Success"
}
} else {
$checkResults += @{
message = "Azure CLI is not installed. Follow the instructions here: https://learn.microsoft.com/en-us/cli/azure/install-azure-cli"
result = "Failure"
}
$hasFailure = $true
}
# Check if Azure CLI is logged in
Write-Verbose "Checking Azure CLI login status"
$azCliAccount = $(az account show -o json) | ConvertFrom-Json
if ($azCliAccount) {
$checkResults += @{
message = "Azure CLI is logged in. Tenant ID: $($azCliAccount.tenantId), Subscription: $($azCliAccount.name) ($($azCliAccount.id))"
result = "Success"
}
} else {
$checkResults += @{
message = "Azure CLI is not logged in. Please login to Azure CLI using 'az login -t `"00000000-0000-0000-0000-000000000000}`"', replacing the empty GUID with your tenant ID."
result = "Failure"
}
$hasFailure = $true
}
}
if($skipAlzModuleVersionCheck.IsPresent) {
Write-Verbose "Skipping ALZ module version check"
} else {
# Check if latest ALZ module is installed
Write-Verbose "Checking ALZ module version"
$alzModuleCurrentVersion = Get-InstalledPSResource -Name ALZ | Select-Object -Property Name, Version | Sort-Object Version -Descending | Select-Object -First 1
if($null -eq $alzModuleCurrentVersion) {
$checkResults += @{
message = "ALZ module is not correctly installed. Please install the latest version using 'Install-PSResource -Name ALZ'."
result = "Failure"
}
$hasFailure = $true
}
$alzModuleLatestVersion = Find-PSResource -Name ALZ
if ($null -ne $alzModuleCurrentVersion) {
if ($alzModuleCurrentVersion.Version -lt $alzModuleLatestVersion.Version) {
$checkResults += @{
message = "ALZ module is not the latest version. Your version: $($alzModuleCurrentVersion.Version), Latest version: $($alzModuleLatestVersion.Version). Please update to the latest version using 'Update-PSResource -Name ALZ'."
result = "Failure"
}
$hasFailure = $true
} else {
$checkResults += @{
message = "ALZ module is the latest version ($($alzModuleCurrentVersion.Version))."
result = "Success"
}
}
}
}
# Check if powershell-yaml module is installed (only when YAML files are being used)
if ($checkYamlModule.IsPresent) {
Write-Verbose "Checking powershell-yaml module installation"
$yamlModule = Get-InstalledPSResource -Name powershell-yaml 2> $null
if ($yamlModule) {
$checkResults += @{
message = "powershell-yaml module is installed (version $($yamlModule.Version))."
result = "Success"
}
} else {
$installSuccessful = $false
try {
Install-PSResource powershell-yaml -TrustRepository
$installSuccessful = $true
} catch {
Write-Verbose "Failed to install powershell-yaml module"
}
if($installSuccessful) {
$checkResults += @{
message = "powershell-yaml module was not installed, but has been successfully installed (version $((Get-InstalledPSResource -Name powershell-yaml).Version))."
result = "Success"
}
} else {
$checkResults += @{
message = "powershell-yaml module is not installed. Please install it using 'Install-PSResource powershell-yaml'."
result = "Failure"
}
$hasFailure = $true
}
}
}
Write-Verbose "Showing check results"
Write-Verbose $(ConvertTo-Json $checkResults -Depth 100)
$checkResults | ForEach-Object {[PSCustomObject]$_} | Format-Table -Property @{
Label = "Check Result"; Expression = {
switch ($_.result) {
'Success' { $color = "92"; break }
'Failure' { $color = "91"; break }
'Warning' { $color = "93"; break }
default { $color = "0" }
}
$e = [char]27
"$e[${color}m$($_.result)${e}[0m"
}
}, @{ Label = "Check Details"; Expression = {$_.message} } -AutoSize -Wrap
if($hasFailure) {
Write-InformationColored "Accelerator software requirements have no been met, please review and install the missing software." -ForegroundColor Red -InformationAction Continue
Write-InformationColored "Cannot continue with Deployment..." -ForegroundColor Red -InformationAction Continue
throw "Accelerator software requirements have no been met, please review and install the missing software."
}
}