This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackend.sh
executable file
·157 lines (132 loc) · 3 KB
/
backend.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
#!/bin/bash
export VERBOSE=
function shout() {
echo -e "${COLOR}${*}${NC}"
}
# Print the usage message
function printHelp() {
echo "Usage: "
echo " backend.sh <Mode> [Flags]"
echo " <Mode>"
echo " - 'up' - bring up all backend components (backend, eventstore, mongo)"
echo " - 'down' - bring down all backend components and clean up"
echo " - 'restart' - restart the all backend components"
echo
echo " Flags:"
echo " -verbose - verbose mode"
echo " backend.sh -h (print this message)"
echo
echo " Possible Mode and flags"
echo " backend.sh up"
echo " backend.sh down"
echo
echo " Taking all defaults:"
echo " backend.sh up"
echo
echo " Examples:"
echo " backend.sh up"
echo " backend.sh down"
exit 0
}
function allUp() {
shout "Bringing up all backend components ..."
echo
docker-compose build
docker-compose up -d 2>&1
docker ps -a
if [ $? -ne 0 ]; then
echo "ERROR !!!! Unable to start backend"
exit 1
fi
exit 0
}
function allDown() {
shout "Bringing down all backend components ..."
echo
docker-compose down --volumes --remove-orphans
clearContainers
removeUnwantedImages
exit 0
}
# Obtain CONTAINER_IDS and remove them
# This function is called when you bring a network down
function clearContainers() {
CONTAINER_IDS=$(docker ps -a | awk '($2 ~ /backend.*/) {print $1}')
if [ -z "$CONTAINER_IDS" -o "$CONTAINER_IDS" == " " ]; then
echo "---- No containers available for deletion ----"
else
docker rm -f $CONTAINER_IDS
fi
}
# Delete any images that were generated as a part of this setup
# specifically the following images are often left behind:
# This function is called when you bring the network down
function removeUnwantedImages() {
DOCKER_IMAGE_IDS=$(docker images | awk '($1 ~ /backend.*/) {print $3}')
if [ -z "$DOCKER_IMAGE_IDS" -o "$DOCKER_IMAGE_IDS" == " " ]; then
echo "---- No images available for deletion ----"
else
docker rmi -f $DOCKER_IMAGE_IDS
fi
}
# Properties
# colors
COLOR='\033[0;36m'
NC='\033[0m' # No Color
###############################################################################
# Parse commandline args
## Parse mode
if [[ $# -lt 1 ]] ; then
printHelp
exit 0
else
MODE=$1
shift
fi
# parse flags
while [[ $# -ge 1 ]] ; do
key="$1"
case $key in
-h )
printHelp
exit 0
;;
-verbose )
VERBOSE="-verbose"
shift
;;
* )
echo
echo "Unknown flag: $key"
echo
printHelp
exit 1
;;
esac
shift
done
# Determine mode of operation and printing out what we asked for
if [ "$MODE" == "up" ]; then
echo "Starting all backend components"
echo
elif [ "$MODE" == "down" ]; then
echo "Stopping all backend components"
echo
elif [ "$MODE" == "restart" ]; then
echo "Restarting all backend components"
echo
else
printHelp
exit 1
fi
if [ "${MODE}" == "up" ]; then
allUp
elif [ "${MODE}" == "down" ]; then
allDown
elif [ "${MODE}" == "restart" ]; then
allDown
allUp
else
printHelp
exit 1
fi