-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.sh
executable file
·114 lines (92 loc) · 2.11 KB
/
test.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
#!/bin/bash
DEPENDENCIES="'docker' 'docker-compose' 'ansible' 'jq'"
CONF=test.cfg
# change working directory to make relative paths working correctly
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# check dependencies
for dependency in $DEPENDENCIES
do
dep=$(whereis $dependency)
if [[ ! $dep ]]; then
echo "needed dependency $dependency missing! Aborting..."
exit 1
fi
done
# setup docker containers
# build docker-compose.yml
echo "creating docker-compose.yml"
cat << EOF > test/docker-compose.yml
version: '3'
services:
EOF
i=0
for instance in $(jq '.[]' $CONF)
do
role=$(jq -rc ".[$i].role" $CONF)
os=$(jq -rc ".[$i].os" $CONF)
number=$(jq -rc ".[$i].number" $CONF)
if [ -d test/$os/ ]; then
cat << EOF >> test/docker-compose.yml
$role:
build:
context: ./$os
command: /usr/sbin/sshd -D
deploy:
replicas: $number
EOF
fi
i=$i+1
done
cd test/
echo "creating docker image"
sudo docker-compose build
# start scale of docker containers
echo "starting containers"
# --compatibility flag used instead of --scale (https://github.com/docker/compose/issues/5586)
# to make use of replicas
sudo docker-compose --compatibility up -d
cd ..
if [ -e hosts ]; then
mv hosts hosts.bak
fi
# create hosts file for ansible
touch hosts
allroles=$(jq -r '.[].role' $CONF)
# get names of running containers
containers=$(sudo docker ps --format '{{.Names}}')
echo $containers
echo $allroles
for role in $allroles
do
echo $role
# create hosts file for ansible
cat << EOF >> hosts
[$role]
EOF
# find all container with role
for container in $containers
do
echo $container
if [[ $container = *$role* ]]; then
echo $container
ip=$(sudo docker exec $container hostname -i)
cat << EOF >> hosts
$container ansible_host=$ip ansible_user=root ansible_ssh_pass=password ansible_connection=paramiko ansible_python_interpreter=/usr/bin/python3
EOF
fi
done
done
# test playbooks
ansible-playbook -i hosts deploy.yml
if [ $?=0 ]; then
echo="playbook worked as expected"
else
echo="some error has occured"
fi
# cleanup
if [ -e hosts.bak ]; then
mv hosts.bak hosts
fi
cd test/
sudo docker-compose down
exit 0