forked from mattsm/boardfarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy-boardfarm-nodes.sh
executable file
·78 lines (61 loc) · 2.55 KB
/
deploy-boardfarm-nodes.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
#!/bin/bash -xe
IFACE=${1:-undefined}
START_VLAN=${2:-101}
END_VLAN=${3:-144}
OPTS=${4:-"both"} # both, odd, even, odd-dhcp, even-dhcp
BRINT=br-bft
random_private_mac () {
echo $1$1$1$1$1$1 | od -An -N6 -tx1 | sed -e 's/^ *//' -e 's/ */:/g' -e 's/:$//' -e 's/^\(.\)[13579bdf]/\10/'
}
local_route () {
# TODO: This is a problem if the router network matches the host network
host_dev=$(ip route list | grep ^default | awk '{print $5}' )
local_route=$(ip route | grep "dev $host_dev" | grep -v ^default | grep -v via | awk '{print $1}' | head -n1)
docker exec $cname ip route add $local_route dev eth0 via 172.17.0.1
}
# eth0 is docker private network, eth1 is vlan on specific interface
create_container_eth1_vlan () {
local vlan=$1
cname=bft-node-$IFACE-$vlan
docker stop $cname && docker rm $cname
docker run --name $cname --privileged -h $cname --restart=always \
-p $(( 5000 + $vlan )):22 \
-p $(( 8000 + $vlan )):8080 \
-d bft:node /usr/sbin/sshd -D
sudo ip link del $IFACE.$vlan || true
sudo ip link add link $IFACE name $IFACE.$vlan type vlan id $vlan
cspace=$(docker inspect --format '{{.State.Pid}}' $cname)
sudo ip link set netns $cspace dev $IFACE.$vlan
docker exec $cname ip link set $IFACE.$vlan name eth1
docker exec $cname ip link set dev eth1 address $(random_private_mac $vlan)
}
# eth0/eth1 are both dhcp on the main network
create_container_eth1_dhcp () {
local vlan=$1
cname=bft-node-$IFACE-$vlan
docker stop $cname && docker rm $cname
docker run --name $cname --privileged -h $cname --restart=always \
-d --network=none bft:node /usr/sbin/sshd -D
cspace=$(docker inspect --format '{{.State.Pid}}' $cname)
# create lab network access port
sudo ip link add tempfoo link $IFACE type macvlan mode bridge
sudo ip link set dev tempfoo up
sudo ip link set netns $cspace dev tempfoo
docker exec $cname ip link set tempfoo name eth1
docker exec $cname ifconfig eth1 up
docker exec $cname dhclient eth1
}
[ "$IFACE" = "undefined" ] && return
echo "Creating nodes starting on vlan $START_VLAN to $END_VLAN on iface $IFACE"
for vlan in $(seq $START_VLAN $END_VLAN); do
echo "Creating node on vlan $vlan"
create_container_eth1_vlan $vlan
[ "$OPTS" = "both" ] && { local_route; continue; }
if [ $((vlan%2)) -eq 0 ]; then
[ "$OPTS" = "even" ] && local_route
elif [ "$OPTS" = "odd" ]; then
local_route
fi
done
echo "Running the command below will stop all containers and clean up everything:"
echo 'docker stop $(docker ps -q) && docker rm $(docker ps -a -q)'