2018-02-10 23:54:09 +00:00
|
|
|
#!/bin/bash
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
DEBUG=${DEBUG:-false}
|
|
|
|
FETCH_INTERVAL=${FETCH_INTERVAL:-3600}
|
2018-03-10 11:55:02 +00:00
|
|
|
REPOS=(public secret system)
|
2018-02-11 10:12:50 +00:00
|
|
|
SSH_KEY=${SSH_KEY:-fafnir}
|
2018-02-10 23:54:09 +00:00
|
|
|
|
|
|
|
# Print debug messages if enabled by ${DEBUG}
|
2018-03-10 11:47:55 +00:00
|
|
|
function debug() {
|
|
|
|
[[ ${DEBUG} == "false" ]] && return
|
2018-02-10 23:54:09 +00:00
|
|
|
echo "$@" >&2
|
|
|
|
}
|
|
|
|
|
2018-03-10 11:47:55 +00:00
|
|
|
function join_by() {
|
|
|
|
local d=$1
|
|
|
|
shift
|
|
|
|
echo -n "$1"
|
|
|
|
shift
|
|
|
|
printf "%s" "${@/#/$d}"
|
|
|
|
}
|
2018-02-11 00:28:39 +00:00
|
|
|
|
2018-02-10 23:54:09 +00:00
|
|
|
# Wrap git to work with git-dir and work-tree being in other locations
|
2018-03-10 11:47:55 +00:00
|
|
|
function gwrap() {
|
2018-06-25 08:27:41 +00:00
|
|
|
git --git-dir=${HOME}/.cfg/${REPO} --work-tree=${HOME} $@
|
2018-02-10 23:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Ensure we're connected to network before acting
|
2018-03-10 11:47:55 +00:00
|
|
|
if ! (ping -q -c 1 8.8.8.8 >/dev/null); then
|
2018-02-10 23:54:09 +00:00
|
|
|
debug "No network connection, not checking"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2018-02-11 00:13:23 +00:00
|
|
|
STAT_PARM="-c %Y"
|
2018-03-10 11:47:55 +00:00
|
|
|
[[ "$(uname -s)" == "Darwin" ]] && STAT_PARM="-f %m"
|
2018-02-11 00:13:23 +00:00
|
|
|
|
2018-02-11 00:28:39 +00:00
|
|
|
NEED_UPDATE=()
|
2018-02-10 23:54:09 +00:00
|
|
|
# Check repos for updates
|
|
|
|
for REPO in ${REPOS[@]}; do
|
2018-02-11 12:07:54 +00:00
|
|
|
LAST_FETCH=0
|
|
|
|
[ -f ~/.cfg/${REPO}/FETCH_HEAD ] && LAST_FETCH=$(stat ${STAT_PARM} ~/.cfg/${REPO}/FETCH_HEAD)
|
2018-03-10 11:47:55 +00:00
|
|
|
if [ $(($(date +%s) - LAST_FETCH)) -gt ${FETCH_INTERVAL} ] ||
|
2018-02-11 12:07:54 +00:00
|
|
|
[ $(wc -c ~/.cfg/${REPO}/FETCH_HEAD | cut -d' ' -f1) -eq 0 ]; then
|
2018-03-10 11:47:55 +00:00
|
|
|
vault-sshadd ${SSH_KEY} 2>&1 >/dev/null || {
|
|
|
|
echo "Unable to load key ${SSH_KEY}"
|
|
|
|
exit 1
|
|
|
|
}
|
2018-03-11 11:37:16 +00:00
|
|
|
gwrap fetch -q origin
|
2018-02-10 23:54:09 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
LOCAL=$(gwrap rev-parse HEAD)
|
|
|
|
REMOTE=$(gwrap rev-parse FETCH_HEAD)
|
|
|
|
|
2018-03-10 11:47:55 +00:00
|
|
|
if ! [[ ${LOCAL} == "${REMOTE}" ]]; then
|
2018-02-11 00:31:56 +00:00
|
|
|
NEED_UPDATE+=("'${REPO}'")
|
2018-02-10 23:54:09 +00:00
|
|
|
fi
|
|
|
|
done
|
2018-02-11 00:28:39 +00:00
|
|
|
|
|
|
|
if [ ${#NEED_UPDATE[@]} -gt 0 ]; then
|
2018-02-11 00:31:56 +00:00
|
|
|
echo "Config repo $(join_by ", " "${NEED_UPDATE[@]}") needs update"
|
2018-06-25 08:27:41 +00:00
|
|
|
else
|
|
|
|
if ( ${HOME}/bin/config-git-status.sh ); then
|
|
|
|
echo "Up-to-date"
|
|
|
|
else
|
|
|
|
echo "Up-to-date with uncommitted changes"
|
|
|
|
fi
|
2018-02-11 00:28:39 +00:00
|
|
|
fi
|