setup-apkrepos.in 4.49 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

Natanael Copa's avatar
Natanael Copa committed
9
10
11
12
13
get_hostname_from_url() {
	local n=${1#*://}
	echo ${n%%/*}
}

Matt Smith's avatar
Matt Smith committed
14
prompt_setup_method() {
15
16
17
18
19
	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
20
21
}

22
add_random_mirror() {
Matt Smith's avatar
Matt Smith committed
23
	local i=0
24
	local random_mirror_index=$(( $RANDOM % $mirror_count ))
Matt Smith's avatar
Matt Smith committed
25
26
27
28
29
30
	
	echo -n "Picking random mirror... "
	for mirror in $MIRRORS; do
		if [ $i -eq $random_mirror_index ]; then
			break
		fi
31
		i=$(( $i + 1 ))
Matt Smith's avatar
Matt Smith committed
32
	done
33
	add_mirror $mirror
Matt Smith's avatar
Matt Smith committed
34
35
}

36
add_fastest_mirror() {
Matt Smith's avatar
Matt Smith committed
37
38
39
40
41
42
43
44
45
46
	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
47
48
49
50
51
52
53
54
55
		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
56
					mirror_lowest_rtt=$tmp_mirror_rtt
Matt Smith's avatar
Matt Smith committed
57
					mirror_lowest_rtt_mirror=$mirror
58
59
60
				fi
			fi
		fi
Matt Smith's avatar
Matt Smith committed
61
62
	done
	mirror=$mirror_lowest_rtt_mirror
63
64
65
66
	if [ -z "$mirror" ]; then
		eecho "Failed to ping any mirror"
		return 1
	fi
67
	add_mirror "$mirror"
Matt Smith's avatar
Matt Smith committed
68
69
}

70
# show mirrors and store how many in global mirror_count
71
show_mirror_list() {
72
73
74
	local mirror i=0
	mirror_count=0
	[ -z "$MIRRORS" ] && return
75
	echo ""
76
	echo "Available mirrors:"
77
78
	for mirror in $MIRRORS; do
		i=$(($i + 1))
79
		echo "$i) $(get_hostname_from_url $mirror)"
80
	done
81
82
	echo ""
	mirror_count=$i
83
84
}

85
86
87
88
89
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
90
	set $MIRRORS
Natanael Copa's avatar
Natanael Copa committed
91
	eval "mirror=\$$mirror_index"
92
	add_mirror "$mirror"
Matt Smith's avatar
Matt Smith committed
93
94
95
}

get_alpine_release() {
96
97
	local version=$(cat /etc/alpine-release 2>/dev/null)
	case "$version" in
Matt Smith's avatar
Matt Smith committed
98
		*_git*) release="edge";;
99
100
		[0-9]*.[0-9]*.[0-9]*)
			# release in x.y.z format, cut last digit
101
102
103
			release=v${version%.[0-9]*};;
		*)	# fallback to edge
			release="edge";;
104
	esac
Matt Smith's avatar
Matt Smith committed
105
}
106

107
108
add_mirror() {
	local mirror="$1"
109
110
	echo "${mirror%/}/${release}/main" >> $APKREPOS_PATH
	echo "#${mirror%/}/${release}/testing" >> $APKREPOS_PATH
111
112
113
	echo "" >> $APKREPOS_PATH
	echo "Added mirror $(get_hostname_from_url $mirror)"
}
Matt Smith's avatar
Matt Smith committed
114

115
116
117
add_from_url() {
	echo "$1" >> $APKREPOS_PATH
	echo "" >> $APKREPOS_PATH
Matt Smith's avatar
Matt Smith committed
118
119
}

120
121
122
123
124
125
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
126

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
usage() {
	cat <<__EOF__
usage: setup-apkrepos [-hr]

Setup apk repositories

options:
 -h  Show this help
 -r  Add a random mirror and do not prompt
__EOF__
	exit 1

}

while getopts "hr" opt; do
        case $opt in
                h) usage;;
                r) JUSTADDRANDOM=1;;
        esac
done


Matt Smith's avatar
Matt Smith committed
149
# main
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# install alpine-mirrors if its not already there
to_uninstall=
if ! apk info -q -e alpine-mirrors; then
	apk add -q alpine-mirrors
	to_uninstall=alpine-mirrors
fi

MIRRORS_PATH=/usr/share/alpine-mirrors/MIRRORS.txt
if [ -z "$MIRRORS" ] && [ -r "$MIRRORS_PATH" ]; then
	MIRRORS=`cat $MIRRORS_PATH`
fi

RELEASES_PATH=/usr/share/alpine-mirrors/RELEASES.txt
if [ -z "$RELEASES" ] && [ -r "$RELEASES_PATH" ]; then
	RELEASES=`cat $RELEASES_PATH`
fi

APKREPOS_PATH=/etc/apk/repositories
if [ -r "$APKREPOS_PATH" ]; then
	APKREPOS=`cat "$APKREPOS_PATH"`
fi

172
173


174
175
176
177
get_alpine_release
default_answer=f
changed=
while true; do
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
	if [ -n "$JUSTADDRANDOM" ]; then
		show_mirror_list > /dev/null
		add_random_mirror && changed=1 && break
	else
		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
195
			break
196
		fi
197
198
	fi
done
Matt Smith's avatar
Matt Smith committed
199

200
201
202
if [ -n "$changed" ]; then
	echo -n "Updating repository indexes... "
	apk update -q && echo "done."
203
fi
204
205
206
207
208

# clean up
if [ -n "$to_uninstall" ]; then
	apk del -q alpine-mirrors
fi