-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathsetup-apkrepos.in
309 lines (270 loc) · 6.82 KB
/
setup-apkrepos.in
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/bin/sh
PROGRAM=setup-apkrepos
PREFIX=@PREFIX@
: ${LIBDIR=$PREFIX/lib}
. "$LIBDIR/libalpine.sh"
: ${MIRRORS_URL:=https://mirrors.alpinelinux.org/mirrors.txt}
if [ "$ROOT" != "/" ]; then
apk_root_opt="--root $ROOT"
fi
get_hostname_from_url() {
local n="${1#*://}"
echo ${n%%/*}
}
get_mirror_count() {
set -- $MIRRORS
echo $#
}
ask_setup_method() {
local cstate="enable"
if [ -z "$community_prefix" ]; then
cstate="disable"
fi
cat <<-__EOF__
(f) Find and use fastest mirror
(s) Show mirrorlist
(r) Use random mirror
(e) Edit ${ROOT}etc/apk/repositories with text editor
(c) Community repo $cstate
(skip) Skip setting up apk repositories
__EOF__
ask "Enter mirror number or URL:" $1
printf "\n"
}
add_random_mirror() {
local i=0
local count=$(get_mirror_count)
if [ ${count:-0} -eq 0 ]; then
echo "Warning! no mirror found" >&2
return 1
fi
local random_mirror_index="$(awk -v count=$count 'BEGIN {srand(); printf("%.0f", rand() * count)}')"
printf %s "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 || return
awk -v start=$start -v end=$(cut -d ' ' -f1 /proc/uptime) \
'BEGIN {print end - start; exit}'
}
find_fastest_mirror() {
local url=
local arch="$(apk --print-arch)"
for url in $MIRRORS; do
# warm up the dns cache
$MOCK nslookup $(get_hostname_from_url $url) >/dev/null 2>&1
local time="$(time_cmd wget --spider -q -T 5 -t 1 \
${url%/}/edge/main/$arch/APKINDEX.tar.gz)"
if [ -n "$time" ]; then
echo "$time $url"
fi
done | tee /dev/stderr | sort -nk1,1 | head -n1 | cut -d' ' -f2
}
add_fastest_mirror() {
echo "Finding fastest mirror... "
local fastest="$(find_fastest_mirror)"
if [ -z "$fastest" ]; then
echo "Warning! No mirror found" >&2
return 1
fi
add_mirror "$fastest"
}
# For every main/ repo, enable corresponding community/ repo
add_community_mirrors() {
for repo in $(grep '^[^#].*/main$' "$APKREPOS_PATH" 2>/dev/null); do
crepo="${repo%%/main}/community"
if ! grep -qx -- "$crepo" "$APKREPOS_PATH"; then
echo ">>> Enabling repository $crepo"
echo "$crepo" >> "$APKREPOS_PATH"
fi
done
}
# show mirrors
show_mirror_list() {
local mirror i=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 ""
}
add_from_list() {
local mirror_index="$1"
if [ $mirror_index -lt 1 ] || [ $mirror_index -gt $(get_mirror_count) ]; then
return 1
fi
set $MIRRORS
eval "mirror=\${$mirror_index}"
add_mirror "$mirror"
}
get_alpine_release() {
# use the main version already configured, or get the version from /etc/alpine-release
local version="$(grep -Eom1 '[^/]+/main/?$' "${ROOT}"etc/apk/repositories 2>/dev/null | grep -Eo '^[^/]+' \
|| cat "${ROOT}"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]*};;
v[0-9]*.[0-9]*)
# release in vx.y format, keep as is
release="${version}";;
*) # fallback to edge
release="edge";;
esac
}
add_mirror() {
local mirror="$1"
mkdir -p "${APKREPOS_PATH%/*}"
echo "${mirror%/}/${release}/main" >> $APKREPOS_PATH
echo "${community_prefix}${mirror%/}/${release}/community" >> $APKREPOS_PATH
echo "Added mirror $(get_hostname_from_url $mirror)"
}
add_from_url() {
mkdir -p "${APKREPOS_PATH%/*}"
echo "$1" >> $APKREPOS_PATH
echo "" >> $APKREPOS_PATH
}
edit_repositories() {
local md5="$(md5sum $APKREPOS_PATH 2>/dev/null)"
mkdir -p "${APKREPOS_PATH%/*}"
${EDITOR:-vi} "$APKREPOS_PATH"
# return true if file changed
test "$(md5sum $APKREPOS_PATH 2>/dev/null)" != "$md5"
}
usage() {
cat <<-__EOF__
usage: setup-apkrepos [-ch] [-f|-r|-1|REPO...]
setup-apkrepos -o
Setup apk repositories
options:
-c Enable the community repo
-f Detect and add fastest mirror
-h Show this help
-o Only enable a community repo for every defined repo
-r Add a random mirror and do not prompt
-1 Add first mirror on the list (normally a CDN)
Option -o cannot be used with options -c, -f, -r or -1.
__EOF__
exit $1
}
community_prefix="#"
add_fastest=false
add_first=false
add_random=false
add_community_repos=false
while getopts "c1fhor" opt; do
case $opt in
c) community_prefix=""
if $add_community_repos; then
usage "1" >&2
fi
;;
f) add_fastest=true
if $add_first || $add_random || $add_community_repos; then
usage "1" >&2
fi
;;
1) add_first=true
if $add_fastest || $add_random || $add_community_repos; then
usage "1" >&2
fi
;;
h) usage 0;;
o) add_community_repos=true
if $add_first || $add_fastest || $add_random \
|| [ $community_prefix != "#" ]; then
usage "1" >&2
fi
;;
r) add_random=true
if $add_first || $add_fastest || $add_community_repos; then
usage "1" >&2
fi
;;
'?') usage "1" >&2;;
esac
done
shift $(($OPTIND - 1))
if [ -z "$MIRRORS" ]; then
MIRRORS=$(wget -qO- $MIRRORS_URL)
fi
APKREPOS_PATH="${ROOT}"etc/apk/repositories
if [ -r "$APKREPOS_PATH" ]; then
APKREPOS=$(cat "$APKREPOS_PATH")
fi
get_alpine_release
changed=false
if [ $# -gt 0 ] && ! $add_community_repos; then
# replace the apk repos with the specified ones
rm -f "$APKREPOS_PATH"
mkdir -p "${APKREPOS_PATH%/*}"
for i; do
echo "$i" >> "$APKREPOS_PATH" && changed=true
done
fi
if $add_first; then
set -- $MIRRORS
if [ $# -eq 0 ]; then
echo "Warning! No mirror found" >&2
exit 1
fi
add_mirror "$1" || exit
changed=true
fi
if $add_random; then
add_random_mirror || exit
changed=true
fi
if $add_fastest; then
add_fastest_mirror || exit
changed=true
fi
if $add_community_repos; then
add_community_mirrors || exit
changed=true
fi
if $add_first || $add_random || $add_fastest || $add_community_repos; then
interactive=false
else
interactive=true
fi
while $interactive && ! $changed; do
if [ -z "$MIRRORS" ]; then
MIRRORS=$(wget -qO- $MIRRORS_URL)
fi
ask_setup_method ${APKREPO_DEFAULT_ANSWER:-1}
case "$resp" in
skip) break;;
[0-9]*) add_from_list $resp && changed=true;;
/*|http://*|ftp://*|https://*) add_from_url "$resp" \
&& changed=true;;
s) show_mirror_list | more;;
r|"r c") add_random_mirror && changed=true;;
f|"f c") add_fastest_mirror && changed=true;;
e) edit_repositories && changed=true;;
c) case "$community_prefix" in
"#") community_prefix=""; cstate="enabled";;
"") community_prefix="#"; cstate="disabled";;
esac
printf "Community repository %s\n\n" "$cstate"
;;
esac
case "$resp" in
*c) community_prefix=""; printf "Community repository enabled\n";;
esac
done
if $changed; then
printf %s "Updating repository indexes... "
apk update --quiet $apk_root_opt && echo "done."
fi