python-support-infra/run.sh

161 lines
3.0 KiB
Bash
Raw Normal View History

#!/bin/bash
set -e ## Exit if Problems
set -u ## Fail on Undefined Variable
SCRIPT_PATH="$(dirname "$(readlink -f "$0")")"
####################
# - Constants
####################
PLAYBOOKS_PATH="$SCRIPT_PATH/playbooks"
INVENTORY="$SCRIPT_PATH/inventory.yml"
PLAYBOOK_HOSTS="$PLAYBOOKS_PATH/playbook.hosts.yml"
PLAYBOOK_WG0="$PLAYBOOKS_PATH/playbook.wg0.yml"
PLAYBOOK_SWARM="$PLAYBOOKS_PATH/playbook.swarm.yml"
PLAYBOOK_STACK_CLEANUP="$SCRIPT_PATH/stacks/cleanup/playbook.yml"
PLAYBOOK_STACK_MESH="$SCRIPT_PATH/stacks/mesh/playbook.yml"
PLAYBOOK_STACK_SITE_SUPPORT="$SCRIPT_PATH/stacks/site-support/playbook.yml"
help() {
less -R << EOF
This script manages the deployment using ansible.
Usage:
./run.sh [COMMAND]
EOF
}
####################
# - Utilities
####################
cmd_exists() {
if type -P "$1" &> /dev/null || [ -x "$1" ]; then
echo true
else
echo false
fi
}
pkg_installed() {
if [ $(dpkg-query -W -f='${Status}' "$1" 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
echo false
else
echo true
fi
}
####################
# - Check Preconditions
####################
if [[ $(whoami) == root ]]; then
echo "Please don't run as root."
exit 1
fi
case $(cat /etc/debian_version | cut -d . -f 1) in
"11")
echo "Detected Debian 11 (Supported)..."
;;
"12")
echo "Detected Debian 12 (Supported)..."
;;
*)
echo "Could not detect a supported OS. Refer to manual for more."
exit 1
;;
esac
if [[ $(cmd_exists ansible) != true ]]; then
echo "This script requires ansible. Press ENTER to install and continue..."
sudo apt install ansible
echo "This script requires latest community.docker module. Press ENTER to install and continue..."
ansible-galaxy collection install community.docker
fi
####################
# - Actions
####################
action_hosts() {
ansible-playbook \
--inventory "$INVENTORY" \
"$PLAYBOOK_HOSTS"
}
action_wg0() {
ansible-playbook \
--inventory "$INVENTORY" \
"$PLAYBOOK_WG0"
}
action_swarm() {
ansible-playbook \
--inventory "$INVENTORY" \
"$PLAYBOOK_SWARM"
}
action_stack_cleanup() {
ansible-playbook \
--inventory "$INVENTORY" \
"$PLAYBOOK_STACK_CLEANUP"
}
action_stack_mesh() {
ansible-playbook \
--inventory "$INVENTORY" \
"$PLAYBOOK_STACK_MESH"
}
action_stack_site_support() {
ansible-playbook \
--inventory "$INVENTORY" \
"$PLAYBOOK_STACK_SITE_SUPPORT"
}
####################
# - Check Dependencies
####################
case $1 in
sync)
action_hosts
action_wg0
action_swarm
action_stack_cleanup
action_stack_mesh
action_stack_site_support
;;
sync-hosts)
action_hosts
;;
sync-wg0)
action_wg0
;;
sync-swarm)
action_swarm
;;
sync-stacks)
action_stack_cleanup
action_stack_mesh
action_stack_site_support
;;
sync-stack-cleanup)
action_stack_cleanup
;;
sync-stack-mesh)
action_stack_mesh
;;
sync-stack-site-support)
action_stack_site_support
;;
# sync-role)
# ansible-playbook \
# --inventory "$INVENTORY" \
# --tags "$2" \
# "$PLAYBOOK"
# ;;
esac