setup-apkrepos.in 5.07 KB
Newer Older
1 2 3 4 5 6 7
#!/bin/sh

PROGRAM=setup-apkrepos
PREFIX=

. $PREFIX/lib/libalpine.sh

8 9 10
if [ "$ROOT" != "/" ]; then
	apk_root_opt="--root $ROOT"
fi
Matt Smith's avatar
Matt Smith committed
11

12 13 14 15 16
get_hostname_from_url() {
	local n=${1#*://}
	echo ${n%%/*}
}

Matt Smith's avatar
Matt Smith committed
17
prompt_setup_method() {
18 19 20 21 22 23
	cat <<-__EOF__
		r) Add random from the above list
		f) Detect and add fastest mirror from above list
		e) Edit "${ROOT}"etc/apk/repositores with text editor

	__EOF__
24
	echo -n "Enter mirror number (1-$mirror_count) or URL to add (or r/f/e/done) [$1]: "
Matt Smith's avatar
Matt Smith committed
25 26
}

27
add_random_mirror() {
Matt Smith's avatar
Matt Smith committed
28
	local i=0
29
	local random_mirror_index=$(( $RANDOM % $mirror_count ))
30

Matt Smith's avatar
Matt Smith committed
31 32 33 34 35
	echo -n "Picking random mirror... "
	for mirror in $MIRRORS; do
		if [ $i -eq $random_mirror_index ]; then
			break
		fi
36
		i=$(( $i + 1 ))
Matt Smith's avatar
Matt Smith committed
37
	done
38
	add_mirror $mirror
Matt Smith's avatar
Matt Smith committed
39 40
}

41 42
time_cmd() {
	local start=$(cut -d ' ' -f1 /proc/uptime)
43
	$@ >&2 || return
44 45 46
	awk -v start=$start -v end=$(cut -d ' ' -f1 /proc/uptime) \
		'BEGIN {print end - start; exit}'
}
Matt Smith's avatar
Matt Smith committed
47

48 49
find_fastest_mirror() {
	local url=
50
	local arch=$(apk --print-arch)
51
	for url in $MIRRORS; do
52 53
		# warm up the dns cache
		nslookup $(get_hostname_from_url $url) >/dev/null 2>&1
54
		local time=$(time_cmd wget --spider -q -T 5 \
55
			${url%/}/edge/main/$arch/APKINDEX.tar.gz)
56 57 58
		if [ -n "$time" ]; then
			echo "$time $url"
		fi
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	done | awk ' {
		if (!current) {
			current=$1
			url=$2
		} else {
			if ($1 < current) {
				current=$1
				url=$2
			}
		}
		printf("%6.2f %s\n", $1, $2) > "/dev/stderr"
	}
	END { print url }'
}

add_fastest_mirror() {
	echo "Finding fastest mirror... "
	local fastest=$(find_fastest_mirror)
	add_mirror "$fastest"
Matt Smith's avatar
Matt Smith committed
78 79
}

80
# show mirrors and store how many in global mirror_count
81
show_mirror_list() {
82 83 84
	local mirror i=0
	mirror_count=0
	[ -z "$MIRRORS" ] && return
85
	echo ""
86
	echo "Available mirrors:"
87 88
	for mirror in $MIRRORS; do
		i=$(($i + 1))
89
		echo "$i) $(get_hostname_from_url $mirror)"
90
	done
91 92
	echo ""
	mirror_count=$i
93 94
}

95 96 97 98 99
add_from_list() {
	local mirror_index=$1
	if [ $mirror_index -lt 1 ] || [ $mirror_index -gt $mirror_count ]; then
		return 1
	fi
Matt Smith's avatar
Matt Smith committed
100
	set $MIRRORS
101
	eval "mirror=\$$mirror_index"
102
	add_mirror "$mirror"
Matt Smith's avatar
Matt Smith committed
103 104 105
}

get_alpine_release() {
106
	local version=$(cat "${ROOT}"etc/alpine-release 2>/dev/null)
107
	case "$version" in
108
		*_git*|*_alpha*) release="edge";;
109 110
		[0-9]*.[0-9]*.[0-9]*)
			# release in x.y.z format, cut last digit
111 112 113
			release=v${version%.[0-9]*};;
		*)	# fallback to edge
			release="edge";;
114
	esac
Matt Smith's avatar
Matt Smith committed
115
}
116

117 118
add_mirror() {
	local mirror="$1"
119
	mkdir -p "${APKREPOS_PATH%/*}"
120
	echo "${mirror%/}/${release}/main" >> $APKREPOS_PATH
121
	echo "#${mirror%/}/${release}/community" >> $APKREPOS_PATH
122 123
	case "$release" in
	v[0-9]*)
Ted Trask's avatar
Ted Trask committed
124
		echo "#${mirror%/}/edge/main" >> $APKREPOS_PATH;
125
		echo "#${mirror%/}/edge/community" >> $APKREPOS_PATH;;
126 127
	esac
	echo "#${mirror%/}/edge/testing" >> $APKREPOS_PATH
128 129 130
	echo "" >> $APKREPOS_PATH
	echo "Added mirror $(get_hostname_from_url $mirror)"
}
Matt Smith's avatar
Matt Smith committed
131

132
add_from_url() {
133
	mkdir -p "${APKREPOS_PATH%/*}"
134 135
	echo "$1" >> $APKREPOS_PATH
	echo "" >> $APKREPOS_PATH
Matt Smith's avatar
Matt Smith committed
136 137
}

138
edit_repositories() {
139 140
	local md5=$(md5sum $APKREPOS_PATH 2>/dev/null)
	mkdir -p "${APKREPOS_PATH%/*}"
141 142
	${EDITOR:-vi} "$APKREPOS_PATH"
	# return true if file changed
143
	test "$(md5sum $APKREPOS_PATH 2>/dev/null)" != "$md5"
144
}
Matt Smith's avatar
Matt Smith committed
145

146
usage() {
147 148
	cat <<-__EOF__
		usage: setup-apkrepos [-fhr] [REPO...]
149

150
		Setup apk repositories
151

152 153 154 155
		options:
		 -f  Detect and add fastest mirror
		 -h  Show this help
		 -r  Add a random mirror and do not prompt
156
		 -1  Add first mirror on the list (normally a CDN)
157
	__EOF__
158 159 160 161
	exit 1

}

162 163 164 165
add_fastest=false
add_first=false
add_random=false
while getopts "1fhr" opt; do
166
	case $opt in
167 168
		f) add_fastest=true;;
		1) add_first=true;;
169
		h) usage;;
170
		r) add_random=true;;
171
	esac
172
done
173
shift $(($OPTIND - 1))
174

Matt Smith's avatar
Matt Smith committed
175
# main
176 177
# install alpine-mirrors if its not already there
to_uninstall=
178
if ! apk info --quiet --installed alpine-mirrors; then
179
	apk add --force --quiet --virtual .setup-apkrepos alpine-mirrors
180
	to_uninstall=.setup-apkrepos
181 182 183 184
fi

MIRRORS_PATH=/usr/share/alpine-mirrors/MIRRORS.txt
if [ -z "$MIRRORS" ] && [ -r "$MIRRORS_PATH" ]; then
185
	MIRRORS=$(cat $MIRRORS_PATH)
186 187
fi

188
APKREPOS_PATH="${ROOT}"etc/apk/repositories
189
if [ -r "$APKREPOS_PATH" ]; then
190
	APKREPOS=$(cat "$APKREPOS_PATH")
191 192
fi

193 194
get_alpine_release
default_answer=f
195 196 197 198 199 200
changed=false

if [ $# -gt 0 ]; then
	# replace the apk repos with the specified ones
	rm -f "$APKREPOS_PATH"
	for i; do
201
		echo "$i" >> "$APKREPOS_PATH" && changed=true
202 203 204
	done
fi

205 206 207 208 209 210
if $add_first; then
	set -- $MIRRORS
	add_mirror "$1" && changed=true
fi

if $add_random; then
211 212 213 214
	show_mirror_list > /dev/null
	add_random_mirror && changed=true
fi

215
if $add_fastest; then
216 217 218 219
	show_mirror_list > /dev/null
	add_fastest_mirror && changed=true
fi

220 221 222 223 224 225 226 227 228 229 230 231 232 233
while ! $changed; do
	show_mirror_list
	prompt_setup_method $default_answer

	default_read answer $default_answer
	case "$answer" in
		"done") break;;
		[0-9]*) add_from_list $answer && changed=true;;
		/*|http://*|ftp://*|https://*) add_from_url "$answer" \
			&& changed=true;;
		r) add_random_mirror && changed=true;;
		f) add_fastest_mirror && changed=true;;
		e) edit_repositories && changed=true;;
	esac
234
done
Matt Smith's avatar
Matt Smith committed
235

236
if $changed; then
237
	echo -n "Updating repository indexes... "
238
	apk update --quiet $apk_root_opt && echo "done."
239
fi
240 241 242

# clean up
if [ -n "$to_uninstall" ]; then
243
	apk del --quiet $to_uninstall
244
fi