-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbig_red_button.sh
executable file
·298 lines (236 loc) · 9.14 KB
/
big_red_button.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
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
#!/bin/bash
#
# Big Red Button completely removes all containers, uninstalls k3s, and removes all files within spacedev.
#
# arguments:
#
#
# Example Usage:
#
# "bash ./scripts/big_red_button.sh"
# shellcheck disable=SC1091
# shellcheck disable=SC2068
source "$(dirname "$(realpath "$0")")/../modules/load_modules.sh" $@ --log_dir /var/log
############################################################
# Help #
############################################################
function show_help() {
# Display Help
echo "Completely removes all containers, uninstalls k3s, and removes all files within spacedev."
echo
echo "Syntax: bash ./scripts/big_red_button.sh"
echo "options:"
echo "--help | -h [OPTIONAL] Help script (this screen)"
echo
exit 1
}
############################################################
# Process the input options. Add options as needed. #
############################################################
# Get the options
while [[ "$#" -gt 0 ]]; do
case $1 in
-h|--help) show_help ;;
*) echo "Unknown parameter '$1'"; show_help ;;
esac
shift
done
function show_header() {
info_log " ____ _ _____ _ ____ _ _ "
info_log " | _ \(_) | __ \ | | | _ \ | | | | "
info_log " | |_) |_ __ _ | |__) |___ __| | | |_) |_ _| |_| |_ ___ _ __ "
info_log " | _ <| |/ _ | | _ // _ \/ _ | | _ <| | | | __| __/ _ \| _ \ "
info_log " | |_) | | (_| | | | \ \ __/ (_| | | |_) | |_| | |_| || (_) | | | |"
info_log " |____/|_|\__, | |_| \_\___|\__,_| |____/ \__,_|\__|\__\___/|_| |_|"
info_log " __/ | "
info_log " |___/ "
}
############################################################
# Stops the k3s service if it's running
############################################################
function check_and_disable_k3s() {
if [[ -f "/etc/systemd/system/k3s.service" ]]; then
info_log "Disabling k3s service"
run_a_script "systemctl disable k3s"
run_a_script "systemctl stop k3s"
fi
}
############################################################
# Stops and removes all docker containers
############################################################
function stop_all_docker_containers() {
info_log "START: ${FUNCNAME[0]}"
is_cmd_available "docker" has_cmd
# shellcheck disable=SC2154
if [[ "${has_cmd}" == false ]]; then
info_log "...docker not found. Nothing do to"
info_log "END: ${FUNCNAME[0]}"
return
fi
info_log "Pausing all docker containers..."
run_a_script "docker ps -q" all_docker_containers --disable_log
for container_id in $all_docker_containers; do
info_log "...pausing container id ${container_id}..."
run_a_script "docker pause ${container_id}" --ignore_error --disable_log
done
info_log "...stopping container processes..."
docker_pids=$(ps -e | grep 'containerd-shim' | awk '{print $1}')
# Kill the Docker container processes
for pid in $docker_pids; do
run_a_script "kill -9 $pid" --disable_log
done
info_log "...removing containers...."
run_a_script "docker ps -a -q" all_docker_containers --disable_log
for container_id in $all_docker_containers; do
info_log "...removing container id ${container_id}..."
run_a_script "docker rm ${container_id} -f" results --ignore_error --disable_log
done
info_log "...all docker containers removed."
info_log "END: ${FUNCNAME[0]}"
}
############################################################
# Uninstall k3s
############################################################
function remove_k3s() {
info_log "START: ${FUNCNAME[0]}"
is_cmd_available "k3s" has_cmd
# shellcheck disable=SC2154
if [[ "${has_cmd}" == false ]]; then
info_log "...k3s not found. Nothing do to"
info_log "END: ${FUNCNAME[0]}"
return
fi
info_log "...k3s found. Uninstalling..."
if [[ -f "/usr/local/bin/k3s-uninstall.sh" ]]; then
run_a_script "/usr/local/bin/k3s-uninstall.sh" k3s_uninstall_pid --background
debug_log "...wating for k3s to finish uninstalling (pid $k3s_uninstall_pid)..."
wait $((k3s_uninstall_pid))
fi
info_log "Cleaning \$PATH array of any k3s references"
#Loop through the PATH array and remove any paths that contain 'k3s'
IFS=':' read -r -a path_array <<< "$PATH"
cleaned_paths=()
for path in "${path_array[@]}"; do
if [[ "$path" != *k3s* ]]; then
cleaned_paths+=("$path")
fi
done
# Rebuild the PATH array and export it back out
cleaned_path=$(IFS=:; echo "${cleaned_paths[*]}")
export PATH=$cleaned_path
info_log "...k3s successfully uninstalled"
info_log "END: ${FUNCNAME[0]}"
}
############################################################
# Prune Docker
############################################################
function prune_docker() {
info_log "START: ${FUNCNAME[0]}"
is_cmd_available "docker" has_cmd
# shellcheck disable=SC2154
if [[ "${has_cmd}" == false ]]; then
info_log "...docker not found. Nothing do to"
info_log "END: ${FUNCNAME[0]}"
return
fi
info_log "Pruning docker..."
run_a_script "docker system prune --all --volumes --force"
info_log "...docker pruned."
info_log "END: ${FUNCNAME[0]}"
}
############################################################
# Check if registry is still running by PID and if so, remove it
############################################################
function prune_registry() {
info_log "START: ${FUNCNAME[0]}"
is_cmd_available "pgrep" HAS_PGREP
info_log "Stopping registry processes (if still running)"
if [[ "${HAS_PGREP}" == true ]]; then
run_a_script "pgrep '^registry'" pids --ignore_error
else
run_a_script "ps aux | grep '^registry' | grep -v grep | awk '{print \$2}'" pids --ignore_error
fi
for pid in $pids; do
debug_log "...terminating process id '${pid}'"
run_a_script "kill -9 ${pid}" --disable_log --ignore_error
done
info_log "...successfully stopped registry processes."
info_log "Stopping pypiserver processes (if still running)"
if [[ "${HAS_PGREP}" == true ]]; then
run_a_script "pgrep '^pypi-server'" pids --ignore_error
else
run_a_script "ps aux | grep '^pypi-server' | grep -v grep | awk '{print \$2}'" pids --ignore_error
fi
for pid in $pids; do
debug_log "...terminating process id '${pid}'"
run_a_script "kill -9 ${pid}" --disable_log --ignore_error
done
info_log "...successfully stopped pypiserver processes."
info_log "END: ${FUNCNAME[0]}"
}
############################################################
# Remove apps that we installed as part of setup
############################################################
function remove_app(){
local app=""
while [[ "$#" -gt 0 ]]; do
case $1 in
--app)
shift
app=$1
;;
*) echo "Unknown parameter '$1'"; show_help ;;
esac
shift
done
run_a_script "which -a ${app}" app_paths --ignore_error
for app_path in $app_paths; do
if [[ -f "$app_path" ]]; then
debug_log "Removing ${app} at $app_path"
run_a_script "sudo rm -f $app_path"
debug_log "...successfull removed old version of ${app} at $app_path"
fi
done
}
############################################################
# Remove k3s data directory if its been changed
############################################################
function remove_k3s_data_dir() {
info_log "START: ${FUNCNAME[0]}"
run_a_script "jq -r '.config.clusterDataDir' ${SPACEFX_DIR}/tmp/config/spacefx-config.json" cluster_data_dir
if [[ -z "${cluster_data_dir}" ]] ; then
info_log "Cluster data directory not set. Nothing to do."
info_log "END: ${FUNCNAME[0]}"
return
fi
if [[ ! -d "${cluster_data_dir}" ]]; then
info_log "Cluster data directory already removed. Nothing to do."
info_log "END: ${FUNCNAME[0]}"
return
fi
info_log "Removing all directories and files under '${cluster_data_dir}'..."
run_a_script "rm -rf ${cluster_data_dir}/*"
info_log "...successfully removed all the directories and files under '${cluster_data_dir}'..."
info_log "END: ${FUNCNAME[0]}"
}
function main() {
show_header
check_and_disable_k3s
stop_all_docker_containers
remove_k3s
prune_docker
prune_registry
remove_k3s_data_dir
info_log "Removing '${SPACEFX_DIR:?}'..."
run_a_script "rm -rf ${SPACEFX_DIR:?}"
info_log "...successfully removed '${SPACEFX_DIR:?}'"
remove_app --app "yq"
remove_app --app "jq"
remove_app --app "regctl"
remove_app --app "cfssl"
remove_app --app "cfssljson"
remove_app --app "helm"
info_log "------------------------------------------"
info_log "END: ${SCRIPT_NAME}"
}
main