-
Notifications
You must be signed in to change notification settings - Fork 0
/
install
executable file
·544 lines (499 loc) · 18.8 KB
/
install
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#!/bin/bash
####################################################################################
#
# Vars
#
####################################################################################
serverRoot=`hostname -f`
dockerUsername=""
dockerPassword=""
databasePassword=""
mongoPassword=""
mongodOpts=""
tomcatAdminPassword=""
defaultUserPassword=""
samlPassword=""
defaulUserApiToken=""
spidaMin=true
postgresql=true
mongo=true
dockerStart=true
hasDockerConfig=false
disableCountdown=false
backupDir="/apps/spidamin"
configDir="/etc/spida"
tag="latest"
postgresqlTag="latest"
mongodbTag="latest"
redisTag="latest"
apacheTag="latest"
MIN_TAG_PASSED=false
POSTGRESQL_TAG_PASSED=false
MONGODB_TAG_PASSED=false
REDIS_TAG_PASSED=false
APACHE_TAG_PASSED=false
defaultApacheApp=""
sendgridApiKey=""
alertEmail=""
interchangeCookieName=""
interchangeCookieSecret=""
postgresBackupCron="30 10 * * *"
mongoBackupCron="30 10 * * *"
####################################################################################
#
# Parses Args
#
####################################################################################
function parseCommandLineArguments() {
while [ $# -gt 0 ]
do
case "$1" in
--tag) tag="$2"; MIN_TAG_PASSED=true; shift;;
--postgresqltag) postgresqlTag="$2"; POSTGRESQL_TAG_PASSED=true; shift;;
--mongodbtag) mongodbTag="$2"; MONGODB_TAG_PASSED=true; shift;;
--redistag) redisTag="$2"; REDIS_TAG_PASSED=true; shift;;
--apachetag) apacheTag="$2"; APACHE_TAG_PASSED=true; shift;;
--serverroot) serverRoot="$2"; shift;;
--username) dockerUsername="$2"; shift;;
--password) dockerPassword="$2"; shift;;
--backupdir) backupDir="$2"; shift;;
--configdir) configDir="$2"; shift;;
--dbpassword) databasePassword="$2"; shift;;
--mongopassword) mongoPassword="$2"; shift;;
--interchangecookiesecret) interchangeCookieSecret="$2";shift;;
--interchangecookiename) interchangeCookieName="$2";shift;;
--mongodopts) mongodOpts="$2"; shift;;
--tomcatpassword) tomcatAdminPassword="$2"; shift;;
--userpassword) defaultUserPassword="$2"; shift;;
--samlpassword) samlPassword="$2"; shift;;
--apitoken) defaulUserApiToken="$2"; shift;;
--no-spidamin) spidaMin=false;;
--no-postgresql) postgresql=false;;
--no-mongodb) mongo=false;;
--no-start) dockerStart=false;;
--has-docker) hasDockerConfig=true;;
--disable-countdown) disableCountdown=true;;
--default-apache-app) defaultApacheApp="$2"; shift;;
--sendgrid-api-key) sendgridApiKey="$2"; shift;;
--alert-email) alertEmail="$2"; shift;;
*)
echo >&2 \
"usage: $0 [option value]
Options:
--tag docker spidamin tag to deploy (defaults to latest)
--postgresqltag docker postgresql tag to deploy (defaults to latest)
--mongodbtag docker mongodb tag to deploy (defaults to latest)
--redistag docker mongodb tag to deploy (defaults to latest)
--apachetag docker apache tag to deploy (defaults to latest)
--username dockerhub username (will prompt for username if argument is not passed)
--password dockerhub password (will prompt for password if argument is not passed)
--serverroot server root that you will navigate to view the application (ex: min.com)
--backupdir directory for mongo data, postgres data, files and backups (defaults to $backupDir). This has to be backed up.
--configdir directory for configuration and scripts (defaults to $configDir).
--dbpassword database password
--mongopassword mongodb password
--mongodopts mongod options
--tomcatpassword tomcat admin password
--userpassword default spidamin user password
--samlpassword saml keystore password
--no-spidamin don't install spidamin
--no-postgresql don't install postgresql
--no-mongodb don't install mongodb
--no-start don't start any docker containers
--has-docker look for existing docker-compose.yml
--disable-countdown turn off countdown jobs
--default-apache-app default apache app to redirect to (defaults to projectmanager)
--sendgrid-api-key) sendgrid apikey that will be used to send emails when there is an error in a cron backup job
--alert-email) email address to send alerts to when a backup job fails
"
exit 1;;
*) break;; # terminate while loop
esac
shift
done
if [[ ! -d "$backupDir" ]]; then
echo "$backupDir does not exist, exiting"
exit 1
fi
echo "--------------------------------------------------------------------------------------"
echo "Data and files will be stored in ${backupDir}. You must take backups of this location."
echo "--------------------------------------------------------------------------------------"
}
waitFor() {
CMD=$1; shift
CHECK_MSG=$1; shift;
WAIT_MSG=$1; shift;
UP_MSG=$1
IS_UP=1
while [ $IS_UP != 0 ]; do
echo "$CHECK_MSG"
eval $CMD
IS_UP=$?
if [ $IS_UP != 0 ]; then
echo "$WAIT_MSG"
sleep 60
fi
done
echo "$UP_MSG"
}
waitForService() {
waitFor \
"sudo docker ps | grep -q '$1'" \
"Checking if $1 is up..." \
"$1 is not up, waiting..." \
"$1 is up"
}
####################################################################################
#
# Installs Docker
#
####################################################################################
function installDocker() {
#Steps Copied From https://docs.docker.com/engine/install/ubuntu/
# Setup apt repository and keyring
# Step 1
sudo apt-get update
sudo apt-get install \
ca-certificates \
curl \
gnupg
# Step 2
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
# Step 3
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install Docker Client
# Step 1
sudo apt-get update
# Step 2 (Installs Latest Docker Version Available to the Repo w/ compatible compose)
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose -y
if [[ -f "/etc/redhat-release" ]]; then
sudo mkdir /etc/systemd/system/docker.service.d
sudo touch /etc/systemd/system/docker.service.d/docker.conf
echo "[Service]
ExecStart=
ExecStart=/usr/bin/docker daemon -H fd:// --storage-opt dm.basesize=50G" > /etc/systemd/system/docker.service.d/docker.conf
sudo systemctl daemon-reload
fi
sudo service docker start
}
####################################################################################
#
# Logs in to Docker
#
####################################################################################
function dockerLogin() {
if [[ "$dockerUsername" = "" ]]; then
read -p "Docker username: " dockerUsername
fi
if [[ "$dockerPassword" = "" ]]; then
read -s -p "Docker password: " dockerPassword
fi
sudo docker login -u $dockerUsername -p "$dockerPassword"
if [ $? -ne 0 ]; then
echo "login failed, exiting."
exit 1
fi
}
####################################################################################
#
# Generates docker-compose.yml
#
####################################################################################
function createDockerComposeFile() {
echo "Creating the docker-compose.yml file"
needEnvFile=false
if [[ "$databasePassword" != "" || "$mongoPassword" != "" || "$tomcatAdminPassword" != "" || "$defaultUserPassword" != "" || "$defaulUserApiToken" != "" || "$sendgridApiKey" != "" || "$alertEmail" != "" ]]; then
needEnvFile=true
fi
dockerComposeFile=$configDir/docker-compose.yml
dockerEnvFile=$configDir/.docker-common.env
mkdir -p $configDir
sudo rm -f $dockerComposeFile
sudo touch $dockerComposeFile
if [[ $needEnvFile = true ]]; then
sudo rm -f $dockerEnvFile
sudo touch $dockerEnvFile
if [[ -n "$databasePassword" ]]; then
echo "DATABASE_PASSWORD=$databasePassword" >> $dockerEnvFile
echo "POSTGRES_PASSWORD=$databasePassword" >> $dockerEnvFile
fi
if [[ -n "$mongoPassword" ]]; then
echo "MONGODB_PASSWORD=$mongoPassword" >> $dockerEnvFile
fi
if [[ -n "$tomcatAdminPassword" ]]; then
echo "TOMCAT_PASSWORD=$tomcatAdminPassword" >> $dockerEnvFile
fi
if [[ -n "$defaultUserPassword" ]]; then
echo "ADMIN_USER_PASSWORD=$defaultUserPassword" >> $dockerEnvFile
fi
if [[ -n "$defaulUserApiToken" ]]; then
echo "ADMIN_API_TOKEN=$defaulUserApiToken" >> $dockerEnvFile
fi
if [[ $disableCountdown = true ]]; then
echo "COUNTDOWN_DISABLED=true" >> $dockerEnvFile
fi
if [[ -n "$sendgridApiKey" ]]; then
echo "BACKUP_JOBS_SENDGRID_API_KEY=${sendgridApiKey}" >> $dockerEnvFile
fi
if [[ -n "$alertEmail" ]]; then
echo "ALERT_EMAIL=${alertEmail}" >> $dockerEnvFile
fi
if [[ -n "$samlPassword" ]]; then
echo "SAML_PW=${samlPassword}" >> $dockerEnvFile
fi
if [[ $postgresql = true ]]; then
echo "POSTGRES_BACKUP_CRON=${postgresBackupCron}" >> $dockerEnvFile
fi
if [[ mongo = true ]]; then
echo "MONGO_BACKUP_CRON=${mongoBackupCron}" >> $dockerEnvFile
fi
fi
# setup mongo data dir, postgres data dir files dir and backsup dir
filesDir="${backupDir}/files"
postgresBackupDir="${backupDir}/postgresBackups"
mongoBackupDir="${backupDir}/mongoBackups"
mongoDataDir="${backupDir}/mongoData"
mongoLogDir="${backupDir}/logs/mongodb"
postgresDataDir="${backupDir}/postgresData"
apachessl="${backupDir}/apachessl"
geoserver="${backupDir}/geoserver"
tomcatLogs="${backupDir}/logs/tomcat"
spidaLogs="${backupDir}/logs/spida"
apacheLogs="${backupDir}/logs/apache"
mkdir -p $filesDir $postgresBackupDir $mongoBackupDir $mongoDataDir $mongoLogDir $postgresDataDir $apachessl $geoserver $tomcatLogs $spidaLogs $apacheLogs
HOST_MACHINE_HOST_NAME=`hostname -f`
if [[ $spidaMin = true ]]; then
echo "spidamin:
image: spidasoftware/min:$tag
restart: always
volumes:
- $filesDir:/var/lib/spida/files
- $geoserver:/var/lib/spida/geoserver
- $tomcatLogs:/usr/local/tomcat/logs
- $spidaLogs:/var/lib/spida/logs
environment:
- HOST_MACHINE_HOST_NAME=$HOST_MACHINE_HOST_NAME" >> $dockerComposeFile
if [[ "$serverRoot" != "" ]]; then
echo " - SERVER_ROOT=$serverRoot" >> $dockerComposeFile
fi
if [[ $needEnvFile = true ]]; then
echo " env_file: $dockerEnvFile" >> $dockerComposeFile
fi
echo " links:" >> $dockerComposeFile
echo " - redis" >> $dockerComposeFile
if [[ $postgresql = true ]]; then
echo " - postgresql" >> $dockerComposeFile
fi
if [[ $mongo = true ]]; then
echo " - mongodb" >> $dockerComposeFile
fi
fi
if [[ $postgresql = true ]]; then
POSTGRES_HOSTNAME="postgresql.${HOST_MACHINE_HOST_NAME}"
if [[ "$serverRoot" != "" ]]; then
POSTGRES_HOSTNAME="postgresql.${serverRoot}"
fi
echo "postgresql:
image: spidasoftware/postgresql:$postgresqlTag
restart: always
hostname: $POSTGRES_HOSTNAME
volumes:
- $postgresBackupDir:/backups
- $postgresDataDir:/var/lib/postgresql/data" >> $dockerComposeFile
if [[ $needEnvFile = true ]]; then
echo " env_file: $dockerEnvFile" >> $dockerComposeFile
fi
fi
if [[ $mongo = true ]]; then
MONGODB_HOSTNAME="mongodb.${HOST_MACHINE_HOST_NAME}"
if [[ "$serverRoot" != "" ]]; then
MONGODB_HOSTNAME="mongodb.${serverRoot}"
fi
echo "mongodb:
image: spidasoftware/mongodb:$mongodbTag
restart: always
hostname: $MONGODB_HOSTNAME
volumes:
- $mongoBackupDir:/backups
- $mongoDataDir:/data/db
- $mongoLogDir:/var/log/mongodb" >> $dockerComposeFile
if [[ $needEnvFile = true ]]; then
echo " env_file: $dockerEnvFile" >> $dockerComposeFile
fi
if [[ "$mongodOpts" != "" ]]; then
echo " environment:
- MONGOD_OPTS=$mongodOpts" >> $dockerComposeFile
fi
fi
if [[ $spidaMin = true ]]; then
echo "redis:
image: spidasoftware/redis:$redisTag
restart: always" >> $dockerComposeFile
echo "proxy:
image: spidasoftware/spida-proxy:latest
restart: always
volumes:
- /apps/spidamin/logs/proxy:/logs
links:
- spidamin" >> $dockerComposeFile
echo "apache:
image: spidasoftware/apache:$apacheTag
restart: always
links:
- proxy
ports:
- \"80:80\"
- \"443:443\"
volumes:
- $apachessl:/var/lib/spida/apache_ssl
- $apacheLogs:/var/log/apache2
environment:
- HOST_MACHINE_HOST_NAME=$HOST_MACHINE_HOST_NAME" >> $dockerComposeFile
if [[ "$serverRoot" != "" ]]; then
echo " - SERVER_ROOT=$serverRoot" >> $dockerComposeFile
fi
if [[ "$defaultApacheApp" != "" ]]; then
echo " - DEFAULT_APP_NAME=$defaultApacheApp" >> $dockerComposeFile
fi
fi
}
####################################################################################
#
# Updates docker-compose.yml
#
####################################################################################
function updateDockerComposeFile() {
dockerComposeFile=$configDir/docker-compose.yml
HOST_MACHINE_HOST_NAME=`hostname -f`
#We may still want to override the COUNTDOWN_DISABLED var
dockerEnvFile=$configDir/.docker-common.env
sed -i '/COUNTDOWN_DISABLED/d' $dockerEnvFile
if [[ $disableCountdown = true ]]; then
echo "COUNTDOWN_DISABLED=true" >> $dockerEnvFile
fi
# Update the server root and the host machine
sed -i "s/\s*- SERVER_ROOT=\S*$/ - SERVER_ROOT=${serverRoot}/g" $dockerComposeFile
sed -i "s/\s*- HOST_MACHINE_HOST_NAME=\S*$/ - HOST_MACHINE_HOST_NAME=${HOST_MACHINE_HOST_NAME}/g" $dockerComposeFile
# Update the docker image tags, if they were passed in, they aren't latest.
if [ $MIN_TAG_PASSED = true ]; then
sed -i "s/spidasoftware\/min:\S*$/spidasoftware\/min:$tag/g" $dockerComposeFile
fi
if [ $POSTGRESQL_TAG_PASSED = true ]; then
sed -i "s/spidasoftware\/postgresql:\S*$/spidasoftware\/postgresql:${postgresqlTag}/" $dockerComposeFile
fi
if [ $MONGODB_TAG_PASSED = true ]; then
sed -i "s/spidasoftware\/mongodb:\S*$/spidasoftware\/mongodb:${mongodbTag}/" $dockerComposeFile
fi
if [ $REDIS_TAG_PASSED = true ]; then
sed -i "s/spidasoftware\/redis:\S*$/spidasoftware\/redis:${redisTag}/" $dockerComposeFile
fi
if [ $APACHE_TAG_PASSED = true ]; then
sed -i "s/spidasoftware\/apache:\S*$/spidasoftware\/apache:${apacheTag}/" $dockerComposeFile
fi
POSTGRES_HOSTNAME="postgresql.${HOST_MACHINE_HOST_NAME}"
if [[ "$serverRoot" != "" ]]; then
POSTGRES_HOSTNAME="postgresql.${serverRoot}"
fi
sed -i "s/\s*hostname: postgresql\.\S*/ hostname: ${POSTGRES_HOSTNAME}/" $dockerComposeFile
MONGODB_HOSTNAME="mongodb.${HOST_MACHINE_HOST_NAME}"
if [[ "$serverRoot" != "" ]]; then
MONGODB_HOSTNAME="mongodb.${serverRoot}"
fi
sed -i "s/\s*hostname: mongodb\.\S*/ hostname: ${MONGODB_HOSTNAME}/" $dockerComposeFile
# Update the passwords, if they're passed in
if [[ -n "$databasePassword" ]]; then
sed -i "/DATABASE_PASSWORD=\S*$/d" $dockerEnvFile
sed -i "/POSTGRES_PASSWORD=\S*$/d" $dockerEnvFile
echo "DATABASE_PASSWORD=$databasePassword" >> $dockerEnvFile
echo "POSTGRES_PASSWORD=$databasePassword" >> $dockerEnvFile
fi
if [[ -n "$mongoPassword" ]]; then
sed -i "/MONGODB_PASSWORD=\S*$/d" $dockerEnvFile
echo "MONGODB_PASSWORD=$mongoPassword" >> $dockerEnvFile
rm $backupDir/mongoData/.mongodb_password_set
fi
if [[ -n "$tomcatAdminPassword" ]]; then
sed -i "/TOMCAT_PASSWORD=\S*$/d" $dockerEnvFile
echo "TOMCAT_PASSWORD=$tomcatAdminPassword" >> $dockerEnvFile
fi
if [[ -n "$defaultUserPassword" ]]; then
sed -i "/ADMIN_USER_PASSWORD=\S*$/d" $dockerEnvFile
echo "ADMIN_USER_PASSWORD=$defaultUserPassword" >> $dockerEnvFile
fi
if [[ -n "$defaulUserApiToken" ]]; then
sed -i "/ADMIN_API_TOKEN=\S*$/d" $dockerEnvFile
echo "ADMIN_API_TOKEN=$defaulUserApiToken" >> $dockerEnvFile
fi
if [[ -n "$sendgridApiKey" ]]; then
sed -i "/BACKUP_JOBS_SENDGRID_API_KEY=\S*$/d" $dockerEnvFile
echo "BACKUP_JOBS_SENDGRID_API_KEY=${sendgridApiKey}" >> $dockerEnvFile
fi
if [[ -n "$alertEmail" ]]; then
sed -i "/ALERT_EMAIL=\S*$/d" $dockerEnvFile
echo "ALERT_EMAIL=${alertEmail}" >> $dockerEnvFile
fi
if [[ -n "$samlPassword" ]]; then
sed -i "/SAML_PASSWORD=\S*$/d" $dockerEnvFile
echo "SAML_PASSWORD=${samlPassword}" >> $dockerEnvFile
fi
if [[ -n "$interchangeCookieName" ]]; then
echo "INTERCHANGE_TOKEN_NAME=${interchangeCookieName}" >> $dockerEnvFile
fi
if [[ -n "$interchangeCookieSecret" ]]; then
echo "INTERCHANGE_CS=${interchangeCookieSecret}" >> $dockerEnvFile
fi
unset dockerEnvFile
}
####################################################################################
#
# Setup docker container log rotation
#
####################################################################################
function setupLogRotate() {
sudo touch /etc/logrotate.d/docker-container
echo "/var/lib/docker/containers/*/*.log {
rotate 7
daily
compress
size=1G
missingok
copytruncate
}" >> /etc/logrotate.d/docker-container
}
####################################################################################
#
# Execute above functions
#
####################################################################################
parseCommandLineArguments $@
installDocker
dockerLogin
if [ $hasDockerConfig = false -o ! \( -f $configDir/docker-compose.yml -a -f $configDir/.docker-common.env \) ]; then
updatePasswords=false
createDockerComposeFile
else
updateDockerComposeFile
updatePasswords=true
fi
setupLogRotate
dockerComposeFile=$configDir/docker-compose.yml
if [ $updatePasswords = true ] && [ $postgresql = true ]; then
sudo docker-compose -f $dockerComposeFile up -d postgresql
waitForService postgresql
sleep 30
sudo docker exec spida_postgresql_1 /reset-passwords.sh
fi
if [ $dockerStart = true ]; then
sudo docker-compose -f $dockerComposeFile up -d
else
if [ $updatePasswords = true ]; then
sudo docker-compose -f $dockerComposeFile down
fi
sudo docker-compose -f $dockerComposeFile pull
fi
sudo docker logout