forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scion.sh
executable file
·489 lines (445 loc) · 13.6 KB
/
scion.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#!/bin/bash
export PYTHONPATH=python/:.
EXTRA_NOSE_ARGS="-w python/ --with-xunit --xunit-file=logs/nosetests.xml"
# BEGIN subcommand functions
cmd_topology() {
set -e
local zkclean
local nobuild
if [ "$1" = "nobuild" ]; then
shift
nobuild="y"
fi
if is_docker_be; then
echo "Shutting down dockerized topology..."
./tools/quiet ./tools/dc down
else
echo "Shutting down: $(./scion.sh stop)"
fi
supervisor/supervisor.sh shutdown
stop_jaeger
mkdir -p logs traces gen gen-cache
find gen gen-cache -mindepth 1 -maxdepth 1 -exec rm -r {} +
if [ "$1" = "zkclean" ]; then
shift
zkclean="y"
fi
if [ -z "$nobuild" ]; then
echo "Compiling..."
cmd_build || exit 1
fi
echo "Create topology, configuration, and execution files."
is_running_in_docker && set -- "$@" --in-docker
python/topology/generator.py "$@"
if is_docker_be; then
./tools/quiet ./tools/dc run utils_chowner
fi
run_jaeger
run_zk "$zkclean"
load_cust_keys
if [ ! -e "gen-certs/tls.pem" -o ! -e "gen-certs/tls.key" ]; then
local old=$(umask)
echo "Generating TLS cert"
mkdir -p "gen-certs"
umask 0177
openssl genrsa -out "gen-certs/tls.key" 2048
umask "$old"
openssl req -new -x509 -key "gen-certs/tls.key" -out "gen-certs/tls.pem" -days 3650 -subj /CN=scion_def_srv
fi
}
cmd_run() {
if [ "$1" != "nobuild" ]; then
echo "Compiling..."
cmd_build || exit 1
if is_docker_be; then
echo "Build scion_base image"
./tools/quiet ./docker.sh base
echo "Build scion image"
./tools/quiet ./docker.sh build
echo "Build perapp images"
./tools/quiet make -C docker/perapp
echo "Build scion tester"
./tools/quiet ./docker.sh tester
fi
fi
run_setup
echo "Running the network..."
# Start dispatcher first, as it is requrired by the border routers.
./tools/quiet ./scion.sh mstart '*disp*'
# Start border routers before all other services to provide connectivity.
./tools/quiet ./scion.sh mstart '*br*'
# Run with docker-compose or supervisor
if is_docker_be; then
./tools/quiet ./tools/dc start 'scion*'
else
./tools/quiet ./supervisor/supervisor.sh start all
fi
}
load_cust_keys() {
if [ -f 'gen/load_custs.sh' ]; then
echo "Loading customer keys..."
./tools/quiet ./gen/load_custs.sh
fi
}
run_zk() {
if [ ! -f "gen/zk-dc.yml" ]; then
return
fi
echo "Running zookeeper..."
./tools/quiet ./tools/dc zk up -d
if [ -n "$1" ]; then
echo "Deleting all Zookeeper state"
# Wait some time, such that zookeeper accepts connections again after startup
sleep 3
local addr="127.0.0.1:2181"
if is_running_in_docker; then
addr="${DOCKER0:-172.17.0.1}:2182"
elif is_docker_be; then
addr="$(./tools/docker-ip):2181"
fi
tools/zkcleanslate --zk "$addr"
fi
}
run_jaeger() {
if [ ! -f "gen/jaeger-dc.yml" ]; then
return
fi
echo "Running jaeger..."
./tools/quiet ./tools/dc jaeger up -d
}
stop_jaeger() {
if [ ! -f "gen/jaeger-dc.yml" ]; then
return
fi
echo "Stopping jaeger..."
./tools/quiet ./tools/dc jaeger down
}
cmd_mstart() {
run_setup
# Run with docker-compose or supervisor
if is_docker_be; then
services="$(glob_docker "$@")"
[ -z "$services" ] && { echo "ERROR: No process matched for $@!"; exit 255; }
./tools/dc scion up -d $services
else
supervisor/supervisor.sh mstart "$@"
fi
}
run_setup() {
python/integration/set_ipv6_addr.py -a
# Create dispatcher and sciond dirs or change owner
local disp_dir="/run/shm/dispatcher"
[ -d "$disp_dir" ] || mkdir "$disp_dir"
[ $(stat -c "%U" "$disp_dir") == "$LOGNAME" ] || { sudo -p "Fixing ownership of $disp_dir - [sudo] password for %p: " chown $LOGNAME: "$disp_dir"; }
local sciond_dir="/run/shm/sciond"
[ -d "$sciond_dir" ] || mkdir "$sciond_dir"
[ $(stat -c "%U" "$sciond_dir") == "$LOGNAME" ] || { sudo -p "Fixing ownership of $sciond_dir - [sudo] password for %p: " chown $LOGNAME: "$sciond_dir"; }
# Make sure zookeeper is running
run_zk
}
cmd_stop() {
echo "Terminating this run of the SCION infrastructure"
if is_docker_be; then
./tools/quiet ./tools/dc stop 'scion*'
else
./tools/quiet ./supervisor/supervisor.sh stop all
fi
if [ "$1" = "clean" ]; then
python/integration/set_ipv6_addr.py -d
fi
for i in /run/shm/{dispatcher,sciond}/; do
if [ -e "$i" ]; then
find "$i" -xdev -mindepth 1 -print0 | xargs -r0 rm -v
fi
done
}
cmd_mstop() {
if is_docker_be; then
services="$(glob_docker "$@")"
[ -z "$services" ] && { echo "ERROR: No process matched for $@!"; exit 255; }
./tools/dc scion stop $services
else
supervisor/supervisor.sh mstop "$@"
fi
}
cmd_status() {
cmd_mstatus '*'
}
cmd_mstatus() {
if is_docker_be; then
services="$(glob_docker "$@")"
[ -z "$services" ] && { echo "ERROR: No process matched for $@!"; exit 255; }
out=$(./tools/dc scion ps $services | tail -n +3)
rscount=$(echo "$out" | grep '\<Up\>' | wc -l) # Number of running services
tscount=$(echo "$services" | wc -w) # Number of all globed services
echo "$out" | grep -v '\<Up\>'
[ $rscount -eq $tscount ]
else
if [ $# -ne 0 ]; then
services="$(glob_supervisor "$@")"
[ -z "$services" ] && { echo "ERROR: No process matched for $@!"; exit 255; }
supervisor/supervisor.sh status "$services" | grep -v RUNNING
else
supervisor/supervisor.sh status | grep -v RUNNING
fi
[ $? -eq 1 ]
fi
# If all tasks are running, then return 0. Else return 1.
return
}
glob_supervisor() {
[ $# -ge 1 ] || set -- '*'
matches=
for proc in $(supervisor/supervisor.sh status | awk '{ print $1 }'); do
for spec in "$@"; do
if glob_match $proc "$spec"; then
matches="$matches $proc"
break
fi
done
done
echo $matches
}
glob_docker() {
[ $# -ge 1 ] || set -- '*'
matches=
for proc in $(./tools/dc scion config --services); do
for spec in "$@"; do
if glob_match $proc "scion_$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
}
is_docker_be() {
[ -f gen/scion-dc.yml ]
}
is_supervisor() {
[ -f gen/dispatcher/supervisord.conf ]
}
is_running_in_docker() {
cut -d: -f 3 /proc/1/cgroup | grep -q '^/docker/'
}
cmd_test(){
local ret=0
case "$1" in
py) shift; py_test "$@"; ret=$((ret+$?));;
go) shift; bazel_test ; ret=$((ret+$?));;
*) py_test; ret=$((ret+$?)); bazel_test; ret=$((ret+$?));;
esac
return $ret
}
py_test() {
python3 -m unittest discover
nosetests3 ${EXTRA_NOSE_ARGS} "$@"
}
bazel_test() {
bazel test //go/... --print_relative_test_log_paths
}
cmd_coverage(){
set -e
case "$1" in
py) shift; py_cover "$@";;
go) shift; go_cover "$@";;
*) py_cover;
echo "============================================="
go_cover;;
esac
}
py_cover() {
nosetests3 ${EXTRA_NOSE_ARGS} --with-cov --cov-report html "$@"
echo
echo "Python coverage report here: file://$PWD/python/htmlcov/index.html"
}
go_cover() {
( cd go && make -s coverage )
}
cmd_lint() {
set -o pipefail
local ret=0
py_lint || ret=1
go_lint || ret=1
md_lint || ret=1
return $ret
}
py_lint() {
lint_header "python"
local ret=0
for i in acceptance python; do
[ -d "$i" ] || continue
local cmd="flake8"
lint_step "$cmd /$i"
( cd "$i" && $cmd --config flake8.ini . ) | sort -t: -k1,1 -k2n,2 -k3n,3 || ((ret++))
done
flake8 --config python/flake8.ini tools/gomocks || ((ret++))
return $ret
}
go_lint() {
lint_header "go"
local TMPDIR=$(mktemp -d /tmp/scion-lint.XXXXXXX)
local LOCAL_DIRS="$(find go/* -maxdepth 0 -type d | grep -v vendor)"
# Find go files to lint, excluding generated code. For linelen and misspell.
find go -type f -iname '*.go' \
-a '!' -ipath 'go/proto/structs.gen.go' \
-a '!' -ipath 'go/proto/*.capnp.go' \
-a '!' -ipath '*mock_*' \
-a '!' -ipath 'go/lib/pathpol/sequence/*' > $TMPDIR/gofiles.list
lint_step "Building lint tools"
bazel build //:lint || return 1
tar -xf bazel-bin/lint.tar -C $TMPDIR || return 1
local ret=0
lint_step "impi"
# Skip CGO (https://github.com/pavius/impi/issues/5) files.
$TMPDIR/impi --local github.com/scionproto/scion --scheme stdThirdPartyLocal --skip '/c\.go$' --skip 'mock_' --skip 'go/proto/.*\.capnp\.go' --skip 'go/proto/structs.gen.go' ./go/... || ret=1
lint_step "gofmt"
# TODO(sustrik): At the moment there are no bazel rules for gofmt.
# See: https://github.com/bazelbuild/rules_go/issues/511
# Instead we'll just run the commands from Go SDK directly.
GOSDK=$(bazel info output_base)/external/go_sdk/bin
out=$($GOSDK/gofmt -d -s $LOCAL_DIRS);
if [ -n "$out" ]; then echo "$out"; ret=1; fi
lint_step "linelen (lll)"
out=$($TMPDIR/lll -w 4 -l 100 --files -e '`comment:"|`ini:"|https?:' < $TMPDIR/gofiles.list);
if [ -n "$out" ]; then echo "$out"; ret=1; fi
lint_step "misspell"
xargs -a $TMPDIR/gofiles.list $TMPDIR/misspell -error || ret=1
lint_step "ineffassign"
$TMPDIR/ineffassign -exclude ineffassign.json go || ret=1
lint_step "bazel"
make gazelle GAZELLE_MODE=diff || ret=1
# Clean up the binaries
rm -rf $TMPDIR
return $ret
}
md_lint() {
lint_header "markdown"
lint_step "mdlint"
./tools/mdlint
}
lint_header() {
printf "\nlint $1\n==============\n"
}
lint_step() {
echo "======> $1"
}
cmd_version() {
cat <<-_EOF
============================================
= SCION =
= https://github.com/scionproto/scion =
============================================
_EOF
}
cmd_build() {
if [ "$1" == "bypass" ]; then
USER_OPTS=-DBYPASS_ROUTERS make -s
else
make -s
fi
}
cmd_clean() {
make -s clean
}
cmd_sciond() {
[ -n "$1" ] || { echo "ISD-AS argument required"; exit 1; }
# Convert the ISD-AS argument into an array, where the first element is the
# ISD, and the second is the AS.
IFS=- read -a ia <<< $1
ISD=${ia[0]:?No ISD provided}
AS=${ia[1]:?No AS provided}
ADDR=${2:-127.0.0.1}
GENDIR=gen/ISD${ISD}/AS${AS}/endhost
[ -d "$GENDIR" ] || { echo "Topology directory for $ISD-$AS doesn't exist: $GENDIR"; exit 1; }
APIADDR="/run/shm/sciond/${ISD}-${AS}.sock"
PYTHONPATH=python/:. python/bin/sciond --addr $ADDR --api-addr $APIADDR sd${ISD}-${AS} $GENDIR &
echo "Sciond running for $ISD-$AS (pid $!)"
wait
exit $?
}
traces_name() {
local name=jaeger_read_badger_traces
echo "$name"
}
cmd_traces() {
set -e
local trace_dir=${1:-"$(readlink -e .)/traces"}
local port=16687
local name=$(traces_name)
cmd_stop_traces
docker run -d --name "$name" \
-u "$(id -u):$(id -g)" \
-e SPAN_STORAGE_TYPE=badger \
-e BADGER_EPHEMERAL=false \
-e BADGER_DIRECTORY_VALUE=/badger/data \
-e BADGER_DIRECTORY_KEY=/badger/key \
-e BADGER_CONSISTENCY=true \
-v "$trace_dir:/badger" \
-p "$port":16686 \
jaegertracing/all-in-one:1.14.0
sleep 3
x-www-browser "http://localhost:$port"
}
cmd_stop_traces() {
local name=$(traces_name)
docker stop "$name" || true
docker rm "$name" || true
}
cmd_help() {
cmd_version
echo
cat <<-_EOF
Usage:
$PROGRAM topology [nobuild] [zkclean]
Create topology, configuration, and execution files. With the 'nobuild'
option, don't build the code. With the 'zkclean' option, also reset
all local Zookeeper state. All other arguments or options are passed
to topology/generator.py
$PROGRAM run
Run network.
$PROGRAM sciond ISD-AS [ADDR]
Start sciond with provided ISD and AS parameters, and bind to ADDR.
ISD-AS must be in file format (e.g., 1-ff00_0_133). If ADDR is not
supplied, sciond will bind to 127.0.0.1.
$PROGRAM mstart PROCESS
Start multiple processes
$PROGRAM stop
Terminate this run of the SCION infrastructure.
$PROGRAM mstop PROCESS
Stop multiple processes
$PROGRAM status
Show all non-running tasks.
$PROGRAM mstatus PROCESS
Show status of provided processes
$PROGRAM test
Run all unit tests.
$PROGRAM coverage
Create a html report with unit test code coverage.
$PROGRAM help
Show this text.
$PROGRAM version
Show version information.
$PROGRAM traces [folder]
Serve jaeger traces from the specified folder (default: traces/)
$PROGRAM stop_traces
Stop the jaeger container started during the traces command
_EOF
}
# END subcommand functions
PROGRAM="${0##*/}"
COMMAND="$1"
shift
case "$COMMAND" in
coverage|help|lint|run|mstart|mstatus|mstop|stop|status|test|topology|version|build|clean|sciond|traces|stop_traces)
"cmd_$COMMAND" "$@" ;;
start) cmd_run "$@" ;;
*) cmd_help; exit 1 ;;
esac