-
Notifications
You must be signed in to change notification settings - Fork 1
/
slowradio.sh
executable file
·153 lines (130 loc) · 2.7 KB
/
slowradio.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
#! /bin/bash
CONTROL_SH=$(basename "$0")
oneline_usage="$CONTROL_SH [-h] command [args]"
usage() {
cat <<-EndUsage
Usage: $oneline_usage
Use '$CONTROL_SH -h' for more information
EndUsage
exit 1
}
helpinfo() {
cat <<-EndHelp
Usage: $oneline_usage
Commands:
build [tag]
Build a new docker image for a given tag
push [tag]
Push latest version up to docker hub
pull [tag]
Pull latest version from docker hub
foreground optfolder [tag] [name]
Run in the foreground
run optfolder [tag] [name]
Run in the background
connect [name]
Connect to a bash session on a running container
stats [name]
Stats on running process
[name] always defaults to *slowradio*
[tag] always defaults to *latest*
Flags:
-h
Show usage info
EndHelp
exit 0
}
die() {
echo "$*"
exit 1
}
readonly REPO="rumblesan"
readonly APP="slowradio"
build() {
echo "Building new $REPO/$APP image with tag $DOCKER_TAG"
docker build -t $REPO/$APP:$DOCKER_TAG .
}
push() {
echo "Pushing new $REPO/$APP image with tag $DOCKER_TAG"
docker push $REPO/$APP:$DOCKER_TAG
}
pull() {
echo "Pulling new $REPO/$APP image with tag $DOCKER_TAG"
docker pull $REPO/$APP:$DOCKER_TAG
}
foreground() {
if [ -z "$1" ]; then
die "Need to give opt folder"
fi
local optfolder="$1"
echo "Running $APP in the foreground"
docker run --name $CONTAINER_NAME -v "$1":/opt/slowradio $REPO/$APP:$DOCKER_TAG
}
run() {
if [ -z "$1" ]; then
die "Need to give opt folder"
fi
local optfolder="$1"
local retries=10
echo "Running $APP"
docker run --name $CONTAINER_NAME --restart=on-failure:"$retries" -d -v "$1":/opt/slowradio $REPO/$APP:$DOCKER_TAG
}
connect() {
echo "Connecting to $CONTAINER_NAME"
docker exec -it $CONTAINER_NAME bash
}
stats() {
echo "Connecting to $CONTAINER_NAME"
docker exec $CONTAINER_NAME ps up $(docker exec $CONTAINER_NAME pgrep -f 'slow')
}
runaction() {
if [ ! -f Dockerfile ]; then
die "This script needs to be run in the same directory as the Dockerfile"
fi
action=$(printf "%s\n" "$1" | tr 'A-Z' 'a-z')
case "$action" in
"build")
build
;;
"push")
push
;;
"pull")
pull
;;
"foreground")
foreground "${@:2}"
;;
"run")
run "${@:2}"
;;
"connect")
connect
;;
"stats")
stats
;;
*)
usage
;;
esac
}
echo "$APP control"
CONTAINER_NAME="slowradio"
DOCKER_TAG="latest"
while getopts "hn::t:" opt "$@"; do
case "$opt" in
h)
helpinfo
;;
n)
CONTAINER_NAME="$OPTARG"
echo "Using docker tag $CONTAINER_NAME"
;;
t)
DOCKER_TAG="$OPTARG"
echo "Using docker tag $DOCKER_TAG"
;;
esac
done
runaction "${@:$OPTIND}"