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

PROGRAM=setup-apkrepos
PREFIX=

. $PREFIX/lib/libalpine.sh

8 9 10
if [ "$ROOT" != "/" ]; then
	apk_root_opt="--root $ROOT"
fi
Matt Smith's avatar
Matt Smith committed
11

12 13 14 15 16
get_hostname_from_url() {
	local n=${1#*://}
	echo ${n%%/*}
}

Matt Smith's avatar
Matt Smith committed
17
prompt_setup_method() {
18 19
	echo "r) Add random from the above list"
	echo "f) Detect and add fastest mirror from above list"
20
	echo "e) Edit "${ROOT}"etc/apk/repositores with text editor"
21 22
	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
23 24
}

25
add_random_mirror() {
Matt Smith's avatar
Matt Smith committed
26
	local i=0
27
	local random_mirror_index=$(( $RANDOM % $mirror_count ))
28

Matt Smith's avatar
Matt Smith committed
29 30 31 32 33
	echo -n "Picking random mirror... "
	for mirror in $MIRRORS; do
		if [ $i -eq $random_mirror_index ]; then
			break
		fi
34
		i=$(( $i + 1 ))
Matt Smith's avatar
Matt Smith committed
35
	done
36
	add_mirror $mirror
Matt Smith's avatar
Matt Smith committed
37 38
}

39 40
time_cmd() {
	local start=$(cut -d ' ' -f1 /proc/uptime)
41
	$@ >&2 || return
42 43 44
	awk -v start=$start -v end=$(cut -d ' ' -f1 /proc/uptime) \
		'BEGIN {print end - start; exit}'
}
Matt Smith's avatar
Matt Smith committed
45

46 47 48
find_fastest_mirror() {
	export http_proxy=
	local url=
49
	local arch=$(apk --print-arch)
50
	for url in $MIRRORS; do
51 52 53 54
		# warm up the dns cache
		nslookup $(get_hostname_from_url $url) >/dev/null 2>&1
		local time=$(time_cmd wget -q -s -T 5 \
			${url%/}/edge/main/$arch/APKINDEX.tar.gz)
55 56 57
		if [ -n "$time" ]; then
			echo "$time $url"
		fi
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	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"
Matt Smith's avatar
Matt Smith committed
77 78
}

79
# show mirrors and store how many in global mirror_count
80
show_mirror_list() {
81 82 83
	local mirror i=0
	mirror_count=0
	[ -z "$MIRRORS" ] && return
84
	echo ""
85
	echo "Available mirrors:"
86 87
	for mirror in $MIRRORS; do
		i=$(($i + 1))
88
		echo "$i) $(get_hostname_from_url $mirror)"
89
	done
90 91
	echo ""
	mirror_count=$i
92 93
}

94 95 96 97 98
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
99
	set $MIRRORS
100
	eval "mirror=\$$mirror_index"
101
	add_mirror "$mirror"
Matt Smith's avatar
Matt Smith committed
102 103 104
}

get_alpine_release() {
105
	local version=$(cat "${ROOT}"etc/alpine-release 2>/dev/null)
106
	case "$version" in
107
		*_git*|*_alpha*) release="edge";;
108 109
		[0-9]*.[0-9]*.[0-9]*)
			# release in x.y.z format, cut last digit
110 111 112
			release=v${version%.[0-9]*};;
		*)	# fallback to edge
			release="edge";;
113
	esac
Matt Smith's avatar
Matt Smith committed
114
}
115

116 117
add_mirror() {
	local mirror="$1"
118
	mkdir -p "${APKREPOS_PATH%/*}"
119
	echo "${mirror%/}/${release}/main" >> $APKREPOS_PATH
120 121 122 123 124
	case "$release" in
	v[0-9]*)
		echo "#${mirror%/}/edge/main" >> $APKREPOS_PATH;;
	esac
	echo "#${mirror%/}/edge/testing" >> $APKREPOS_PATH
125 126 127
	echo "" >> $APKREPOS_PATH
	echo "Added mirror $(get_hostname_from_url $mirror)"
}
Matt Smith's avatar
Matt Smith committed
128

129
add_from_url() {
130
	mkdir -p "${APKREPOS_PATH%/*}"
131 132
	echo "$1" >> $APKREPOS_PATH
	echo "" >> $APKREPOS_PATH
Matt Smith's avatar
Matt Smith committed
133 134
}

135
edit_repositories() {
136 137
	local md5=$(md5sum $APKREPOS_PATH 2>/dev/null)
	mkdir -p "${APKREPOS_PATH%/*}"
138 139
	${EDITOR:-vi} "$APKREPOS_PATH"
	# return true if file changed
140
	test "$(md5sum $APKREPOS_PATH 2>/dev/null)" != "$md5"
141
}
Matt Smith's avatar
Matt Smith committed
142

143 144
usage() {
	cat <<__EOF__
145
usage: setup-apkrepos [-fhr] [REPO...]
146 147 148 149

Setup apk repositories

options:
150
 -f  Detect and add fastest mirror
151 152 153 154 155 156 157
 -h  Show this help
 -r  Add a random mirror and do not prompt
__EOF__
	exit 1

}

158
while getopts "fhr" opt; do
159
        case $opt in
160
                f) JUSTADDFASTEST=1;;
161 162 163 164
                h) usage;;
                r) JUSTADDRANDOM=1;;
        esac
done
165
shift $(($OPTIND - 1))
166

Matt Smith's avatar
Matt Smith committed
167
# main
168 169
# install alpine-mirrors if its not already there
to_uninstall=
170
if ! apk info --quiet --installed alpine-mirrors; then
171
	apk add --force --quiet --virtual .setup-apkrepos alpine-mirrors
172
	to_uninstall=.setup-apkrepos
173 174 175 176 177 178 179
fi

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

180
APKREPOS_PATH="${ROOT}"etc/apk/repositories
181 182 183 184
if [ -r "$APKREPOS_PATH" ]; then
	APKREPOS=`cat "$APKREPOS_PATH"`
fi

185 186
get_alpine_release
default_answer=f
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
changed=false

if [ $# -gt 0 ]; then
	# replace the apk repos with the specified ones
	rm -f "$APKREPOS_PATH"
	for i; do
		echo "$i" >> "$APKREPOS_PATH"
	done
	changed=true
fi

if [ -n "$JUSTADDRANDOM" ]; then
	show_mirror_list > /dev/null
	add_random_mirror && changed=true
fi

203 204 205 206 207
if [ -n "$JUSTADDFASTEST" ]; then
	show_mirror_list > /dev/null
	add_fastest_mirror && changed=true
fi

208 209 210 211 212 213 214 215 216 217 218 219 220 221
while ! $changed; 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=true;;
		/*|http://*|ftp://*|https://*) add_from_url "$answer" \
			&& changed=true;;
		r) add_random_mirror && changed=true;;
		f) add_fastest_mirror && changed=true;;
		e) edit_repositories && changed=true;;
	esac
222
done
Matt Smith's avatar
Matt Smith committed
223

224
if $changed; then
225
	echo -n "Updating repository indexes... "
226
	apk update --quiet $apk_root_opt && echo "done."
227
fi
228 229 230

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