aboutsummaryrefslogtreecommitdiffstats
path: root/setup-apkrepos.in
blob: 8cb940f331231521d12b81333cfe438c2f9701d9 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/bin/sh

PROGRAM=setup-apkrepos
PREFIX=

. $PREFIX/lib/libalpine.sh


get_hostname_from_url() {
	local n=${1#*://}
	echo ${n%%/*}
}

prompt_setup_method() {
	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]: "
}

add_random_mirror() {
	local i=0
	local random_mirror_index=$(( $RANDOM % $mirror_count ))
	
	echo -n "Picking random mirror... "
	for mirror in $MIRRORS; do
		if [ $i -eq $random_mirror_index ]; then
			break
		fi
		i=$(( $i + 1 ))
	done
	add_mirror $mirror
}

time_cmd() {
	local start=$(cut -d ' ' -f1 /proc/uptime)
	$@ >&2
	awk -v start=$start -v end=$(cut -d ' ' -f1 /proc/uptime) \
		'BEGIN {print end - start; exit}'
}

find_fastest_mirror() {
	export http_proxy=
	local url=
	for url in $MIRRORS; do
		echo $(time_cmd apk update --quiet \
				--repository $url/edge/main \
				--repositories-file /dev/null) $url
	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"
}

# show mirrors and store how many in global mirror_count
show_mirror_list() {
	local mirror i=0
	mirror_count=0
	[ -z "$MIRRORS" ] && return
	echo ""
	echo "Available mirrors:"
	for mirror in $MIRRORS; do
		i=$(($i + 1))
		echo "$i) $(get_hostname_from_url $mirror)"
	done
	echo ""
	mirror_count=$i
}

add_from_list() {
	local mirror_index=$1
	if [ $mirror_index -lt 1 ] || [ $mirror_index -gt $mirror_count ]; then
		return 1
	fi
	set $MIRRORS
	eval "mirror=\$$mirror_index"
	add_mirror "$mirror"
}

get_alpine_release() {
	local version=$(cat /etc/alpine-release 2>/dev/null)
	case "$version" in
		*_git*|*_alpha*) release="edge";;
		[0-9]*.[0-9]*.[0-9]*)
			# release in x.y.z format, cut last digit
			release=v${version%.[0-9]*};;
		*)	# fallback to edge
			release="edge";;
	esac
}

add_mirror() {
	local mirror="$1"
	echo "${mirror%/}/${release}/main" >> $APKREPOS_PATH
	case "$release" in
	v[0-9]*)
		echo "#${mirror%/}/edge/main" >> $APKREPOS_PATH;;
	esac
	echo "#${mirror%/}/edge/testing" >> $APKREPOS_PATH
	echo "" >> $APKREPOS_PATH
	echo "Added mirror $(get_hostname_from_url $mirror)"
}

add_from_url() {
	echo "$1" >> $APKREPOS_PATH
	echo "" >> $APKREPOS_PATH
}

edit_repositories() {
	local md5=$(md5sum $APKREPOS_PATH)
	${EDITOR:-vi} "$APKREPOS_PATH"
	# return true if file changed
	test "$(md5sum $APKREPOS_PATH)" != "$md5"
}

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


# main
# install alpine-mirrors if its not already there
to_uninstall=
if ! apk info --quiet --installed alpine-mirrors; then
	apk add --quiet 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

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



get_alpine_release
default_answer=f
changed=
while true; do
	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
			break
		fi
	fi
done

if [ -n "$changed" ]; then
	echo -n "Updating repository indexes... "
	apk update --quiet && echo "done."
fi

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