Skip to content

Instantly share code, notes, and snippets.

@kbni
Created September 21, 2016 07:53
Show Gist options
  • Save kbni/44f8d02c024a8ac3f6295ef16e686d34 to your computer and use it in GitHub Desktop.
Save kbni/44f8d02c024a8ac3f6295ef16e686d34 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Author: Alex Wilson <admin@kbni.net>
#
# Basic script to assist with managing zato hosts
function script_help() {
echo "$(basename "$0") - Zato control script"
echo "author: Alex Wilson <admin@kbni.net>"
echo ""
echo "arguments:"
echo " echo echo each hosts name, role and path"
echo " sleep sleep (might want to use between calls)"
echo " start start servers that are not load_balancer or web_admin)"
echo " startall start ALL servers including load_balancer and web_admin"
echo " stop stop servers that are not load_balancer or web_admin)"
echo " stopall stop ALL servers including load_balancer and web_admin"
echo " buildzep creates zato_extra_paths from pyenv + pysrc directories"
echo " pushzep pushes zato_extra_paths to remote hosts"
echo " zep buildzep+pushzep"
echo ""
echo "hosts file is ${hosts_file}"
echo " should contain list of hosts, their path and role:"
echo " zato@server:/opt/zato/path-to-env/server_dir:role"
echo " where role should be server, web_admin or load_balancer"
echo ""
exit 0
}
this_file=$(readlink -nf "$0")
this_dir=$(dirname "$this_file")
pyenv_dir="${this_dir}/pyenv"
pysrc_dir="${this_dir}/pysrc"
zep_dir="${this_dir}/zato_extra_paths"
hosts_file="${this_dir}/hosts"
zato_bin="/opt/zato/current/bin/zato"
function zato_echo() {
zato_host=$1
zato_path=$2
echo "$zato_host"
}
function zato_sleep() {
zato_host=$1
zato_path=$2
sleep 2
}
function zato_stop() {
zato_host=$1
zato_path=$2
echo -n "stopping ${zato_host}:"
ssh "$zato_host" "$zato_bin" stop "$zato_path" < /dev/null # don't gobble stdin
}
function zato_start() {
zato_host=$1
zato_path=$2
echo -n "starting ${zato_host}: "
ssh "$zato_host" "$zato_bin" start "$zato_path" < /dev/null # don't gobble stdin
}
function zato_clearpid() {
zato_host=$1
zato_path=$2
ssh "$zato_host" rm "${zato_path}/pidfile" < /dev/null # don't gobble stdin
}
function zato_sync() {
zato_host=$1
zep_dir=$2
rsync -rz "${zep_dir}/" "${zato_host}:current/zato_extra_paths/" --exclude numpy --exclude scipy --delete-after
}
function generate_zep() {
pyenv_dir=$1
pysrc_dir=$2
zep_dir=$3
if [ -d "$zep_dir" ]; then
echo -n "Cleaning up ${zep_dir}.."
rm -rf "$zep_dir" | echo "."
fi
mkdir "$zep_dir" && echo "Recreating ${zep_dir}"
# Copy items from pyenv_dir into zep_dir
for ifile in "${pyenv_dir}/lib/python2.7/site-packages"/*/__init__.py; do
if [ ! -e "$ifile" ]; then
echo "Warning: Nothing found in pyenv: ${pyenv_dir}" > /dev/stderr
break
else
dir=$(dirname "$ifile")
echo -n " copying from pyenv: ${dir} .."
rsync -rz --copy-links --exclude .git --exclude .pyc "${dir}" "${zep_dir}/" && echo ". OK"
fi
done
# Copy items from pysrc_dir into zep_dir
for dir in "${pysrc_dir}/"*; do
if [ ! -e "$dir" ]; then
echo "Warning: Nothing found in pysrc: ${pysrc_dir}" > /dev/stderr
break
else
[[ -d "$dir" || "$dir" == *".py" ]] || continue
[[ "$dir" == "setuptools" ]] && continue
echo -n " copying from pysrc: ${dir} .."
rsync -rz --copy-links --exclude .git --exclude .pyc "${dir}" "${zep_dir}/" && echo ". OK"
fi
done
}
[[ "$1" == "" ]] && script_help
for arg in "$@"; do
# Global options are parsed here
case "$arg" in
zep|buildzep)
generate_zep "$pyenv_dir" "$pysrc_dir" "$zep_dir" ;;
-h|--help|help)
script_help ;;
esac
# Per host options are parsed here
while IFS=: read zato_host zato_path zato_role; do
pidfile="${zato_path}/pidfile"
case "$arg" in
multitail|mt)
ssh $zato_host tail -f "${zato_path}/logs/server.log" < /dev/null & ;;
echo)
zato_echo "$zato_host" "$zato_path" ;;
start)
[[ $zato_role == "server" ]] && zato_start "$zato_host" "$zato_path" ;;
stop)
[[ $zato_role == "server" ]] && zato_stop "$zato_host" "$zato_path" ;;
startall)
zato_start "$zato_host" "$zato_path" ;;
stopall)
zato_stop "$zato_host" "$zato_path" ;;
sleep)
zato_sleep "$zato_host" "$zato_path" ;;
zep|pushzep)
[[ $zato_role == "server" ]] && zato_sync "$zato_host" "$zep_dir" ;;
clearpid|cleanpid)
zato_clearpid "$zato_host" "$zato_path" ;;
esac
done < $hosts_file
# Global options are parsed here
case "$arg" in
multitail|mt)
trap "kill $(pgrep -P $$ -d' ')" EXIT
wait
;;
esac
done
@naffan2014
Copy link

good very good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment