This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
uninstall.sh
62 lines (50 loc) · 1.87 KB
/
uninstall.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
#!/bin/bash
NETWORK_NAME="prem-gateway"
volume_exists() {
local volume_name="$1"
docker volume ls -q | grep -q "^$volume_name$"
return $?
}
# Ask if the user wants a "yes to all" approach.
read -p "Do you want to say 'yes' to all and proceed with a full uninstall without further confirmations? (y/n): " yes_to_all
# Helper function to check if yes to all was selected.
should_proceed() {
if [ "$yes_to_all" == "y" ]; then
echo "y"
else
read -p "$1 (y/n): " response
echo "$response"
fi
}
# Stop and remove containers and their associated volumes on the network
containers_to_remove=$(docker network inspect $NETWORK_NAME --format '{{range .Containers}}{{.Name}} {{end}}')
if [[ ! -z "$containers_to_remove" ]]; then
docker stop $containers_to_remove
# Capture the named volumes of the containers
volumes_to_remove=()
for container in $containers_to_remove; do
container_volumes=$(docker inspect $container --format '{{range .Mounts}}{{if .Name}}{{.Name}}{{end}}{{end}}')
for volume in $container_volumes; do
volumes_to_remove+=($volume)
done
done
docker rm -v $containers_to_remove
# Remove the captured named volumes
for volume in "${volumes_to_remove[@]}"; do
if volume_exists "$volume"; then
docker volume rm "$volume"
fi
done
fi
# Optionally remove the network itself
remove_network=$(should_proceed "Do you want to remove the network $NETWORK_NAME?")
if [ "$remove_network" == "y" ]; then
docker network rm $NETWORK_NAME
fi
# Check if the datadir should be removed.
ORIGINAL_HOME=$(eval echo ~$SUDO_USER)
remove_datadir=$(should_proceed "Do you want to remove the datadir located in $ORIGINAL_HOME/prem? (This deletes data!)")
if [ "$remove_datadir" == "y" ]; then
rm -rf $ORIGINAL_HOME/prem
fi
echo "Uninstallation completed!"