Skip to content

Instantly share code, notes, and snippets.

@kshcherban
Created August 25, 2016 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kshcherban/029b090a6a35d2dee7334e5031866ce8 to your computer and use it in GitHub Desktop.
Save kshcherban/029b090a6a35d2dee7334e5031866ce8 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# solo-cli - CLI for Solo API.
#
# It is recommended to link it to your PATH before using.
#
set -e
action=${1:-list}
solo_name=${2:-solo1}
role=${ROLE:-base}
run_list=""
base_image="/opt/kvm-images/ubuntu-14.04-chef.raw"
if [ ! -f $base_image ]; then
echo "ERROR: specify base kvm image in $0"
exit 1
elif ! ( virtup ls &>/dev/null); then
echo "ERROR: install https://github.com/kshcherban/virtup in your PATH"
exit 1
fi
if [ -n "$RUN_LIST" ]
then
run_list="$RUN_LIST"
echo $run_list
fi
if [ ! -n "$VERBOSE" ]; then
VERBOSE=off
fi
chef_dir=${CHEF_DIR:-$(pwd)}
if [ ! -f "$chef_dir/solo.rb" ]; then
cat >&2 <<EOF
solo.rb not found. You need to export CHEF_DIR to your local chef repo
or change your working directory to it.
EOF
exit 2
fi
ssh_user="root"
ssh_port="22"
get_hostname () {
hostname=$(virtup ls -ip | grep $solo_name | awk '{print $NF}')
if [ -z $hostname ]; then
echo "Solo is not running, start it first" 1>&2
exit 1
fi
echo $hostname
}
case $action in
list|ls)
virtup ls
;;
create|open|up)
virtup import -m 1G -i $base_image -c 2 $solo_name
virtup up $solo_name
;;
upload|sync)
hostname=$(get_hostname)
echo "Updating cookbooks"
cd $chef_dir
bundle exec berks vendor "${chef_dir}/cookbooks" -q
rsync -ahczP \
-e "ssh -p${ssh_port}" \
--delete \
--exclude vendor/bundle/ --exclude .bundle/ \
--exclude .git/ --exclude .kitchen/ \
--exclude tmp/ \
"$chef_dir/" "${ssh_user}@${hostname}:chef/"
;;
ssh)
hostname=$(get_hostname)
ssh -p"${ssh_port}" "${ssh_user}@${hostname}"
;;
run|provision)
hostname=$(get_hostname)
# Update the remote chef directory forcefully before provision to keep it sync'd
$0 sync
# recipe[chef-solo-search] is used by some cookbooks like Icinga, and is already
# included in the former Vagrantfile.
ssh -p"${ssh_port}" "${ssh_user}@${hostname}" "VERBOSE=$VERBOSE chef-solo -c chef/solo.rb -o 'recipe[chef-solo-search],$run_list'"
;;
destroy|close)
hostname=$(get_hostname)
virtup down $solo_name
virtup rm --full $solo_name
# Remove the current SSH host key because it will be re-generated every rebuild
ssh-keygen -R "${hostname}" >/dev/null 2>&1
;;
*)
echo "Usage: $(basename $0) {up|ls|destroy|sync|ssh|provision}"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment