-
Notifications
You must be signed in to change notification settings - Fork 20
/
quickstart.sh
304 lines (268 loc) · 8.66 KB
/
quickstart.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
#!/usr/bin/env bash
# (c) 2016, Matthew Hartstonge <[email protected]>
#
# Copyright (C) 2016 Imagination Technologies
# All Rights Reserved. Proprietary and confidential.
# Unauthorized copying of this file, via any medium is strictly prohibited.
#
# Description:
# A Kickstarting bash script for the linux (ubuntu) user. Will run either a
# Docker Compose stack, or fire up a vagrant virtual machine via virtualbox
# Install the needed applications and then runs the Docker Compose stack
# within the Virtualbox virtual machine.
#
# Install prerequisites:
# docker-compose:
# Host:
# - Native Linux/Ubuntu
# - Virtual Linux/Ubuntu
# Applications:
# - docker-engine==1.10+
# - docker-compose==1.6.0+
# vagrant:
# Host:
# - Native Linux/Ubuntu
# Applications:
# - virtualbox==5.0+
# - vagrant==1.8+
# Constants
LINE="-------------------------------------------------------------------------------"
IS_NATIVE_DOCKER="False"
MINIMUM_DOCKER_ENGINE_MAJOR=1
MINIMUM_DOCKER_ENGINE_MINOR=10
MINIMUM_DOCKER_COMPOSE_MAJOR=1
MINIMUM_DOCKER_COMPOSE_MINOR=6
function limitNumberOfAttempts() {
limit=$1
attempts=$2
if [ -z "$attempts" ]; then
attempts=1;
else
attempts=$((attempts + 1));
fi
if [ $attempts -ge $limit ]; then
echo "Too many tries, failing..";
exit 1;
fi
return 0;
}
# Check prerequisites
# - Internet Connection
# - Vagrant
function checkInternetConnectionActive() {
ping google.com -c 2 > /dev/null 2>&1;
case "$?" in
0) echo "[ OK ] Internet Connection found" ;;
*) echo "[FAIL] Internet Connection not found"
echo " Please Ensure you have an available internet connection!"
return 1;;
esac
return 0;
}
function checkVirtualBoxInstalled() {
virtualbox version > /dev/null 2>&1
case "$?" in
0) echo "[ OK ] Virtualbox installed" ;;
*) echo "[FAIL] Virtualbox check has failed..."
echo " Please make sure Virtualbox is installed!"
return 1;;
esac
return 0;
}
function checkVagrantInstalled() {
vagrant version > /dev/null 2>&1
case "$?" in
0) echo "[ OK ] Vagrant installed" ;;
*) echo "[FAIL] Vagrant check has failed..."
echo " Please make sure Vagrant is installed!"
return 1 ;;
esac
return 0;
}
function checkDockerEngineVersion() {
IFS='.' read -ra docker_engine_version <<< $(docker version -f '{{ .Server.Version }}')
docker_engine_major="${docker_engine_version[0]}";
docker_engine_minor="${docker_engine_version[1]}";
docker_engine_patch="${docker_engine_version[2]}";
if (( $docker_engine_minor >= $MINIMUM_DOCKER_ENGINE_MINOR )); then
echo "[ OK ] Docker-engine version greater or equal to 1.10!"
elif (( $docker_engine_minor < $MINIMUM_DOCKER_ENGINE_MINOR )); then
echo "[FAIL] Docker-engine version lower than 1.10!"
return 1
fi
return 0
}
function checkDockerComposeVersion() {
IFS='.' read -ra docker_compose_version <<< $(docker-compose version --short)
docker_compose_major="${docker_compose_version[0]}"
docker_compose_minor="${docker_compose_version[1]}"
docker_compose_patch="${docker_compose_version[2]}"
if (( $docker_compose_minor >= $MINIMUM_DOCKER_COMPOSE_MINOR )); then
echo "[ OK ] Docker-compose version greater than 1.6.0!"
elif (( $docker_compose_minor < $MINIMUM_DOCKER_COMPOSE_MINOR )); then
echo "[FAIL] Docker-compose version lower than 1.6.0!"
return 1
fi
return 0;
}
function performNativeDockerPrerequisiteChecks() {
checkDockerEngineVersion && \
checkDockerComposeVersion
}
function performVagrantPrerequisiteChecks() {
checkInternetConnectionActive && \
checkVirtualBoxInstalled && \
checkVagrantInstalled
}
function performPrerequisiteChecks() {
echo
echo $LINE
echo "Performing Prerequisite Checks"
echo $LINE
if [ "${IS_NATIVE_DOCKER}" == "False" ]; then
performVagrantPrerequisiteChecks
else
performNativeDockerPrerequisiteChecks
fi
case "$?" in
0) echo "[ OK ] All prerequisite Checks passed!" ;;
*) echo "[FAIL] Prerequisite Checks Failed!"
echo "exiting..."
exit 1;;
esac
echo $LINE
echo
return 0;
}
function decideOnDomainName() {
attempts=$1
read -r -p "Enter a FQDN hostname for the Device Server: " domainName
if [ -z "$domainName" ]; then
limitNumberOfAttempts 3 ${attempts}
decideOnDomainName ${attempts}
fi
}
function generateEnvFile() {
echo
echo $LINE
echo "Setup an environment file"
echo $LINE
decideOnDomainName
echo "# Host Environment variables for docker-compose services" > .env
echo "export DEVICESERVER_HOSTNAME=${domainName}" >> .env
# Import ENVar for the script
source .env
grep DEVICESERVER_HOSTNAME /etc/profile.d/deviceserver.sh > /dev/null 2>&1
if [ $? -ne 0 ]; then
sudo mv /etc/profile.d/deviceserver.sh .
sudo cat .env >> deviceserver.sh
sudo mv deviceserver.sh /etc/profile.d/
fi
echo "[ OK ] Environment file created!"
echo $LINE
}
function generateTlsCertificates() {
echo
echo $LINE
echo "Setup Self Signed TLS Certificates"
echo $LINE
# Take in vars for CSR request
read -r -p "Country Name (2 letter code) [AU]: " countryName
read -r -p "State or Province Name (full name) [Some-State]: " provinceName
read -r -p "Locality Name (eg, city) []: " localityName
read -r -p "Organization Name (eg, company) [Internet Widgits Pty Ltd]: " organisationName
read -r -p "Organizational Unit Name (eg, section) []: " organisationalUnitName
read -r -p "Common Name (e.g. domain name/server FQDN) []: " commonName
# Generate a Private Key and a CSR
openssl req \
-newkey rsa:2048 -nodes -keyout docker/ssl/domain_key.pem \
-subj "/C=$countryName/ST=$provinceName/L=$localityName/O=$organisationName/OU=$organisationalUnitName/CN=$commonName" \
-out docker/ssl/domain_csr.pem
# Generate a Self-Signed Certificate from an Existing Private Key and CSR
openssl x509 \
-signkey docker/ssl/domain_key.pem \
-in docker/ssl/domain_csr.pem \
-req -days 365 -out docker/ssl/domain_cert.pem
# Generate Diffie-Hellman certificate
sudo openssl dhparam -out docker/ssl/dhparam.pem 2048
# rename certificates for fabio
cp -p docker/ssl/domain_cert.pem docker/ssl/cert.pem
cp -p docker/ssl/domain_key.pem docker/ssl/key.pem
echo "[ OK ] TLS Certificates created!"
echo $LINE
}
function runDockerComposeUp() {
echo
echo $LINE
echo "Setup Docker host"
echo $LINE
docker-compose up -d --force-recreate
echo
case "$?" in
0) echo "[ OK ] docker-compose up succeeded!" ;;
*) echo "[FAIL] docker-compose up Failed!"
echo "exiting..."
exit 1;;
esac
echo "The device server should now be accessible on http://localhost/"
echo $LINE
}
function runVagrantUp() {
echo
echo $LINE
echo "Setup Vagrant host"
echo $LINE
vagrant up
echo
case "$?" in
0) echo "[ OK ] Vagrant up succeeded!" ;;
*) echo "[FAIL] Vagrant up Failed!"
echo "exiting..."
exit 1;;
esac
echo "The device server should now be accessible on http://10.0.0.100/"
echo $LINE
}
function runDeviceServerStack() {
if [ "${IS_NATIVE_DOCKER}" = "True" ]; then
runDockerComposeUp;
else
runVagrantUp;
fi
}
function decideOnDockerOrVagrant() {
attempts=$1
read -r -p "Would you prefer native docker over Vagrant? [y/n]: " decision
if [ -z "${decision}" ] && [ "${decision}" != "y" ] && [ "${decision}" != "Y" ] && [ "${decision}" != "n" ] && [ "${decision}" != "N" ]; then
limitNumberOfAttempts 3 ${attempts}
decideOnDockerOrVagrant ${attempts}
fi
}
function areYouUsingDockerOrVagrant() {
if [ -z "${attempts}" ]; then
echo $LINE
echo "Docker or Vagrant?"
echo $LINE
fi
decideOnDockerOrVagrant
if [ "${decision}" = "Y" ] || [ "${decision}" = "y" ]; then
IS_NATIVE_DOCKER="True"
echo "[ OK ] Using native docker"
else
IS_NATIVE_DOCKER="False"
echo "[ OK ] Using Vagrant"
fi
}
function main() {
if [ "$1" = "dockermode" ]; then
IS_NATIVE_DOCKER="True"
echo "[ OK ] Using native docker"
else
areYouUsingDockerOrVagrant
fi
performPrerequisiteChecks
generateEnvFile
generateTlsCertificates
runDeviceServerStack
}
main $1