Gentoo Archives: gentoo-commits

From: "Robin H. Johnson" <robbat2@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/gentoo-mirrorstats:master commit in: /
Date: Wed, 29 Apr 2020 05:34:01
Message-Id: 1588138428.adddc138088d7f2157cfcdd72402b9395be4ccb0.robbat2@gentoo
1 commit: adddc138088d7f2157cfcdd72402b9395be4ccb0
2 Author: Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
3 AuthorDate: Wed Apr 29 05:33:48 2020 +0000
4 Commit: Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
5 CommitDate: Wed Apr 29 05:33:48 2020 +0000
6 URL: https://gitweb.gentoo.org/proj/gentoo-mirrorstats.git/commit/?id=adddc138
7
8 wrapper: input validations
9
10 Signed-off-by: Robin H. Johnson <robbat2 <AT> gentoo.org>
11
12 mirmon-wrapper.sh | 74 +++++++++++++++++++++++++++++++++++++------------------
13 1 file changed, 50 insertions(+), 24 deletions(-)
14
15 diff --git a/mirmon-wrapper.sh b/mirmon-wrapper.sh
16 index ba731c6..2c50369 100755
17 --- a/mirmon-wrapper.sh
18 +++ b/mirmon-wrapper.sh
19 @@ -24,9 +24,6 @@ die() {
20 }
21
22 main() {
23 - [[ -z "$MODE" ]] && die "--mode unset"
24 - [[ -z "$XML_SRC" ]] && die "--xml-src unset"
25 - [[ -z "$URL" ]] && die "--url unset"
26 SITEDIR=/var/www/mirrorstats.gentoo.org
27 REPODIR=${SITEDIR}/gentoo-mirrorstats/
28 MODEDIR=${REPODIR}/${MODE}_mirrors/
29 @@ -35,35 +32,40 @@ main() {
30 MIRMON=$(readlink -f "${SITEDIR}"/mirmon/mirmon)
31 CONFFILE=$(readlink -f "${MODEDIR}"/mirmon.conf)
32 VAR_GMIRRORS=${VARDIR}/g.mirrors
33 -
34 - # Grab mirrors from the web
35 - mkdir -p "${VARDIR}"
36 - "${REPODIR}"/get-mirrors-from-${XML_SRC}-xml.rb > "${VAR_GMIRRORS}".tmp
37 -
38 EXTRA_MIRRORS=${MODEDIR}/g.mirrors.extra
39 +
40 + # Grab mirrors from the web
41 + mkdir -p "${VARDIR}" "${HTDOCS}" || die "Failed to mkdir"
42 + "${REPODIR}/get-mirrors-from-${XML_SRC}-xml.rb" > "${VAR_GMIRRORS}".tmp || die "Unable to fetch mirror list"
43 +
44 [[ -e "${EXTRA_MIRRORS}" ]] && cat "${EXTRA_MIRRORS}" >>"${VAR_GMIRRORS}".tmp
45 -
46 +
47 # Atomic rotate into place
48 - mv -f "${VAR_GMIRRORS}"{.tmp,}
49 -
50 + mv -f "${VAR_GMIRRORS}"{.tmp,} || die "Failed to set mirror list"
51 +
52 # fatal if the state file is NOT present.
53 - [[ -e ${VARDIR}/mirmon.state ]] || touch "${VARDIR}/mirmon.state"
54 -
55 + if [[ ! -e ${VARDIR}/mirmon.state ]]; then
56 + touch "${VARDIR}/mirmon.state" || die "Could not create state file"
57 + else
58 + test -w "${VARDIR}/mirmon.state" || die "Cannot write state file!"
59 + fi
60 +
61 # run mirmon
62 - /usr/bin/perl "${MIRMON}" -c "${CONFFILE}" -q -get update
63 -
64 + /usr/bin/perl "${MIRMON}" -c "${CONFFILE}" -q -get update || die "mirmon failed: $?"
65 +
66 # Set up a nice link to our mirror page directly:
67 - mkdir -p ${HTDOCS}
68 sed \
69 - -e "s#mirrors</H2>#<a href="${URL}">mirrors</a></H2>#" \
70 + -e "s#mirrors</H2>#<a href=\"${URL}\">mirrors</a></H2>#" \
71 >"${HTDOCS}"/index.html.tmp <"${HTDOCS}"/index-wip.html \
72 - && mv -f "${HTDOCS}"/index.html{.tmp,}
73 -
74 + || die "Failed to fix URLs(sed)"
75 + mv -f "${HTDOCS}"/index.html{.tmp,} || die "Failed to fix URLs(mv)"
76 +
77 # Generate a json file containing the state of each mirror
78 "${REPODIR}"/generate-json.py \
79 "${VARDIR}"/mirmon.state \
80 >"${HTDOCS}"/state.json.tmp \
81 - && mv -f "${HTDOCS}"/state.json{.tmp,}
82 + || die "Failed to generate JSON (py)"
83 + mv -f "${HTDOCS}"/state.json{.tmp,} || die "Failed to generate JSON (mv)"
84
85 # Done
86 exit 0
87 @@ -115,11 +117,35 @@ while [[ $# -gt 0 ]]; do
88 esac
89 done
90
91 +# Validations
92 +MODE_INPUTS=(
93 + distfiles
94 + rsync
95 + snapshots
96 + experimental
97 + releases
98 +)
99 +MODE_VALID=0
100 +for m in "${MODE_INPUTS[@]}"; do
101 + [[ "$m" == "$MODE" ]] && MODE_VALID=1
102 +done
103 +[[ $MODE_VALID -eq 1 ]] || MODE=''
104 +
105 +XML_SRC_INPUTS=(
106 + distfiles
107 + rsync
108 +)
109 +XML_SRC_VALID=0
110 +for m in "${XML_SRC_INPUTS[@]}"; do
111 + [[ "$m" == "$XML_SRC" ]] && XML_SRC_VALID=1
112 +done
113 +[[ $XML_SRC_VALID -eq 1 ]] || XML_SRC=''
114 +
115 +[[ -z "$MODE" ]] && die "--mode must be one of: ${MODE_INPUTS[*]}"
116 +[[ -z "$XML_SRC" ]] && die "--xml-src must be one of: ${XML_SRC_INPUTS[*]}"
117 +[[ -z "$URL" ]] && die "--url unset"
118 +
119 [[ $INVALID -eq 1 ]] && usage
120 [[ $HELP -eq 1 ]] && dohelp
121
122 main
123 -
124 -#MODE=snapshots
125 -#XML_SRC=distfiles
126 -#URL=https://www.gentoo.org/downloads/mirrors/