blob: f4770070ddb537f7199e30b762cd7549712b6c31 [file] [log] [blame]
Tomáš Kukrál2c7026a2017-03-08 18:19:53 +01001#!/usr/bin/env bash
2
3set -e
4if [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
5 set -x
6fi
7
8## Overrideable options
9DOCKER_IMAGE=${DOCKER_IMAGE:-"ubuntu:16.04"}
10DIST=${DIST:-xenial}
11RECLASS_ROOT=${RECLASS_ROOT:-$(pwd)}
12SALT_OPTS="${SALT_OPTS} --retcode-passthrough --force-color"
13DOCKER_OPTS="${DOCKER_OPTS} -e DEBIAN_FRONTEND=noninteractive"
14SKIP_CLEANUP=${SKIP_CLEANUP:-0}
Jakub Josef5ec16552017-05-05 17:58:54 +020015USER_ID=$(id -u)
16GROUP_ID=$(id -g)
Tomáš Kukrál2c7026a2017-03-08 18:19:53 +010017
18declare -a CONTAINERS
19
20## Functions
21log_info() {
22 echo "[INFO] $*"
23}
24
25log_err() {
26 echo "[ERROR] $*" >&2
27}
28
29docker_exec() {
30 if [[ $DETACH =~ ^(True|true|1|yes)$ ]]; then
31 exec_opts="-d"
32 else
33 exec_opts=""
34 fi
35
36 docker exec ${exec_opts} ${CONTAINER} /bin/bash -c "$*"
37}
38
39_atexit() {
40 RETVAL=$?
41 trap true INT TERM EXIT
42 if [ $SKIP_CLEANUP -eq 1 ]; then
43 return $RETVAL
44 fi
45
46 log_info "Cleaning up"
47
48 for container in ${CONTAINERS[@]}; do
Jakub Josef5ec16552017-05-05 17:58:54 +020049 CONTAINER=$container docker_exec "chown -R ${USER_ID}:${GROUP_ID} /srv/salt/reclass" || true
Tomáš Kukrál2c7026a2017-03-08 18:19:53 +010050 docker rm -f $container >/dev/null || true
51 done
52
53 if [ $RETVAL -ne 0 ]; then
54 log_err "Execution failed"
55 else
56 log_info "Execution successful"
57 fi
58
59 return $RETVAL
60}
61
62run_container() {
63 MASTER_HOSTNAME=$1
Martin Polreich441229f2017-03-31 16:20:16 +020064 CONTAINER=$(docker run ${DOCKER_OPTS} --name ${MASTER_HOSTNAME}-$(date +%s) -h $(echo ${MASTER_HOSTNAME}|cut -d . -f 1) -v ${RECLASS_ROOT}:/srv/salt/reclass -i -t -d ${DOCKER_IMAGE})
Tomáš Kukrál2c7026a2017-03-08 18:19:53 +010065 echo $CONTAINER
66}
67
68test_master() {
69 MASTER_HOSTNAME=$1
70 log_info "Installing packages"
71 docker_exec "which wget >/dev/null || (apt-get update; apt-get install -y wget)"
72 docker_exec "echo 'deb [arch=amd64] http://apt-mk.mirantis.com/${DIST}/ nightly salt salt-latest' > /etc/apt/sources.list.d/apt-mk.list"
73 docker_exec "wget -O - http://apt-mk.mirantis.com/public.gpg | apt-key add -"
74
75 docker_exec "apt-get update"
76 docker_exec "apt-get install -y salt-master python-psutil iproute2 curl reclass salt-formula-*"
77
78 log_info "Setting up Salt master"
79 # TODO: remove grains.d hack when fixed in formula
80 docker_exec "mkdir -p /etc/salt/grains.d && touch /etc/salt/grains.d/dummy"
81 docker_exec "[ ! -d /etc/salt/pki/minion ] && mkdir -p /etc/salt/pki/minion"
82 docker_exec "[ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d || true"
83 docker_exec "cat << 'EOF' >> /etc/salt/master.d/master.conf
84file_roots:
85 base:
86 - /usr/share/salt-formulas/env
87pillar_opts: False
88open_mode: True
89reclass: &reclass
90 storage_type: yaml_fs
91 inventory_base_uri: /srv/salt/reclass
92ext_pillar:
93 - reclass: *reclass
94master_tops:
95 reclass: *reclass
96EOF"
97
98 log_info "Setting up reclass"
99 docker_exec "[ -d /srv/salt/reclass/classes/service ] || mkdir -p /srv/salt/reclass/classes/service || true"
100 docker_exec "for i in /usr/share/salt-formulas/reclass/service/*; do
101 [ -e /srv/salt/reclass/classes/service/\$(basename \$i) ] || ln -s \$i /srv/salt/reclass/classes/service/\$(basename \$i)
102 done"
Jakub Josef5ec16552017-05-05 17:58:54 +0200103 docker_exec "chown -R ${USER_ID}:${GROUP_ID} /srv/salt/reclass/classes/service"
Tomáš Kukrál2c7026a2017-03-08 18:19:53 +0100104 docker_exec "[ ! -d /etc/reclass ] && mkdir /etc/reclass || true"
105 docker_exec "cat << 'EOF' >> /etc/reclass/reclass-config.yml
106storage_type: yaml_fs
107pretty_print: True
108output: yaml
109inventory_base_uri: /srv/salt/reclass
110EOF"
111
112 log_info "Setting up Salt minion"
113 docker_exec "apt-get install -y salt-minion"
114 docker_exec "[ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d || true"
115 docker_exec "cat << EOF >> /etc/salt/minion.d/minion.conf
116id: ${MASTER_HOSTNAME}
117master: localhost
118EOF"
119
120 log_info "Starting Salt master service"
121 DETACH=1 docker_exec "/usr/bin/salt-master"
122 sleep 3
123
124 docker_exec "salt-call saltutil.sync_all"
125 log_info "Running states to finish Salt master setup"
126 docker_exec "reclass --nodeinfo ${MASTER_HOSTNAME} >/dev/null"
127 docker_exec "salt-call ${SALT_OPTS} state.show_top"
128
129 if [[ $SALT_MASTER_FULL =~ ^(True|true|1|yes)$ ]]; then
130 # TODO: can fail on "hostname: you must be root to change the host name"
131 docker_exec "salt-call ${SALT_OPTS} state.sls linux,openssh" || true
132 docker_exec "salt-call ${SALT_OPTS} state.sls salt,reclass"
133 else
134 docker_exec "salt-call ${SALT_OPTS} state.sls reclass.storage.node" || true
135 fi
136
137 NODES=$(docker_exec "find /srv/salt/reclass/nodes -type f -name *.yml ! -name cfg*")
138 for node in ${NODES}; do
139 node=$(basename $node .yml)
140 log_info "Testing node ${node}"
141 docker_exec "reclass --nodeinfo ${node} >/dev/null"
142 docker_exec "salt-call ${SALT_OPTS} --id=${node} state.show_top"
143 docker_exec "salt-call ${SALT_OPTS} --id=${node} state.show_lowstate >/dev/null"
144 done
145}
146
147
148## Main
149trap _atexit INT TERM EXIT
150
151masters=$(find nodes -type f -name cfg*.yml)
152for master in ${masters[@]}; do
153 master=$(basename $master .yml)
154 log_info "Testing Salt master ${master}"
155 log_info "Creating docker container from image ${DOCKER_IMAGE}"
156 CONTAINER=$(run_container $master)
157 CONTAINERS+=(${CONTAINER})
158 test_master $master
159done