Created
May 15, 2014 00:08
-
-
Save alfredgamulo/cc0bdb88b5c6c89f8481 to your computer and use it in GitHub Desktop.
Single line dynamic chef cookbook installer
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/bash | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
APPLICATION="${0##*/}" | |
USAGE="Cookbook installer | |
usage: $APPLICATION -c cookbooks | |
Ensures chef as a dependency and installs cookbooks specified with -c. | |
THIS MUST BE RUN AS ROOT. This script only works with Redhat Enterprise Linux 5 or Mac OSX. | |
Options: | |
-a Install all the cookbooks. | |
-c COOKBOOKS Comma-separated list of cookbook targets to install. | |
Specify 'help' or 'list' to print out potential targets. | |
-o OVERRIDE JSON object of overriding attributes. | |
" | |
error() { | |
local ecode="$1" | |
shift | |
echo "$*" 1>&2 | |
exit "$ecode" | |
} | |
# confirm sudo or root | |
if [[ $UID != 0 ]]; then | |
error 2 "$USAGE" | |
fi | |
while getopts 'ac:o:' f; do | |
case "$f" in | |
a) ALL='y';; | |
c) COOKBOOKS="$COOKBOOKS${COOKBOOKS:+","}$OPTARG";; | |
o) OVERRIDES="$OPTARG";; | |
\?) error 2 "$USAGE";; | |
esac | |
done | |
# throw error if no arguments are passed | |
if [ -z "$ALL" -a -z "$COOKBOOKS" ]; then | |
error 2 "$USAGE" | |
fi | |
# Confirm or list target cookbooks if requested | |
if [ -z "$ALL" -a -n "$COOKBOOKS" ]; then | |
c="${COOKBOOKS%,}," | |
while [ -n "$c" ]; do | |
COOKBOOK="${c%%,*}" | |
c="${c#*,}" | |
if [ -z "$COOKBOOK" ]; then | |
continue | |
elif [ "$COOKBOOK" = 'help' -o "$COOKBOOK" = 'list' ]; then | |
COOKBOOKS='help' | |
echo "Available cookbooks:" 1>&2 | |
for c in "$DIR/cookbooks/"*; do | |
COOKBOOK="${c##*/}" | |
echo $COOKBOOK | |
done | |
exit 2 | |
elif [ ! -r "$DIR/cookbooks/$COOKBOOK" ] || \ | |
! (COOKBOOKS='check'; echo "$DIR/cookbooks/$COOKBOOK"); then | |
error 2 "Invalid cookbook \"$COOKBOOK\"." | |
fi | |
done | |
# Create custom json | |
c="${COOKBOOKS%,}," | |
JSON="{\"run_list\": [" | |
while [ -n "$c" ]; do | |
COOKBOOK="${c%%,*}" | |
c="${c#*,}" | |
JSON="${JSON%}\"recipe[$COOKBOOK]\"," | |
done | |
JSON="${JSON%,}]}" | |
echo "$JSON" >> $DIR/temp.json | |
fi | |
if [ -z "$ALL" -a -n "$COOKBOOKS" -a -n "$OVERRIDES" ]; then | |
sed -i'' -e '$s/}/,'"$OVERRIDES"'}/' $DIR/temp.json | |
fi | |
chef_binary=/opt/chef/bin/chef-solo | |
if ! test -f "$chef_binary"; then | |
if [[ `uname` == 'Linux' ]]; then | |
rpm -ivh $DIR/payload/chef-11.4.4-2.el5.x86_64.rpm | |
elif [[ `uname` == 'Darwin' ]]; then | |
curl -L https://www.opscode.com/chef/install.sh | sudo bash | |
fi | |
fi && | |
if [ -n "$ALL" ]; then | |
"$chef_binary" -c "$DIR"/solo.rb -j "$DIR"/solo.json | |
elif [ -n "$COOKBOOKS" ]; then | |
"$chef_binary" -c "$DIR"/solo.rb -j "$DIR"/temp.json | |
rm -rf $DIR/temp.json | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment