blob: 4286ad1c5f6bab8e4ca281c54eadce393e8e76dd [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"
Matthew Mosesohn79b90b82017-05-24 17:12:18 +030076 docker_exec "apt-get install -y salt-master python-psutil iproute2 curl python-dev python-pip reclass salt-formula-*"
77 # FIXME: Use master reclass to debug undefined vars. Remove when reclass is updated
78 docker_exec "pip install -U https://github.com/madduck/reclass/archive/master.zip"
Tomáš Kukrál2c7026a2017-03-08 18:19:53 +010079
80 log_info "Setting up Salt master"
81 # TODO: remove grains.d hack when fixed in formula
82 docker_exec "mkdir -p /etc/salt/grains.d && touch /etc/salt/grains.d/dummy"
83 docker_exec "[ ! -d /etc/salt/pki/minion ] && mkdir -p /etc/salt/pki/minion"
84 docker_exec "[ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d || true"
85 docker_exec "cat << 'EOF' >> /etc/salt/master.d/master.conf
86file_roots:
87 base:
88 - /usr/share/salt-formulas/env
89pillar_opts: False
90open_mode: True
91reclass: &reclass
92 storage_type: yaml_fs
93 inventory_base_uri: /srv/salt/reclass
94ext_pillar:
95 - reclass: *reclass
96master_tops:
97 reclass: *reclass
98EOF"
99
100 log_info "Setting up reclass"
101 docker_exec "[ -d /srv/salt/reclass/classes/service ] || mkdir -p /srv/salt/reclass/classes/service || true"
102 docker_exec "for i in /usr/share/salt-formulas/reclass/service/*; do
103 [ -e /srv/salt/reclass/classes/service/\$(basename \$i) ] || ln -s \$i /srv/salt/reclass/classes/service/\$(basename \$i)
104 done"
Jakub Josef5ec16552017-05-05 17:58:54 +0200105 docker_exec "chown -R ${USER_ID}:${GROUP_ID} /srv/salt/reclass/classes/service"
Tomáš Kukrál2c7026a2017-03-08 18:19:53 +0100106 docker_exec "[ ! -d /etc/reclass ] && mkdir /etc/reclass || true"
107 docker_exec "cat << 'EOF' >> /etc/reclass/reclass-config.yml
108storage_type: yaml_fs
109pretty_print: True
110output: yaml
111inventory_base_uri: /srv/salt/reclass
112EOF"
113
114 log_info "Setting up Salt minion"
115 docker_exec "apt-get install -y salt-minion"
116 docker_exec "[ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d || true"
117 docker_exec "cat << EOF >> /etc/salt/minion.d/minion.conf
118id: ${MASTER_HOSTNAME}
119master: localhost
120EOF"
121
122 log_info "Starting Salt master service"
123 DETACH=1 docker_exec "/usr/bin/salt-master"
124 sleep 3
125
126 docker_exec "salt-call saltutil.sync_all"
127 log_info "Running states to finish Salt master setup"
128 docker_exec "reclass --nodeinfo ${MASTER_HOSTNAME} >/dev/null"
129 docker_exec "salt-call ${SALT_OPTS} state.show_top"
130
131 if [[ $SALT_MASTER_FULL =~ ^(True|true|1|yes)$ ]]; then
132 # TODO: can fail on "hostname: you must be root to change the host name"
133 docker_exec "salt-call ${SALT_OPTS} state.sls linux,openssh" || true
134 docker_exec "salt-call ${SALT_OPTS} state.sls salt,reclass"
135 else
136 docker_exec "salt-call ${SALT_OPTS} state.sls reclass.storage.node" || true
137 fi
138
139 NODES=$(docker_exec "find /srv/salt/reclass/nodes -type f -name *.yml ! -name cfg*")
140 for node in ${NODES}; do
141 node=$(basename $node .yml)
142 log_info "Testing node ${node}"
143 docker_exec "reclass --nodeinfo ${node} >/dev/null"
144 docker_exec "salt-call ${SALT_OPTS} --id=${node} state.show_top"
145 docker_exec "salt-call ${SALT_OPTS} --id=${node} state.show_lowstate >/dev/null"
146 done
147}
148
149
150## Main
151trap _atexit INT TERM EXIT
152
Tomáš Kukrál9b58f5b2017-05-11 12:04:01 +0200153masters=$(find nodes -type f -name 'cfg*.yml')
Tomáš Kukrál2c7026a2017-03-08 18:19:53 +0100154for master in ${masters[@]}; do
155 master=$(basename $master .yml)
156 log_info "Testing Salt master ${master}"
157 log_info "Creating docker container from image ${DOCKER_IMAGE}"
158 CONTAINER=$(run_container $master)
159 CONTAINERS+=(${CONTAINER})
160 test_master $master
161done