Last active
December 21, 2015 17:28
-
-
Save tonistiigi/6340194 to your computer and use it in GitHub Desktop.
Simple chef-solo prepare+cook script for SmartOS NGZ(SmartMachine)
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
#!/bin/bash | |
# Simple cook script for Chef Solo on SmartOS NGZ | |
# Will prepare Chef-11.6.0 fat client and push cookbooks+attributes to node | |
# Based on fat client by @benr http://wiki.smartos.org/display/DOC/Using+Chef | |
function usage { | |
cat <<EOF | |
USAGE: $0 [opt] user@host [ssh-opt] | |
Options: | |
-r Path to recipes [./cookbooks] | |
-j Path to JSON attributes [./chef.json] | |
-p SSH port [22] | |
EOF | |
if [ ! $1 ]; then | |
exit 1 | |
fi | |
exit 0 | |
} | |
function ssh_exec { | |
ssh -T -p $SSH_PORT $SSH_OPT $1 | |
} | |
function rsync_exec { | |
rsync -chazp -e ssh --port $SSH_PORT $1 $SSH_HOST:$2 | |
} | |
SSH_PORT=22 | |
RECIPES=./cookbooks | |
JSON=./chef.json | |
FAT_CLIENT_URL=https://us-east.manta.joyent.com/tonistiigi/public/smartos-chef/chef-fatclient-smartos-11.6.0.tar.bz2 | |
while getopts ":r:j:p:h" opt; do | |
case $opt in | |
r) | |
RECIPES=$OPTARG | |
;; | |
p) | |
SSH_PORT=$OPTARG | |
;; | |
j) | |
JSON=$OPTARG | |
;; | |
h) | |
usage true | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
usage | |
;; | |
esac | |
done | |
shift $(($OPTIND - 1)) | |
if [ $# -eq 0 ]; then | |
usage | |
fi | |
if [ ! -d "$RECIPES" ]; then | |
echo "Error: no such directory $RECIPES" | |
usage | |
fi | |
if [ ! -f "$JSON" ]; then | |
echo "Error: no such file $JSON" | |
usage | |
fi | |
SSH_OPT=$@ | |
SSH_HOST=$1 | |
ssh_exec <<EOF | |
if [[ -f /opt/custom/smf/chef-solo.xml && -f /var/chef/solo.rb ]]; then | |
echo "INFO: Chef Solo already installed and configured." | |
exit | |
fi | |
cd /tmp | |
echo "-> Downloading Chef-11.6.0 Fat Client" | |
curl -O $FAT_CLIENT_URL | |
echo "-> Unpacking" | |
cd / && gtar xfj /tmp/chef-fatclient-smartos-11.6.0.tar.bz2 | |
cp /usr/bin/gtar /opt/chef/bin/tar | |
mkdir -p /opt/custom/smf /var/chef/cookbooks/__empty | |
# Empty JSON file | |
cat <<END >/var/chef/chef.json | |
{ | |
"run_list": [] | |
} | |
END | |
# Create Chef Solo Configuration | |
cat <<END >/var/chef/solo.rb | |
## | |
## Joyent Chef Solo Configuration | |
## | |
file_cache_path "/var/chef" | |
cookbook_path "/var/chef/cookbooks" | |
json_attribs "/var/chef/chef.json" | |
END | |
# Startup script | |
cat <<END >/opt/custom/smf/chef-solo.xml | |
<?xml version="1.0"?> | |
<!DOCTYPE service_bundle SYSTEM '/usr/share/lib/xml/dtd/service_bundle.dtd.1'> | |
<service_bundle type="manifest" name="export"> | |
<service name="application/management/chef-solo" type="service" version="0"> | |
<create_default_instance enabled="true"/> | |
<single_instance/> | |
<dependency name="milestone" grouping="require_all" restart_on="none" type="service"> | |
<service_fmri value="svc:/milestone/sysconfig"/> | |
</dependency> | |
<dependency name="fs-local" grouping="require_all" restart_on="none" type="service"> | |
<service_fmri value="svc:/system/filesystem/local"/> | |
</dependency> | |
<dependency name="name-services" grouping="optional_all" restart_on="none" type="service"> | |
<service_fmri value="svc:/milestone/name-services"/> | |
</dependency> | |
<dependency name="network" grouping="require_all" restart_on="restart" type="service"> | |
<service_fmri value="svc:/milestone/network"/> | |
</dependency> | |
<dependency name="config-file" grouping="require_all" restart_on="refresh" type="path"> | |
<service_fmri value="file://localhost/var/chef/solo.rb"/> | |
</dependency> | |
<exec_method name="start" type="method" exec="/opt/chef/bin/chef-solo --config /var/chef/solo.rb" timeout_seconds="60"> | |
<method_context working_directory="/var/chef"> | |
<method_credential user="root" group="root"/> | |
<method_environment> | |
<envvar name="PATH" value="/opt/chef/bin:/usr/bin:/usr/sbin:/smartdc/bin:/opt/local/bin:/opt/local/sbin"/> | |
</method_environment> | |
</method_context> | |
</exec_method> | |
<exec_method name="stop" type="method" exec=":true" timeout_seconds="60"/> | |
<property_group name="startd" type="framework"> | |
<propval name="duration" type="astring" value="transient"/> | |
</property_group> | |
<template> | |
<common_name> | |
<loctext xml:lang="C">Chef Solo Service</loctext> | |
</common_name> | |
</template> | |
</service> | |
</service_bundle> | |
END | |
svccfg import /opt/custom/smf/chef-solo.xml | |
svcs -Ho state chef-solo >/dev/null | |
rm -f /tmp/chef-fatclient-smartos-11.6.0.tar.bz2 | |
EOF | |
rsync_exec "$RECIPES/*" /var/chef/cookbooks | |
rsync_exec "$JSON" /var/chef/chef.json | |
ssh_exec "/opt/chef/bin/chef-solo -c /var/chef/solo.rb --log_level debug" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment