-
Notifications
You must be signed in to change notification settings - Fork 0
/
on-vm-process.ps1
200 lines (161 loc) · 5.61 KB
/
on-vm-process.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
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
# ----------------------------------------------------------------------
# on-vm-process.ps1
#
# Called by on-vm-launch.ps1 which is executed by the Custom Script
# extension.
#
# * Installs the Lazarus Free Pascal IDE software.
# * If a GitHub repo URI is specified:
# * Installs PortableGit.
# * Clones the repo using the URI.
# * If a GitHub repo URI is not specified:
# * Extracts the source code from a ZIP archive.
# * Builds the project.
# * Copies the resulting executable to the storage blob container.
#
# WARNING: Commands and errors are written to text files that can be
# examined on the VM for debugging. This is not a secure practice
# because some secrets may be included in the files. However, in this
# case, the VM is intended to be short-lived and the files are deleted
# when the VM is deallocated.
#
# ----------------------------------------------------------------------
param($FilesURI, $GHRepoURI)
function Set-RunLocation($runDir)
{
Set-Location $runDir
[environment]::CurrentDirectory = $runDir
}
function Invoke-Cmd($cmd)
{
$cmd >> .\debug.txt
try {
Invoke-Expression -Command $cmd -ErrorAction Stop
if ($LASTEXITCODE -ne 0) {
"ERROR LASTEXITCODE $LASTEXITCODE" >> .\error.txt
$cmd >> .\error.txt
}
}
catch {
"ERROR catch" >> .\error.txt
$cmd >> .\error.txt
}
}
Set-RunLocation "C:\action"
$config = Import-PowerShellDataFile -Path ./on-vm-settings.psd1
$config >> .\debug.txt
$srcHome = "C:\action\$($config.repoDirName)"
# Install the Lazarus IDE which includes Free Pascal compiler.
if (Test-Path "C:\lazarus\lazbuild.exe") {
Write-Host "Lazarus is already installed."
}
else {
$cmdPath = ".\$($config.lazInstallerName)"
$argList = ('/SILENT', '/SUPPRESSMSGBOXES', '/LOG', '/NORESTART')
($cmdPath + ' ' + $argList) >> .\debug.txt
Start-Process -FilePath $cmdPath -ArgumentList $argList -Wait
}
if (-not (Test-Path "C:\lazarus\lazbuild.exe")) {
$errmsg = "Failed to install Lazarus"
Write-Host $errmsg
$errmsg >> .\error.txt
Return
}
if ($GHRepoURI.Length -gt 0) {
# If a GitHub repo URI is specified, clone the repo using the URI.
# Extract PortableGit.
$cmdPath = ".\$($config.gitInstallerName)"
$argList = (' -oC:\action\PortableGit', '-y')
($cmdPath + ' ' + $argList) >> .\debug.txt
Start-Process -FilePath $cmdPath -ArgumentList $argList -Wait
if (-not (Test-Path "C:\action\PortableGit\cmd\git.exe")) {
$errmsg = "Failed to install PortableGit"
Write-Host $errmsg
$errmsg >> .\error.txt
Return
}
# Configure the environment to use git.
$env:gitdir = "C:\action\PortableGit\cmd"
$env:Path = $env:gitdir + ';' + $env:Path
# Prevent the credential helper-selector from prompting for input.
$cmd = "& git config --system --unset credential.helper"
Invoke-Cmd $cmd
# Clone the git repository.
$cmd = "& git clone `"$GHRepoURI`""
Invoke-Cmd $cmd
}
else {
# If a GitHub repo URI was not specified, assume the source was included in
# files copied from storage.
# Confirm the source code archive exists.
if (-not (Test-Path ".\$($config.srcZipName)")) {
$errmsg = "Cannot find $($config.srcZipName)"
Write-Host $errmsg
$errmsg >> .\error.txt
Return
}
# Extract the source code archive. Use -Force to overwrite existing files.
Expand-Archive -LiteralPath ".\$($config.srcZipName)" -DestinationPath C:\action -Force
}
# If there is an AutoHotKey installer zip file, extract it.
if ($config.ahkInstallerName.Length -gt 0) {
if (-not (Test-Path ".\$($config.ahkInstallerName)")) {
$errmsg = "Cannot find $($config.ahkInstallerName)"
Write-Host $errmsg
$errmsg >> .\error.txt
Return
}
Expand-Archive -LiteralPath ".\$($config.ahkInstallerName)" -DestinationPath C:\action\ahk -Force
}
# If there are additional files in the kit, extract them.
if ($config.kitZipName.Length -gt 0) {
if (-not (Test-Path ".\$($config.kitZipName)")) {
$errmsg = "Cannot find $($config.kitZipName)"
Write-Host $errmsg
$errmsg >> .\error.txt
Return
}
Expand-Archive -LiteralPath ".\$($config.kitZipName)" -DestinationPath C:\action -Force
}
$outExe = "$srcHome\$($config.outputFileName)"
# Delete existing output from previous build.
if (Test-Path $outExe) {
Remove-Item $outExe
}
$lazProjFile = "$srcHome\$($config.lazProjectFileName)"
if (-not (Test-Path $lazProjFile)) {
$errmsg = "Cannot find Lazarus project file."
Write-Host $errmsg
$errmsg >> .\error.txt
Return
}
# Build the Lazarus project.
Set-RunLocation $srcHome
$cmdPath = 'C:\lazarus\lazbuild.exe'
$argList = "$($config.lazProjectFileName) --build-all --verbose"
$outputFile = ".\laz-build-windows-output.txt"
($cmdPath + ' ' + $argList) >> .\debug.txt
Start-Process -FilePath $cmdPath -ArgumentList $argList -Wait -RedirectStandardOutput $outputFile
if (-not (Test-Path $outExe)) {
$errmsg = "Failed to build executable."
Write-Host $errmsg
$errmsg >> .\error.txt
Set-RunLocation "C:\action"
Return
}
# Copy the built executable to blob storage using azcopy.
Set-RunLocation "C:\action"
$dst = $FilesURI # URI must include SAS with Create permission.
$cmd = "&`".\azcopy.exe`" cp `"$outExe`" `"$dst`" --overwrite=true"
Invoke-Cmd $cmd
# Copy the redirected output from the build to blob storage using azcopy.
$buildOutput = "$srcHome\laz-build-windows-output.txt"
if (Test-Path $buildOutput) {
$cmd = "&`".\azcopy.exe`" cp `"$buildOutput`" `"$dst`" --overwrite=true"
Invoke-Cmd $cmd
}
# If a post-process script is specified, run it.
if ($config.postProcessScript.Length -gt 0) {
$cmd = "&`".\$($config.postProcessScript)`" -FilesURI `"$FilesURI`" -GHRepoURI `"$GHRepoURI`""
Invoke-Cmd $cmd
}