-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-run.sh
executable file
·169 lines (132 loc) · 4.06 KB
/
docker-run.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
#!/bin/bash
#
# run-local.sh - run a local docker network for small scale experiments
#
# Syntax: ./run-local.sh
#
set -e
IMAGE_NAME='chop-chop'
NETWORK_NAME='chop-chop-network'
ROLES=(
'rendezvous'
'server'
'load-broker' 'honest-broker'
'load-client' 'honest-client'
)
setup_path="$1" ; shift
settings_path="$1" ; shift
assets="$1"
tmpdir=
# Cleanup phase
#
# The cleanup routine is implemented in a subroutine which is called when the
# script exits.
#
atexit() {
set +e
echo ":: Cleanup" >&2
echo "==> Kill nodes" >&2
sudo docker inspect --format='{{ range $key, $value := .Containers }}{{ printf "%s\n" $key}}{{ end }}' "$(cat "${tmpdir}/network")" \
| while read container
do
if [ "x${container}" = 'x' ] ; then
continue
fi
printf ' -> ' >&2
sudo docker container kill "${container}"
done
echo "==> Remove network '${NETWORK_NAME}'" >&2
printf ' -> ' >&2
sudo docker network rm "$(cat "${tmpdir}/network")" >&2
echo "==> Delete temporary working directory: '${tmpdir}'" >&2
if [ "x${tmpdir}" != 'x' ] ; then
rm -rf "${tmpdir}"
fi
}
trap atexit 'EXIT'
# Setup phase -----------------------------------------------------------------
#
# Create the virtual network with all the nodes specified in the setup file.
#
echo ":: Setup" >&2
# Create a temp directory where to put all the working files during the
# execution of the script.
# It is cleaned up automatically at the script exit.
#
printf '==> Create temporary working directory: ' >&2
tmpdir="$(mktemp -d --suffix='.d' "${0##*/}.XXXXXX")"
echo "'${tmpdir}'" >&2
# Create a Docker virtual network where the future Docker containers will
# attach to form a test environment.
# The subnet is specified in the setup file with a line:
#
# network <ip>/<mask>
#
# The Docker id of the network is stored in the temp directory.
#
grep '^network' "${setup_path}" | while read _ ip ; do
echo "==> Create network '${NETWORK_NAME}'" >&2
sudo docker network create \
--attachable \
--subnet="${ip}" \
"${NETWORK_NAME}" \
> "${tmpdir}/network"
echo " -> $(cat "${tmpdir}/network")" >&2
done
# Create the Docker contrainers which run the nodes.
# For each container, boot it with the default entry point which is a Silk
# server then invoke a script command (in another container for convenience)
# to set 5 properties as key/values:
#
# role role of the node in the setup, e.g. server, etc...
# id id of the node within its role, starting from 0
# ip ip address of the node
# silk_port tcp port where Silk listens
# target for clients, the IP of the broker to connect to
#
echo "==> Create nodes" >&2
while read role ip _ ; do
# Do not consider the line indicating the network or empty lines
#
if [ "x${role}" = 'x' -o "x${role}" = 'xnetwork' ] ; then
continue
fi
# Boot the container.
#
printf " -> " >&2
sudo docker run \
--detach \
--rm \
--network="${NETWORK_NAME}" \
--ip="${ip}" \
--mount type=bind,source="${PWD}/script",target='/home/ubuntu/script' \
"${IMAGE_NAME}" \
>&2
done < "${setup_path}"
# Create the control node.
#
# This Docker container starts in interactive mode.
# You should use this node to start experiments with the script included.
#
# When you exit the interactive shell of this container, it shuts down and the
# cleanup phase starts.
#
echo "==> Create control node" >&2
if [ -e "${tmpdir}/mnt" ] ; then
rm -rf "${tmpdir}/mnt"
fi
mkdir "${tmpdir}/mnt"
cp "${setup_path}" "${tmpdir}/mnt/setup.txt"
cp "${settings_path}" "${tmpdir}/mnt/settings.txt"
if [ "x${assets}" != 'x' ] ; then
cp -R --link "${assets}" "${tmpdir}/mnt/assets"
fi
sudo docker run \
-it \
--rm \
--mount type=bind,source="${PWD}/${tmpdir}/mnt",target='/home/ubuntu/mnt' \
--mount type=bind,source="${PWD}/script",target='/home/ubuntu/script' \
--mount type=bind,source="${PWD}/docker-init.sh",target='/home/ubuntu/init.sh' \
--network="${NETWORK_NAME}" \
--entrypoint='/home/ubuntu/init.sh' \
"${IMAGE_NAME}"