-
Notifications
You must be signed in to change notification settings - Fork 5
/
run.sh
executable file
·139 lines (123 loc) · 3.71 KB
/
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
#!/bin/bash
BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Please modify the number of Hosts !
NOF_HOSTS=8
NETWORK_NAME="ansible.lab"
WORKSPACE="${BASEDIR}/lab"
HOSTPORT_BASE=${HOSTPORT_BASE:-42726}
DOCKER_IMAGETAG=${DOCKER_IMAGETAG:-latest}
DOCKER_HOST_IMAGE="centos-7-ansible-docker-host:${DOCKER_IMAGETAG}"
LAB_IMAGE="ansible-controller:${DOCKER_IMAGETAG}"
function help() {
echo -ne "-h, --help prints this help message
-r, --remove remove created containers and network
-t, --test run ansible version
"
}
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))
echo "starting container ${name}: mapping hostport $port1 -> container port 80"
if doesContainerExist ${name}; then
docker start "${name}" > /dev/null
else
docker run --privileged -d -p $port1:80 --net ${NETWORK_NAME} --name="${name}" "${image}" >/dev/null
fi
if [ $? -ne 0 ]; then
echo "Could not start host container. Exiting!"
exit 1
fi
}
function runControllerContainer() {
local entrypoint=""
local args=""
if [ -n "${TEST}" ]; then
entrypoint="--entrypoint ansible"
args="--version"
fi
killContainerIfExists ansible.controller > /dev/null
echo "starting container ansible.controller"
docker run --privileged -it -v "${WORKSPACE}":/root/lab:Z --net ${NETWORK_NAME} \
--env HOSTPORT_BASE=$HOSTPORT_BASE \
${entrypoint} --name="ansible.controller" "${LAB_IMAGE}" ${args}
return $?
}
function remove () {
for ((i = 0; i < $NOF_HOSTS; i++)); do
killContainerIfExists node$i
done
removeNetworkIfExists ${NETWORK_NAME}
}
function setupFiles() {
local inventory="${WORKSPACE}/inventory"
rm -f "${inventory}"
echo "[nodes]" > "${inventory}"
for ((i = 0; i < $NOF_HOSTS; i++)); do
ip=$(docker network inspect --format="{{range \$id, \$container := .Containers}}{{if eq \$container.Name \"node$i\"}}{{\$container.IPv4Address}} {{end}}{{end}}" ${NETWORK_NAME} | cut -d/ -f1)
echo "node$i ansible_host=$ip ansible_user=root" >> "${inventory}"
done
local config="${WORKSPACE}/ansible.cfg"
rm -f "${config}"
cat << EOF > ${config}
[defaults]
host_key_checking = False
inventory = ./inventory
# Logging
callback_whitelist = profile_tasks
EOF
}
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 node$i || runHostContainer node$i ${DOCKER_HOST_IMAGE} $i
done
setupFiles
runControllerContainer
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