-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun-ruby-test.ps1
More file actions
494 lines (413 loc) · 17.6 KB
/
run-ruby-test.ps1
File metadata and controls
494 lines (413 loc) · 17.6 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/usr/bin/env powershell
<#
.SYNOPSIS
Runs Ruby 3.1.7 and chef-powershell 3.1.7 test in Windows Server Core 2022 Docker container
.DESCRIPTION
This script creates and runs a Windows Server Core 2022 Docker container,
installs Ruby 3.1.7 using OneClickInstaller2, installs chef-powershell 3.1.7,
and then drops you into the container for interactive testing.
.PARAMETER RubyVersion
Ruby version to install (default: 3.1.7)
.PARAMETER ChefPowerShellVersion
chef-powershell gem version (default: latest)
.PARAMETER ChefPowerShellGemPath
Path to local chef-powershell*.gem file to install instead of downloading from RubyGems
.PARAMETER ContainerName
Name for the Docker container (default: ruby-chef-ps-test)
.PARAMETER KeepContainer
Keep the container after exit (don't auto-remove)
.EXAMPLE
.\run-ruby-test.ps1
.\run-ruby-test.ps1 -RubyVersion "3.1.6" -ChefPowerShellVersion "3.1.6"
.\run-ruby-test.ps1 -ChefPowerShellVersion "latest"
.\run-ruby-test.ps1 -ChefPowerShellGemPath "C:\path\to\chef-powershell-3.2.0.gem"
.\run-ruby-test.ps1 -ContainerName "my-ruby-test" -KeepContainer
#>
param(
[string]$RubyVersion = "3.1.7",
[string]$ChefPowerShellVersion = "latest",
[string]$ChefPowerShellGemPath,
[string]$ContainerName = "ruby-chef-ps-test",
[switch]$KeepContainer
)
$ErrorActionPreference = "Stop"
# Configuration
$BaseImage = "mcr.microsoft.com/windows/servercore:ltsc2022"
$RubyImageTag = "ruby-devkit:$RubyVersion-windows"
$RubyInstallerUrl = "https://github.com/oneclick/rubyinstaller2/releases/download/RubyInstaller-$RubyVersion-1/rubyinstaller-devkit-$RubyVersion-1-x64.exe"
$RubyInstallPath = "C:\Ruby$($RubyVersion.Replace('.', ''))"
function Write-Status {
param([string]$Message, [string]$Color = "Green")
Write-Host "=== $Message ===" -ForegroundColor $Color
}
function Write-Error-Status {
param([string]$Message)
Write-Host "ERROR: $Message" -ForegroundColor Red
}
function Test-DockerAvailable {
try {
$dockerVersion = docker --version 2>$null
if ($dockerVersion) {
Write-Host "Docker available: $dockerVersion" -ForegroundColor Green
return $true
}
return $false
}
catch {
return $false
}
}
function Test-RubyImageExists {
param([string]$ImageTag)
try {
$images = docker images --format "table {{.Repository}}:{{.Tag}}" | Select-String -Pattern "^$ImageTag$"
if ($images) {
Write-Host "Found existing Ruby image: $ImageTag" -ForegroundColor Green
return $true
}
Write-Host "Ruby image $ImageTag not found, will build new image" -ForegroundColor Yellow
return $false
}
catch {
Write-Host "Error checking for Ruby image: $($_.Exception.Message)" -ForegroundColor Red
return $false
}
}
function Build-RubyImage {
param([string]$ImageTag)
Write-Status "Building Ruby $RubyVersion Docker image using commit approach"
try {
# Create a temporary container name for building
$buildContainerName = "ruby-build-temp-$([guid]::NewGuid().ToString('N').Substring(0,8))"
# Create installation script
$buildScript = Create-RubyInstallationScript
$scriptDir = Join-Path $env:TEMP "ruby-build-setup"
if (-not (Test-Path $scriptDir)) {
New-Item -ItemType Directory -Path $scriptDir -Force | Out-Null
}
$scriptPath = Join-Path $scriptDir "install-ruby.ps1"
$buildScript | Out-File -FilePath $scriptPath -Encoding UTF8
Write-Host "Created Ruby installation script at: $scriptPath"
Write-Host "Starting build container: $buildContainerName"
# Run container to install Ruby
$dockerArgs = @(
"run"
"--name", $buildContainerName
"-v", "${scriptDir}:C:\setup"
$BaseImage
"powershell", "-ExecutionPolicy", "Bypass", "-File", "C:\setup\install-ruby.ps1"
)
Write-Host "Running: docker $($dockerArgs -join ' ')"
& docker @dockerArgs
if ($LASTEXITCODE -ne 0) {
throw "Ruby installation in build container failed with exit code: $LASTEXITCODE"
}
Write-Host "Ruby installation completed, committing container as image..."
# Commit the container as a new image
& docker commit $buildContainerName $ImageTag
if ($LASTEXITCODE -ne 0) {
throw "Failed to commit container as image with exit code: $LASTEXITCODE"
}
Write-Status "Ruby image $ImageTag created successfully" "Green"
return $true
}
catch {
Write-Error-Status "Failed to build Ruby image: $($_.Exception.Message)"
throw
}
finally {
# Cleanup build container and temp files
if ($buildContainerName) {
docker rm -f $buildContainerName 2>$null | Out-Null
}
if (Test-Path $scriptDir) {
Remove-Item $scriptDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
}
function Create-RubyInstallationScript {
Write-Status "Creating Ruby installation script for image building"
$installScript = @"
# Ruby installation script for image building
Write-Host "=== Starting Ruby $RubyVersion installation for image ==="
# Download Ruby installer
Write-Host "Downloading Ruby installer..."
`$rubyUrl = "$RubyInstallerUrl"
`$installerPath = "C:\rubyinstaller.exe"
try {
Invoke-WebRequest -Uri `$rubyUrl -OutFile `$installerPath -UseBasicParsing
Write-Host "Downloaded Ruby installer successfully"
} catch {
Write-Host "Failed to download Ruby installer: `$(`$_.Exception.Message)" -ForegroundColor Red
exit 1
}
# Install Ruby with Devkit silently
Write-Host "Installing Ruby $RubyVersion with Devkit..."
`$installArgs = @(
"/VERYSILENT",
"/SUPPRESSMSGBOXES",
"/NOCLOSEAPPLICATIONS",
"/NORESTART",
"/NOCANCEL",
"/SP-",
"/DIR=$RubyInstallPath",
"/TASKS=assocfiles,modpath,devkit"
)
try {
Write-Host "Running installer with args: `$(`$installArgs -join ' ')"
`$process = Start-Process -FilePath `$installerPath -ArgumentList `$installArgs -Wait -PassThru -NoNewWindow
Write-Host "Installer process completed with exit code: `$(`$process.ExitCode)"
if (`$process.ExitCode -ne 0) {
throw "Ruby installer failed with exit code: `$(`$process.ExitCode)"
}
Write-Host "Ruby installed successfully!" -ForegroundColor Green
} catch {
Write-Host "Ruby installation failed: `$(`$_.Exception.Message)" -ForegroundColor Red
exit 1
}
# Update PATH and refresh environment
`$rubyBinPath = "$RubyInstallPath\bin"
[Environment]::SetEnvironmentVariable("PATH", [Environment]::GetEnvironmentVariable("PATH", "Machine") + ";`$rubyBinPath", "Machine")
# Verify Ruby installation
try {
if (Test-Path "`$rubyBinPath\ruby.exe") {
Write-Host "Ruby executable verified at: `$rubyBinPath\ruby.exe" -ForegroundColor Green
} else {
throw "Ruby executable not found at expected location"
}
} catch {
Write-Host "Warning: Could not verify Ruby installation" -ForegroundColor Yellow
exit 1
}
# Cleanup installer
Remove-Item `$installerPath -Force -ErrorAction SilentlyContinue
Write-Host "=== Ruby $RubyVersion installation for image completed successfully ==="
"@
return $installScript
}
function Create-InstallationScript {
Write-Status "Creating chef-powershell installation script"
$installScript = @"
# chef-powershell installation script for container
Write-Host "=== Starting chef-powershell $ChefPowerShellVersion installation ==="
# Set Ruby path
`$rubyBinPath = "$RubyInstallPath\bin"
`$env:PATH = "`$rubyBinPath;`$env:PATH"
# Verify Ruby installation
Write-Host "Verifying Ruby installation..."
try {
`$rubyVersion = & "`$rubyBinPath\ruby.exe" --version
Write-Host "Ruby verification: `$rubyVersion" -ForegroundColor Green
} catch {
Write-Host "Error: Ruby not found in image" -ForegroundColor Red
exit 1
}
# Install chef-powershell gem
if ("$ChefPowerShellGemPath" -ne "") {
Write-Host "Installing chef-powershell from local gem file: $ChefPowerShellGemPath..."
if (Test-Path "C:\setup\chef-powershell.gem") {
try {
& "`$rubyBinPath\gem.cmd" install "C:\setup\chef-powershell.gem" --no-document --quiet --no-verbose --force
Write-Host "chef-powershell gem installed from local file successfully!" -ForegroundColor Green
} catch {
Write-Host "Failed to install chef-powershell gem from local file: `$(`$_.Exception.Message)" -ForegroundColor Red
exit 1
}
} else {
Write-Host "Error: Local gem file not found at C:\setup\chef-powershell.gem" -ForegroundColor Red
exit 1
}
} elseif ("$ChefPowerShellVersion" -eq "latest") {
Write-Host "Installing latest chef-powershell gem..."
try {
& "`$rubyBinPath\gem.cmd" install chef-powershell --no-document --quiet --no-verbose
Write-Host "chef-powershell gem (latest) installed successfully!" -ForegroundColor Green
} catch {
Write-Host "Failed to install chef-powershell gem: `$(`$_.Exception.Message)" -ForegroundColor Red
exit 1
}
} else {
Write-Host "Installing chef-powershell gem $ChefPowerShellVersion..."
try {
& "`$rubyBinPath\gem.cmd" install chef-powershell -v $ChefPowerShellVersion --no-document --quiet --no-verbose
Write-Host "chef-powershell gem $ChefPowerShellVersion installed successfully!" -ForegroundColor Green
} catch {
Write-Host "Failed to install chef-powershell gem: `$(`$_.Exception.Message)" -ForegroundColor Red
exit 1
}
}
# Test installation using external test file
Write-Host "Running chef-powershell test suite..."
try {
`$gemList = & "`$rubyBinPath\gem.cmd" list chef-powershell
Write-Host "Installed gems: `$gemList" -ForegroundColor Cyan
# Run external test file
Write-Host "Executing test-chef-ps.rb test suite..."
if (Test-Path "C:\setup\test-chef-ps.rb") {
`$testResult = & "`$rubyBinPath\ruby.exe" "C:\setup\test-chef-ps.rb"
Write-Host "Test suite completed" -ForegroundColor Green
} else {
Write-Host "Warning: test-chef-ps.rb not found, running basic test..." -ForegroundColor Yellow
`$basicTest = & "`$rubyBinPath\ruby.exe" -e "require 'chef/powershell'; puts 'chef-powershell loaded successfully'"
Write-Host "Basic test result: `$basicTest" -ForegroundColor Green
}
} catch {
Write-Host "chef-powershell test failed: `$(`$_.Exception.Message)" -ForegroundColor Yellow
}
Write-Host "=== Installation completed! ==="
Write-Host "Ruby $RubyVersion and chef-powershell $ChefPowerShellVersion are ready for testing"
Write-Host "Ruby is installed at: $RubyInstallPath"
Write-Host "Use 'ruby --version' and 'gem list chef-powershell' to verify"
Write-Host ""
Write-Host "=== Entering interactive session ==="
Write-Host "You can now test Ruby and chef-powershell interactively"
Write-Host "Type 'exit' to leave the container"
Write-Host ""
# Start interactive PowerShell session
& powershell -NoLogo
"@
return $installScript
}
function Run-DockerContainer {
Write-Status "Setting up Ruby $RubyVersion container"
try {
# Check if Ruby image exists, build if needed
if (-not (Test-RubyImageExists -ImageTag $RubyImageTag)) {
Build-RubyImage -ImageTag $RubyImageTag
}
# Check if container already exists and remove it
$existingContainer = docker ps -a --filter "name=$ContainerName" --format "{{.Names}}" 2>$null
if ($existingContainer -eq $ContainerName) {
Write-Host "Removing existing container: $ContainerName"
docker rm -f $ContainerName 2>$null | Out-Null
}
# Create chef-powershell installation script in a dedicated directory
$scriptDir = Join-Path $env:TEMP "ruby-container-setup"
if (-not (Test-Path $scriptDir)) {
New-Item -ItemType Directory -Path $scriptDir -Force | Out-Null
}
# Copy the test file to the shared directory
$scriptLocation = $PSScriptRoot
if (-not $scriptLocation) {
$scriptLocation = Split-Path -Parent $MyInvocation.MyCommand.Definition
}
if (-not $scriptLocation) {
$scriptLocation = Get-Location
}
$testFilePath = Join-Path $scriptLocation "test-chef-ps.rb"
$targetTestPath = Join-Path $scriptDir "test-chef-ps.rb"
if (Test-Path $testFilePath) {
Copy-Item $testFilePath $targetTestPath -Force
Write-Host "Copied test-chef-ps.rb to shared volume"
} else {
Write-Host "Warning: test-chef-ps.rb not found at $testFilePath" -ForegroundColor Yellow
Write-Host "Searched in: $scriptLocation" -ForegroundColor Yellow
}
# Copy local gem file if specified
if ($ChefPowerShellGemPath) {
if (Test-Path $ChefPowerShellGemPath) {
$targetGemPath = Join-Path $scriptDir "chef-powershell.gem"
Copy-Item $ChefPowerShellGemPath $targetGemPath -Force
Write-Host "Copied local gem file to shared volume: $ChefPowerShellGemPath"
} else {
Write-Error-Status "Local gem file not found: $ChefPowerShellGemPath"
throw "Gem file path specified but file does not exist"
}
}
$installScript = Create-InstallationScript
$scriptPath = Join-Path $scriptDir "install-script.ps1"
$installScript | Out-File -FilePath $scriptPath -Encoding UTF8
Write-Host "Created chef-powershell installation script at: $scriptPath"
# Determine removal flag
$removeFlag = if ($KeepContainer) { "" } else { "--rm" }
# Run container with installation script using pre-built Ruby image
Write-Host "Starting container: $ContainerName"
Write-Host "Ruby image: $RubyImageTag"
$dockerArgs = @(
"run"
"-it"
if ($removeFlag) { $removeFlag }
"--name", $ContainerName
"-v", "${scriptDir}:C:\setup"
$RubyImageTag
"powershell", "-ExecutionPolicy", "Bypass", "-File", "C:\setup\install-script.ps1"
) | Where-Object { $_ -ne $null -and $_ -ne "" }
Write-Host "Running: docker $($dockerArgs -join ' ')"
# Execute the installation in detached mode first
Write-Host "Running: docker $($dockerArgs -join ' ')"
& docker @dockerArgs
if ($LASTEXITCODE -eq 0) {
Write-Host "Installation completed successfully!" -ForegroundColor Green
# If container was created with --rm, we can't exec into it
if (-not $KeepContainer) {
Write-Host "Note: Container was auto-removed after installation completed" -ForegroundColor Yellow
Write-Host "To keep container for interactive testing, use -KeepContainer switch" -ForegroundColor Yellow
} else {
# Now drop into interactive session in the existing container
Write-Status "Dropping into interactive container session"
Write-Host "You can now test Ruby and chef-powershell interactively"
Write-Host "Type 'exit' to leave the container"
$interactiveArgs = @(
"exec", "-it", $ContainerName, "powershell"
)
& docker @interactiveArgs
}
} else {
throw "Container installation failed with exit code: $LASTEXITCODE"
}
}
catch {
Write-Error-Status "Docker container operation failed: $($_.Exception.Message)"
throw
}
finally {
# Cleanup temp script directory
if (Test-Path $scriptDir) {
Remove-Item $scriptDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
}
function Show-Usage {
$gemSource = if ($ChefPowerShellGemPath) {
"local gem file: $ChefPowerShellGemPath"
} else {
"version $ChefPowerShellVersion from RubyGems"
}
Write-Host @"
=== Ruby $RubyVersion & chef-powershell Container Test ===
This script will:
1. Check for existing Ruby $RubyVersion Docker image (builds if needed)
2. Run container with pre-installed Ruby $RubyVersion + Devkit
3. Install chef-powershell gem from $gemSource
4. Run comprehensive test suite (test-chef-ps.rb)
5. Drop you into an interactive PowerShell session in the container
Available commands in the container:
ruby --version # Check Ruby version
gem list chef-powershell # List chef-powershell gems
ruby test-chef-ps.rb # Run comprehensive test suite
ruby -e "require 'chef/powershell'; puts 'OK'" # Quick test
Ruby image: $RubyImageTag
Container name: $ContainerName
Auto-remove: $(-not $KeepContainer)
"@
}
function Main {
param([string[]]$Arguments)
try {
Show-Usage
Write-Status "Starting Ruby $RubyVersion and chef-powershell $ChefPowerShellVersion container test"
# Check Docker availability
if (-not (Test-DockerAvailable)) {
throw "Docker is not available. Please ensure Docker Desktop is running."
}
# Run the Docker container with installation and interactive session
Run-DockerContainer
Write-Status "Container test completed!" "Green"
}
catch {
Write-Error-Status "Container test failed: $($_.Exception.Message)"
exit 1
}
}
# Run main function
Main $args