setup-apkrepos.in 7.9 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
Natanael Copa's avatar
Natanael Copa committed
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
Natanael Copa's avatar
Natanael Copa committed
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
Natanael Copa's avatar
Natanael Copa committed
19
20
21
if [ -r "$APKREPOS_PATH" ]; then
	APKREPOS=`cat "$APKREPOS_PATH"`
fi
Matt Smith's avatar
Matt Smith committed
22

Natanael Copa's avatar
Natanael Copa committed
23
24
25
26
27
get_hostname_from_url() {
	local n=${1#*://}
	echo ${n%%/*}
}

28
29
30
31
count() {
	echo $1 | sed 's/ /\n/g' | wc -l
}

Matt Smith's avatar
Matt Smith committed
32
33
prompt_setup_method() {
	while true; do
34
35
36
37
38
39
40
41
		echo "How would you like to set up internet apk repositories?"
		echo -e "r) Automatically pick a random mirror"
		echo -e "f) Automatically pick the fastest mirror (must be connected)"
		echo -e "l) Manually pick mirror from list"
		echo -e "e) Manually enter local/remote mirror(s)"
		echo -e "n) Do not configure internet apk repositories at all"
		echo -en "\nPlease select how to setup APK repositories (r/f/l/e/n) [n]: "
		default_read setup_method n
Matt Smith's avatar
Matt Smith committed
42
		case "$setup_method" in
43
			r|f|l|e|n) break;;
Matt Smith's avatar
Matt Smith committed
44
45
46
47
48
49
		esac
	done
}

get_random_mirror() {
	local i=0
50
	local mirror_count=$(count $MIRRORS)
51
	local random_mirror_index=$(( $RANDOM % $mirror_count ))
Matt Smith's avatar
Matt Smith committed
52
53
54
55
56
57
58
	local random_mirror_hostname
	
	echo -n "Picking random mirror... "
	for mirror in $MIRRORS; do
		if [ $i -eq $random_mirror_index ]; then
			break
		fi
59
		i=$(( $i + 1 ))
Matt Smith's avatar
Matt Smith committed
60
61
	done

Natanael Copa's avatar
Natanael Copa committed
62
	echo "using $(get_hostname_from_url $mirror)"
Matt Smith's avatar
Matt Smith committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
}

get_fastest_mirror() {
	local mirror_hostname
	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_hostname
	local mirror_lowest_rtt_mirror

	echo -n "Finding fastest mirror... "
	mirror_lowest_rtt=-1
	for mirror in $MIRRORS; do
		mirror_hostname=`echo $mirror | awk -F '://' '{print $2}' | awk -F '/' '{print $1}'`
Jeff Bilyk's avatar
Jeff Bilyk committed
79
80
81
82
83
84
85
86
87
88
		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_hostname=$mirror_hostname
				mirror_lowest_rtt_mirror=$mirror
			else
				if [ $tmp_mirror_rtt -lt $mirror_lowest_rtt ]; then
89
90
					mirror_lowest_rtt=$tmp_mirror_rtt
					mirror_lowest_rtt_hostname=$mirror_hostname
Matt Smith's avatar
Matt Smith committed
91
					mirror_lowest_rtt_mirror=$mirror
92
93
94
				fi
			fi
		fi
Matt Smith's avatar
Matt Smith committed
95
96
97
98
99
100
101
102
103
	done
	echo "using $mirror_lowest_rtt_hostname."
	mirror=$mirror_lowest_rtt_mirror
}

prompt_mirror_list() {
	local i
	local mirror_hostname
	local mirror_index
104
	local mirror_count=$(count $MIRRORS)
Matt Smith's avatar
Matt Smith committed
105
106

	while true; do
Natanael Copa's avatar
Natanael Copa committed
107
		echo ""
Matt Smith's avatar
Matt Smith committed
108
109
		i=1
		for mirror in $MIRRORS; do
Natanael Copa's avatar
Natanael Copa committed
110
111
			echo "$i) $(get_hostname_from_url $mirror)"
			i=$(($i + 1))
Matt Smith's avatar
Matt Smith committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
		done

		echo -en "\nPlease select a mirror from the above list: "
		read mirror_index
		case $mirror_index in
			[1-9]|[1-9][0-9])
				if [ $mirror_index -ge 1 -a $mirror_index -le $mirror_count ]; then
					break
				fi
				;;
		esac
	done

	set $MIRRORS
Natanael Copa's avatar
Natanael Copa committed
126
127
	eval "mirror=\$$mirror_index"
	echo "Mirror chosen: $(get_hostname_from_url $mirror)"
Matt Smith's avatar
Matt Smith committed
128
129
130
131
132
133
134
135
136
}

add_repo() {
	local new_repo=$1

	if [ ${#new_repos} -eq 0 ]; then
		new_repos="$new_repo"
	else
		new_repos=`echo -e "$new_repos\n$new_repo"`
137
	fi
Matt Smith's avatar
Matt Smith committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
}

prompt_custom_mirror() {
	local start_fresh=$1
	local called_by_self=${2-0}
	local new_repo
	local add_another

	while true; do
		echo -e "\nPlease enter a local path or URL to the mirror:"
		read new_repo
		case "$new_repo" in
			/*|http://*|ftp://*|https://*)
				# Allow for local paths and http/ftp/https URLs
				break;;
		esac
	done

	add_repo "$new_repo"
157

Matt Smith's avatar
Matt Smith committed
158
159
160
161
162
163
164
165
	while true; do
		echo -en "\nDo you want to add another mirror? [y/n]: "
		read add_another
		case "$add_another" in
			[yY]) prompt_custom_mirror 0 1; break;;
			[nN]) break;;
		esac
	done
166

Matt Smith's avatar
Matt Smith committed
167
168
169
170
171
172
173
174
	if [ $called_by_self -eq 0 ]; then
		echo -n "Updating ${APKREPOS_PATH}... "
		if [ $start_fresh -eq 1 ]; then
			echo "$new_repos" > "$APKREPOS_PATH" || die "failed."
		elif [ $start_fresh -eq 0 ]; then
			echo "$new_repos" >> "$APKREPOS_PATH" || die "failed."
		fi
		echo "done."
175
	fi
Matt Smith's avatar
Matt Smith committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
}

prompt_clear_repositories() {
	local start_fresh

	while true; do
		echo -en "\nBefore adding custom mirrors, do you want to clear\n"
		echo -n "${APKREPOS_PATH} and start fresh? [y/n]: "
		read start_fresh
		case "$start_fresh" in
			[yY]) start_fresh=1; break;;
			[nN]) start_fresh=0; break;;
		esac
	done

	prompt_custom_mirror $start_fresh
}

prompt_alpine_release() {
	local i
	local release_index
197
	local release_count=$(count $RELEASES)
Matt Smith's avatar
Matt Smith committed
198
199
200
201
202
203

	while true; do
		echo -en "\n"
		i=1
		for _release in $RELEASES; do
			echo "$i) $_release"
204
			i=$(( $i + 1 ))
Matt Smith's avatar
Matt Smith committed
205
		done
206

Matt Smith's avatar
Matt Smith committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
		echo -n "Please select a release from the above list: "
		read release_index
		case "$release_index" in
			[1-9]|[1-9][0-9])
				if [ $release_index -ge 1 -a $release_index -le $release_count ]; then
					break
				fi
				;;
		esac
	done

	set $RELEASES
	_tmp="\$$release_index"
	release=`eval echo $_tmp`

	echo "Release chosen: $release"
}

get_alpine_release() {
	local change_release

	release=`apk version "alpine-base" 2>/dev/null | sed -r -e '/alpine-base/!d' -e 's/alpine-base-//' -e 's/^([^ 	]+)-r[0-9]+[ 	]*=.*$/\1/'`
229
	case "$release" in
Matt Smith's avatar
Matt Smith committed
230
		*_git*) release="edge";;
231
232
		[0-9]*.[0-9]*.[0-9]*)
			# release in x.y.z format, cut last digit
Matt Smith's avatar
Matt Smith committed
233
			release=v${release%.[0-9]*};;
234
235
	esac
	while true; do
Matt Smith's avatar
Matt Smith committed
236
237
238
239
240
241
242
243
		echo -en "\nSystem release is: $release.  Change? (i.e., to upgrade) [y/N]: "
		default_read change_release "n"
		case "$change_release" in
			[nN]) break;;
			[yY]) prompt_alpine_release; break;;
		esac
	done
}
244

Matt Smith's avatar
Matt Smith committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
update_apk_repositories() {
	new_repos=
	local apkrepo
	local new_repo
	local main_repo_exists=0
	local testing_repo_exists=0
	local enable_testing

	# Prompt user to enable the testing repo.
	while true; do
		echo -en "\nDo you want to enable the testing repository? [y/N]: "
		default_read enable_testing "n"
		case "$enable_testing" in
			[yY]) enable_testing=1; break;;
			[nN]) enable_testing=0; break;;
		esac
261
262
	done

Matt Smith's avatar
Matt Smith committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
	# Update existing repositories to the new mirror selection
	echo -n "Updating ${APKREPOS_PATH}... "
	for apkrepo in $APKREPOS; do
		case "$apkrepo" in
			*/alpine/*/main*)
				new_repo="${mirror}${release}/main"
				main_repo_exists=1
				;;
			*/alpine/*/testing*)
				if [ $enable_testing -eq 1 ]; then
					new_repo="${mirror}${release}/testing"
				else
					new_repo="#${mirror}${release}/testing"
				fi
				testing_repo_exists=1
				;;
			*)
				new_repo="$apkrepo"
				;;
		esac
283

Matt Smith's avatar
Matt Smith committed
284
285
		add_repo "$new_repo"
	done
286

Matt Smith's avatar
Matt Smith committed
287
288
289
	# Add main repo if not found
	if [ $main_repo_exists -eq 0 ]; then
		add_repo "${mirror}${release}/main"
290
291
	fi

Matt Smith's avatar
Matt Smith committed
292
293
294
295
296
297
298
	# Add testing repo if not found
	if [ $testing_repo_exists -eq 0 ]; then
		if [ $enable_testing -eq 1 ]; then
			add_repo "${mirror}${release}/testing"
		else
			add_repo "#${mirror}${release}/testing"
		fi
299
	fi
Matt Smith's avatar
Matt Smith committed
300
301
302

	# Update repositories file
	echo "$new_repos" > "$APKREPOS_PATH" || die "failed."
303
	echo "done."
Matt Smith's avatar
Matt Smith committed
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
}

prompt_update_apk() {
	while true; do
		echo -e "\nDo you want to update repository indexes?"
		echo -n "(You must be connected when using remote repositories.) [y/N]: "
		default_read update_repo_indexes "n"
		case "$update_repo_indexes" in
			[yY])
				echo -n "Updating repository indexes... "
				local apkupdate_errors=`apk update 2>&1 | grep -E "^(WARNING|ERROR)" | sed -r "s/(WARNING|ERROR)/\n\1/g"`
				if [ ${#apkupdate_errors} -gt 0 ]; then
					echo "$apkupdate_errors"
				else
					echo "done."
				fi
				break
			;;
			[nN]) break;;
		esac
	done
}

# main
prompt_setup_method

330
331
332
333
334
335
case "$setup_method" in
	r) get_random_mirror;;
	f) get_fastest_mirror;;
	l) prompt_mirror_list;;
	e) prompt_clear_repositories;;
esac
Matt Smith's avatar
Matt Smith committed
336

Natanael Copa's avatar
Natanael Copa committed
337
if [ "$setup_method" != "e" ]; then
Matt Smith's avatar
Matt Smith committed
338
339
340
341
342
	# It doesn't make sense to run get_alpine_release for custom_mirror
	get_alpine_release

	# prompt_custom_mirror() will update the apk repositories file instead
	update_apk_repositories
343
fi
Matt Smith's avatar
Matt Smith committed
344
345

prompt_update_apk