summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin H. Johnson <robbat2@gentoo.org>2020-04-28 22:33:48 -0700
committerRobin H. Johnson <robbat2@gentoo.org>2020-04-28 22:33:48 -0700
commitadddc138088d7f2157cfcdd72402b9395be4ccb0 (patch)
treedc8b1643aa7b7acd4d9b74c1526bf09f8d140ca9
parentswitch to new refactored scripts (diff)
downloadgentoo-mirrorstats-adddc138088d7f2157cfcdd72402b9395be4ccb0.tar.gz
gentoo-mirrorstats-adddc138088d7f2157cfcdd72402b9395be4ccb0.tar.bz2
gentoo-mirrorstats-adddc138088d7f2157cfcdd72402b9395be4ccb0.zip
wrapper: input validations
Signed-off-by: Robin H. Johnson <robbat2@gentoo.org>
-rwxr-xr-xmirmon-wrapper.sh74
1 files changed, 50 insertions, 24 deletions
diff --git a/mirmon-wrapper.sh b/mirmon-wrapper.sh
index ba731c6..2c50369 100755
--- a/mirmon-wrapper.sh
+++ b/mirmon-wrapper.sh
@@ -24,9 +24,6 @@ die() {
}
main() {
- [[ -z "$MODE" ]] && die "--mode unset"
- [[ -z "$XML_SRC" ]] && die "--xml-src unset"
- [[ -z "$URL" ]] && die "--url unset"
SITEDIR=/var/www/mirrorstats.gentoo.org
REPODIR=${SITEDIR}/gentoo-mirrorstats/
MODEDIR=${REPODIR}/${MODE}_mirrors/
@@ -35,35 +32,40 @@ main() {
MIRMON=$(readlink -f "${SITEDIR}"/mirmon/mirmon)
CONFFILE=$(readlink -f "${MODEDIR}"/mirmon.conf)
VAR_GMIRRORS=${VARDIR}/g.mirrors
-
- # Grab mirrors from the web
- mkdir -p "${VARDIR}"
- "${REPODIR}"/get-mirrors-from-${XML_SRC}-xml.rb > "${VAR_GMIRRORS}".tmp
-
EXTRA_MIRRORS=${MODEDIR}/g.mirrors.extra
+
+ # Grab mirrors from the web
+ mkdir -p "${VARDIR}" "${HTDOCS}" || die "Failed to mkdir"
+ "${REPODIR}/get-mirrors-from-${XML_SRC}-xml.rb" > "${VAR_GMIRRORS}".tmp || die "Unable to fetch mirror list"
+
[[ -e "${EXTRA_MIRRORS}" ]] && cat "${EXTRA_MIRRORS}" >>"${VAR_GMIRRORS}".tmp
-
+
# Atomic rotate into place
- mv -f "${VAR_GMIRRORS}"{.tmp,}
-
+ mv -f "${VAR_GMIRRORS}"{.tmp,} || die "Failed to set mirror list"
+
# fatal if the state file is NOT present.
- [[ -e ${VARDIR}/mirmon.state ]] || touch "${VARDIR}/mirmon.state"
-
+ if [[ ! -e ${VARDIR}/mirmon.state ]]; then
+ touch "${VARDIR}/mirmon.state" || die "Could not create state file"
+ else
+ test -w "${VARDIR}/mirmon.state" || die "Cannot write state file!"
+ fi
+
# run mirmon
- /usr/bin/perl "${MIRMON}" -c "${CONFFILE}" -q -get update
-
+ /usr/bin/perl "${MIRMON}" -c "${CONFFILE}" -q -get update || die "mirmon failed: $?"
+
# Set up a nice link to our mirror page directly:
- mkdir -p ${HTDOCS}
sed \
- -e "s#mirrors</H2>#<a href="${URL}">mirrors</a></H2>#" \
+ -e "s#mirrors</H2>#<a href=\"${URL}\">mirrors</a></H2>#" \
>"${HTDOCS}"/index.html.tmp <"${HTDOCS}"/index-wip.html \
- && mv -f "${HTDOCS}"/index.html{.tmp,}
-
+ || die "Failed to fix URLs(sed)"
+ mv -f "${HTDOCS}"/index.html{.tmp,} || die "Failed to fix URLs(mv)"
+
# Generate a json file containing the state of each mirror
"${REPODIR}"/generate-json.py \
"${VARDIR}"/mirmon.state \
>"${HTDOCS}"/state.json.tmp \
- && mv -f "${HTDOCS}"/state.json{.tmp,}
+ || die "Failed to generate JSON (py)"
+ mv -f "${HTDOCS}"/state.json{.tmp,} || die "Failed to generate JSON (mv)"
# Done
exit 0
@@ -115,11 +117,35 @@ while [[ $# -gt 0 ]]; do
esac
done
+# Validations
+MODE_INPUTS=(
+ distfiles
+ rsync
+ snapshots
+ experimental
+ releases
+)
+MODE_VALID=0
+for m in "${MODE_INPUTS[@]}"; do
+ [[ "$m" == "$MODE" ]] && MODE_VALID=1
+done
+[[ $MODE_VALID -eq 1 ]] || MODE=''
+
+XML_SRC_INPUTS=(
+ distfiles
+ rsync
+)
+XML_SRC_VALID=0
+for m in "${XML_SRC_INPUTS[@]}"; do
+ [[ "$m" == "$XML_SRC" ]] && XML_SRC_VALID=1
+done
+[[ $XML_SRC_VALID -eq 1 ]] || XML_SRC=''
+
+[[ -z "$MODE" ]] && die "--mode must be one of: ${MODE_INPUTS[*]}"
+[[ -z "$XML_SRC" ]] && die "--xml-src must be one of: ${XML_SRC_INPUTS[*]}"
+[[ -z "$URL" ]] && die "--url unset"
+
[[ $INVALID -eq 1 ]] && usage
[[ $HELP -eq 1 ]] && dohelp
main
-
-#MODE=snapshots
-#XML_SRC=distfiles
-#URL=https://www.gentoo.org/downloads/mirrors/