-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrewpi.sh
executable file
·221 lines (189 loc) · 4.49 KB
/
brewpi.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
#!/bin/bash
DATADIR=/home/pi/brewpi-data
CONFIG="${DATADIR}/settings/config.cfg"
CONTAINER=brewpi
IMAGE=brewpi/brewpi-raspbian
PRG=$( basename $0 )
function is_running() {
[[ $DEBUG -eq 1 ]] && set -x
num=$( docker ps -q \
--filter "name=$CONTAINER" \
--filter "status=running" \
| wc -l )
[[ $num -gt 0 ]]
}
function container_id() {
[[ $DEBUG -eq 1 ]] && set -x
docker ps -a -f "status=exited" --format '{{.ID}} {{.Names}}' \
| grep "$CONTAINER" \
| cut -d' ' -f1
}
function container_exists() {
[[ $DEBUG -eq 1 ]] && set -x
[[ -n $(container_id) ]]
}
function docker_start_old() {
[[ $DEBUG -eq 1 ]] && set -x
docker container restart $( container_id )
}
function docker_start_new() {
[[ $DEBUG -eq 1 ]] && set -x
local _restart='--restart always'
[[ -n "$1" ]] && _restart=''
docker run -d \
--name $CONTAINER \
-p 80:80 \
-v ~/brewpi-data:/data \
-v /etc/timezone:/etc/timezone \
-v /etc/localtime:/etc/localtime \
$restart \
brewpi/brewpi-raspbian $*
}
function docker_start() {
[[ $DEBUG -eq 1 ]] && set -x
if is_running; then
: #do nothing
elif container_exists; then
docker_start_old
else
docker_start_new $*
fi
}
function docker_stop() {
[[ $DEBUG -eq 1 ]] && set -x
is_running && {
docker ps -q --filter "name=brewpi" \
| xargs -r docker stop
}
}
function docker_status() {
[[ $DEBUG -eq 1 ]] && set -x
rc=1
msg=Stopped
is_running && {
msg=Running
rc=0
}
echo $msg
return $rc
}
function docker_clean() {
[[ $DEBUG -eq 1 ]] && set -x
container_id | xargs -r docker rm
}
function check_update() {
###
# Return 0 (true) if update is available, 1 (false) otherwise
###
[[ $DEBUG -eq 1 ]] && set -x
parts=( $( docker images $IMAGE --format '{{.Tag}} {{.CreatedAt}}' ) )
tag=${parts[0]}
current=$( date -d "${parts[1]} ${parts[2]}" "+%s" )
registry="https://registry.hub.docker.com/v2/repositories"
url="$registry/$IMAGE/tags/"
latest=$(
curl -s "$url" \
| jq ".results[] | select( .name == \"$tag\" ).last_updated" \
| xargs -n1 -I{} date -d "{}" "+%s" )
rc=1
if [[ $latest -gt $current ]] ; then
old=$( date -d @$current "+%Y-%m-%d %H:%M:%S" )
new=$( date -d @$latest "+%Y-%m-%d %H:%M:%S" )
echo "Update available: ${IMAGE}:$tag (current:$old) [latest:$new]"
rc=0
fi
return $rc
}
function disable_spark_usb() {
[[ $DEBUG -eq 1 ]] && set -x
sudo sed -ie '/^port = auto/ s/^/# /' "$CONFIG"
}
function disable_spark_wifi() {
[[ $DEBUG -eq 1 ]] && set -x
sudo sed -ie '/^port = socket/ s/^/# /' "$CONFIG"
}
function enable_spark_usb() {
[[ $DEBUG -eq 1 ]] && set -x
disable_spark_wifi
sudo sed -ie '/^# port = auto/ s/^# //' "$CONFIG"
}
function enable_spark_wifi() {
[[ $DEBUG -eq 1 ]] && set -x
[[ $# -eq 1 ]] || die "Missing ip addr for enable_wifi"
local _ip="$1"
disable_spark_usb
sudo sed -ie "/port = socket/cport = socket://${_ip}:6666" "$CONFIG"
}
function usage() {
CMDS=( status start stop restart check-update update mkdatadir enable-wifi enable-usb clean )
cat <<ENDHERE
Usage: $0 CMD
where CMD is one of: ${CMDS[@]}
ENDHERE
}
### Process cmdline options
DEBUG=0
while getopts ":d" opt; do
case $opt in
d) DEBUG=1 ;;
esac
done
shift $((OPTIND-1))
[[ $DEBUG -eq 1 ]] && set -x
### Source common functions
_base="$(dirname "$(readlink -e ${BASH_SOURCE[$i]})" )"
fn="${_base}/bash.common"
[[ -r $fn ]] || { echo "Cant access file '$fn'" 1>&2; exit 1
}
source $fn
action=$1
shift
case $action in
status)
docker_status
;;
start)
docker_start
;;
stop)
docker_stop
;;
restart)
docker_stop
sleep 1
docker_start
;;
mkdatadir)
[[ -d $DATADIR ]] || docker_start sleep 1
sleep 1
docker_stop
docker_clean
;;
enable-wifi)
enable_spark_wifi $*
;;
enable-usb)
enable_spark_usb
;;
clean)
docker_clean
;;
check-update)
check_update
;;
update)
docker_stop
sleep 1
docker_clean
sleep 1
docker pull $IMAGE
sleep 1
docker_start
;;
test)
container_exists && echo "true" || echo "false"
;;
*)
usage
;;
esac