-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial.sh
More file actions
147 lines (132 loc) · 4.79 KB
/
tutorial.sh
File metadata and controls
147 lines (132 loc) · 4.79 KB
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
#!/bin/bash
BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
NOF_HOSTS=2
NETWORK_NAME="reacteev.tutorial"
WORKSPACE="${BASEDIR}/workspace"
TUTORIALS_FOLDER="${BASEDIR}/tutorials"
HOSTPORT_BASE=${HOSTPORT_BASE:-33000}
# Extra ports per host to expose. Should contain $NOF_HOSTS variables
EXTRA_PORTS=( "8080" "8080" "8080" )
# Port Mapping
# +-----------+----------------+-------------------+
# | Container | Container Port | Host Port |
# +-----------+----------------+-------------------+
# | host0 | 80 | $HOSTPORT_BASE |
# +-----------+----------------+-------------------+
# | host1 | 80 | $HOSTPORT_BASE+1 |
# +-----------+----------------+-------------------+
# | host2 | 80 | $HOSTPORT_BASE+2 |
# +-----------+----------------+-------------------+
# | host0 | EXTRA_PORTS[0] | $HOSTPORT_BASE+3 |
# +-----------+----------------+-------------------+
# | host1 | EXTRA_PORTS[1] | $HOSTPORT_BASE+4 |
# +-----------+----------------+-------------------+
# | host2 | EXTRA_PORTS[2] | $HOSTPORT_BASE+5 |
# +-----------+----------------+-------------------+
DOCKER_IMAGETAG=static-v1
DOCKER_HOST_IMAGE="reacteev/ubuntu_ssh_docker:${DOCKER_IMAGETAG}"
TUTORIAL_IMAGE="reacteev/ansible_docker:${DOCKER_IMAGETAG}"
function help() {
echo -ne "-h, --help prints this help message
-r, --remove remove created containers and network
-t, --test run lesson tests
"
}
function doesNetworkExist() {
return $(docker network inspect $1 >/dev/null 2>&1)
}
function removeNetworkIfExists() {
doesNetworkExist $1 && echo "removing network $1" && docker network rm $1 >/dev/null
}
function doesContainerExist() {
return $(docker inspect $1 >/dev/null 2>&1)
}
function isContainerRunning() {
[[ "$(docker inspect -f "{{.State.Running}}" $1 2>/dev/null)" == "true" ]]
}
function killContainerIfExists() {
doesContainerExist $1 && echo "killing/removing container $1" && { docker kill $1 >/dev/null 2>&1; docker rm $1 >/dev/null 2>&1; };
}
function runHostContainer() {
local name=$1
local image=$2
local port1=$(($HOSTPORT_BASE + $3))
local port2=$(($HOSTPORT_BASE + $3 + $NOF_HOSTS))
echo "starting container ${name}: mapping hostport $port1 -> container port 80 && hostport $port2 -> container port ${EXTRA_PORTS[$3]}"
if doesContainerExist ${name}; then
docker start "${name}" > /dev/null
else
docker run -d -i -p $port1:80 -p $port2:${EXTRA_PORTS[$3]} --net ${NETWORK_NAME} --name="${name}" "${image}" >/dev/null
fi
if [ $? -ne 0 ]; then
echo "Could not start host container. Exiting!"
exit 1
fi
}
function runTutorialContainer() {
local entrypoint=""
local args=""
killContainerIfExists ansible.tutorial > /dev/null
echo "starting container ansible.tutorial"
docker run -it -v "${WORKSPACE}":/home/homer/workspace:Z --net ${NETWORK_NAME} \
--env HOSTPORT_BASE=$HOSTPORT_BASE \
${entrypoint} --name="ansible.tutorial" "${TUTORIAL_IMAGE}" ${args}
return $?
}
function remove () {
for ((i = 1; i <= $NOF_HOSTS; i++)); do
killContainerIfExists homer$i.reacteev.tuto
done
removeNetworkIfExists ${NETWORK_NAME}
}
function setupFiles() {
local hosts_file="${WORKSPACE}/inventory.txt"
local hosts_etc="${WORKSPACE}/etc_hosts"
rm -f "${hosts_file}" "${hosts_etc}"
echo "[homer]" >> "${hosts_file}"
for ((i = 1; i <= $NOF_HOSTS; i++)); do
#ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' host$i.example.org)
ip=$(docker network inspect --format="{{range \$id, \$container := .Containers}}{{if eq \$container.Name \"homer$i.reacteev.tuto\"}}{{\$container.IPv4Address}} {{end}}{{end}}" ${NETWORK_NAME} | cut -d/ -f1)
echo "homer$i ansible_host=$ip ansible_user=homer ansible_ssh_private_key_file=~/.ssh/id-rsa-homer" >> "${hosts_file}"
echo "$ip homer$i.reacteev.tuto homer-host$i" >> "${hosts_etc}"
done
}
function init () {
mkdir -p "${WORKSPACE}"
doesNetworkExist "${NETWORK_NAME}" || { echo "creating network ${NETWORK_NAME}" && docker network create "${NETWORK_NAME}" >/dev/null; }
for ((i = 1; i <= $NOF_HOSTS; i++)); do
isContainerRunning homer$i.reacteev.tuto || runHostContainer homer$i.reacteev.tuto ${DOCKER_HOST_IMAGE} $i
done
setupFiles
runTutorialContainer
exit $?
}
###
MODE="init"
TEST=""
for i in "$@"; do
case $i in
-r|--remove)
MODE="remove"
shift # past argument=value
;;
-t|--test)
TEST="yes"
shift # past argument=value
;;
-h|--help)
help
exit 0
shift # past argument=value
;;
*)
echo "Unknown argument ${i#*=}"
exit 1
esac
done
if [ "${MODE}" == "remove" ]; then
remove
elif [ "${MODE}" == "init" ]; then
init
fi
exit 0