-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstands-engine.ps1
More file actions
113 lines (95 loc) · 3.76 KB
/
Copy pathstands-engine.ps1
File metadata and controls
113 lines (95 loc) · 3.76 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
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)]
[ValidateSet("validate", "create", "destroy")]
[string]$Operation,
[Parameter(Mandatory = $true, Position = 1)]
[string]$Manifest,
[ValidateSet("docker", "podman")]
[string]$Runtime = $env:CONTAINER_RUNTIME,
[string]$Image = $(if ($env:STANDS_ENGINE_IMAGE) { $env:STANDS_ENGINE_IMAGE } else { "stands-engine:local" }),
[string[]]$EnvFile = @(),
[string[]]$Resource = @()
)
$ErrorActionPreference = "Stop"
if (-not $Runtime) {
if (Get-Command podman -ErrorAction SilentlyContinue) {
$Runtime = "podman"
}
elseif (Get-Command docker -ErrorAction SilentlyContinue) {
$Runtime = "docker"
}
else {
throw "Neither podman nor docker was found in PATH."
}
}
$manifestPath = (Resolve-Path -LiteralPath $Manifest).Path
if (-not (Test-Path -LiteralPath $manifestPath -PathType Leaf)) {
throw "Manifest does not exist: $Manifest"
}
$currentDirectory = (Get-Location).Path
$manifestDirectory = Split-Path -Parent $manifestPath
$relativeManifest = [System.IO.Path]::GetRelativePath($currentDirectory, $manifestPath)
if ($relativeManifest -notmatch '^\.\.[\\/]') {
$workspace = $currentDirectory
$containerManifest = "/workspace/" + ($relativeManifest -replace '\\', '/')
}
else {
$workspace = $manifestDirectory
$containerManifest = "/workspace/" + (Split-Path -Leaf $manifestPath)
}
$runArgs = @("run", "--rm")
foreach ($envFilePath in $EnvFile) {
if (-not (Test-Path -LiteralPath $envFilePath -PathType Leaf)) {
throw "Environment file does not exist: $envFilePath"
}
$resolvedEnvFile = (Resolve-Path -LiteralPath $envFilePath).Path
$runArgs += @("--env-file", $resolvedEnvFile)
}
$resourceNames = @{}
$resolvedResources = @()
foreach ($resourceSpec in $Resource) {
$parts = $resourceSpec.Split('=', 2)
if ($parts.Count -ne 2 -or $parts[0] -notmatch '^[A-Za-z][A-Za-z0-9_-]*$' -or -not $parts[1]) {
throw "Invalid resource '$resourceSpec'; expected NAME=PATH with a valid name."
}
$resourceName = $parts[0]
if ($resourceNames.ContainsKey($resourceName)) {
throw "Resource '$resourceName' was specified more than once."
}
$resourcePath = (Resolve-Path -LiteralPath $parts[1]).Path
if (-not (Test-Path -LiteralPath $resourcePath -PathType Container)) {
throw "Resource '$resourceName' is not a directory: $resourcePath"
}
$resourceNames[$resourceName] = $true
$resolvedResources += [PSCustomObject]@{ Name = $resourceName; Path = $resourcePath }
}
$runArgs += @("--volume", "${workspace}:/workspace:ro")
if ($Operation -eq "validate") {
$runArgs += @(
"--env", "STAND__PATH_TO_KEY=/tmp/stands-engine-validation/id_ed25519",
"--env", "STAND__PATH_TO_CONFIGSET=/tmp/stands-engine-validation/configsets",
"--env", "OUTPUT__FILE_PATH=/tmp/stands-engine-validation/output"
)
}
else {
$dataDirectory = Join-Path $currentDirectory ".stands-engine"
@("keys", "configsets", "output") | ForEach-Object {
New-Item -ItemType Directory -Force -Path (Join-Path $dataDirectory $_) | Out-Null
}
$runArgs += @(
"--volume", "${dataDirectory}:/data",
"--env", "STAND__PATH_TO_KEY=/data/keys/id_ed25519",
"--env", "STAND__PATH_TO_CONFIGSET=/data/configsets",
"--env", "OUTPUT__FILE_PATH=/data/output"
)
}
$engineArgs = @()
foreach ($resource in $resolvedResources) {
$containerResourcePath = "/resources/$($resource.Name)"
$runArgs += @("--volume", "$($resource.Path):${containerResourcePath}:ro")
$engineArgs += @("--resource", "$($resource.Name)=${containerResourcePath}")
}
$runArgs += @($Image) + $engineArgs + @($Operation, $containerManifest)
& $Runtime @runArgs
exit $LASTEXITCODE