forked from torizon/vscode-torizon-templates
-
Notifications
You must be signed in to change notification settings - Fork 1
/
runContainerIfNotExists.ps1
58 lines (50 loc) · 1.58 KB
/
runContainerIfNotExists.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
# suppress warnings that we need to use
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSAvoidOverwritingBuiltInCmdlets', ""
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSAvoidUsingWriteHost', ""
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSAvoidUsingInvokeExpression', ""
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSAvoidUsingPositionalParameters', ""
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSAvoidGlobalVars', ""
)]
param(
[string] $ContainerRuntime,
[string] $RunArguments,
[string] $ContainerName
)
$env:DOCKER_HOST = ""
if ($Env:GITLAB_CI -eq $true) {
Write-Host "ℹ️ :: GITLAB_CI :: ℹ️"
$Env:DOCKER_HOST = "tcp://docker:2375"
}
$_containerRuntime = $ContainerRuntime
$_runArguments = $RunArguments.Trim("'").Trim('"');
$_containerName = $ContainerName
# debug
Write-Host "Container Runtime: $_containerRuntime"
Write-Host "Run Arguments: $_runArguments"
Write-Host "Container Name: $_containerName"
$_containerInfo =
Invoke-Expression "$_containerRuntime container inspect $_containerName" | `
ConvertFrom-Json
if ($null -ne $_containerInfo) {
$_containerInfo = $_containerInfo[0]
}
if ($null -ne $_containerInfo) {
Write-Host "Container Exists"
if ($_containerInfo.State.Running -eq $false) {
Invoke-Expression "$_containerRuntime start $_containerName"
} else {
Write-Host "Container is running"
}
} else {
Write-Host "Container does not exist. Starting ..."
Invoke-Expression "$_containerRuntime run --name $_containerName $_runArguments"
}