| 1 |
#!/usr/bin/env bash |
| 2 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 3 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 4 |
# |
| 5 |
# This program is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
usage () { |
| 19 |
cat << __USAGE__ |
| 20 |
swarm [OPTIONS] (build|configure|run) |
| 21 |
|
| 22 |
Build, configure and run a swarm of docker containers for test purposes. |
| 23 |
|
| 24 |
This swarm is both for local development and automated testing to provide |
| 25 |
a stable testing/development platform. |
| 26 |
|
| 27 |
Containers ports are correctly exposed so they can be used either natively |
| 28 |
or in virtual machines. |
| 29 |
|
| 30 |
Commands: |
| 31 |
build Build docker images. |
| 32 |
configure Configure system ssh and cylc configuration files. |
| 33 |
deconfigure Undo configuration changes. |
| 34 |
interactive Run a docker container interactively. |
| 35 |
run Run docker containers. |
| 36 |
ps List running detached containers started with "run" |
| 37 |
kill Kill detached containers started with "run" |
| 38 |
|
| 39 |
Note: |
| 40 |
"ps" and "kill" only track the most resent swarm of containers started |
| 41 |
by "run". |
| 42 |
|
| 43 |
Options: |
| 44 |
-y --yes Don't prompt. |
| 45 |
--debug Runs the script with set -x for debug. |
| 46 |
|
| 47 |
Basic usage: |
| 48 |
$ swarm configure |
| 49 |
$ swarm build |
| 50 |
$ swarm run |
| 51 |
$ run-functional-tests -p all tests/f |
| 52 |
$ swarm kill |
| 53 |
$ swarm deconfigure |
| 54 |
|
| 55 |
Virtual Machines and Docker Desktop: |
| 56 |
All containers in this swarm should work when run in a virtual machine |
| 57 |
(e.g. VirtualBox, Docker Desktop for Mac OS or Windows, etc). |
| 58 |
|
| 59 |
The Containers: |
| 60 |
This "swarm" is made up of several containers. |
| 61 |
|
| 62 |
* Each container exposes its SSH port (22). |
| 63 |
* The SSH config maps these ports onto hostnames. |
| 64 |
* The Cylc config maps hostnames onto test platforms. |
| 65 |
|
| 66 |
See tests/functional/README.md for more information on the test |
| 67 |
platform matrix. |
| 68 |
|
| 69 |
Running Tests Without The Swarm: |
| 70 |
Configure the platforms with the same names as the swarm platforms. |
| 71 |
|
| 72 |
For example if you have a remote host compatible with TCP comms you |
| 73 |
could configure it like so: |
| 74 |
|
| 75 |
[platforms] |
| 76 |
[[_remote_background_indep_tcp]] |
| 77 |
host = my_remote_host |
| 78 |
|
| 79 |
NOTE: You will have to have ensure you have the same branch installed |
| 80 |
on the remote hosts as on your local host. Remember to pip install. |
| 81 |
|
| 82 |
Caveats: |
| 83 |
For TCP comms the firewall on the Docker host must accept packets from |
| 84 |
containers on the "docker0" network adapter. You may need to configure |
| 85 |
iptables for this, e.g.: |
| 86 |
$ sudo iptables -A INPUT -i docker0 -j ACCEPT |
| 87 |
This must appear early in the table to avoid preemption by reject or drop. |
| 88 |
$ sudo iptables-save > iptables.txt |
| 89 |
$ # (edit iptables.txt to put the new line above other INPUT lines). |
| 90 |
$ sudo iptables-restore < iptables.txt |
| 91 |
|
| 92 |
At present TCP comms are not possible FROM the host TO the container. |
| 93 |
This is not generally an issue as the standard test approach is to run |
| 94 |
a workflow on the host submitting its jobs to the container. |
| 95 |
|
| 96 |
However some of the shared filesystem tests actually run entire workflows |
| 97 |
inside the container. Communicating with these workflows FROM the host |
| 98 |
is not possible at present (perhaps if you meddle /etc/hosts you *might* |
| 99 |
be able to get it to work generally - note containers running in VMs |
| 100 |
make this an interesting problem). |
| 101 |
|
| 102 |
Port Mapping: |
| 103 |
Each container uses one port to enable SSH, the SSH config maps these |
| 104 |
ports to hostnames. The ports are as follows: |
| 105 |
|
| 106 |
Job Runner: |
| 107 |
* 42200 - "background/at" |
| 108 |
|
| 109 |
Filesystem: |
| 110 |
* +20 - "indep" |
| 111 |
* +30 - "shared" |
| 112 |
|
| 113 |
Comms Method: |
| 114 |
* +0 - "tcp" & "ssh" |
| 115 |
* +1 - "poll" |
| 116 |
__USAGE__ |
| 117 |
exit 0 |
| 118 |
} |
| 119 |
|
| 120 |
set -eu |
| 121 |
|
| 122 |
cd "$(dirname "$0")/../../" |
| 123 |
|
| 124 |
YES=false |
| 125 |
SSHD="$HOME/.ssh" |
| 126 |
SSH_CONF="$SSHD/config" |
| 127 |
CYLC_CONF="$HOME/.cylc/flow/$(cylc version)/global.cylc" |
| 128 |
CYLC_TEST_CONF="$HOME/.cylc/flow/$(cylc version)/global-tests.cylc" |
| 129 |
HERE="$(realpath "$PWD")" |
| 130 |
ACTIVE_CONTAINERS="$HERE/.docker-active-containers" |
| 131 |
|
| 132 |
# display message, ask [yn] question, return 0 (y) or 1 (n) |
| 133 |
# if YES=true return 0 |
| 134 |
prompt () { |
| 135 |
# the message to display |
| 136 |
local MSG="$1" |
| 137 |
|
| 138 |
if $YES; then |
| 139 |
return 0 |
| 140 |
fi |
| 141 |
|
| 142 |
local USR='' |
| 143 |
while true; do |
| 144 |
read -rp "$MSG [y/n]: " USR |
| 145 |
case $USR in |
| 146 |
[Yy]) |
| 147 |
return 0 |
| 148 |
;; |
| 149 |
[Nn]) |
| 150 |
return 1 |
| 151 |
;; |
| 152 |
esac |
| 153 |
done |
| 154 |
} |
| 155 |
|
| 156 |
# wrapper of ssh-keygen |
| 157 |
generate_keys () { |
| 158 |
# the name of the key to generate |
| 159 |
local KEY="$1" |
| 160 |
|
| 161 |
if ! [[ -f "$SSHD/$KEY" ]]; then |
| 162 |
if prompt "(Re)Generate ssh key: $KEY?"; then |
| 163 |
ssh-keygen \ |
| 164 |
-t rsa \ |
| 165 |
-b 4096 \ |
| 166 |
-C 'docker@localhost' \ |
| 167 |
-f "$SSHD/$KEY" \ |
| 168 |
-P '' |
| 169 |
else |
| 170 |
exit 1 |
| 171 |
fi |
| 172 |
fi |
| 173 |
} |
| 174 |
|
| 175 |
# generate ssh keys for host-container comms |
| 176 |
generate_ssh_keys () { |
| 177 |
local BASE='.docker-ssh-keys' |
| 178 |
local KEY |
| 179 |
mkdir "$BASE" -p |
| 180 |
|
| 181 |
# ssh key pair for connecting to docker containers |
| 182 |
KEY='cylc-docker' |
| 183 |
generate_keys "$KEY" "$BASE" |
| 184 |
cp "$SSHD/$KEY"* "$BASE" |
| 185 |
} |
| 186 |
|
| 187 |
# add a line to a configuration file touching the file if not present |
| 188 |
append_config () { |
| 189 |
# the line to add to the config |
| 190 |
local LINE="$1" |
| 191 |
# the config to add the line to |
| 192 |
local LOC="$2" |
| 193 |
# whether the line should appear at the top or bottom (default bottom) |
| 194 |
local POS=${3:-bottom} |
| 195 |
|
| 196 |
# create the config if not already there |
| 197 |
mkdir -p "$(dirname "$LOC")" |
| 198 |
touch "$LOC" |
| 199 |
|
| 200 |
# add the line to the config... |
| 201 |
if ! grep -q "$LINE" "$LOC"; then |
| 202 |
if prompt "Write \"$LINE\" to \"$LOC\"?"; then |
| 203 |
if [[ "$POS" == top ]]; then |
| 204 |
# ... at the top of the file |
| 205 |
echo -e "${LINE}\n$(cat "$LOC")" > "$LOC" |
| 206 |
elif [[ "$POS" == bottom ]]; then |
| 207 |
# ... at the bottom of the file |
| 208 |
echo -e "\n$LINE" >> "$LOC" |
| 209 |
else |
| 210 |
# ... nowhere |
| 211 |
echo "Invalid position :$PWD" >&2 |
| 212 |
exit 1 |
| 213 |
fi |
| 214 |
|
| 215 |
else |
| 216 |
exit 1 |
| 217 |
fi |
| 218 |
fi |
| 219 |
} |
| 220 |
|
| 221 |
# docker run opts for independent filesystem images |
| 222 |
#INDEP_FS_OPTS=( |
| 223 |
#) |
| 224 |
# docker run opts for shared filesystem images |
| 225 |
SHARED_FS_OPTS=( |
| 226 |
'-v' "$HOME/cylc-run:/root/cylc-run" |
| 227 |
#'-p' '43001-43100:43001-43100' # all cylc run ports |
| 228 |
'-P' # all ports |
| 229 |
) |
| 230 |
# docker run opts for tcp communication images |
| 231 |
TCP_OPTS=( |
| 232 |
'-e' "HOST_HOSTNAME=$(hostname -f)" |
| 233 |
) |
| 234 |
if [[ $(uname) == "Linux" ]]; then |
| 235 |
# host.docker.internal is not set on Linux. The default address of the host |
| 236 |
# on the docker0 network is 172.17.0.1, but let's extract it just in case. |
| 237 |
DOCKER_HOST=$(ip -4 addr show docker0 | grep -Po 'inet \K[\d.]+') |
| 238 |
TCP_OPTS+=( |
| 239 |
'--add-host' "host.docker.internal:${DOCKER_HOST}" |
| 240 |
) |
| 241 |
fi |
| 242 |
|
| 243 |
# docker run opts for polling communication images |
| 244 |
POLL_OPTS=( |
| 245 |
'-e' 'HOST_HOSTNAME=false' |
| 246 |
) |
| 247 |
# the lowest port to use for communicating with containers |
| 248 |
BASE_PORT=42220 |
| 249 |
|
| 250 |
# wrapper for running containers with the required options |
| 251 |
_run () { |
| 252 |
# run a container |
| 253 |
local fs="$1" # i.e. indep, shared |
| 254 |
local comms="$2" # i.e. tcp, poll |
| 255 |
local detached="${3:-false}" # i.e. true, false |
| 256 |
local port |
| 257 |
|
| 258 |
# set the detach args |
| 259 |
local opts=() |
| 260 |
if ${detached}; then |
| 261 |
opts+=('-i') |
| 262 |
else |
| 263 |
opts+=('-d') |
| 264 |
fi |
| 265 |
|
| 266 |
# set the filesystem args |
| 267 |
if [[ $fs == indep ]]; then |
| 268 |
port=$BASE_PORT |
| 269 |
# opts+=("${INDEP_FS_OPTS[@]}") |
| 270 |
elif [[ $fs == shared ]]; then |
| 271 |
port=$(( BASE_PORT + 10 )) |
| 272 |
opts+=("${SHARED_FS_OPTS[@]}") |
| 273 |
else |
| 274 |
echo "invalid fs type '$fs'" >&2 |
| 275 |
exit 1 |
| 276 |
fi |
| 277 |
|
| 278 |
# set the communications args |
| 279 |
if [[ $comms == tcp ]]; then |
| 280 |
opts+=("${TCP_OPTS[@]}") |
| 281 |
elif [[ $comms == poll ]]; then |
| 282 |
opts+=("${POLL_OPTS[@]}") |
| 283 |
(( port += 1 )) |
| 284 |
else |
| 285 |
echo "invalid comms method '$comms'" >&2 |
| 286 |
exit 1 |
| 287 |
fi |
| 288 |
|
| 289 |
# run docker run |
| 290 |
docker run \ |
| 291 |
--rm \ |
| 292 |
-t --privileged \ |
| 293 |
-p "$port:22" \ |
| 294 |
"${opts[@]}" \ |
| 295 |
cylc-remote:latest |
| 296 |
} |
| 297 |
|
| 298 |
# build all docker images |
| 299 |
build () { |
| 300 |
docker build . \ |
| 301 |
-f dockerfiles/cylc-dev/Dockerfile \ |
| 302 |
-t cylc-dev:latest |
| 303 |
docker build . \ |
| 304 |
-f dockerfiles/cylc-remote/Dockerfile \ |
| 305 |
-t cylc-remote:latest |
| 306 |
} |
| 307 |
|
| 308 |
# configure the system so we can use these images as cylc platforms |
| 309 |
configure () { |
| 310 |
# generate ssh keys for host - docker image communication |
| 311 |
generate_ssh_keys |
| 312 |
|
| 313 |
# map exposed docker ports to hostnames |
| 314 |
append_config \ |
| 315 |
"Include $HERE/etc/conf/ssh_config" \ |
| 316 |
"${SSH_CONF}" \ |
| 317 |
top # ssh config includes must be at the top |
| 318 |
|
| 319 |
# map ssh hostnames to cylc platforms |
| 320 |
append_config \ |
| 321 |
"%include '$HERE/etc/conf/global.cylc'" \ |
| 322 |
"${CYLC_CONF}" |
| 323 |
append_config \ |
| 324 |
"%include '$HERE/etc/conf/global.cylc'" \ |
| 325 |
"${CYLC_TEST_CONF}" |
| 326 |
} |
| 327 |
|
| 328 |
# undo configurations make in configure () |
| 329 |
deconfigure () { |
| 330 |
sed -i "\|$HERE/etc/conf|d" "${SSH_CONF}" |
| 331 |
sed -i "\|$HERE/etc/conf|d" "${CYLC_CONF}" |
| 332 |
sed -i "\|$HERE/etc/conf|d" "${CYLC_TEST_CONF}" |
| 333 |
} |
| 334 |
|
| 335 |
# run one container interactively |
| 336 |
interactive () { |
| 337 |
_run indep tcp true |
| 338 |
} |
| 339 |
|
| 340 |
# run all containers detached |
| 341 |
run () { |
| 342 |
( |
| 343 |
_run indep tcp |
| 344 |
_run indep poll |
| 345 |
_run shared tcp |
| 346 |
) > "$ACTIVE_CONTAINERS" |
| 347 |
cat "$ACTIVE_CONTAINERS" |
| 348 |
} |
| 349 |
|
| 350 |
# list all containers started with run () |
| 351 |
ps () { |
| 352 |
if [[ -f "$ACTIVE_CONTAINERS" ]]; then |
| 353 |
cat "$ACTIVE_CONTAINERS" |
| 354 |
fi |
| 355 |
} |
| 356 |
|
| 357 |
# kill all containers started with run () |
| 358 |
kill () { |
| 359 |
# shellcheck disable=SC2046 |
| 360 |
if [[ -f "$ACTIVE_CONTAINERS" ]]; then |
| 361 |
docker kill $(cat "$ACTIVE_CONTAINERS") |
| 362 |
rm "$ACTIVE_CONTAINERS" |
| 363 |
fi |
| 364 |
} |
| 365 |
|
| 366 |
cmds=() |
| 367 |
for arg in "$@"; do |
| 368 |
case "$arg" in |
| 369 |
--help) |
| 370 |
usage |
| 371 |
;; |
| 372 |
-y|--yes) |
| 373 |
YES=true |
| 374 |
;; |
| 375 |
--debug) |
| 376 |
set -x |
| 377 |
;; |
| 378 |
build|deconfigure|configure|run|interactive|ps|kill) |
| 379 |
cmds+=("$arg") |
| 380 |
;; |
| 381 |
*) |
| 382 |
echo "Invalid argument '$arg'" >&2 |
| 383 |
exit 1 |
| 384 |
esac |
| 385 |
done |
| 386 |
|
| 387 |
if [[ ${#cmds[@]} -eq 0 ]]; then |
| 388 |
usage |
| 389 |
fi |
| 390 |
|
| 391 |
for cmd in "${cmds[@]}"; do |
| 392 |
"${cmd}" |
| 393 |
done |