Skip to content

Instantly share code, notes, and snippets.

@sbrinkmeyer
Created June 3, 2016 22:32
Show Gist options
  • Save sbrinkmeyer/e0e64c5446eaddecfdce04ce39773847 to your computer and use it in GitHub Desktop.
Save sbrinkmeyer/e0e64c5446eaddecfdce04ce39773847 to your computer and use it in GitHub Desktop.
echo "hello"
@octopustree
Copy link

!/usr/bin/env bash

declare -r docker_image=docker.phx1.jivehosted.com/generik/ansible:v2.0.2.0-1
declare -r ssh_dir=/srv/ssh
declare -r ansible_ssh_key=${ssh_dir}/ansible
declare -r ansible_dir=/srv/ansible-coreos
declare -r ansible_playbook=site.yaml
declare -r docker_run_retries=3

get_ansible_hostname() {
if [ $(hostname -f | grep "compute.internal$") ]; then
curl http://169.254.169.254/latest/meta-data/local-ipv4
else
hostname -f 2>/dev/null || hostname
fi
}

setup_ssh_keys() {
if [ ! -d ${ssh_dir} ] ; then
sudo mkdir $ssh_dir
fi
if [ ! -f ${ssh_dir}/ansible ] ; then
sudo chown core:core $ssh_dir
ssh-keygen -b 2048 -t rsa -f ${ssh_dir}/ansible -q -N ""
cp ${ssh_dir}/ansible.pub /home/core/.ssh/authorized_keys.d
update-ssh-keys
fi
}

find_inventory_group() {
if [ $(hostname -f | grep "compute.internal$") ]; then
declare -g -r ansible_inventory=inventory_ec2
else
ansible_hostname=$(get_ansible_hostname)
declare -r group=$(grep --include=hosts -ril $ansible_hostname ${ansible_dir}/inventory)
if [ $? -ne 0 ] ; then
echo "Could not locate $ansible_hostname in any inventory files."
exit 1
else
declare -g -r ansible_inventory=${group##${ansible_dir}/}
fi
fi
}

run_docker_run() {
ansible_hostname=$(get_ansible_hostname)
docker run --net=host
--rm=true -v ${ssh_dir}:/srv/ssh
-v ${ansible_dir}:${ansible_dir} -w $ansible_dir
-e ANSIBLE_HOSTNAME=${ansible_hostname}
-e ANSIBLE_INVENTORY=${ansible_inventory}
-e ANSIBLE_SSH_KEY=${ssh_dir}/ansible
-e ANSIBLE_PLAYBOOK=${ansible_playbook}
$docker_image /bin/sh bin/run-ansible-playbook.sh
}

setup_ssh_keys
find_inventory_group

We want to catch a failure scenario that is an ansible failure

but not anything related to the serverspec tests. If serverspec is

failing and we loop on a non 0 return code we will loop for along time.

However by checking that the serverspec run file exists means we got

far enough in the playbook run to consider the run a success. When

the failure does occur it seems to be extremely early.

loop=0
while ((loop <= docker_run_retries)); do
((loop++))
run_docker_run
if [ -f /opt/serverspec/bin/serverspec-run.sh ]; then
break 2
fi
if ((loop >= docker_run_retries)); then
echo "i want to reboot" >> /reboot.txt
fi
if [[ $(hostname -f | grep "compute.internal$") ]]; then
echo "i want to shutdown" >> /shutdown.txt
fi
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment