-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdockersand.ps1
150 lines (142 loc) · 5.56 KB
/
dockersand.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
# *****************************************************************************
# This file is part of dirtsand.
#
# dirtsand is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# dirtsand is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with dirtsand. If not, see <http://www.gnu.org/licenses/>.
# *****************************************************************************
param(
[Parameter(Position=0)]
[ValidateSet("attach", "build", "help", "restart", "start", "status", "stop")]
[string]
$Command = "help"
)
# --- CONFIGURABLE ---
$ComposeFile = "docker-compose.yml"
# The directory this script is in. If you customize this file outside of the repo, hardcode the
# path to the DIRTSAND sources.
$DSDir = $PSScriptRoot
# The prefix for the containers. Defaults to the name of the DIRTSAND source directory ("dirtsand"),
# so the containers are usually: dirtsand_moul_1 and dirtsand_db_1. Change this to something else
# if you intend to run multiple shards on the same machine.
$ProjectName = (Split-Path -Leaf $DSDir).ToLower() -Replace "[^a-z0-9_]","_"
function Get-DirtsandContainer() {
$containers = @(docker-compose -p $ProjectName ps | Select-String "$($ProjectName)[-_]moul[-_][0-9]*").Matches
if ($containers.Length -gt 0) {
return $containers[0].Value
}
return $false
}
function Test-Docker() {
try {
Get-Command -ErrorAction Stop docker | Out-Null
Get-Command -ErrorAction Stop docker-compose | Out-Null
} catch {
throw "ERROR: Docker does not seem to be installed. Install docker desktop for Windows from https://www.docker.com/get-started/"
}
}
# docker-compose requires the CWD have the docker-compose.yml file.
Push-Location $DSDir
trap { Pop-Location }
if ($Command -eq "help") {
Write-Host "dockersand - DIRTSAND for Docker"
Write-Host "Available commands:"
Write-Host " attach - attach to the DIRTSAND console"
Write-Host " build - rebuild the DIRTSAND containers from the source and compose files"
Write-Host " restart - restart the DIRTSAND containers"
Write-Host " start - start the DIRTSAND containers"
Write-Host " status - check if the DIRTSAND containers are running"
Write-Host " stop - stop the DIRTSAND containers"
} elseif ($Command -eq "attach") {
Test-Docker
$container = Get-DirtsandContainer
if (!$container) {
Write-Warning "dockersand is not running! Starting..."
docker-compose -p $ProjectName -f $ComposeFile up -d
$container = Get-DirtsandContainer
}
if (!$container) {
throw "ERROR: Unable to determine DIRTSAND container name. Sorry :("
}
Write-Host "Attaching to DIRTSAND... Press Ctrl+P Ctrl+Q to detach!"
docker attach $container
} elseif($Command -eq "build") {
Test-Docker
$running = Get-DirtsandContainer
if ($running) {
Write-Host -ForegroundColor Cyan "Stopping dockersand..."
docker-compose -p $ProjectName down
}
Write-Host -ForegroundColor Cyan "Building dockersand..."
docker-compose -p $ProjectName -f $ComposeFile build
if ($LASTEXITCODE -ne 0) {
throw "FAILED"
}
if ($running) {
Write-Host -ForegroundColor Cyan "Starting dockersand..."
docker-compose -p $ProjectName -f $ComposeFile up -d
if ($LASTEXITCODE -eq 0) {
Write-Host -NoNewline -ForegroundColor Green "SUCCESS"
Write-Host ": To manage DIRTSAND, use ./dockersand attach"
} else {
throw "FAILED"
}
}
} elseif ($Command -eq "restart") {
Test-Docker
if (Get-DirtsandContainer) {
Write-Host -ForegroundColor Cyan "Restarting dockersand..."
docker-compose -p $ProjectName restart
} else {
Write-Warning "dockersand is not running! Starting..."
docker-compose -p $ProjectName -f $ComposeFile up -d
}
if ($LASTEXITCODE -eq 0) {
Write-Host -NoNewline -ForegroundColor Green "SUCCESS"
Write-Host ": To manage DIRTSAND, use ./dockersand attach"
} else {
throw "FAILED"
}
} elseif ($Command -eq "start") {
Test-Docker
if (!$(Get-DirtsandContainer)) {
Write-Host -ForegroundColor Cyan "Starting dockersand..."
docker-compose -p $ProjectName -f $ComposeFile up -d
if ($LASTEXITCODE -eq 0) {
Write-Host -NoNewline -ForegroundColor Green "SUCCESS"
Write-Host ": To manage DIRTSAND, use ./dockersand attach"
} else {
throw "FAILED"
}
} else {
Write-Warning "dockersand is already running!"
}
} elseif ($Command -eq "status") {
Test-Docker
Write-Host -NoNewline "dockersand is "
if (Get-DirtsandContainer) {
Write-Host -ForegroundColor Green "UP"
} else {
Write-Host -ForegroundColor Red "DOWN"
}
} elseif ($Command -eq "stop") {
Test-Docker
if (Get-DirtsandContainer) {
Write-Host -ForegroundColor Cyan "Stopping dockersand..."
docker-compose -p $ProjectName down
} else {
Write-Warning "dockersand is not running!"
}
} else {
# This should never happen due to the validate set.
throw "ERROR: Unrecognized option $Command"
}