summaryrefslogtreecommitdiff
blob: 74a12d64ed961e4709f98b918d2f7f2ee8da9780 (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
#!/bin/bash -e

NAME=$(basename "$0")

OPTIONS=(
	"mode"
	"xml-src"
	"url"
)

usage() {
	echo "Usage: ${NAME}$(printf " --%s=..." "${OPTIONS[@]}" )" 1>&2
	exit 3
}

dohelp() {
	echo "${NAME} TODO" 1>&2
	exit 0
}

die() {
	echo "$*" 1>&2
	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
	[ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en "${VARDIR}/flock" "$0" "--xml-src=${XML_SRC}" "--mode=${MODE}" "--url=${URL}" || :

	# 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

	# run mirmon
	/usr/bin/perl "${MIRMON}" -c "${CONFFILE}" -q -get update || 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[@]}" )" \
	--name "$(basename "$0")" \
	--options "" \
	-- "$@"
)

eval set --$opts

MODE=''
XML_SRC=''
URL=''
HELP=0
INVALID=0

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

        --xml-src)
            XML_SRC=$2
            shift 2
            ;;

        --url)
            URL=$2
            shift 2
            ;;
		--help)
			HELP=1
			break
			;;
		--)
			# 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=''

[[ -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

OPTS=( ${opts} ) # for flock
main