aboutsummaryrefslogtreecommitdiffstats
path: root/main/aports-build/aports-build
blob: 8c972a92200215d73bf68b1c98dbb220171c6a39 (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
#!/bin/sh

lockfile=/var/run/aports-build/aports-build.pid
pid=$(cat $lockfile 2>/dev/null)

if [ -n "$pid" ]; then
	if [ -d /proc/"$pid" ]; then
		# already running
		exit 0
	fi
fi
echo $$ > $lockfile || exit 1

logurl=
conf=/etc/conf.d/aports-build
. $conf || exit 1

if [ -z "$git_branch" ]; then
	echo "Please set 'git_branch' in $conf" >&2
	exit 1
fi

rel=$upload_release

if [ -z "$rel" ]; then
	case "$git_branch" in
		master)		rel="edge";;
		[0-9]*-stable)	rel=v${git_branch%-stable} ;;
	esac
fi

arch=$(abuild -A)

aports=${APORTS:-$HOME/aports}
isodir=$HOME/alpine-iso
packages=${REPOSDIR:-$HOME/packages}
repos=${REPOS:-"main testing"}

: ${buildrepo:="buildrepo -p"}
: ${upload_host:="rsync.alpinelinux.org"}
: ${upload_prefix:="$upload_host:alpine"}
upload_pkg="$upload_prefix/$rel/"
upload_iso="$upload_prefix/$rel/releases/$arch/"

: ${ircchannel:="#alpine-devel"}
: ${hostname:=$(hostname)}
: ${mqtt_broker:="msg.alpinelinux.org"}
: ${status_msg:="mosquitto_pub -h $mqtt_broker -t build/$hostname -r -m"}
: ${upload_msg:="mosquitto_pub -h $mqtt_broker -t rsync/$upload_host/$rel/$arch -m"}
: ${irc_msg:="mosquitto_pub -h $mqtt_broker -t irc/${ircchannel//#/%} -m"}
: ${logdir:="/var/cache/distfiles/buildlogs"}
: ${logurlprefix:="http://bld1.alpinelinux.org/buildlogs"}

log() {
	echo "$hostname: $@"
	$status_msg "$1"
}

send_irc() {
	#echo "$1" | sircbot-send "$ircchannel"
	$irc_msg "$hostname: $1"
}

# create new_release
new_release() {
	local new=$1
	local target="all-release"
	if [ "$rel" = "edge" ]; then
		target="edge"
	fi
	cd "$isodir"
	echo "$new" > current
	rm -rf isotmp.*
	sudo apk add --virtual .alpine-release xdelta3 lua
	if ! make $target; then
		rm current
		return 1
	fi
	sh mklatest.sh || return 1
	mkdir -p "$upload_iso"
	rsync -ruv .latest.txt *"$new"* "$upload_iso" || return 1
	sudo apk del .alpine-release
	mv current previous
	return 0
}

build() {
	( $buildrepo $repos || echo "FAIL" ) | while read line; do
		case "$line" in
		FAIL) return 1;;
		[0-9]*/[0-9]*) $status_msg "$line";;
		*) echo "$line";;
		esac
	done
}

cd $aports || return 1
[ -z "$repos" ] && return 1

# parse opts
while getopts "fu:" opt; do
	case $opt in
	'f') force=true;;
	'u') logurl=" $OPTARG";;
	esac
done
shift $(( $OPTIND - 1 ))

while true; do
	local do_release=false
	cd $aports || return 1
	# check if we need to rebuild
	log "pulling git"
	_old=$(git describe)
	_old_tag=$(git describe --abbrev=0)
	git checkout $git_branch
	git pull || return 1
	_current=$(git describe)
	_current_tag=$(git describe --abbrev=0)
	if [ "$_old_tag" != "$_current_tag" ]; then
		log "$_old_tag -> $_current_tag"
		if [ -n "$release_formats" ]; then
			do_release=true
		fi
	fi
	if [ "$_old" = "$_current" ] && [ -f /tmp/uploaded ] && [ -z "$force" ] && [ "$_current_tag" = "$_old_tag" ]; then
		break
	fi

	force=

	# check if we need make new release
	if $do_release; then
		_new_release=1
		# we want build the realease from this tag
		git checkout "$_current_tag"
		_current=$(git describe)
	fi

	rm -f /tmp/uploaded

	# do the compile, send output to log
	log "building $_current"
	if ! build >"$logdir"/$hostname.log 2>&1 ; then
		local errlog=$hostname.$_current.log
		cp "$logdir"/$hostname.log "$logdir"/$errlog
		# todo: revert last commit?
		send_irc "failed to build $_current: $logurlprefix/$errlog"
		exit 1
	fi

	# upgrade our running system
	log "upgrading system"
	sudo apk upgrade -U -a --quiet || log 'failed to apk upgrade'

	# copy for distribution
	cd "$packages" || return 1
	if [ -z "$upload_pkg" ]; then
		break
	fi
	log "uploading packages"
	rc=0
	for i in $upload_pkg; do
		local repo
		for repo in $repos; do
			rsync -ruv --delete-delay --delay-updates $repo/$arch $i/$repo/
			rc=$(( $rc + $? ))
		done
	done
	[ $rc -eq 0 ] && touch /tmp/uploaded && sudo apk update

	# report to irc that we are done
	send_irc "files from $_current uploaded"

	if $do_release; then
		if new_release ${_current_tag#v}; then
			send_irc "release $_current_tag uploaded"
		fi
	fi

	# notify mirrors that they can upload now if they want
	$upload_msg "$_current"
done

# cleanup
log "idle"
rm -f $lockfile