From 3af05ee69815ad5d48a3cb319b8da8854e419c3c Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Mon, 3 May 2021 14:44:36 +0200 Subject: [PATCH] Adding bin/pull-known-hosts --- bin/pull-known-hosts | 303 +++++++++++++++++++++++++++++++++++++++++++ lib/functions.rc | 2 +- 2 files changed, 304 insertions(+), 1 deletion(-) create mode 100755 bin/pull-known-hosts diff --git a/bin/pull-known-hosts b/bin/pull-known-hosts new file mode 100755 index 0000000..cc084ce --- /dev/null +++ b/bin/pull-known-hosts @@ -0,0 +1,303 @@ +#!/bin/bash + +set -e +set -u + +BASE_NAME="$( basename ${0} )" +MY_REAL_NAME=$( readlink -f $0 ) +BIN_DIR=$( dirname "${MY_REAL_NAME}" ) +BASE_DIR=$( dirname "${BIN_DIR}" ) +LIB_DIR="${BASE_DIR}/lib" +CONF_DIR="${BASE_DIR}/etc" + +if [[ -f "${LIB_DIR}/functions.rc" ]] ; then + . "${LIB_DIR}/functions.rc" +else + echo "Bash resource file '${LIB_DIR}/functions.rc' not found" >&2 + exit 5 +fi + +GIT_WORKDIR="${HOME}/Develop/gitlab-pp/ppadmin/known_hosts" +ONESHOT="n" +SLEEP_ONLINE=300 +SLEEP_OFFLINE=3600 +GIT_HOST="git.pixelpark.com" + +DESCRIPTION=$( cat <<-EOF + Updating Git repository below '${CYAN}${GIT_WORKDIR}${NORMAL}'." + + EOF +) + +detect_color + +#------------------------------------------------------------------------------ +usage() { + cat <<-EOF + Usage: ${BASE_NAME} [-D DIRECTORY] [-o|-1|--oneshot] [-S SECONDS] [-O SECONDS] [Common Options] + ${BASE_NAME} [-h|--help] + ${BASE_NAME} [-V|--version] + + Common Options: + ${STD_USAGE_MSG} + + Backup Options: + -D|--dir|--workdir DIRECTORY + The working directory, which to update. Defaults to '${CYAN}${GIT_WORKDIR}${NORMAL}'. + -o|-1|--oneshot Do not run in an infinite loop, leave this script after the first execution. + -S|--sleep-online SECONDS + Sleep so many seconds after an successful execution of '${CYAN}git pull${NORMAL}'. + Defaults to ${CYAN}${SLEEP_ONLINE} seconds${NORMAL}. + -O|--sleep-offline SECONDS + Sleep so many seconds after a not successful attempt to execute '${CYAN}git pull${NORMAL}'. + Defaults to ${CYAN}${SLEEP_OFFLINE} seconds${NORMAL}. + EOF + +} + +#------------------------------------------------------------------------------ +eval_my_options() { + + REMAINING_ARGS=() + REMAINING_OPTS=() + + if [[ "$#" -gt 0 ]] ; then + while true ; do + case "$1" in + -D|--dir|--workdir) + GIT_WORKDIR="$2" + shift + shift + ;; + -o|-1|--oneshot) + ONESHOT="y" + shift + ;; + -S|--sleep-online) + SLEEP_ONLINE="$2" + shift + shift + ;; + -O|--sleep-offline) + SLEEP_OFFLINE="$2" + shift + shift + ;; + --) shift + break + ;; + *) REMAINING_OPTS+=($1) + shift + ;; + esac + done + fi + + if [[ "$#" -gt "0" ]] ; then + REMAINING_ARGS=("--") + while [[ "$#" -gt "0" ]] ; do + REMAINING_ARGS+=($1) + shift + done + fi + + debug "Git working directory: '${CYAN}${GIT_WORKDIR}${NORMAL}'" + debug "Sleeps: online ${CYAN}${SLEEP_ONLINE} seconds${NORMAL}, offline ${CYAN}${SLEEP_OFFLINE} seconds${NORMAL}." +} + +#------------------------------------------------------------------------------ +get_options() { + + local tmp= + local base_dir= + + local this_short_options='D:o1S:O:' + local this_long_options='dir:,workdir:,oneshot:,sleep-online:,sleep-offline:' + + set +e + tmp=$( getopt -o "${STD_SHORT_OPTIONS}${this_short_options}" \ + --long "${STD_LONG_OPTIONS},${this_long_options}" \ + -n "${BASE_NAME}" -- "$@" ) + if [[ $? != 0 ]] ; then + echo "" >&2 + usage >&2 + exit 1 + fi + set -e + + # Note the quotes around `$TEMP': they are essential! + eval set -- "${tmp}" + eval_common_options "$@" + if [[ "${DEBUG}" == 'y' ]] ; then + declare -p REMAINING_OPTS + declare -p REMAINING_ARGS + fi + + eval_my_options "${REMAINING_OPTS[@]}" -- "${REMAINING_ARGS[@]}" + + if [[ "${#REMAINING_OPTS[@]}" -gt 0 ]] ; then + error "Unknown options: ${REMAINING_OPTS[*]}" + echo >&2 + usage >&2 + exit 2 + fi + + if [[ "${#REMAINING_ARGS[@]}" -gt 0 ]] ; then + error "Invalid arguments: ${REMAINING_ARGS[*]}" + echo >&2 + usage >&2 + exit 2 + fi + +} + +#------------------------------------------------------------------------------ +check_preferences() { + + info "Checking preferences ..." + local all_ok="y" + + local -a tools=('git' 'zenity' ) + local tool= + local folder= + + for tool in "${tools[@]}" ; do + debug "Checking for '${CYAN}${tool}${NORMAL}' ..." + if type -p ${tool} >/dev/null ; then + : + else + all_ok="n" + error "Did not found '${RED}${tool}${NORMAL}'. Maybe not installed?" + fi + done + + for folder in "${GIT_WORKDIR}" "${GIT_WORKDIR}/.git" ; do + debug "Checking for directory '${CYAN}${folder}${NORMAL}' ..." + if [[ ! -d "${folder}" ]]; then + all_ok="n" + error "Did not found directory '${RED}${folder}${NORMAL}'." + fi + done + + local dir_owner=$(stat --format '%U' "${GIT_WORKDIR}" ) + local cur_user=$( id -u -n ) + debug "Checking match directory owner '${CYAN}${dir_owner}${NORMAL}' to current user '${CYAN}${cur_user}${NORMAL}' ..." + if [[ "${dir_owner}" != "${cur_user}" ]] ; then + all_ok="n" + error "Current user '${RED}${cur_user}${NORMAL}' is not directory owner '${RED}${dir_owner}${NORMAL}'." + fi + + if [[ "${all_ok}" != "y" ]] ; then + exit 5 + fi + +} + +#------------------------------------------------------------------------------ +sleep_seconds() { + + local sleep_time="$1" + local i=0 + + debug "Sleeping for ${CYAN}${sleep_time} seconds${NORMAL} ..." + while [[ "$i" -lt "${sleep_time}" ]] ; do + sleep 1 + i=$(( $i + 1 )) + done + +} + +#------------------------------------------------------------------------------ +do_update() { + + local msg= + local git_out= + local git_ret= + local head_current= + local head_new= + + if cd "${GIT_WORKDIR}" ; then + : + else + error "Could not change into '${RED}${GIT_WORKDIR}${NORMAL}'." + zenity --error --text "Could not change to directory '${GIT_WORKDIR}'" + exit 5 + fi + + while true; do + + info "Waking up ..." + + debug "Checking for Git host '${CYAN}${GIT_HOST}${NORMAL}' ..." + if ping -q -c3 -W 5 git.pixelpark.com >/dev/null 2>&1 ; then + : + else + msg="Git host '${GIT_HOST}' is currently not available. " + msg+="Possibly VPN is down." + if [[ "${ONESHOT}" == "y" ]] ; then + warn "${msg}" + zenity --notification --title "${BASE_NAME}" --text "${msg}" + break + fi + msg+=" Trying again later." + warn "${msg}" + zenity --notification --title "${BASE_NAME}" --text "${msg}" + sleep_seconds "${SLEEP_OFFLINE}" + continue + fi + + head_current=$( git log --oneline HEAD | head -n1 | awk '{print $1}' ) + set +e + git_out=$( git pull 2>&1 ) + git_ret="$?" + set -e + if [[ "${git_ret}" != "0" ]] ; then + msg="Pulling known_hosts failed, please investigate: ${git_out}" + error "${msg}" + zenity --notification --title "${BASE_NAME}" --text "${msg}" + if [[ "${ONESHOT}" == "y" ]] ; then + break + fi + sleep_seconds "${SLEEP_ONLINE}" + continue + fi + head_new=$( git log --oneline HEAD | head -n1 | awk '{print $1}' ) + if [[ "${head_current}" == "${head_new}" ]] ; then + info "No changes to known_hosts detected. Doing nothing." + else + msg="The known_hosts repository has been pulled successfully." + info "${msg}" + zenity --notification --title "${BASE_NAME}" --text "${msg}" + fi + if [[ "${ONESHOT}" == "y" ]] ; then + break + fi + sleep_seconds "${SLEEP_ONLINE}" + + done + +} + +#------------------------------------------------------------------------------ +main() { + + get_options "$@" + umask 0022 + + empty_line + dline + info "Starting update of Git working directory '${CYAN}${GIT_WORKDIR}${NORMAL}' ..." + empty_line + + check_preferences + do_update + + info "Finished." +} + +main "$@" + +exit 0 + + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list diff --git a/lib/functions.rc b/lib/functions.rc index 89d6900..d124156 100644 --- a/lib/functions.rc +++ b/lib/functions.rc @@ -10,7 +10,7 @@ BLUE="" CYAN="" NORMAL="" -VERSION="0.4.0" +VERSION="0.4.1" STD_SHORT_OPTIONS="sdvqhV" STD_LONG_OPTIONS="simulate,debug,verbose,quiet,nocolor,help,version" -- 2.39.5