This repository was archived by the owner on Jan 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathavm.ps1
More file actions
193 lines (163 loc) · 5.1 KB
/
avm.ps1
File metadata and controls
193 lines (163 loc) · 5.1 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
#!/usr/bin/env pwsh
[CmdletBinding()]
param(
[Parameter(Position = 0, Mandatory = $true)]
[string]$Target
)
Set-StrictMode -Version 3.0
$ErrorActionPreference = "Stop"
function Show-Usage {
Write-Host "Usage: avm <make target>"
}
# Default values for environment variables
$CONTAINER_RUNTIME = if ($env:CONTAINER_RUNTIME) { $env:CONTAINER_RUNTIME } else { "docker" }
$CONTAINER_IMAGE = if ($env:CONTAINER_IMAGE) { $env:CONTAINER_IMAGE } else { "mcr.microsoft.com/azterraform:avm-latest" }
$CONTAINER_PULL_POLICY = if ($env:CONTAINER_PULL_POLICY) { $env:CONTAINER_PULL_POLICY } else { "always" }
$MAKEFILE_REF = if ($env:MAKEFILE_REF) { $env:MAKEFILE_REF } else { "main" }
$PORCH_REF = if ($env:PORCH_REF) { $env:PORCH_REF } else { "main" }
# Check if container runtime is available
if (-not (Get-Command $CONTAINER_RUNTIME -ErrorAction SilentlyContinue) -and -not $env:AVM_IN_CONTAINER) {
Write-Error "Error: $CONTAINER_RUNTIME is not installed. Please install $CONTAINER_RUNTIME first."
exit 1
}
# Check if AZURE_CONFIG_DIR is set, if not, set it to ~/.azure
$AZURE_CONFIG_DIR = if ($env:AZURE_CONFIG_DIR) {
$env:AZURE_CONFIG_DIR
}
else {
if ($IsWindows) {
Join-Path $env:USERPROFILE ".azure"
}
else {
Join-Path $env:HOME ".azure"
}
}
# Check if AZURE_CONFIG_DIR exists, if it does, mount it to the container
$AZURE_CONFIG_MOUNT = $null
$AZURE_CONFIG_MOUNT_PATH = $null
if (Test-Path $AZURE_CONFIG_DIR) {
$AZURE_CONFIG_MOUNT = "-v"
$AZURE_CONFIG_MOUNT_PATH = "${AZURE_CONFIG_DIR}:/home/runtimeuser/.azure"
}
# New: allow overriding TUI behavior with PORCH_FORCE_TUI and PORCH_NO_TUI environment variables.
# - If PORCH_FORCE_TUI is set, force TUI and interactive mode (even in GH Actions).
# - If PORCH_NO_TUI is set, explicitly disable TUI.
# - Otherwise, fallback to previous behavior: enable TUI only when not in GitHub Actions and NO_COLOR is not set.
$TUI = $null
$DOCKER_INTERACTIVE = $null
if ($env:PORCH_FORCE_TUI -and $env:PORCH_FORCE_TUI -ne "") {
$TUI = "--tui"
$DOCKER_INTERACTIVE = "-it"
$env:FORCE_COLOR = "1"
}
elseif ($env:PORCH_NO_TUI -and $env:PORCH_NO_TUI -ne "") {
# Explicitly disable TUI and interactive flags
$TUI = $null
$DOCKER_INTERACTIVE = $null
}
else {
# If we are not in GitHub Actions and NO_COLOR is not set, we want to use TUI and interactive mode
if (-not $env:GITHUB_RUN_ID -and -not $env:NO_COLOR) {
$TUI = "--tui"
$DOCKER_INTERACTIVE = "-it"
$env:FORCE_COLOR = "1"
}
}
# if PORCH_BASE_URL is set, we want to add it to the make command
$PORCH_BASE_URL_MAKE_ADD = $null
if ($env:PORCH_BASE_URL) {
$PORCH_BASE_URL_MAKE_ADD = "PORCH_BASE_URL=$($env:PORCH_BASE_URL)"
}
# Check if we are running in a container
# If we are then just run make directly
if (-not $env:AVM_IN_CONTAINER) {
# Build the docker command arguments
$dockerArgs = @(
"run"
"--pull", $CONTAINER_PULL_POLICY
"--rm"
)
# Add user parameter only on Unix-like systems
if (-not $IsWindows) {
try {
$userId = & id -u
$groupId = & id -g
$dockerArgs += @("--user", "${userId}:${groupId}")
}
catch {
Write-Warning "Could not determine user/group ID, running without --user parameter"
}
}
if ($DOCKER_INTERACTIVE) {
$dockerArgs += $DOCKER_INTERACTIVE
}
$dockerArgs += @(
"-v", "$(Get-Location):/src"
)
if ($AZURE_CONFIG_MOUNT -and $AZURE_CONFIG_MOUNT_PATH) {
$dockerArgs += @($AZURE_CONFIG_MOUNT, $AZURE_CONFIG_MOUNT_PATH)
}
# Add environment variables
$envVars = @(
"ARM_CLIENT_ID",
"ARM_OIDC_REQUEST_TOKEN",
"ARM_OIDC_REQUEST_URL",
"ARM_SUBSCRIPTION_ID",
"ARM_TENANT_ID",
"ARM_USE_OIDC",
"AVM_EXAMPLE",
"CONFTEST_APRL_URL",
"CONFTEST_AVMSEC_URL",
"CONFTEST_EXCEPTIONS_URL",
"FORCE_COLOR",
"GITHUB_TOKEN",
"GREPT_URL",
"MPTF_URL",
"NO_COLOR",
"PORCH_LOG_LEVEL",
"TEST_TYPE",
"TFLINT_CONFIG_URL"
)
foreach ($envVar in $envVars) {
$envValue = [System.Environment]::GetEnvironmentVariable($envVar)
if ($null -ne $envValue -and $envValue -ne "") {
$dockerArgs += @("-e", $envVar)
}
}
# Add TF_IN_AUTOMATION
$dockerArgs += @("-e", "TF_IN_AUTOMATION=1")
# Add TF_VAR_ environment variables
Get-ChildItem env: | Where-Object { $_.Name -like "TF_VAR_*" } | ForEach-Object {
$dockerArgs += @("-e", "$($_.Name)=$($_.Value)")
}
# Add AVM_ environment variables
Get-ChildItem env: | Where-Object { $_.Name -like "AVM_*" } | ForEach-Object {
$dockerArgs += @("-e", "$($_.Name)=$($_.Value)")
}
$dockerArgs += $CONTAINER_IMAGE
$dockerArgs += "make"
if ($TUI) {
$dockerArgs += "TUI=$TUI"
}
$dockerArgs += "MAKEFILE_REF=$MAKEFILE_REF"
if ($PORCH_BASE_URL_MAKE_ADD) {
$dockerArgs += $PORCH_BASE_URL_MAKE_ADD
}
$dockerArgs += "PORCH_REF=$PORCH_REF"
$dockerArgs += $Target
& $CONTAINER_RUNTIME @dockerArgs
}
else {
# Build make command arguments
$makeArgs = @()
if ($TUI) {
$makeArgs += "TUI=$TUI"
}
$makeArgs += "MAKEFILE_REF=$MAKEFILE_REF"
if ($PORCH_BASE_URL_MAKE_ADD) {
$makeArgs += $PORCH_BASE_URL_MAKE_ADD
}
$makeArgs += "PORCH_REF=$PORCH_REF"
$makeArgs += $Target
& make @makeArgs
}