Skip to content

Instantly share code, notes, and snippets.

@EntropyWorks
Last active August 29, 2015 14:20
Show Gist options
  • Save EntropyWorks/490f37eca44e0f5acc06 to your computer and use it in GitHub Desktop.
Save EntropyWorks/490f37eca44e0f5acc06 to your computer and use it in GitHub Desktop.
Starts up coreos galera cluster via docker.
#!/bin/bash
#asciinema rec -w 1 -t do-it.sh -c ./do-it.sh -y /tmp/do-it.record
#
# You can change these to whatever you need to prevent confliting
# ip ranges.
#
IP_VIRTUALBOX="192.168.240"
IP_VMWARE="172.17.8"
# Where the https://github.com/EntropyWorks/fleet-units-galera-cluster repo is placed
FLEET_UNIT_DIR="/tmp/fleet-units-galera-cluster"
# Sets default provider so you don't need to use -p on the commandline
PROVIDER=${VAGRANT_DEFAULT_PROVIDER}
# NOTICE: Uncomment if your script depends on bashisms.
if [ -z "$BASH_VERSION" ]; then bash $0 $@ ; exit $? ; fi
tempfile=$(mktemp /tmp/do-it.XXXXXX) || { echo "Failed to create temp file"; exit 1; }
function cleanup {
rm -rf ${tempfile}
exit
}
function fail {
errcode=$? # save the exit code as the first thing done in the trap function
echo "error $errorcode"
echo "the command executing at the time of the error was"
echo "$BASH_COMMAND"
echo "on line ${BASH_LINENO[0]}"
# do some error handling, cleanup, logging, notification
# $BASH_COMMAND contains the command that was being executed at the time of the trap
# ${BASH_LINENO[0]} contains the line number in the script of that command
# exit the script or return to try again, etc.
cleanup
exit $errcode # or use some other value or do return instead
}
# Catch the crtl-c and others nicely
trap cleanup EXIT SIGHUP SIGINT SIGTERM
trap fail ERR
#echo "Before"
#for i ; do echo - $i ; done
# Code template for parsing command line parameters using only portable shell
# code, while handling both long and short params, handling '-f file' and
# '-f=file' style param data and also capturing non-parameters to be inserted
# back into the shell positional parameters.
function command_readme(){
cat << EOF
Very basic commands so far...
-p <provider> Sets your provider (supports vmware and virtualbox)
-D Destroys everything when re-run. Without only core-# are removed
EOF
cleanup
exit
}
while [ -n "$1" ]; do
# Copy so we can modify it (can't modify $1)
OPT="$1"
# Detect argument termination
if [ x"$OPT" = x"--" ]; then
shift
for OPT ; do
REMAINS="$REMAINS \"$OPT\""
done
break
fi
# Parse current opt
while [ x"$OPT" != x"-" ] ; do
case "$OPT" in
-p* | --provider )
PROVIDER="$2"
shift
;;
-h* | --help )
command_readme
shift
;;
# Anything unknown is recorded for later
* )
REMAINS="$REMAINS \"$OPT\""
break
;;
esac
# Check for multiple short options
# NOTICE: be sure to update this pattern to match valid options
NEXTOPT="${OPT#-[p]}" # try removing single short opt
if [ x"$OPT" != x"$NEXTOPT" ] ; then
OPT="-$NEXTOPT" # multiple short opts, keep going
else
break # long form, exit inner loop
fi
done
# Done with that param. move to next
shift
done
# Set the non-parameters back into the positional parameters ($1 $2 ..)
eval set -- $REMAINS
#echo -e "After: \n configfile='$CONFIGFILE' \n provider='$PROVIDER' \n force='$FORCE' \n retry='$RETRY' \n remains='$REMAINS'"
#for i ; do echo - $i ; done
function msg(){
if [ -f `which figlet` ] ; then
#figlet -w 150 -c -f stampatello "\-\-\-\-\-\-\-\-\-\-\-\-\-\-"
echo
figlet -w 150 -f stampatello $@
echo
#figlet -w 150 -c -f stampatello "\-\-\-\-\-\-\-\-\-\-\-\-\-\-"
else
line="------------------------------------------------"
echo ${line}
echo $@
echo ${line}
fi
}
# Doing this make it easy to run virtualbox and then switch to
# using vmware_fusion. Other wise the there are network collisions
CHOICE=${PROVIDER:-virtualbox}
if [ ${CHOICE} == virtualbox ] ; then
IP=${IP_VIRTUALBOX}
cat config.rb.sample | sed -e s/__CHANGE_IP__/${IP}/g > config.rb
else
IP=${IP_VMWARE}
cat config.rb.sample | sed -e s/__CHANGE_IP__/${IP}/g > config.rb
fi
function destroy_vagrant_boxes(){
# Start from statch again. Is you want to just destroy
# the minions core-XX just run the following
# vagrant destroy -f core-0{1..3}
msg Destroy old vagrant boxes
#if [ -n ${DESTROY} ] ; then
# for vm in $(vagrant status | grep core | awk '{ print $1 }') ; do
# vagrant destroy -f ${vm}
# done
# else
vagrant destroy -f
# Removing old user-data.*.yaml files that contain etcd
# discovery url that shouldn't be reused
if [ -f user-data.master.yaml ] ; then
rm user-data.master.yaml
fi
if [ -f user-data.node.yaml ] ; then
rm user-data.node.yaml
fi
#fi
}
function build_vagrant_boxes(){
# Building the master-01 first so I can capture the SSH port forwarding
# to use later.
msg "Build new vagrant boxes using ${PROVIDER:-virtualbox}"
vagrant up --provider ${PROVIDER:-virtualbox} master-01| tee $tempfile
vagrant up --provider ${PROVIDER:-virtualbox}
# I have a way to manage my ssh config. Create starting with numbers to
# fix the order the ~/.ssh/config is created
# alias ssh='[[ -d ~/.ssh/config.d ]] && cat ~/.ssh/config.d/*.cfg > ~/.ssh/config ; /usr/bin/ssh'
if [ -f ~/.ssh/config.d/01-vagrant.cfg ] ; then
vagrant ssh-config > ~/.ssh/config.d/01-vagrant.cfg
fi
}
function set_fleetctl_env(){
# Since there may be other VM's running already I check to see
# what the port forwarding for SSH has been set to.
export TUNNEL_PORT=$(vagrant ssh-config master-01 | grep Port | awk '{print $2}')
msg "Setting tunnel port to ${TUNNEL_PORT}"
export FLEETCTL_TUNNEL=127.0.0.1:${TUNNEL_PORT}
export FLEETCTL_SSH_USERNAME=core
export FLEETCTL_STRICT_HOST_KEY_CHECKING=false
export FLEETCTL_KNOWN_HOSTS_FILE=/dev/null
}
function check_fleet_machines(){
# Verify that machines are seen
msg "Checking fleet machines"
fleetctl list-machines
}
function checkout_fleet_units(){
if [ ! -d /tmp/fleet-units-galera-cluster ] ; then
msg "Checkout fleet units galera cluster"
git clone https://github.com/EntropyWorks/fleet-units-galera-cluster.git ${FLEET_UNIT_DIR}
fi
msg "Create [email protected] fleet-unit"
cat ${FLEET_UNIT_DIR}/[email protected] | sed -e s/__FLEETCTL_ETC_ENDPOINT__/${IP}:4001/g > ${FLEET_UNIT_DIR}/[email protected]
}
function check_submit_fleet_units(){
# Submitting the fleet units to be started later
msg "Submit fleet units"
fleetctl submit ${FLEET_UNIT_DIR}/*.service
fleetctl list-unit-files
}
function start_mysql(){
# Start the mysql first
msg Start the fleet-units
for i in 1 2 3 ; do
fleetctl start galera@${i}.service
sleep 1
while true ; do
echo -n "."
fleetctl journal galera@${i}.service 2>/dev/null | grep "mysqld: ready for connections" && break
sleep 1
done
# Start the sidekick now that mysql is running.`
fleetctl start galera-sidekick@${i}.service
sleep 1
# Don't start the next mysql process until the sidekick reports in.
msg "Wait for sidekick"
while true ; do
echo -n "."
etcdctl ls /galera/galera-${i} 2>/dev/null && break
sleep 1
done
echo ""
# Report what has been stored in etcd
echo "galera-sidekick@${i}.service has set /galera/galera-${i} to $(etcdctl get /galera/galera-${i})"
done
}
function check_mysql(){
# Check if mysql it ready for connections
for i in 1 2 3 ; do
while true ; do
echo -n "."
fleetctl journal galera@${i}.service 2>/dev/null | grep "mysqld: ready for connections" && break
sleep 1
done
done
echo " Done!"
}
function check_sidekick(){
# Check that the sidekick has recorded the flannel IP to etcd
msg "Sidekick reported"
for i in 1 2 3 ; do
#echo "/galera/galera-${i} = $(etcdctl get /galera/galera-${i})"
echo "galera-sidekick@${i}.service has set /galera/galera-${i} to $(etcdctl get /galera/galera-${i})"
done
}
# The order to run the above functions.
destroy_vagrant_boxes
build_vagrant_boxes
set_fleetctl_env
check_fleet_machines
checkout_fleet_units
check_submit_fleet_units
start_mysql
#check_mysql
check_sidekick
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment