-
Notifications
You must be signed in to change notification settings - Fork 3
/
zebrunner.sh
executable file
·181 lines (156 loc) · 3.38 KB
/
zebrunner.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
#!/bin/bash
CONTAINER_NAME="sonarqube"
echo_warning() {
echo "
WARNING! $1"
}
echo_telegram() {
echo "
For more help join telegram channel: https://t.me/zebrunner
"
}
status() {
source .env
local container_status=$(docker inspect $CONTAINER_NAME -f {{.State.Health.Status}} 2> /dev/null)
if [[ -z "$container_status" ]]; then
echo_warning "There's no container $CONTAINER_NAME"
exit -1
fi
echo "$CONTAINER_NAME is $container_status"
case "$container_status" in
"healthy")
return 0
;;
"unhealthy")
return 1
;;
"starting")
return 2
;;
esac
}
setup() {
echo
}
shutdown() {
if [[ -f .disabled ]]; then
rm -f .disabled
exit 0 #no need to proceed as nothing was configured
fi
docker compose down -v
}
start() {
if [[ -f .disabled ]]; then
exit 0
fi
docker network inspect infra >/dev/null 2>&1 || docker network create infra
docker compose up -d
}
stop() {
if [[ -f .disabled ]]; then
exit 0
fi
docker compose stop
}
down() {
if [[ -f .disabled ]]; then
exit 0
fi
docker compose down
}
backup() {
if [[ -f .disabled ]]; then
exit 0
fi
status
if [[ $? -ne -1 ]]; then
echo "Backup $CONTAINER_NAME container"
docker run --rm --volumes-from $CONTAINER_NAME -v "$(pwd)"/backup:/var/backup "ubuntu" tar -czvf /var/backup/sonarqube.tar.gz /opt/sonarqube
else
echo_warning "Impossible backup $CONTAINER_NAME container!"
echo_telegram
fi
}
restore() {
if [[ -f .disabled ]]; then
exit 0
fi
status
if [[ $? -ne -1 ]]; then
echo "Restore $CONTAINER_NAME container"
stop
docker run --rm --volumes-from $CONTAINER_NAME -v "$(pwd)"/backup:/var/backup "ubuntu" bash -c "cd / && tar -xzvf /var/backup/sonarqube.tar.gz"
down
else
echo_warning "Impossible restore $CONTAINER_NAME container"
echo_telegram
fi
}
version() {
if [[ -f .disabled ]]; then
exit 0
fi
source .env
echo "sonarqube: ${TAG_SONAR}"
}
echo_help() {
echo "
Usage: ./zebrunner.sh [option]
Flags:
--help | -h Print help
Arguments:
start Start container
stop Stop and keep container
status Show $CONTAINER_NAME container status
restart Restart container
down Stop and remove container
shutdown Stop and remove container, clear volumes
backup Backup container
restore Restore container
version Version of container"
echo_telegram
exit 0
}
BASEDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "${BASEDIR}" || exit
case "$1" in
setup)
setup
;;
start)
start
;;
stop)
stop
;;
restart)
down
start
;;
down)
down
;;
shutdown)
shutdown
;;
backup)
backup
;;
restore)
restore
;;
status)
status
;;
version)
version
;;
--help | -h)
echo_help
;;
*)
echo "Invalid option detected: $1"
echo_help
exit 1
;;
esac