#!/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" PLAYBOOK="$SCRIPT_PATH/playbook.yml" help() { less -R << EOF This script manages the deployment using ansible. Usage: ./run.sh [COMMAND] Commands: sync [TAGS] - Specify comma-seperated TAGS to restrict execution to particular stages/stacks. 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 [ ! -d "$SCRIPT_PATH/.venv" ]; then python3 -m venv .venv fi . .venv/bin/activate if [[ $(cmd_exists ansible) != true ]]; then pip install -r "$SCRIPT_PATH/requirements.txt" ansible-galaxy collection install community.docker ansible-galaxy collection install community.digitalocean fi #################### # - Actions #################### action_sync() { ansible-playbook \ --inventory "$INVENTORY" \ "$PLAYBOOK" } action_sync_tags() { ansible-playbook \ --inventory "$INVENTORY" \ "$PLAYBOOK" \ --tags "$1" } #################### # - Check Dependencies #################### case $1 in sync) if [ -z "${2-}" ]; then action_sync else action_sync_tags "$2" fi ;; esac