-
Notifications
You must be signed in to change notification settings - Fork 4
/
dockerTask.sh
312 lines (290 loc) · 8.54 KB
/
dockerTask.sh
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
#!/bin/bash
arrayContains () {
local array="$1[@]"
local seeking=$2
local index=-1
for element in "${!array}"; do
index=$((index + 1))
if [[ $element == $seeking ]]; then
echo $index
return
fi
done
echo -1
}
check () {
retval=$1
if [ $retval -ne 0 ]; then
>&2 echo "Return code was not zero but $retval"
exit 999
fi
}
imageNameExists () {
if [[ $imageName == "" ]]; then
>&2 echo "Missing image name. Use --image to provide an image name."
exit 4
fi
}
killExistingSsh () {
existingSshPid=$(pgrep ssh)
if [[ $existingSshPid != "" ]]; then
echo "Killing existing ssh."
kill -9 $existingSshPid
check $?
fi
}
makeVars () {
local name="$1[@]"
local seeking=$2
local index=-1
local array=("${!name}")
for element in "${array[@]}"; do
index=$((index + 1))
if [[ $element == $seeking ]]; then
environmentVar=${array[$(($index + 1))]}
if [[ $environmentVar == --* ]]; then
>&2 echo "You must supply a valid value to --env."
exit 15
fi
echo $environmentVar
fi
done
}
exportEnvs () {
local name="$1[@]"
for element in ${!name}; do
IFS='=' read -a values <<< "$element"
variableName=${values[0]}
variableValue=${values[1]}
if [ -z $variableValue ]; then
>&2 echo "You must supply a valid value to the enviornment variables."
exit 14
fi
echo "Exporting $variableName"
export $variableName=$variableValue
done
}
version=$(date +%Y-%m-%d_%H-%M-%S)
versionFileName=imageVersion.txt
versionFile=$(pwd)/$versionFileName
args=("$@")
#export variables defined with -e:
echo "Exporting variables supplied as arguments..."
envs=$(makeVars args '-e')
exportEnvs envs
echo "Variables exported."
envIndex=$(arrayContains args "--env")
if [ $envIndex -gt -1 ]; then
env=${args[$(($envIndex + 1))]}
if [[ $env == --* ]]; then
>&2 echo "You must supply a valid value to --env."
exit 5
fi
else
env='Debug'
fi
imageIndex=$(arrayContains args "--image")
if [ $imageIndex -gt -1 ]; then
imageName=${args[$(($imageIndex + 1))]}
if [[ $imageName == --* ]]; then
>&2 echo "You must supply a valid value to --image."
exit 6
fi
fi
sshServerIndex=$(arrayContains args "--server")
if [ $sshServerIndex -gt -1 ]; then
sshServer=${args[$(($sshServerIndex + 1))]}
if [[ $sshServer == --* ]]; then
>&2 echo "You must supply a valid value to --server"
exit 7
fi
fi
sshPortIndex=$(arrayContains args "--port")
if [ $sshPortIndex -gt -1 ]; then
sshPort=${args[$(($sshPortIndex + 1))]}
if [[ $sshPort == --* ]]; then
>&2 echo "You must supply a valid value to --port"
exit 8
fi
fi
sshUserIndex=$(arrayContains args "--user")
if [ $sshUserIndex -gt -1 ]; then
sshUser=${args[$(($sshUserIndex + 1))]}
if [[ $sshUser == --* ]]; then
>&2 echo "You must supply a valid value to --user"
exit 9
fi
fi
projectNameIndex=$(arrayContains args "--project")
if [ $projectNameIndex -gt -1 ]; then
projectName=${args[$(($projectNameIndex + 1))]}
if [[ $projectName == --* ]]; then
>&2 echo "You must supply a valid value to --project"
exit 12
fi
fi
sshKeyIndex=$(arrayContains args "--key")
if [ $sshKeyIndex -gt -1 ]; then
sshKey=${args[$(($sshKeyIndex + 1))]}
if ! [[ $sshKey =~ ^'-----BEGIN RSA PRIVATE KEY-----\n'.*'\n-----END RSA PRIVATE KEY-----'$ ]]; then
>&2 echo -e "You must supply a valid value to --key. Value supplied (first 20 characters):\n${sshKey:0:20}"
exit 13
fi
fi
buildIndex=$(arrayContains args "--build")
if [ $buildIndex -gt -1 ]; then
# get the context
contextIndex=$(arrayContains args "--context")
if [ $contextIndex -gt -1 ]; then
context=${args[$(($contextIndex + 1))]}
if [[ $context == --* ]]; then
>&2 echo "You must supply a valid value to --context"
exit 16
fi
else
context='.'
fi
imageNameExists
if [ $env == "Debug" ]; then
dockerFileName="Dockerfile"
else
dockerFileName="Dockerfile.$env"
fi
if [ -f $dockerFileName ]; then
echo "Building the image $imageName ($env)."
docker build -f $dockerFileName -t "$imageName:$version" $context
check $?
docker tag "$imageName:$version" "$imageName:latest"
check $?
echo $version > $versionFile
exit 0
else
>&2 echo "$env is not a valid parameter. File '$dockerFileName' does not exist."
exit 1
fi
exit 0
fi
composeIndex=$(arrayContains args "--compose")
if [ $composeIndex -gt -1 ]; then
if [ $env == "Debug" ]; then
composeFileName="docker-compose.yml"
else
composeFileName="docker-compose.$env.yml"
fi
if [ -f $composeFileName ]; then
if [[ ( ! ( $sshServer == "" && $sshPort == "" && $sshUser == "" && $sshKey == "" )) && ( ! ( $sshServer != "" && $sshPort != "" && $sshUser != "" && $sshKey != "" )) ]]; then
>&2 echo "You must supply all options --server, --port, --user and --key or none of them."
exit 10
fi
if [[ $sshServer != "" ]]; then
echo "Connecting to $sshServer:$sshPort with user $sshUser..."
killExistingSsh
keyFile=$(tempfile)
echo -e $sshKey > $keyFile
ssh $sshUser@$sshServer -L 2375:localhost:2375 -p $sshPort -oStrictHostKeyChecking=no -i $keyFile -N &
sshPid=$!
export DOCKER_HOST=tcp://localhost:2375
connectExitCode=1
tries=0
while [ $connectExitCode -ne 0 ]; do
tries=$((tries+1))
if [ $tries -gt 5 ]; then
>&2 echo "Could not connect to server, exiting..."
killExistingSsh
exit 11
fi
echo "Testing if port 2375 is open..."
timeout 1 bash -c "cat < /dev/null > /dev/tcp/localhost/2375"
connectExitCode=$?
if [ $connectExitCode != "0" ]; then
sleep 3
fi
done
fi
echo "Searching for image version file $versionFile..."
if [ ! -f $versionFile ]; then
echo "Could not find $versionFile, searching for $versionFileName..."
versionFile=$(find . -name $versionFileName)
fi
if [ ! -z "$versionFile" ] && [ -f $versionFile ]; then
echo "Found $versionFile."
export IMAGE_VERSION=$(cat $versionFile)
else
echo "Found $versionFile, using default value 'lastest'."
export IMAGE_VERSION="latest"
fi
echo "Version is '$IMAGE_VERSION'."
echo "Running compose file $composeFileName ($env)."
echo "Removing previous environment..."
if [[ $projectName == "" ]]; then
projectName=$(echo ${PWD##*/} | tr '[:upper:]' '[:lower:]')
fi
docker-compose -f $composeFileName -p $projectName down
check $?
echo "Creating new environment..."
docker-compose -f $composeFileName -p $projectName up -d
exitCode=$?
if [[ $sshServer != "" ]]; then
echo "Killing SSH connection..."
kill -9 $sshPid
rm $keyFile
fi
check $exitCode
exit 0
else
>&2 echo "$env may not be a valid parameter. File '$composeFileName' does not exist."
exit 2
fi
exit 0
fi
cleanIndex=$(arrayContains args "--clean")
if [ $cleanIndex -gt -1 ]; then
imageNameExists
if [ $env == "Debug" ]; then
composeFileName="docker-compose.yml"
else
composeFileName="docker-compose.$env.yml"
fi
if [ -f $composeFileName ]; then
echo "Killing with compose file $composeFileName ($env)."
docker-compose -f $composeFileName down
check $?
else
>&2 echo "$env may not be a valid parameter. File '$composeFileName' does not exist."
exit 6
fi
containerIds=$(docker ps | tail -n +2 | grep -w "$imageName" | cut -f1 -d ' ')
for containerId in $containerIds; do
docker rm -f $containerId
check $?
done
exit 0
fi
pushIndex=$(arrayContains args "--push")
if [ $pushIndex -gt -1 ]; then
imageNameExists
if [ -f $versionFile ]; then
version=`cat $versionFile`
echo "Pushing the image $imageName with version $version."
docker push "$imageName:$version"
check $?
docker push "$imageName:latest"
check $?
exit 0
else
>&2 echo "File '$versionFile' does not exist."
exit 3
fi
exit 0
fi
helpIndex=$(arrayContains args "--help")
if [ $helpIndex -gt -1 ]; then
echo "
Usage:
./dockerTask.sh --clean --image <IMAGE_NAME> [--env (Debug|Release)]
./dockerTask.sh --build --image <IMAGE_NAME> [--env (Debug|Release)] [--context <BUILD_CONTEXT>]
./dockerTask.sh --compose [--server <SSH_SERVER> --port <SSH_PORT> --user <SSH_USER> --key <SSH_KEY>] [--env (Debug|Release)] [ --project <DOCKER_COMPOSE_PROJECT_NAME> ]
./dockerTask.sh --push --image <IMAGE_NAME>
./dockerTask.sh --help"
exit 0
fi