-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage-stack.sh
executable file
·108 lines (93 loc) · 2.36 KB
/
manage-stack.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
#!/usr/bin/env bash
set -eu
ME=$(basename "$0")
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
readonly DOCKER_COMPOSE_FILE=${DIR}/docker/docker-compose.yml
export STACK_DIR=${DIR}/data
export STACK_ID="${1:-}"
export STACK_PORT="${2:-}"
export ACTION="${3:-}"
logOut() {
echo -e "[${ME}] $(date +%Y-%m-%dT%H:%M:%S%z) | ${1}"
}
failErrOut() {
echo "[${ME}] $(date +%Y-%m-%dT%H:%M:%S%z) | ERROR - ${1} ! Exiting ..." >&2
exit 1
}
printHelp() {
echo "Usage: ${ME} <stack-id> <stack-port> <action>"
echo
echo " stack-id The stack runs as a user with the 'stack-id' uid."
echo " stack-port The port that apache2 binds to."
echo " action 'create' or 'destroy' the stack."
echo
echo " Example:"
echo " ./${ME} "
echo
}
validateInput() {
if [[ -z $STACK_ID ]]; then
printHelp
failErrOut "stack-id must be set"
fi
if [[ -z $STACK_PORT ]]; then
printHelp
failErrOut "stack-port must be set"
fi
if [[ -z $ACTION ]]; then
printHelp
failErrOut "action must be set"
fi
return 0
}
createStackDir() {
logOut 'Creating directories ...'
mkdir -p ${STACK_DIR}/${STACK_ID}/{secrets,mysql}
chmod 700 ${STACK_DIR}/${STACK_ID}
chown -R ${STACK_ID}:${STACK_ID} ${STACK_DIR}/${STACK_ID}
logOut 'Done.'
}
destroyStackDir() {
logOut 'Destroying directories ...'
# TODO: implement mysql backup
rm -rf ${STACK_DIR}/${STACK_ID}
logOut 'Done.'
}
runDockerCompose() {
logOut 'Running docker compose ...'
local dc_action=$1
if [ "$dc_action" = "up" ]; then
docker-compose -f $DOCKER_COMPOSE_FILE -p $STACK_ID up -d
elif [ "$dc_action" = "down" ]; then
docker-compose -f $DOCKER_COMPOSE_FILE -p $STACK_ID down
else
failErrOut "Unknown docker-compose action '${dc_action}'"
fi
logOut 'Done.'
}
generateRandomString() {
echo -n $(openssl rand -base64 12)
}
generateSecrets() {
logOut 'Generating secrets ...'
generateRandomString > ${STACK_DIR}/${STACK_ID}/secrets/rpass
generateRandomString > ${STACK_DIR}/${STACK_ID}/secrets/dbuser
generateRandomString > ${STACK_DIR}/${STACK_ID}/secrets/dbpass
logOut 'Done.'
}
validateInput
case $ACTION in
create)
createStackDir
generateSecrets
runDockerCompose 'up'
;;
destroy)
runDockerCompose 'down'
destroyStackDir
;;
*)
printHelp
failErrOut "Only 'create' and 'destroy' actions are allowed"
;;
esac