forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dc
executable file
·155 lines (136 loc) · 3.63 KB
/
dc
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
#!/bin/bash
# Copyright 2018 ETH Zurich
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
cmd_help() {
echo
cat <<-_EOF
Usage:
$PROGRAM start [SERVICE]
Creates and starts containers
$PROGRAM stop [SERVICE]
Stops containers
$PROGRAM down
Stops and removes all containers, networks and volumes
$PROGRAM run [SERVICE]
Runs the service and removes its container afterwards
$PROGRAM [GROUP] [COMMAND]
Run the docker compose command COMMAND for the service group GROUP.
$PROGRAM exec_tester [IA] [COMMAND]
Exec a command in the specified service.
$PROGRAM collect_logs [GROUP] [LOG_DIR]
Collect logs from all services in the service group GROUP to the log directory LOG_DIR
Options:
- [SERVICE]: As scion service glob, e.g. cs1*.
- [GROUP]:
- scion: For all scion services.
- zk: For the zookeeper service.
- prom: For the prometheus service.
- [COMMAND]: A docker-compose command like 'up -d' or 'down'
- [IA]: An IA number like 1-ff00:0:110
- [LOG_DIR]: A folder.
_EOF
}
cmd_start() {
services="$(glob_docker "$@")"
if [ -n "$services" ]; then
cmd_scion up -d $services < /dev/null
fi
}
cmd_stop() {
services="$(glob_docker "$@")"
if [ -n "$services" ]; then
cmd_scion stop $services
fi
}
cmd_down() {
cmd_stop "*"
cmd_run utils_cleaner
cmd_scion down
}
cmd_run() {
cmd_scion run --rm "$@"
}
cmd_scion() {
dc "scion" "$@"
}
cmd_zk() {
dc "zk" "$@"
}
cmd_jaeger() {
dc "jaeger" "$@"
}
cmd_prom() {
dc "prom" "$@"
}
# Runs docker compose for the given project
dc() {
local project="$1"
local dc_file="gen/$1-dc.yml"
shift
is_running_in_docker && project="${project}_docker"
COMPOSE_FILE="$dc_file" docker-compose -p "$project" --no-ansi "$@"
}
cmd_collect_logs() {
[ $# -ge 2 ] || { cmd_help; exit 1; }
local group="$1"
local out_dir="$2"
mkdir -p "$out_dir"
for svc in $(cmd_$group config --services); do
cmd_$group logs $svc &> $out_dir/$svc.log
done
}
exec_tester() {
local service="tester_$1"
shift
service_running "$service" || cmd_start "$service" &>/dev/null
cmd_scion exec -T -u scion "$service" bash -l -c "$*"
}
glob_docker() {
[ $# -ge 1 ] || set -- '*'
matches=
for proc in $(cmd_scion config --services); do
for spec in "$@"; do
if glob_match $proc "$spec"; then
matches="$matches $proc"
break
fi
done
done
echo $matches
}
glob_match() {
# If $1 is matched by $2, return true
case "$1" in
$2) return 0;;
esac
return 1
}
service_running() {
cntr="$(cmd_scion ps -q $1)"
[ -n "$cntr" ] && [ -n "$(docker ps -q --no-trunc | grep $cntr)" ]
}
is_running_in_docker() {
cut -d: -f 3 /proc/1/cgroup | grep -q '^/docker/'
}
PROGRAM="${0##*/}"
COMMAND="$1"
shift
case "$COMMAND" in
start|stop|down|run|scion|zk|jaeger|prom|collect_logs)
"cmd_$COMMAND" "$@" ;;
exec_tester)
"exec_tester" "$@" ;;
*) cmd_help; exit 1 ;;
esac