blob: 6cf42597df300819d9493a5969ea8c09d726016d [file] [log] [blame]
#!/usr/bin/env bash
set -e
if [[ $DEBUG =~ ^(True|true|1|yes)$ ]]; then
set -x
fi
## Overrideable options
DOCKER_IMAGE=${DOCKER_IMAGE:-"ubuntu:16.04"}
DIST=${DIST:-xenial}
RECLASS_ROOT=${RECLASS_ROOT:-$(pwd)}
SALT_OPTS="${SALT_OPTS} --retcode-passthrough --force-color"
DOCKER_OPTS="${DOCKER_OPTS} -e DEBIAN_FRONTEND=noninteractive"
SKIP_CLEANUP=${SKIP_CLEANUP:-0}
USER_ID=$(id -u)
GROUP_ID=$(id -g)
declare -a CONTAINERS
## Functions
log_info() {
echo "[INFO] $*"
}
log_err() {
echo "[ERROR] $*" >&2
}
docker_exec() {
if [[ $DETACH =~ ^(True|true|1|yes)$ ]]; then
exec_opts="-d"
else
exec_opts=""
fi
docker exec ${exec_opts} ${CONTAINER} /bin/bash -c "$*"
}
_atexit() {
RETVAL=$?
trap true INT TERM EXIT
if [ $SKIP_CLEANUP -eq 1 ]; then
return $RETVAL
fi
log_info "Cleaning up"
for container in ${CONTAINERS[@]}; do
CONTAINER=$container docker_exec "chown -R ${USER_ID}:${GROUP_ID} /srv/salt/reclass" || true
docker rm -f $container >/dev/null || true
done
if [ $RETVAL -ne 0 ]; then
log_err "Execution failed"
else
log_info "Execution successful"
fi
return $RETVAL
}
run_container() {
MASTER_HOSTNAME=$1
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})
echo $CONTAINER
}
test_master() {
MASTER_HOSTNAME=$1
log_info "Installing packages"
docker_exec "which wget >/dev/null || (apt-get update; apt-get install -y wget)"
docker_exec "echo 'deb [arch=amd64] http://apt-mk.mirantis.com/${DIST}/ nightly salt salt-latest' > /etc/apt/sources.list.d/apt-mk.list"
docker_exec "wget -O - http://apt-mk.mirantis.com/public.gpg | apt-key add -"
docker_exec "apt-get update"
docker_exec "apt-get install -y salt-master python-psutil iproute2 curl reclass salt-formula-*"
log_info "Setting up Salt master"
# TODO: remove grains.d hack when fixed in formula
docker_exec "mkdir -p /etc/salt/grains.d && touch /etc/salt/grains.d/dummy"
docker_exec "[ ! -d /etc/salt/pki/minion ] && mkdir -p /etc/salt/pki/minion"
docker_exec "[ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d || true"
docker_exec "cat << 'EOF' >> /etc/salt/master.d/master.conf
file_roots:
base:
- /usr/share/salt-formulas/env
pillar_opts: False
open_mode: True
reclass: &reclass
storage_type: yaml_fs
inventory_base_uri: /srv/salt/reclass
ext_pillar:
- reclass: *reclass
master_tops:
reclass: *reclass
EOF"
log_info "Setting up reclass"
docker_exec "[ -d /srv/salt/reclass/classes/service ] || mkdir -p /srv/salt/reclass/classes/service || true"
docker_exec "for i in /usr/share/salt-formulas/reclass/service/*; do
[ -e /srv/salt/reclass/classes/service/\$(basename \$i) ] || ln -s \$i /srv/salt/reclass/classes/service/\$(basename \$i)
done"
docker_exec "chown -R ${USER_ID}:${GROUP_ID} /srv/salt/reclass/classes/service"
docker_exec "[ ! -d /etc/reclass ] && mkdir /etc/reclass || true"
docker_exec "cat << 'EOF' >> /etc/reclass/reclass-config.yml
storage_type: yaml_fs
pretty_print: True
output: yaml
inventory_base_uri: /srv/salt/reclass
EOF"
log_info "Setting up Salt minion"
docker_exec "apt-get install -y salt-minion"
docker_exec "[ ! -d /etc/salt/minion.d ] && mkdir -p /etc/salt/minion.d || true"
docker_exec "cat << EOF >> /etc/salt/minion.d/minion.conf
id: ${MASTER_HOSTNAME}
master: localhost
EOF"
log_info "Starting Salt master service"
DETACH=1 docker_exec "/usr/bin/salt-master"
sleep 3
docker_exec "salt-call saltutil.sync_all"
log_info "Running states to finish Salt master setup"
docker_exec "reclass --nodeinfo ${MASTER_HOSTNAME} >/dev/null"
docker_exec "salt-call ${SALT_OPTS} state.show_top"
if [[ $SALT_MASTER_FULL =~ ^(True|true|1|yes)$ ]]; then
# TODO: can fail on "hostname: you must be root to change the host name"
docker_exec "salt-call ${SALT_OPTS} state.sls linux,openssh" || true
docker_exec "salt-call ${SALT_OPTS} state.sls salt,reclass"
else
docker_exec "salt-call ${SALT_OPTS} state.sls reclass.storage.node" || true
fi
NODES=$(docker_exec "find /srv/salt/reclass/nodes -type f -name *.yml ! -name cfg*")
for node in ${NODES}; do
node=$(basename $node .yml)
log_info "Testing node ${node}"
docker_exec "reclass --nodeinfo ${node} >/dev/null"
docker_exec "salt-call ${SALT_OPTS} --id=${node} state.show_top"
docker_exec "salt-call ${SALT_OPTS} --id=${node} state.show_lowstate >/dev/null"
done
}
## Main
trap _atexit INT TERM EXIT
masters=$(find nodes -type f -name 'cfg*.yml')
for master in ${masters[@]}; do
master=$(basename $master .yml)
log_info "Testing Salt master ${master}"
log_info "Creating docker container from image ${DOCKER_IMAGE}"
CONTAINER=$(run_container $master)
CONTAINERS+=(${CONTAINER})
test_master $master
done