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

PROGRAM=setup-apkrepos
PREFIX=

. $PREFIX/lib/libalpine.sh

Matt Smith's avatar
Matt Smith committed
8
MIRRORS_PATH=/usr/share/alpine-mirrors/MIRRORS.txt
9 10 11
if [ -z "$MIRRORS" ] && [ -r "$MIRRORS_PATH" ]; then
	MIRRORS=`cat $MIRRORS_PATH`
fi
12

Matt Smith's avatar
Matt Smith committed
13
RELEASES_PATH=/usr/share/alpine-mirrors/RELEASES.txt
14 15 16
if [ -z "$RELEASES" ] && [ -r "$RELEASES_PATH" ]; then
	RELEASES=`cat $RELEASES_PATH`
fi
Matt Smith's avatar
Matt Smith committed
17 18

APKREPOS_PATH=/etc/apk/repositories
19 20 21
if [ -r "$APKREPOS_PATH" ]; then
	APKREPOS=`cat "$APKREPOS_PATH"`
fi
Matt Smith's avatar
Matt Smith committed
22

23 24 25 26 27
get_hostname_from_url() {
	local n=${1#*://}
	echo ${n%%/*}
}

Matt Smith's avatar
Matt Smith committed
28
prompt_setup_method() {
29 30 31 32 33
	echo "r) Add random from the above list"
	echo "f) Detect and add fastest mirror from above list"
	echo "e) Edit /etc/apk/repositores with text editor"
	echo ""
	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
34 35
}

36
add_random_mirror() {
Matt Smith's avatar
Matt Smith committed
37
	local i=0
38
	local random_mirror_index=$(( $RANDOM % $mirror_count ))
Matt Smith's avatar
Matt Smith committed
39 40 41 42 43 44
	
	echo -n "Picking random mirror... "
	for mirror in $MIRRORS; do
		if [ $i -eq $random_mirror_index ]; then
			break
		fi
45
		i=$(( $i + 1 ))
Matt Smith's avatar
Matt Smith committed
46
	done
47
	add_mirror $mirror
Matt Smith's avatar
Matt Smith committed
48 49
}

50
add_fastest_mirror() {
Matt Smith's avatar
Matt Smith committed
51 52 53 54 55 56 57 58 59 60
	local tmp_mirror_nslookup
	local tmp_mirror_time
	local tmp_mirror_time_failed
	local tmp_mirror_rtt
	local mirror_lowest_rtt
	local mirror_lowest_rtt_mirror

	echo -n "Finding fastest mirror... "
	mirror_lowest_rtt=-1
	for mirror in $MIRRORS; do
Jeff Bilyk's avatar
Jeff Bilyk committed
61 62 63 64 65 66 67 68 69
		tmp_mirror_time="`(time wget -qO - $mirror) 2>&1 || echo "E_MIRROR_FAILED"`"
		tmp_mirror_time_failed=`echo $tmp_mirror_time | grep "E_MIRROR_FAILED"`
		if [ ${#tmp_mirror_time_failed} -eq 0 ]; then
			tmp_mirror_rtt=`echo "$tmp_mirror_time" | grep -E "^real" | sed -r "s/^real[ 	]+[0-9]+m[ 	]+([0-9]+)\.([0-9]+)s$/\1\2/"`
			if [ $mirror_lowest_rtt -eq -1 ]; then
				mirror_lowest_rtt=$tmp_mirror_rtt
				mirror_lowest_rtt_mirror=$mirror
			else
				if [ $tmp_mirror_rtt -lt $mirror_lowest_rtt ]; then
70
					mirror_lowest_rtt=$tmp_mirror_rtt
Matt Smith's avatar
Matt Smith committed
71
					mirror_lowest_rtt_mirror=$mirror
72 73 74
				fi
			fi
		fi
Matt Smith's avatar
Matt Smith committed
75 76
	done
	mirror=$mirror_lowest_rtt_mirror
77
	add_mirror "$mirror"
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 107
	local version=$(cat /etc/alpine-release 2>/dev/null)
	case "$version" in
Matt Smith's avatar
Matt Smith committed
108
		*_git*) 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 119 120 121 122 123
add_mirror() {
	local mirror="$1"
	echo "$mirror/$release/main" >> $APKREPOS_PATH
	echo "#$mirror/$release/testing" >> $APKREPOS_PATH
	echo "" >> $APKREPOS_PATH
	echo "Added mirror $(get_hostname_from_url $mirror)"
}
Matt Smith's avatar
Matt Smith committed
124

125 126 127
add_from_url() {
	echo "$1" >> $APKREPOS_PATH
	echo "" >> $APKREPOS_PATH
Matt Smith's avatar
Matt Smith committed
128 129
}

130 131 132 133 134 135
edit_repositories() {
	local md5=$(md5sum $APKREPOS_PATH)
	${EDITOR:-vi} "$APKREPOS_PATH"
	# return true if file changed
	test "$(md5sum $APKREPOS_PATH)" != "$md5"
}
Matt Smith's avatar
Matt Smith committed
136 137

# main
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
get_alpine_release
default_answer=f
changed=
while true; 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=1;;
		/*|http://*|ftp://*|https://*) add_from_url "$answer" && changed=1;;
		r) add_random_mirror && changed=1;;
		f) add_fastest_mirror && changed=1;;
		e) edit_repositories && changed=1 && break;;
	esac
	if [ -n "$changed" ]; then
		default_answer="done"
	fi
done
Matt Smith's avatar
Matt Smith committed
158

159 160 161
if [ -n "$changed" ]; then
	echo -n "Updating repository indexes... "
	apk update -q && echo "done."
162
fi