clean up locking
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
SCRIPT_NAME="$(basename "${0}")"
|
||||||
|
|
||||||
# - Process command line
|
# - Process command line
|
||||||
GIT_REPO_BRANCH="production"
|
GIT_REPO_BRANCH="production"
|
||||||
while [ $# -gt 0 ]; do
|
while [ $# -gt 0 ]; do
|
||||||
@@ -10,7 +12,7 @@ while [ $# -gt 0 ]; do
|
|||||||
;;
|
;;
|
||||||
--branch)
|
--branch)
|
||||||
shift 1
|
shift 1
|
||||||
if [ $# -eq 0 ] ; then echo "$0: ERROR - Branch not specified." ; exit 1 ; fi
|
if [ $# -eq 0 ] ; then echo "${SCRIPT_NAME}: ERROR - Branch not specified." ; exit 1 ; fi
|
||||||
GIT_REPO_BRANCH="$1"
|
GIT_REPO_BRANCH="$1"
|
||||||
shift 1
|
shift 1
|
||||||
;;
|
;;
|
||||||
@@ -19,33 +21,38 @@ while [ $# -gt 0 ]; do
|
|||||||
break
|
break
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
echo "$0: ERROR - Invalid argument." ; exit 2
|
echo "${SCRIPT_NAME}: ERROR - Invalid argument." ; exit 2
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
# - Include ansible virtual environment (in case not already done - we don't know how we're being run)
|
VENV_ACTIVATE_SCRIPT="${HOME}/.ansible-venv/bin/activate"
|
||||||
VIRTUAL_ENV_DISABLE_PROMPT=true
|
LOCK_FILE="/var/run/lock/ansible-pull.sh.lock"
|
||||||
. "${HOME}/.ansible-venv/bin/activate"
|
|
||||||
|
|
||||||
SCRIPT_NAME="$(basename "${0}")"
|
|
||||||
GIT_REPO_BASE="https://gitea.admin-a.hpetersenfamily.com/heath/ansible"
|
|
||||||
GIT_REPO="${GIT_REPO_BASE}.git"
|
|
||||||
LOG_DIR="/var/log/ansible-pull.sh"
|
LOG_DIR="/var/log/ansible-pull.sh"
|
||||||
LOG_FILE="${LOG_DIR}/ansible-pull.sh.log"
|
LOG_FILE="${LOG_DIR}/ansible-pull.sh.log"
|
||||||
LOCK_FILE="/tmp/ansible-pull.sh.lock"
|
GIT_REPO_BASE="https://gitea.admin-a.hpetersenfamily.com/heath/ansible"
|
||||||
|
GIT_REPO="${GIT_REPO_BASE}.git"
|
||||||
|
|
||||||
#???????????????????????????????? change the following to lock the log file?
|
# - Include ansible virtual environment (in case not already done - we don't know how we're being run)
|
||||||
|
VIRTUAL_ENV_DISABLE_PROMPT=true
|
||||||
|
. "${VENV_ACTIVATE_SCRIPT}"
|
||||||
|
|
||||||
# - If we can't lock the lock file, don't proceed
|
# - If we can't get a lock, don't proceed
|
||||||
if ! exec 9>"${LOCK_FILE}" ; then echo "ERROR - Unable to open the lock file (${LOCK_FILE})! Exiting..." ; exit 10 ; fi
|
if ! exec 9>"${LOCK_FILE}" ; then
|
||||||
if ! flock -n 9 ; then echo "ERROR - Another copy of ${SCRIPT_NAME} is already running! Exiting..." ; exit 11 ; fi
|
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
|
||||||
|
|
||||||
# - Append all further STDOUT and STDERR to the log file
|
# - Append all further STDOUT and STDERR to the log file
|
||||||
exec >>"${LOG_FILE}" 2>&1
|
exec >>"${LOG_FILE}" 2>&1
|
||||||
|
|
||||||
# - Log that we've gotten this far
|
# - Log that we've gotten this far
|
||||||
echo "$(basename "${0}"): $(date "+%Y-%m-%d %H:%M:%S")"
|
echo
|
||||||
|
echo "${SCRIPT_NAME}: $(date "+%Y-%m-%d %H:%M:%S")"
|
||||||
|
|
||||||
# - Do our work
|
# - Do our work
|
||||||
ansible-pull --only-if-changed --url "${GIT_REPO}" --checkout "${GIT_REPO_BRANCH}" "${@}"
|
ansible-pull --only-if-changed --url "${GIT_REPO}" --checkout "${GIT_REPO_BRANCH}" "${@}"
|
||||||
Reference in New Issue
Block a user