Skip to content

Instantly share code, notes, and snippets.

@Valiev
Created December 30, 2014 13:11
Show Gist options
  • Save Valiev/603410d8ce66abdcf42e to your computer and use it in GitHub Desktop.
Save Valiev/603410d8ce66abdcf42e to your computer and use it in GitHub Desktop.
pre-commit hook for Chef configuration repo
#!/usr/bin/env sh
set -e
# set -x
set -o pipefail
die() { echo "$@" 1>&2 ; exit 1; }
# ========================================================================
# Initialization
# ========================================================================
which chef-zero > /dev/null || die "No chef-zero command found"
chef-zero &
sleep 1
CURRENT_PID=$$
CHEF_ZERO_PID=$(ps ax -o pid,ppid,args | grep chef-zero | grep -v grep | awk '{ print $1 }')
KNIFE_CONFIG=`mktemp -t knife.rb`
CLIENT_KEY=`mktemp -u -t client.pem` # just name
CHEF_REPO_PATH="."
ssh-keygen -f $CLIENT_KEY -N '' -t rsa > /dev/null
cat > $KNIFE_CONFIG <<-EOF
node_name 'precommit-hook'
client_key "$CLIENT_KEY"
chef_server_url 'http://127.0.0.1:8889'
EOF
cleanup() {
rm $KNIFE_CONFIG
rm $CLIENT_KEY
kill $CHEF_ZERO_PID
}
terminate() {
cleanup
die $1
}
# ========================================================================
# Checks
# ========================================================================
echo "Checking data bags upload"
knife upload -c $KNIFE_CONFIG --chef-repo-path $CHEF_REPO_PATH data_bags \
|| terminate "Failed to check data bags"
echo "Checking environment upload"
knife environment from file environments/*.{json,rb} -c $KNIFE_CONFIG \
|| terminate "Failed to check environments"
echo "Checking roles upload"
knife role from file roles/*.{json,rb} -c $KNIFE_CONFIG \
|| terminate "Failed to check roles"
# ========================================================================
# Clean up
# ========================================================================
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment