Skip to content

Instantly share code, notes, and snippets.

@igor47
Created January 21, 2014 19:13
Show Gist options
  • Save igor47/8546302 to your computer and use it in GitHub Desktop.
Save igor47/8546302 to your computer and use it in GitHub Desktop.
airbnb's chef-solo converge script
#!/bin/bash -eu
repo="/etc/chef/src"
origin=`cat /etc/chef/origin`
branch=`cat /etc/chef/branch`
role=`cat /etc/chef/role`
env=`cat /etc/chef/environment`
# create temporary files
git_clone=`mktemp -d`
json_file=`tempfile`
trap "{ rm -f '$json_file' ; rm -rf '$git_clone' ; }" EXIT
# clone the repo
GIT_SSH=/etc/chef/git_wrapper git clone --depth 1 --branch $branch -- $origin $git_clone 2>/dev/null
# use the new clone
if [ $? -ne 0 ]
then
echo "WARNING: git clone failed; converging from old commit"
rm -rf $git_clone
else
rm -rf $repo && mv $git_clone $repo
fi
cd $repo
# generate the json
json="{\"role\": \"${role}\", \"env\": \"${env}\", \"branch\": \"${branch}\"}"
echo $json > $json_file
# generate the run list
runlist="role[${role}]"
if [ -f "roles/${env}.rb" ]
then
runlist+=",role[${env}]"
fi
# do the converge
echo "****************************************"
echo "* Beginning Converge *"
echo "****************************************"
echo "converging to: `git log -1 --pretty=%B 2>/dev/null`"
echo "running chef-solo with:"
echo " runlist: ${runlist}"
echo " json: ${json}"
echo ""
chef-solo -o "${runlist}" -j ${json_file} "$@"
# final output
if [ $? -ne 0 ]
then
echo "****************************************"
echo "* WARNING: converge failed! *"
echo "* See above for error messages *"
echo "****************************************"
else
echo "* Converge Succeeded *"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment