Created
November 15, 2012 00:01
-
-
Save devops-ar/4075735 to your computer and use it in GitHub Desktop.
git hook for chef deployments
This file contains 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
#!/bin/sh | |
#TODO | |
# add error checking to knife operations | |
IRC_URL="http://irc.example.com/irc.php" | |
RUNDECK_URL="https://rundeck.example.com/rundeck/" | |
template_msg="modified template:" | |
node_msg="modified node:" | |
envr_msg="modified environment:" | |
role_msg="modified role:" | |
cookbook_msg="modified cookbook:" | |
boot_msg="boot-node started in rundeck - progress: " | |
run_msg="run-client started in rundeck - progres: " | |
from="gitolite-chef-server" | |
# Safety check | |
if [ -z "$GIT_DIR" ]; then | |
echo "Don't run this script from the command line." >&2 | |
exit 1 | |
fi | |
tmpdir=/var/tmp/$$.$RANDOM | |
mkdir $tmpdir | |
read oldrev newrev refname | |
BRANCH=${refname#refs/heads/} | |
files=`git diff-tree --stat --summary --find-copies-harder $oldrev..$newrev` | |
for i in $files | |
do | |
case "$i" in | |
data_bags/nodes/*) | |
k=`echo $i | cut -f"3" -d"/"` | |
j=`basename $k .json` | |
if [ $j = "template" ]; then | |
curl -X POST -d 'json={"msg":"'${template_msg}' '${j}'","from":"'${from}'"}' ${IRC_URL} | |
exit 0 | |
fi | |
git show HEAD:$i > $tmpdir/$j.json | |
knife data bag from file nodes $tmpdir/$j.json | |
curl -X POST -d 'json={"msg":"'${node_msg}' '${j}'","from":"'${from}'"}' ${IRC_URL} | |
search=`knife node list | grep $j` | |
STATUS=$? | |
#STATUS=1 | |
if [ $STATUS -ne 0 ]; then | |
# boostrap node | |
IP=`sed -n '/eth0/{n; s/^.*target":"\(.*\)".*/\1/p}' $tmpdir/$j.json` | |
FQDN=`sed -n 's/^.*fqdn".*"\(.*\)".*/\1/p' $tmpdir/$j.json` | |
ENVIRONMENT=`sed -n 's/^.*environment".*"\(.*\)".*/\1/p' $tmpdir/$j.json` | |
#echo "the ip: $IP and the env: $ENVIRONMENT" | |
out=`ssh chef@rundeck.example.com run -j "boot-node" -p chef-workstation -- -ip ${IP} -fqdn ${FQDN} -env ${ENVIRONMENT}` | |
# the url changed from http://rundeck.example.com:4440/ | |
# to https://rundeck.example.com/rundeck/ | |
#url=`echo $out | sed 's/.*<\(.*\)>$/\1/'` | |
url=`echo $out | sed 's/.*4440\/\(.*\)>$/\1/'` | |
url=$RUNDECK_URL$url | |
curl -X POST -d 'json={"msg":"'${boot_msg}' '${url}'","from":"'${from}'"}' ${IRC_URL} | |
# run-client | |
out=`ssh chef@rundeck.example.com run -j "run-chef-client" -p chef-workstation -- -fqdn ${FQDN}` | |
url=`echo $out | sed 's/.*4440\/\(.*\)>$/\1/'` | |
url=$RUNDECK_URL$url | |
curl -X POST -d 'json={"msg":"'${run_msg}' '${url}'","from":"'${from}'"}' ${IRC_URL} | |
else | |
# run-client | |
fqdn=`echo ${search} | xargs knife node show | head -n 1 | awk '{print $3}'` | |
out=`ssh chef@rundeck.example.com run -j "run-chef-client" -p chef-workstation -- -fqdn $fqdn` | |
url=`echo $out | sed 's/.*4440\/\(.*\)>$/\1/'` | |
url=$RUNDECK_URL$url | |
curl -X POST -d 'json={"msg":"'${run_msg}' '${url}'","from":"'${from}'"}' ${IRC_URL} | |
fi | |
;; | |
roles/*) | |
k=`echo $i | cut -f"2" -d"/"` | |
#knife data bag from file nodes $tmpdir/$j.json | |
#curl -X POST -d 'json={"msg":"se modifico el node: '${j}'","from":"'${from}'"}' http://rundeck.example.com/irc.php | |
#echo "from roles!" | |
j=`basename $k .rb` | |
git show HEAD:$i > $tmpdir/$k | |
knife role from file $tmpdir/$k | |
curl -X POST -d 'json={"msg":"'${role_msg}' '${j}'","from":"'${from}'"}' ${IRC_URL} | |
;; | |
environments/*) | |
k=`echo $i | cut -f"2" -d"/"` | |
j=`basename $k .rb` | |
git show HEAD:$i > $tmpdir/$k | |
knife environment from file $tmpdir/$k | |
curl -X POST -d 'json={"msg":"'${envr_msg}' '${j}'","from":"'${from}'"}' ${IRC_URL} | |
;; | |
cookbooks/*) | |
k=`echo $i | cut -f"2" -d"/"` | |
#echo "knife cookbook upload $k from $tmpdir" | |
GIT_WORK_TREE=$tmpdir git checkout -f | |
cd $tmpdir | |
knife cookbook upload $k | |
curl -X POST -d 'json={"msg":"'${cookbook_msg}' '${k}'","from":"'${from}'"}' ${IRC_URL} | |
;; | |
*/files/*) | |
k=`echo $i | cut -f"2" -d"/"` | |
curl -X POST -d 'json={"msg":"'${cookbook_msg}' '${k}'","from":"'${from}'"}' ${IRC_URL} | |
;; | |
*/templates/*) | |
k=`echo $i | cut -f"2" -d"/"` | |
curl -X POST -d 'json={"msg":"'${cookbook_msg}' '${k}'","from":"'${from}'"}' ${IRC_URL} | |
;; | |
*) | |
# Anything else (is there anything else?) | |
#echo "anything else ... $i" | |
#exit 1 | |
;; | |
esac | |
done | |
rm -fr $tmpdir | |
exit 0 |
This file contains 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
chef-server/data_bags/nodes/template.json | |
{ | |
"id": "ID", | |
"interfaces": [ | |
{ | |
"device": "eth0", | |
"target":"IP", | |
"hwaddr":"HWADDR" | |
} | |
], | |
"fqdn": "FQDN", | |
"environment": "ENVIRONMENT", | |
"run_list": "role[base]", | |
"cname": "CNAME" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment