forked from turkenh/ansible-interactive-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial.sh
executable file
·149 lines (134 loc) · 4.8 KB
/
tutorial.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
#!/bin/bash
BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
NOF_HOSTS=3
NETWORK_NAME="ansible.tutorial"
WORKSPACE="${BASEDIR}/workspace"
TUTORIALS_FOLDER="${BASEDIR}/tutorials"
HOSTPORT_BASE=${HOSTPORT_BASE:-42726}
# Extra ports per host to expose. Should contain $NOF_HOSTS variables
EXTRA_PORTS=( "8080" "30000" "443" )
# 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=${DOCKER_IMAGETAG:-1.1}
DOCKER_HOST_IMAGE="turkenh/ubuntu-1604-ansible-docker-host:${DOCKER_IMAGETAG}"
TUTORIAL_IMAGE="turkenh/ansible-tutorial:${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 -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=""
if [ -n "${TEST}" ]; then
entrypoint="--entrypoint nutsh"
args="test /tutorials ${LESSON_NAME}"
fi
killContainerIfExists ansible.tutorial > /dev/null
echo "starting container ansible.tutorial"
docker run -it -v "${WORKSPACE}":/root/workspace:Z -v "${TUTORIALS_FOLDER}":/tutorials:Z --net ${NETWORK_NAME} \
--env HOSTPORT_BASE=$HOSTPORT_BASE \
${entrypoint} --name="ansible.tutorial" "${TUTORIAL_IMAGE}" ${args}
return $?
}
function remove () {
for ((i = 0; i < $NOF_HOSTS; i++)); do
killContainerIfExists host$i.example.org
done
removeNetworkIfExists ${NETWORK_NAME}
}
function setupFiles() {
# step-01/02
local step_01_hosts_file="${BASEDIR}/tutorials/files/step-1-2/hosts"
rm -f "${step_01_hosts_file}"
for ((i = 0; 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 \"host$i.example.org\"}}{{\$container.IPv4Address}} {{end}}{{end}}" ${NETWORK_NAME} | cut -d/ -f1)
echo "host$i.example.org ansible_host=$ip ansible_user=root" >> "${step_01_hosts_file}"
done
}
function init () {
mkdir -p "${WORKSPACE}"
doesNetworkExist "${NETWORK_NAME}" || { echo "creating network ${NETWORK_NAME}" && docker network create "${NETWORK_NAME}" >/dev/null; }
for ((i = 0; i < $NOF_HOSTS; i++)); do
isContainerRunning host$i.example.org || runHostContainer host$i.example.org ${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 "Unknow argument ${i#*=}"
exit 1
esac
done
if [ "${MODE}" == "remove" ]; then
remove
elif [ "${MODE}" == "init" ]; then
init
fi
exit 0