#!/usr/bin/env bash if [ -r /etc/ansible-pull.sh.conf ] ; then . /etc/ansible-pull.sh.conf fi SCRIPT_NAME="$(basename "${0}")" GIT_REPO_BRANCH="${GIT_REPO_BRANCH:-production}" OIC_FLAG="--only-if-changed" VERBOSE_FLAG="--verbose" DEBUG_FLAG="false" # - Process command line while [ $# -gt 0 ]; do case "$1" in -h|--help) echo "Usage: $0 [--branch ] [--force] [--quiet] [-- ]" exit 0 ;; -d|--debug) VERBOSE_FLAG="-vvv" DEBUG_FLAG="true" shift 1 ;; -b|--branch) shift 1 if [ $# -eq 0 ] ; then echo "${SCRIPT_NAME}: ERROR - Branch not specified." ; exit 1 ; fi GIT_REPO_BRANCH="$1" shift 1 ;; -f|--force) OIC_FLAG="" shift 1 ;; -q|--quiet) VERBOSE_FLAG="" shift 1 ;; --) shift 1 break ;; *) echo "${SCRIPT_NAME}: ERROR - Invalid argument '${1}'." ; exit 2 ;; esac done VENV_ACTIVATE_SCRIPT="${HOME}/.ansible-venv/bin/activate" LOCK_FILE="/var/run/lock/ansible-pull.sh.lock" LOG_DIR="/var/log/ansible-pull.sh" LOG_FILE="${LOG_DIR}/ansible-pull.sh.log" GIT_REPO_BASE="https://gitea.admin-a.hpetersenfamily.com/heath/ansible" GIT_REPO="${GIT_REPO_BASE}.git" # - Include ansible virtual environment (in case not already done - we don't know how we're being run) . "${VENV_ACTIVATE_SCRIPT}" # - If we can't get a lock, don't proceed if ! exec 9>"${LOCK_FILE}" ; then echo "${SCRIPT_NAME}: ERROR - Unable to open the lock file (${LOCK_FILE})! Exiting..." >&2 exit 10 fi if ! flock -n 9 ; then echo "${SCRIPT_NAME}: ERROR - Another copy is already running! Exiting..." >&2 exit 11 fi # - If debugging requested or running from a terminal . . . if [ "${DEBUG_FLAG}" = "true" ] || test -t 0 ; then exec > >(tee -a "${LOG_FILE}") 2>&1 # - send STDOUT and STDERR to both STDOUT and the log file else exec >>"${LOG_FILE}" 2>&1 # - send STDOUT and STDERR to the log file only fi # - Log that we've gotten this far echo echo "${SCRIPT_NAME}: ------------------------------- $(date "+%Y-%m-%d %H:%M:%S") -------------------------------" # - Do our work ansible-pull ${OIC_FLAG} ${VERBOSE_FLAG} --url "${GIT_REPO}" --checkout "${GIT_REPO_BRANCH}" "${@}"