-
Notifications
You must be signed in to change notification settings - Fork 3
/
cxy_sdn
executable file
·739 lines (632 loc) · 19 KB
/
cxy_sdn
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
#!/bin/bash
# cxy-k8s-network-plugin
# This plugin act as a client of cxy-sdn. The heavy lifting work is done by cxy-sdn server. They are communicating via HTTP
# According to https://github.com/GoogleCloudPlatform/kubernetes/pull/5069
# This plugin need to implement two specific hooks
# 1 After pod infra container is launched (before other containers of the pod are launched)
# 2 Before pod infra container is deleted
set -e
usage() {
cat << EOF
NAME:
cxy_sdn - Install, Configure and Run cxy-sdn
VERSION:
10.0
USAGE:
$0 <options> <command> [command_options] [arguments...]
COMMANDS:
help
Help and usage
install [unattended] [nopowerstrip]
Install cxy-sdn (installs docker and ovs)
uninstall
Remove cxy-sdn installation
clean
Remove cxy-sdn installation and dependencies (docker and openvswitch)
deps
Show cxy-sdn dependencies
agent {stop|start|restart|logs}
Start/Stop/Restart the cxy-sdn container or show its logs
info [container_id]
Show cxy-sdn info for all containers, or for a given container_id
run [-n foo] <docker_run_args>
Run a container and optionally specify which network to attach to
start <container_id>
Start a <container_id>
stop <container_id>
Stop the <container_id>
rm <container_id>
Remove the <container_id>
attach <container_id>
Attach to the <container_id>
cluster bind <interface>
Bind clustering to a specific interface
cluster join <address>
Join the cluster at the specified address
cluster leave
Leave the cluster
network list
List all created networks
network info <name>
Display information about a given network
network create <name> [cidr]
Create a network
network delete <name> [cidr]
Delete a network
network agent start
Starts an existing cxy-sdn image if it is not already running
network agent stop
Stops a running cxy-sdn image. This will not delete the local image
init
just init for k8s
setup namespace pod cid
setup after the infra pod is created
teardown namespace pod cid
teardown before the infra pod is killed
status namespace pod cid
get pod ip(aka pod infra container ip)
setQoS cid bw delay
set up container's egress bandwidth and latency whose container id is CID
EOF
}
# Utility function to test if a command exists
command_exists() {
hash $@ 2>/dev/null
}
# Colorized Command Output
log_info() {
printf "\033[0;36m$@\033[0m\n"
}
log_notice() {
printf "\033[0;32m$@\033[0m\n"
}
log_warn() {
printf "\033[0;33m$@\033[0m\n"
}
log_error() {
printf "\033[0;35m$@\033[0m\n"
}
log_fatal() {
printf "\033[0;31m$@\033[0m\n"
}
log_debug() {
printf "\033[0;37m$@\033[0m\n"
}
log_step() {
log_info "-----> $@"
}
indent() {
sed -u "s/^/ /"
}
get_status() {
OS="NOT_LINUX"
RELEASE="NOT_LINUX"
CODENAME="NOT_LINUX"
ARCH=$(uname -m)
if [ "$ARCH" = "x86_64" ]; then
ARCH="amd64";
fi
if [ "$ARCH" = "i686" ]; then
ARCH="i386";
fi
if command_exists lsb_release; then
OS=$(lsb_release -is)
RELEASE=$(lsb_release -rs)
CODENAME=$(lsb_release -cs)
elif [ -f /etc/debian_version ]; then
OS="Debian"
RELEASE="UNDETECTED"
CODENAME="UNDETECTED"
elif [ -f /etc/redhat-release ]; then
OS="RedHat"
RELEASE="UNDETECTED"
CODENAME="UNDETECTED"
fi
DOCKER_SVER="NOT_INSTALLED"
DOCKER_CVER="NOT_INSTALLED"
if command_exists docker || command_exists lxc-docker; then
DOCKER_SVER=$(docker version | grep "Server version:" | awk '{ print $3 }')
DOCKER_CVER=$(docker version | grep "Client API version:" | awk '{ print $4 }')
fi
OVS_SVER="NOT_INSTALLED"
if command_exists ovsdb-server && command_exists ovs-vswitchd ; then
OVS_SVER=$(ovs-appctl -V | grep "ovs-" | awk '{ print $4 }')
fi
}
deps() {
echo "cxy-sdn Docker Host Requirements:"
echo ".. Open vSwitch Environment:"
echo ".... Archicture: amd64 or i386"
echo ".... Current: $ARCH"
echo ".... Operating System: Ubuntu, Debian and Fedora"
echo ".... Current: $lsb_dist"
echo ".... Open vSwitch Version: 2.3.1"
echo ".... Current: $OVS_SVER"
echo ".. Docker Environment:"
echo ".... Docker Server Version: 1.4 or higher"
echo ".... Current: $DOCKER_SVER"
echo ".... Docker Client Version: 1.16 or higher"
echo ".... Current: $DOCKER_CVER"
}
kernel_opts(){
log_step "Setting Linux Kernel Options"
if [ $(cat /etc/sysctl.conf | grep icmp_echo_ignore_broadcasts) ]; then
sed -i 's/^#\?net\.ipv4\.icmp_echo_ignore_broadcasts.*$/net\.ipv4\.icmp_echo_ignore_broadcasts=0/g' /etc/sysctl.conf
else
echo 'net.ipv4.icmp_echo_ignore_broadcasts=0' >> /etc/sysctl.conf
fi
if [ $(cat /etc/sysctl.conf | grep ip_forward ) ]; then
sed -i 's/^#\?net\.ipv4\.ip_forward.*$/net\.ipv4\.ip_forward=1/g' /etc/sysctl.conf
else
echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
fi
sysctl -p | indent
}
pkg_update() {
log_step "Ensuring Package Repositories are up to date"
$pkg update > /dev/null
}
install_curl() {
if command_exists curl; then
log_step "Curl already installed!"
else
log_step "Installing Curl"
$pkg install curl | indent
fi
}
install_ovs() {
if command_exists ovsdb-server && command_exists ovs-vswitchd ; then
log_step "Open vSwitch already installed!"
else
if command_exists getenforce && [ -z $(getenforce | grep -E 'Enforcing|Permissive') ] ; then
log_step "Checking Open vSwitch dependencies.."
$pkg install $policy
sudo semodule -d openvswitch 2>/dev/null || true
fi
log_step "Installing Open vSwitch.."
$pkg install $ovs | indent
fi
# Make sure the processes are started
case "$lsb_dist" in
debian|ubuntu)
if $(service openvswitch-switch status | grep "stop"); then
service openvswitch-switch start
fi
;;
fedora)
systemctl start openvswitch.service
;;
esac
sleep 1
log_step "Setting OVSDB Listener" #important!
ovs-vsctl set-manager ptcp:6640
}
remove_ovs() {
log_step "Removing existing Open vSwitch packages..."
$pkg remove $ovs
}
install_docker() {
if command_exists docker; then
log_step "Docker already installed!"
else
log_step "Installing Docker..."
case $lsb_dist in
fedora)
$pkg remove docker > /dev/null
;;
esac
if command_exists curl; then
curl -sSL https://get.docker.com/ | sh
elif command_exists wget; then
wget -qO- https://get.docker.com/ | sh
fi
fi
case $lsb_dist in
debian|ubuntu)
if [ -f /etc/init.d/docker ]; then
if $(service docker status | grep "stop"); then
service docker start
fi
else
if $(service docker.io status | grep "stop"); then
service docker.io start
fi
fi
;;
fedora)
systemctl start docker.service
;;
esac
}
start_cxy-sdn() {
log_step "Starting the cxy-sdn container"
if [ -n "$(docker ps | grep registry.aliyuncs.com/wizardcxy/cxy-sdn | awk '{ print $1 }')" ]; then
log_fatal "A cxy-sdn container is already running"
return 1
fi
flags="--iface=eth0"
while true; do
read -p "Is this a server node in the cluster? (y/n)" yn
case $yn in
[Yy]* )
flags="$flags --server=true"
read -p "what is the server number: " num
flags="$flags --expectedServerNum=$num"
break
;;
[Nn]* )
break
;;
* )
echo "Please answer yes or no."
;;
esac
done
cid=$(docker run -itd --privileged=true \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /usr/bin/ovs-vsctl:/usr/bin/ovs-vsctl -v /var/run/openvswitch/db.sock:/var/run/openvswitch/db.sock \
-v /usr/bin/docker:/usr/bin/docker -v /proc:/hostproc -e PROCFS=/hostproc \
--net=host registry.aliyuncs.com/wizardcxy/cxy-sdn /cxy-sdn $flags)
if [ -n "$cid" ]; then
log_info "A cxy-sdn container was started" | indent
else
log_fatal "Error starting the cxy-sdn container"
exit 1
fi
mkdir -p /var/run/cxy-sdn
echo $cid > /var/run/cxy-sdn/cid
}
stop_cxy-sdn() {
log_step "Stopping cxy-sdn agent"
if ! command_exists docker; then
log_fatal "Docker is not installed, please run \"./cxy-sdn install\""
exit 1
fi
for CID in $(docker ps | grep registry.aliyuncs.com/wizardcxy/cxy-sdn | awk '{ print $1; }'); do
log_info "Stopping the cxy-sdn container $CID" | indent
docker kill -s=15 ${CID} > /dev/null
done
if [ -z $(docker ps | grep registry.aliyuncs.com/wizardcxy/cxy-sdn | awk '{ print $1 }') ]; then
log_info "All cxy-sdn agent containers are stopped. Please run \"cxy-sdn agent start\" to start them again"
fi
}
remove_cxy-sdn() {
log_step "remove cxy-sdn Agent"
if ! command_exists docker; then
log_fatal "Docker is not installed"
exit 1
fi
for CID in $(docker ps -a | grep registry.aliyuncs.com/wizardcxy/cxy-sdn | awk '{ print $1; }'); do
log_info "Stopping cxy-sdn container: $CID" | indent
docker stop $CID > /dev/null
sleep 1
log_info "Removing cxy-sdn container: $CID" | indent
docker rm $CID > /dev/null
done
log_info "cxy-sdn container deleted" | indent
}
logs() {
if [ ! -f /var/run/cxy-sdn/cid ] || [ -z $(cat /var/run/cxy-sdn/cid) ]; then
log_fatal "cxy-sdn container is not running"
exit 1
fi
docker logs $@ $(cat /var/run/cxy-sdn/cid)
}
info() {
if [ -z "$1" ]; then
curl -s -X GET http://localhost:8888/connections | python -m json.tool
else
containerId=$(docker ps -a --no-trunc=true | grep $1 | awk {' print $1'})
if [ -z "$containerId" ]; then
log_fatal "Could not find a Container with Id : $1"
else
curl -s -X GET http://localhost:8888/connection/$containerId | python -m json.tool
fi
fi
}
container_run() {
network=""
requestIp=""
if [ $1 == '-n' ]; then
network=$2
shift 2
fi
if [ $1 == "-z" ]; then
requestIp=$2
shift 2
fi
attach="false"
if [ -z "$(echo "$@" | grep -e '-[a-zA-Z]*d[a-zA-Z]*\s')" ]; then
attach="true"
fi
if [ "$attach" = "false" ]; then
cid=$(docker run --net=none $@)
else
cid=$(docker run --net=none -d $@)
fi
cPid=$(docker inspect --format='{{ .State.Pid }}' $cid)
cName=$(docker inspect --format='{{ .Name }}' $cid)
json=$(curl -s -X POST http://localhost:8888/connection -d "{ \"containerID\": \"$cid\", \"containerName\": \"$cName\", \"requestIP\": \"$requestIp\", \"containerPID\": \"$cPid\", \"network\": \"$network\" }")
result=$(echo $json | sed 's/[,{}]/\n/g' | sed 's/^".*":"\(.*\)"/\1/g' | awk -v RS="" '{ print $7, $8, $9, $10, $11 }')
if [ "$attach" = "false" ]; then
echo $cid
else
docker attach $cid
fi
}
container_stop() {
docker stop $@
find -L /var/run/netns -type l -delete
}
container_start() {
docker start $1 > /dev/null
cid=$(docker ps --no-trunc=true | grep $1 | awk {' print $1'})
cPid=$(docker inspect --format='{{ .State.Pid }}' $cid)
cName=$(docker inspect --format='{{ .Name }}' $cid)
json=$(curl -s -X GET http://localhost:8888/connection/$cid)
result=$(echo $json | sed 's/[,{}]/\n/g' | sed 's/^".*":"\(.*\)"/\1/g' | awk -v RS="" '{ print $7, $8, $9, $10, $11 }')
attach $result $cPid
# ToDo: PUT new container info to the api
echo $cid
}
container_delete() {
cid=$(docker ps -a --no-trunc=true | grep $1 | awk {' print $1'})
docker rm $@
curl -s -X DELETE http://localhost:8888/connection/$cid
sleep 1
}
cluster_join(){
log_info "Requesting cxy-sdn to join the cluster at $*"
curl -s -X POST http://localhost:8888/cluster/join?address=$*
}
cluster_leave(){
log_info "Requesting cxy-sdn to leave cluster"
curl -s -X POST http://localhost:8888/cluster/leave
}
network_list() {
curl -s -X GET http://localhost:8888/networks | python -m json.tool
}
network_info() {
curl -s -X GET http://localhost:8888/network/$1 | python -m json.tool
}
network_create() #name
#cidr
{
#ToDo: Check CIDR is valid
curl -s -X POST http://localhost:8888/network -d "{ \"name\": \"$1\", \"subnet\": \"$2\" }" | python -m json.tool
}
network_delete() {
curl -s -X DELETE http://localhost:8888/network/$@
}
# Run as root only
if [ "$(id -u)" != "0" ]; then
log_fatal "Please run as root"
exit 1
fi
# perform some very rudimentary platform detection
lsb_dist=''
if command_exists lsb_release; then
lsb_dist="$(lsb_release -si)"
fi
if [ -z "$lsb_dist" ] && [ -r /etc/lsb-release ]; then
lsb_dist="$(. /etc/lsb-release && echo "$DISTRIB_ID")"
fi
if [ -z "$lsb_dist" ] && [ -r /etc/debian_version ]; then
lsb_dist='debian'
fi
if [ -z "$lsb_dist" ] && [ -r /etc/fedora-release ]; then
lsb_dist='fedora'
fi
if [ -z "$lsb_dist" ] && [ -r /etc/os-release ]; then
lsb_dist="$(. /etc/os-release && echo "$ID")"
fi
lsb_dist="$(echo "$lsb_dist" | tr '[:upper:]' '[:lower:]')"
if [ -z lsb_dist ]; then
log_fatal "Operating System could not be detected"
exit 1
fi
if [ -z "$(echo "$lsb_dist" | grep -E 'ubuntu|debian|fedora')" ]; then
log_fatal "Operating System $lsb_dist is not yet supported. Please contact support@cxy-sdn.io"
exit 1
fi
case "$lsb_dist" in
debian|ubuntu)
pkg="apt-get -q -y --force-yes"
ovs="openvswitch-switch"
policy="policycoreutils"
;;
fedora)
pkg="yum -q -y"
ovs="openvswitch"
policy="policycoreutils-python"
;;
esac
while getopts ":D" opt; do
case $opt in
D)
DEBUG="true"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
shift $((OPTIND-1))
if [ "$DEBUG" = "true" ]; then
set -x
fi
case "$1" in
init)
echo "hello in cxy-sdn init"
;;
setup)
echo "set up network"
# $0 is command name, $2 is pod namespace used as network name for cxy-sdn
# $4 is container id
cPid=$(docker inspect --format='{{ .State.Pid }}' $4)
cName=$(docker inspect --format='{{ .Name }}' $4)
json=$(curl -s -X POST http://localhost:8888/connection -d "{ \"containerID\": \"$4\", \"containerName\": \"$cName\", \"requestIP\": \"$requestIp\", \"containerPID\": \"$cPid\", \"network\": \"$2\" }")
result=$(echo $json | sed 's/[,{}]/\n/g' | sed 's/^".*":"\(.*\)"/\1/g' | awk -v RS="" '{ print $7, $8, $9, $10, $11 }')
;;
teardown)
echo "teardown pod"
# $0 is command name, $2 is pod namespace used as network name for cxy-sdn
# $4 is container id
cid=$(docker ps -a --no-trunc=true | grep $4 | awk {' print $1'})
curl -s -X DELETE http://localhost:8888/connection/$cid
sleep 1
;;
status)
# $0 is command name, $2 is pod namespace used as network name for cxy-sdn
# $4 is container id
containerId=$(docker ps -a --no-trunc=true | grep $4 | awk {' print $1'})
if [ -z "$containerId" ]; then
log_fatal "Could not find a Container with Id : $containerId"
else
json=$(curl -s -X GET http://localhost:8888/connection/$containerId | python -m json.tool | grep ip)
ip=$(echo $json | sed 's/[,{}]/\n/g' | awk -v RS="" '{print $2}')
echo ""{ \"kind\": \"PodNetworkStatus\", \"apiVersion\": \"v1beta1\", \"ip\": $ip }""
fi
;;
help)
usage
;;
install)
shift
log_notice "Installing cxy-sdn..."
kernel_opts
#pkg_update
install_curl
install_ovs
install_docker
start_cxy-sdn $@
log_notice "Done!!!"
;;
uninstall)
log_notice "Uninstalling cxy-sdn..."
stop_cxy-sdn
remove_cxy-sdn
log_notice "Done!!!"
;;
clean)
log_notice "Removing cxy-sdn and all its dependencies..."
get_status
remove_ovs
stop_cxy-sdn
remove_cxy-sdn
log_notice "Done!!!"
;;
deps)
get_status
deps
;;
info)
shift
info $@
;;
run)
shift
container_run $@
;;
stop)
shift
container_stop $@
;;
start)
shift
container_start $@
;;
rm)
shift
container_delete $@
;;
attach)
shift
docker attach $@
;;
cluster)
shift
case $1 in
join)
shift
cluster_join $@
;;
leave)
cluster_leave
;;
*)
log_fatal "Unknown Command"
usage
exit 1
esac
;;
network)
shift
case "$1" in
list)
network_list
;;
info)
shift
network_info $@
;;
create)
shift
network_create $@
;;
delete)
shift
network_delete $@
;;
*)
log_fatal "Unknown Command"
usage
exit 1
esac
;;
agent)
shift 1
case "$1" in
start)
start_cxy-sdn
;;
stop)
stop_cxy-sdn
;;
restart)
stop_cxy-sdn
start_cxy-sdn
;;
logs)
shift 1
logs $@
;;
*)
log_fatal "\"cxy-sdn agent\" {stop|start|restart|logs}"
exit 1
;;
esac
;;
qos)
shift
# QoS setting
case "$1" in
add)
shift
curl -X POST -d "bw=${2}&delay=${3}" http://127.0.0.1:8888/qos/${1}
;;
update)
shift
curl -X PUT -d "bw=${2}&delay=${3}" http://127.0.0.1:8888/qos/${1}
;;
*)
log_fatal "\"cxy-sdn qos\" {add|update}"
exit 1
;;
esac
;;
*)
usage
exit 1
;;
esac