2023-08-13 04:49:19 +02:00
|
|
|
#!/bin/bash
|
|
|
|
set -e ## Exit if Problems
|
|
|
|
set -u ## Fail on Undefined Variable
|
|
|
|
|
|
|
|
SCRIPT_PATH="$(dirname "$(readlink -f "$0")")"
|
|
|
|
|
|
|
|
####################
|
|
|
|
# - Constants
|
|
|
|
####################
|
|
|
|
INVENTORY="$SCRIPT_PATH/inventory.yml"
|
2023-08-21 09:03:34 +02:00
|
|
|
PLAYBOOK="$SCRIPT_PATH/playbook.yml"
|
2023-08-13 04:49:19 +02:00
|
|
|
|
|
|
|
help() {
|
|
|
|
less -R << EOF
|
|
|
|
This script manages the deployment using ansible.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
./run.sh [COMMAND]
|
2023-08-21 09:03:34 +02:00
|
|
|
|
|
|
|
Commands:
|
|
|
|
sync [TAGS]
|
|
|
|
- Specify comma-seperated TAGS to restrict execution to particular stages/stacks.
|
2023-08-13 04:49:19 +02:00
|
|
|
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
|
|
|
|
|
2023-08-21 09:03:34 +02:00
|
|
|
if [ ! -d "$SCRIPT_PATH/.venv" ]; then
|
|
|
|
python3 -m venv .venv
|
|
|
|
fi
|
|
|
|
|
|
|
|
. .venv/bin/activate
|
|
|
|
|
2023-08-13 04:49:19 +02:00
|
|
|
if [[ $(cmd_exists ansible) != true ]]; then
|
2023-08-21 09:03:34 +02:00
|
|
|
pip install -r "$SCRIPT_PATH/requirements.txt"
|
2023-08-13 04:49:19 +02:00
|
|
|
|
|
|
|
ansible-galaxy collection install community.docker
|
2023-08-21 09:03:34 +02:00
|
|
|
ansible-galaxy collection install community.digitalocean
|
2023-08-13 04:49:19 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
####################
|
|
|
|
# - Actions
|
|
|
|
####################
|
2023-08-21 09:03:34 +02:00
|
|
|
action_sync() {
|
2023-08-13 04:49:19 +02:00
|
|
|
ansible-playbook \
|
|
|
|
--inventory "$INVENTORY" \
|
2023-08-21 09:03:34 +02:00
|
|
|
"$PLAYBOOK"
|
2023-08-13 04:49:19 +02:00
|
|
|
}
|
2023-08-21 09:03:34 +02:00
|
|
|
action_sync_tags() {
|
2023-08-13 04:49:19 +02:00
|
|
|
ansible-playbook \
|
|
|
|
--inventory "$INVENTORY" \
|
2023-08-21 09:03:34 +02:00
|
|
|
"$PLAYBOOK" \
|
|
|
|
--tags "$1"
|
2023-08-13 04:49:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################
|
|
|
|
# - Check Dependencies
|
|
|
|
####################
|
|
|
|
case $1 in
|
|
|
|
sync)
|
2023-08-21 09:03:34 +02:00
|
|
|
if [ -z "${2-}" ]; then
|
|
|
|
action_sync
|
|
|
|
else
|
|
|
|
action_sync_tags "$2"
|
|
|
|
fi
|
2023-08-13 04:49:19 +02:00
|
|
|
;;
|
|
|
|
esac
|