Created
December 30, 2014 13:11
-
-
Save Valiev/603410d8ce66abdcf42e to your computer and use it in GitHub Desktop.
pre-commit hook for Chef configuration repo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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