summaryrefslogtreecommitdiff
blob: e641c7578aeb968fbb3defa0c7ce89b71bafeebe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/bash -e

NAME=$(basename "$0")

OPTIONS_STR=(
	"mode"
	"xml-src"
	"url"
)
OPTIONS_BOOL=(
	"help"
	"debug"
	"verbose"
	"quiet"
)


helpmsg() {
	echo "$*" 1>&2
}

usage() {
	helpmsg "Usage: ${NAME}$(printf " --%s=..." "${OPTIONS_STR[@]}" )$(printf " [--%s]" "${OPTIONS_BOOL[@]}")"
	exit 3
}

dohelp() {
	helpmsg "${NAME} TODO"
	exit 0
}

die() {
	helpmsg "$*"
	exit 2
}

main() {
	SITEDIR=/var/www/mirrorstats.gentoo.org
	REPODIR=${SITEDIR}/gentoo-mirrorstats/
	CONFDIR=${REPODIR}/conf/${MODE}/
	VARDIR=${SITEDIR}/var/${MODE}
	HTDOCS=${SITEDIR}/htdocs/${MODE}
	MIRMON=$(readlink -f "${SITEDIR}"/mirmon/mirmon)
	CONFFILE=$(readlink -f "${CONFDIR}"/mirmon.conf)
	VAR_GMIRRORS=${VARDIR}/g.mirrors
	EXTRA_MIRRORS=${CONFDIR}/g.mirrors.extra
	# Lock outselves
	if [ "${FLOCKER}" != "$0" ]; then
		exec \
		env FLOCKER="$0" flock -en "${VARDIR}/flock" \
		"$0" \
		${DEBUG:+--debug} \
		${QUIET:+--quiet} \
		${VERBOSE:+--verbose} \
		"--xml-src=${XML_SRC}" \
		"--mode=${MODE}" \
		"--url=${URL}"
	fi

	# 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,} || die "Failed to set mirror list"

	# fatal if the state file is NOT present.
	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

	MIRMON_OPTS=(
		 -c "${CONFFILE}"
		 -get update
	)
	[[ $VERBOSE -eq 1 ]] && MIRMON_OPTS+=( '-v' )
	[[ $DEBUG -eq 1 ]] && MIRMON_OPTS+=( '-d' )
	[[ $QUIET -eq 1 ]] && MIRMON_OPTS+=( '-q' )

	# run mirmon
	/usr/bin/perl "${MIRMON}" "${MIRMON_OPTS[@]}" || die "mirmon failed: $?"

	# Set up a nice link to our mirror page directly:
	sed \
	    -e "s#mirrors</H2>#<a href=\"${URL}\">mirrors</a></H2>#" \
	    >"${HTDOCS}"/index.html.tmp <"${HTDOCS}"/index-wip.html \
		|| 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 \
	|| die "Failed to generate JSON (py)"
	mv -f "${HTDOCS}"/state.json{.tmp,} || die "Failed to generate JSON (mv)"

	# Done
	exit 0
}

opts=$(getopt \
	--longoptions "$(printf "%s:," "${OPTIONS_STR[@]}" )" \
	--longoptions "$(printf "%s," "${OPTIONS_BOOL[@]}" )" \
	--name "$(basename "$0")" \
	--options "" \
	-- "$@"
)

eval set --$opts

MODE=
XML_SRC=
URL=
HELP=
DEBUG=
VERBOSE=
INVALID=

while [[ $# -gt 0 ]]; do
    case "$1" in
		# Strs:
        --mode)
            MODE=$2
            shift 2
            ;;

        --xml-src)
            XML_SRC=$2
            shift 2
            ;;
        --url)
            URL=$2
            shift 2
            ;;
		# Bools:
		--quiet)
			QUIET=1
			shift
			;;
		--debug)
			DEBUG=1
			shift
			;;
		--verbose)
			VERBOSE=1
			shift
			;;
		--help)
			HELP=1
			shift
			;;
		# End of options
		--)
			break
			;;
        *)
			INVALID=1
            break
            ;;
    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=''

[[ "$INVALID" == 1 ]] && usage
[[ "$HELP" == 1 ]] && dohelp

if [[ -z "$MODE" ]]; then helpmsg "--mode must be one of: ${MODE_INPUTS[*]}" ; INVALID=1 ; fi
if [[ -z "$XML_SRC" ]]; then helpmsg "--xml-src must be one of: ${XML_SRC_INPUTS[*]}" ; INVALID=1 ; fi
if [[ -z "$URL" ]]; then helpmsg "--url unset" ; INVALID=1 ; fi
[[ "$INVALID" == 1 ]] && usage

main