#!/bin/bash # Parameters: # $1 is the vagrant install mode or user name to SSH as (see "Usage:" below) # $2 is the IP address of the bootstrap node # $3 is the optional knife recipe name, default "Test-Laptop" source ./virtualbox_env.sh source ./vmware/vmware_env.sh if [[ $OSTYPE == msys || $OSTYPE == cygwin ]] { # try to fix permission mismatch between windows and real unix setglobal RSYNCEXTRA = '"--perms --chmod=a=rwx,Da+x'" } if [[ -f ./proxy_setup.sh ]] { source ./proxy_setup.sh } set -e setglobal DIR = $[dirname $0] if [[ $# -gt 1 ]] { setglobal KEYFILE = '"bootstrap_chef.id_rsa'" setglobal IP = $2 setglobal BCPC_DIR = '"chef-bcpc'" setglobal VAGRANT = ''"" setglobal VMWARE = ''"" setglobal VBOX = ''"" # override the ssh-user and keyfile if using Vagrant if [[ $1 == "--vagrant-local" ]] { echo "Running on the local Vagrant VM" setglobal VAGRANT = '"true'" setglobal VBOX = '"true'" setglobal BCPC_DIR = '"~vagrant/chef-bcpc'" setglobal SSH_USER = $USER setglobal SSH_CMD = '"bash -c'" } elif [[ $1 == "--vagrant-remote" ]] { echo "SSHing to the Vagrant VM" setglobal VAGRANT = '"true'" setglobal VBOX = '"true'" setglobal BCPC_DIR = '"~vagrant/chef-bcpc'" setglobal SSH_USER = '"vagrant'" setglobal SSH_CMD = '"vagrant ssh -c'" } elif [[ $1 == "--vagrant-vmware" ]] { echo "SSHing to the Vagrant VM (on VMware)" setglobal VAGRANT = '"true'" setglobal VMWARE = '"true'" setglobal BCPC_DIR = '"~vagrant/chef-bcpc'" setglobal SSH_USER = '"vagrant'" setglobal SSH_CMD = '"vagrant ssh -c'" setglobal DIR = "$DIR/vmware" if [[ -z "$VMRUN" ]] { echo "vmrun not found!" > !2 echo " Please ensure VMWare is installed and vmrun is accessible." > !2 exit 1 } } else { setglobal SSH_USER = $1 setglobal SSH_CMD = ""ssh -t -i $KEYFILE $(SSH_USER)@$(IP)"" echo "SSHing to the non-Vagrant machine $(IP) as $(SSH_USER)" } if [[ $# -ge 3 ]] { setglobal CHEF_ENVIRONMENT = $3 } else { setglobal CHEF_ENVIRONMENT = '"Test-Laptop'" } echo "Chef environment: $(CHEF_ENVIRONMENT)" } else { echo "Usage: $[basename $0] --vagrant-local|--vagrant-remote|--vagrant-vmware| IP-Address [chef environment]" >> /dev/stderr exit } # protect against rsyncing to the wrong bootstrap node if [[ ! -f "environments/${CHEF_ENVIRONMENT}.json" ]] { echo "Error: environment file $(CHEF_ENVIRONMENT).json not found" exit } pushd $DIR if [[ -z $VAGRANT ]] { setglobal ISO = 'ubuntu-14.04-mini.iso' if [[ ! -f $KEYFILE ]] { ssh-keygen -N "" -f $KEYFILE } echo "Running rsync of non-Vagrant install" rsync $RSYNCEXTRA -avP -e "ssh -i $KEYFILE" --exclude vbox --exclude vmware --exclude $KEYFILE --exclude .chef . $(SSH_USER)@$IP:chef-bcpc rsync $RSYNCEXTRA -avP -e "ssh -i $KEYFILE" vbox/$ISO $(SSH_USER)@$IP:chef-bcpc/cookbooks/bcpc/files/default/bins $SSH_CMD "cd $BCPC_DIR && ./setup_ssh_keys.sh $(KEYFILE).pub" } else { echo "Running rsync of Vagrant install" $SSH_CMD "rsync $RSYNCEXTRA -avP --exclude vbox --exclude vmware --exclude .chef /chef-bcpc-host/ /home/vagrant/chef-bcpc/" echo "Rsync over the hypervisor mini ISO to avoid redownloading" $SSH_CMD "rsync $RSYNCEXTRA -avP /chef-bcpc-host/vbox/$ISO /home/vagrant/chef-bcpc/cookbooks/bcpc/files/default/bins" } echo "Updating server" $SSH_CMD "cd $BCPC_DIR && sudo apt-get -y update && sudo apt-get -y dist-upgrade" if [[ -n "$VBOX" && -z `$VBM snapshot bcpc-bootstrap list | grep dist-upgrade` ]] { echo "Taking snapshot (VirtualBox)" $VBM snapshot bcpc-bootstrap take dist-upgrade-complete } if test -n $VMWARE { setglobal VM_PATH = $[ls -d .vagrant/machines/bootstrap/vmware_fusion/*/] setglobal VMX_FILE = "$VM_PATH/precise64.vmx" if [[ -z `"$VMRUN" listSnapshots $VMX_FILE | grep dist-upgrade` ]] { echo "Taking snapshot (VMware)" $VMRUN snapshot $VMX_FILE dist-upgrade-complete } } echo "Building binaries" $SSH_CMD "cd $BCPC_DIR && sudo ./cookbooks/bcpc/files/default/build_bins.sh" echo "Setting up chef server" $SSH_CMD "cd $BCPC_DIR && sudo ./setup_chef_server.sh $(IP)" echo "Setting up chef cookbooks" $SSH_CMD "cd $BCPC_DIR && ./setup_chef_cookbooks.sh $(IP) $(SSH_USER)" echo "Setting up chef environment, roles, and uploading cookbooks" $SSH_CMD "cd $BCPC_DIR && knife environment from file environments/$(CHEF_ENVIRONMENT).json && knife role from file roles/*.json && knife cookbook upload -a -o cookbooks" echo "Enrolling local bootstrap node into chef" $SSH_CMD "cd $BCPC_DIR && ./setup_chef_bootstrap_node.sh $(IP) $(CHEF_ENVIRONMENT)" echo "Configuring SSH access keys for bootstrap procedure" $SSH_CMD "cd $BCPC_DIR && sudo ./install_bootstrap_ssh_key.sh" popd echo "Finished bootstrap of Chef server!"