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

PROGRAM=setup-apkrepos
PREFIX=

. $PREFIX/lib/libalpine.sh

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
echo -n "Fetching mirrors list... "
MIRRORS=`wget -qO - http://www.alpinelinux.org/alpine/MIRRORS.txt 2>&1 || (echo "failed." && exit 1)`
echo "done."

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}'`
	tmp_mirror_nslookup=`nslookup $mirror_hostname | grep "Non-authoritative answer"`
	if [ ${#tmp_mirror_nslookup} -gt 0 ]; then
		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
			else
				if [ $tmp_mirror_rtt -lt $mirror_lowest_rtt ]; then
					mirror_lowest_rtt=$tmp_mirror_rtt
					mirror_lowest_rtt_hostname=$mirror_hostname
				fi
			fi
		fi
	fi
done
echo "done."

echo "Selected mirror: $mirror_lowest_rtt_hostname"

# Get URL for lowest-RTT mirror (will be $mirror)
for mirror in $MIRRORS; do
	tmp_mirror_lowest_rtt_url="`echo $mirror | grep $mirror_lowest_rtt_hostname`"
	if [ ${#tmp_mirror_lowest_rtt_url} -gt 0 ]; then
		break
	fi
done
46 47 48 49 50 51 52 53 54 55

# Retrieve version from alpine-release if available
if [ -e /etc/alpine-release ]; then
	release=$(cut -f1 /etc/alpine-release)
	case "$release" in
		*_git*) repo="edge";;
		[0-9]*.[0-9]*.[0-9]*)
			# release in x.y.z format, cut last digit
			repo=v${release%.[0-9]*};;
	esac
56
	echo "Using current repository: $repo"
57 58 59 60
else
	while true; do
		printf "Enter repository branch from list below [v2.2]: \n"
		# Retrieve list of versions available on mirror 2.0 and above
61
		wget "$mirror" > /dev/null 2>&1
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
		availablerepos=""
		for version in `links ./index.html -dump | grep "v[2-9]" | awk -F '/' '{print $1}'`; do

			availablerepos="$availablerepos $version"
		done
		rm index.html
		availablerepos="$availablerepos edge"
		for i in `echo $availablerepos`; do
			printf "$i\n"
		done
		default_read repo "v2.2"
		if [ $repo ]; then
			break
		fi
	done
77
fi
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
echo -n "Updating /etc/apk/repositories... "
APKREPOS=`cat /etc/apk/repositories`
new_repos=
main_repo_exists=0
testing_repo_exists=0
# Update existing repositories to the new mirror selection
for apkrepo in $APKREPOS; do
	case "$apkrepo" in
		*${repo}/main*)
			new_repo="${mirror}${repo}/main"
			main_repo_exists=1
			;;
		*${repo}/testing*)
			new_repo="${mirror}${repo}/testing"
			testing_repo_exists=1
			;;
		*)
			new_repo="$apkrepo"
			;;
	esac

	if [ ${#new_repos} -eq 0 ]; then
		new_repos="$new_repo"
	else
		new_repos=`echo -e "$new_repos\n$new_repo"`
	fi
done
# Add main repo if not found
if [ $main_repo_exists -eq 0 ]; then
	new_repo="${mirror}${repo}/main"

	if [ ${#new_repos} -eq 0 ]; then
		new_repos="$new_repo"
	else
		new_repos=`echo -e "$new_repos\n$new_repo"`
	fi
fi
# Add (commented out) testing repo if not found
if [ $testing_repo_exists -eq 0 ]; then
	new_repo="#${mirror}${repo}/testing"

	if [ ${#new_repos} -eq 0 ]; then
		new_repos="$new_repo"
	else
		new_repos=`echo -e "$new_repos\n$new_repo"`
	fi
fi
# Update repositories file
echo "$new_repos" > /etc/apk/repositories || (echo "failed." && exit 1)
echo "done."

echo -n "Updating repository indexes... "
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