-
Notifications
You must be signed in to change notification settings - Fork 0
/
Docker-Container.ps1
128 lines (111 loc) · 3.04 KB
/
Docker-Container.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
Function Deploy-Container {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string] $fullname,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string] $nickname = $fullname,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string] $filePath = $null,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string[]] $arguments = @(),
[Parameter(Mandatory=$false)]
[ScriptBlock] $scriptBlock = $null
)
Begin {
$previously_running = $false
if ($null -eq $filePath -or $filePath -eq "") {
$filePath = ".\Streamistry.RabbitMQ.Testing\run-$fullname-docker.cmd"
}
}
Process {
$running = & docker container ls --format "{{.ID}}" --filter "name=$nickname"
if ($null -ne $running) {
$previously_running = $true
Write-Host "`tContainer for '$fullname' is already running with ID '$running'."
} else {
Write-Host "`tStarting new container"
Start-Process -FilePath $filePath -ArgumentList $arguments
do {
$running = & docker container ls --format "{{.ID}}" --filter "name=$nickname"
if ($null -eq $running) {
Start-Sleep -s 1
}
} while($null -eq $running)
Write-Host "`tContainer for '$fullname' started with ID '$running'."
if ($null -ne $scriptBlock) {
$startWait = Get-Date
do {
$isRunning = Invoke-Command -ScriptBlock $scriptBlock
$wait = New-TimeSpan -Start $startWait
if (!$isRunning) {
if ($wait -gt (New-TimeSpan -Seconds 1)) {
Write-Host "`t`tWaiting since $($wait.ToString("ss")) seconds ..."
}
Start-Sleep -s 1
}
} while (!$isRunning -and !($wait -gt (New-TimeSpan -Seconds 60)))
if (!$isRunning) {
Write-Warning "Waited during $($wait.ToString("ss")) seconds. Stopping test harness."
exit 0
} else {
Write-Host "`tServer is available: waited $($wait.ToString("ss")) seconds to get it live."
}
}
}
}
End {
return $previously_running, $running
}
}
Function Remove-Container {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string] $container
)
Begin {
if ($null -eq $container) {
exit
}
}
Process {
Write-Host "`tForcefully removing container '$container' ..."
& docker rm --force $container | Out-Null
Write-Host "`tContainer removed."
}
End {
}
}
Function Connect-Network {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string] $container,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string] $network
)
Begin {
if ($null -eq $container) {
exit
}
}
Process {
if ($containers.Contains($container)) {
Write-Host "`tContainer '$container' already connected to network '$network'."
} else {
Write-Host "`tConnecting container '$container' to network '$network' ..."
& docker network connect $network $container
Write-Host "`tContainer '$container' connected to network '$network'."
}
}
End {
}
}