Skip to content

Instantly share code, notes, and snippets.

@weavenet
Created January 26, 2012 16:03
Show Gist options
  • Save weavenet/1683492 to your computer and use it in GitHub Desktop.
Save weavenet/1683492 to your computer and use it in GitHub Desktop.
Pull Metadata From Cloud Formation And Run Chef
#!/bin/bash
if [ `whoami` != root ]; then
echo "---- Please run this as the 'root' user";
exit 1
fi
# Static settings
dir=/etc/chef/lwe-repo
md_file=/etc/chef/metadata.json
# Validate Cloud Formation Metadata retrieval script is present
# See /etc/rc.local for generation of this file
if [ ! -x /etc/chef/get-metadata.sh ];then
echo "/etc/chef/get-metadata.sh not present, exiting."
exit 1
fi
# Retrieve metadata from Cloud Formation
/etc/chef/get-metadata.sh > $md_file
if [ $? -ne 0 ]; then
echo "Error retrieving metadata"
exit 1
fi
# Chef settings
role=`cat $md_file | grep '"Role"' |awk '{print $2}' |cut -d\" -f2`
repo=`cat $md_file | grep '"ChefGitRepo"' |awk '{print $2}' |cut -d\" -f2`
version=`cat $md_file | grep '"ChefRepoVersion"' |awk '{print $2}' |cut -d\" -f2`
# S3 Config
ssh_key=`cat $md_file | grep '"SSHKey"' |awk '{print $2}' |cut -d\" -f2`
ssh_keys_bucket=`cat $md_file | grep '"SSHKeyBucket"' |awk '{print $2}' |cut -d\" -f2`
aws_access_key_id=`cat $md_file | grep '"Access-Key"' |awk '{print $2}' |cut -d\" -f2`
aws_secret_access_key=`cat $md_file | grep '"Secret-Key"' |awk '{print $2}' |cut -d\" -f2`
if [ ! -f /root/.s3cfg ]; then
# Configure s3cmd to access our acct
touch /root/.s3cfg
chmod 700 /root/.s3cfg
echo "[default]" >> /root/.s3cfg
echo "access_key = $aws_access_key_id" >> /root/.s3cfg
echo "secret_key = $aws_secret_access_key" >> /root/.s3cfg
fi
if [ ! -f /root/.ssh/id_rsa ]; then
# Downlaod the users ssh key
runuser -c "s3cmd get s3://$ssh_keys_bucket/$ssh_key /root/.ssh/id_rsa" root
chmod 600 /root/.ssh/id_rsa
fi
if [ ! -d $dir ]; then
env GIT_SSH='/etc/chef/git_ssh.sh' git clone $repo $dir
else
git checkout master
cd $dir && env GIT_SSH='/etc/chef/git_ssh.sh' git pull
fi
if [ $? -ne 0 ]; then
echo "Error retrieving chef repository"
exit 1
fi
git checkout $version
if [ $? -ne 0 ]; then
echo "Error checking out requested version"
exit 1
fi
git reset --hard $version
if [ $? -ne 0 ]; then
echo "Error reseting head"
exit 1
fi
# Execute chef solor for the nodes assigned runtime role
cd $dir && /usr/bin/chef-solo -c config/solo.rb -j nodes/runtime/$role.json
if [ $? -ne 0 ]; then
exit 1
fi
cd $OLDPWD
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment