-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmb
executable file
·181 lines (155 loc) · 4.95 KB
/
mb
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
#!/bin/bash
#set -euo pipefail
set -uo pipefail
dodocker() {
docker "$@"
}
docompose() {
docker compose "$@"
}
determine_distro() {
# Determine OS platform
__uname=$(uname | tr "[:upper:]" "[:lower:]")
# If Linux, try to determine specific distribution
if [ "$__uname" == "linux" ]; then
# If available, use LSB to identify distribution
if [ -f /etc/lsb-release -o -d /etc/lsb-release.d ]; then
__distro=$(lsb_release -i | cut -d: -f2 | sed s/'^\t'//)
# Otherwise, use release info file
else
__distro=$(ls -d /etc/[A-Za-z]*[_-][rv]e[lr]* | grep -v "lsb" | cut -d'/' -f3 | cut -d'-' -f1 | cut -d'_' -f1)
fi
fi
# For everything else (or if above failed), just use generic identifier
[ "$__distro" == "" ] && __distro=$__uname
unset __uname
__distro=$(echo $__distro | tr "[:upper:]" "[:lower:]")
}
start() {
docompose up -d --remove-orphans
}
up() {
start
}
stop() {
docompose down --remove-orphans
}
down() {
stop
}
restart() {
stop
start
}
logs() {
docompose logs "$@"
}
# pull() {
# docompose pull "$@"
# }
shell() {
docompose exec "$@" bash
}
check_for_snap() {
if [[ "$__distro" = "ubuntu" && -n "$(command -v snap)" ]] && snap list 2>/dev/null | grep -qw 'docker'; then
echo
echo "WARNING! Snap docker package detected. This WILL result in issues."
echo "Removing the package will delete volumes and require a resync,"
echo "as well as re-import of all validator keys."
echo
echo "Doing so is still highly recommended however."
echo
echo "The exact steps depend a little on whether there already is"
echo "an apt version of docker installed as well, but in a nutshell"
echo '"./mb stop" followed by "sudo snap remove --purge docker"'
echo "and then a reboot, and as needed install docker.io or docker-ce with apt,"
echo "and restart mb."
echo
echo "Aborting, this is not safe"
exit 1
fi
}
# Arguments are passed, but shellcheck doesn't recognize that
# shellcheck disable=SC2120
update() {
__free_space=$(df -P $(pwd) | awk '/[0-9]%/{print $(NF-2)}')
re='^[0-9]+$'
if ! [[ "${__free_space}" =~ $re ]] ; then
echo "Unable to determine free disk space. This is likely a bug."
echo "df reports $(df -P $(pwd)) and __free_space is ${__free_space}"
elif [ "$(df -P $(pwd) | awk '/[0-9]%/{print $(NF-2)}')" -lt 1024 ]; then
echo "You have less than 1 MiB of space left on $(pwd)."
echo "Aborting, as an update is not safe."
exit 1
fi
__docker_dir=$(dodocker system info --format '{{.DockerRootDir}}')
__free_space=$(df -P ${__docker_dir} | awk '/[0-9]%/{print $(NF-2)}')
re='^[0-9]+$'
if ! [[ "${__free_space}" =~ $re ]] ; then
echo "Unable to determine free disk space. This is likely a bug."
echo "df reports $(df -P ${__docker_dir}) and __free_space is ${__free_space}"
elif [ "${__free_space}" -lt 1048576 ]; then
echo "You have less than 1 GiB of space left on ${__docker_dir}."
echo "Aborting, as an update is not safe."
exit 1
fi
dodocker system prune --force
docompose --profile tools pull # --ignore-pull-failures --quiet || true
docompose --profile tools build --pull
echo
echo "An \"./mb up\" command will start using the new images."
check_for_snap
}
printhelp() {
me=$(basename "${BASH_SOURCE}")
echo "usage: ${me} [help|-h|--help] <subcommand>"
echo ""
echo "optional arguments:"
echo " help | -h | --help"
echo " print this message and exit"
echo ""
echo "subcommands:"
echo " start"
echo " starts the configured files"
echo " stop"
echo " stops"
echo " restart"
echo " restarts, a combination of stop and start"
echo " update"
echo " pulls updated images"
echo " shell"
echo " open a shell to the targeted container"
echo " logs"
echo " shows logs"
echo " docompose <command>"
echo " executes any arbitrary docker compose command. Use \"docompose help\" to list them"
echo ""
echo ""
echo "The logs subcommand can be appended by flags and specify the container(s). example: "
echo ""
echo " $me logs -f --tail 500 plex"
echo " shows logs only for plex service"
echo ""
echo ""
echo "Be sure to run commands either with sudo, or as a user who is part of the \"docker\" group"
echo ""
exit 0
}
if [[ "$#" -eq 0 || "$1" == "help" || "$1" == "-h" || "$1" == "--help" ]]; then
printhelp
fi
cd $(dirname $(realpath ${BASH_SOURCE}))
# Use this to make sure root doesn't end up owning files
if [[ "$OSTYPE" == "darwin"* ]]; then
OWNER=$(stat -f '$Su' .)
else
OWNER=$(stat -c '%U' .)
fi
if [ "${OWNER}" == "root" ]; then
echo "Please install as a non-root user."
exit 1
fi
command="$1"
shift
determine_distro
"$command" $@