-
Notifications
You must be signed in to change notification settings - Fork 17
/
dockerbunker.sh
executable file
·192 lines (171 loc) · 5.39 KB
/
dockerbunker.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
#!/usr/bin/env bash
if [[ -r /etc/redhat-release ]];then
if ! dnf list installed docker-ce &>/dev/null;then
docker_missing=1
fi
elif [[ -r /etc/debian_version ]];then
if ! dpkg -l docker &>/dev/null;then
docker_missing=1
fi
fi
[[ $docker_missing ]] \
&& echo -e "\n\e[3m\xe2\x86\x92 \e[1mCould not find docker.\e[3m\n\nMost systems can install Docker by running:\n\nwget -qO- https://get.docker.com/ | sh\n";
# Find base dir
BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "${BASE_DIR}"/data/include/init.sh
[[ -f "data/env/dockerbunker.env" ]] && source "data/env/dockerbunker.env" || init_dockerbunker
unset AVAILABLE_SERVICES count
# All available services
IFS=$'\n' declare -a ALL_SERVICES=( "$(cat ${BASE_DIR}/included_services)" )
[[ -f "${BASE_DIR}"/included_services.custom ]] \
&& IFS=$'\n' declare -a ALL_SERVICES+=( "$(cat ${BASE_DIR}/included_services.custom | sed '/^#/d')" )
IFS=$'\n' sorted=($(printf '%s\n' "${ALL_SERVICES[@]}"|sort))
# style menu according to what status service has
declare -A SERVICES_ARR
for service in "${sorted[@]}";do
service_name="$(echo -e "${service,,}" | tr -cd '[:alnum:]')"
if elementInArray "$service" "${INSTALLED_SERVICES[@]}";then
if elementInArray "$service" "${STOPPED_SERVICES[@]}";then
service_status="$(printf "\e[32m${service}\e[0m \e[31m(Stopped)\e[0m")"
else
service_status="$(printf "\e[32m${service}\e[0m")"
fi
SERVICES_ARR+=( ["$service_status"]="${service_name}" )
AVAILABLE_SERVICES+=( "$service_status" )
elif elementInArray "${service}" "${CONFIGURED_SERVICES[@]}";then
service_status="$(printf "\e[33m${service}\e[0m")"
SERVICES_ARR+=( ["$service_status"]="${service_name}" )
AVAILABLE_SERVICES+=( "$service_status" )
else
service_status="$(printf "${service}")"
SERVICES_ARR+=( ["$service_status"]="${service_name}" )
AVAILABLE_SERVICES+=( "$service_status" )
fi
done
startall=$(printf "\e[1;4;33mStart all stopped containers\e[0m")
stopall=$(printf "\e[1;4;33mStop all running containers\e[0m")
startnginx=$(printf "\e[1;4;33mStart nginx container\e[0m")
stopnginx=$(printf "\e[1;4;33mStop nginx container\e[0m")
restartnginx=$(printf "\e[1;4;33mRestart nginx container\e[0m")
renewcerts=$(printf "\e[1;4;33mRenew Let's Encrypt certificates\e[0m")
restartall=$(printf "\e[1;4;33mRestart all containers\e[0m")
destroyall=$(printf "\e[1;4;33mDestroy everything\e[0m")
exitmenu=$(printf "\e[1;4;33mExit\e[0m")
count=$((${#AVAILABLE_SERVICES[@]}+1))
[[ ${STOPPED_SERVICES[0]} ]] \
&& AVAILABLE_SERVICES+=( "$startall" )
[[ $(docker ps -q --filter "status=running" --filter name=^/nginx-dockerbunker$) ]] \
&& AVAILABLE_SERVICES+=( "$stopnginx" ) \
&& AVAILABLE_SERVICES+=( "$restartnginx") \
&& count=$(($count+2))
[[ -d "${CONF_DIR}"/nginx/ssl/letsencrypt/live ]] \
&& [[ ${#INSTALLED_SERVICES[@]} > 0 ]] \
&& [[ $(ls -A "${CONF_DIR}"/nginx/ssl/letsencrypt/live) ]] \
&& AVAILABLE_SERVICES+=( "$renewcerts" ) && cound=$(($count+1))
[[ ${#INSTALLED_SERVICES[@]} > 0 \
|| ${#STATIC_SITES[@]} > 0 \
|| ${#CONFIGURED_SERVICES[@]} > 0 ]] \
&& AVAILABLE_SERVICES+=( "$destroyall" ) \
&& count=$(($count+1))
[[ $(docker ps -q --filter "status=exited" --filter name=^/nginx-dockerbunker$) ]] \
&& AVAILABLE_SERVICES+=( "$startnginx" )
[[ $(docker ps -q --filter "status=running" --filter name=dockerbunker) \
&& ${#INSTALLED_SERVICES[@]} > 1 ]] \
&& AVAILABLE_SERVICES+=( "$stopall" ) \
&& count=$(($count+1))
[[ $(docker ps -q --filter "status=exited" --filter name=dockerbunker) \
&& ${#STOPPED_SERVICES[@]} > 1 ]] \
&& AVAILABLE_SERVICES+=( "$startall" ) \
&& count=$(($count+1))
[[ ${#INSTALLED_SERVICES[@]} > 1 ]] \
&& AVAILABLE_SERVICES+=( "$restartall" ) \
&& count=$(($count+1))
echo ""
echo "Please select the service you want to manage"
echo ""
[[ ${INSTALLED_SERVICES[@]} ]] && echo -e " \e[32mGreen\e[0m: Installed"
[[ ${CONFIGURED_SERVICES[@]} ]] && echo -e " \e[33mOrange\e[0m: Configured"
echo ""
COLUMNS=12
select choice in "${AVAILABLE_SERVICES[@]}" "$exitmenu"
do
case $choice in
"$exitmenu")
exit 0
;;
"$startnginx")
echo ""
start_nginx
say_done
sleep 1
break
;;
"$stopnginx")
echo ""
stop_nginx
say_done
sleep 1
break
;;
"$restartnginx")
prevent_nginx_restart=1
echo ""
restart_nginx
say_done
sleep 1
break
;;
"$renewcerts")
echo -e "\n\e[3m\xe2\x86\x92 Renew Let's Encrypt certificates\e[0m\n"
"${BASE_DIR}"/certbot.sh
restart_nginx
say_done
sleep 1
break
;;
"$startall")
prevent_nginx_restart=1
start_all
say_done
sleep 1
break
;;
"$stopall")
prevent_nginx_restart=1
stop_all
say_done
sleep 1
break
;;
"$destroyall")
echo -e "\n\e[3m\xe2\x86\x92 Destroy everything\e[0m"
echo ""
echo -e "\e[1mReset dockerbunker to its initial state\e[0m"
echo ""
echo "The following will be removed:"
echo ""
echo "- All dockerbunker container(s)"
echo "- All dockerbunker volume(s)"
echo "- All environment file(s)"
echo "- All nginx configuration files"
echo "- All SSL Certificates"
echo ""
prompt_confirm "Continue?" \
&& destroy_all=1 destroy_all
say_done
exit 0
;;
$choice)
if [[ -z $choice ]];then
echo "Please choose a number from 1 to $count"
else
service="$(echo -e "${choice,,}" | tr -d '[:space:]')"
echo ""
echo -e "\n\e[3m\xe2\x86\x92 Checking service status"
echo ""
source "${BASE_DIR}"/data/services/${SERVICES_ARR[$choice]}/${SERVICES_ARR[$choice]}.sh
break
fi
;;
esac
done