aboutsummaryrefslogtreecommitdiffstats
path: root/main/aports-build/aports-build
blob: 423eb9b8d8bdc29d7db4bb5e10a0f605a159eecc (plain) (blame)
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
#!/bin/sh

lockfile=/var/run/mqtt-exec.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/mqtt-exec.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}
packages=${REPOSDIR:-$HOME/packages}
releasedir="$packages/releases/$arch"
repos=${REPOS:-"main community testing"}

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

: ${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"}
: ${logdir:="/var/cache/distfiles/buildlogs"}
: ${logurlprefix:="http://build.alpinelinux.org/buildlogs"}

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

# create new_release
create_release() {
	local release="$1"
	local release_deps="abuild apk-tools alpine-conf busybox fakeroot
		xorriso rsync openssh-client squashfs-tools acct mkinitfs
		mtools"
	case "$arch" in
	aarch64|arm*) release_deps="$release_deps mtools grub-efi";;
	x86*) release_deps="$release_deps syslinux mtools grub-efi";;
	ppc64le) release_deps="$release_deps grub grub-ieee1275";;
	s390x) release_deps="$release_deps s390-tools";;
	esac

	log "creating $release release"
	cd "$aports"
	abuild-apk add --virtual .alpine-release-deps $release_deps
	(
	if [ "$rel" = "edge" ]; then
		sh scripts/mkimage.sh --repository $packages/main --yaml \
			--tag "$release" --outdir $releasedir --profile "minirootfs netboot" \
			|| return 1
	else
		sh scripts/mkimage.sh --repository $packages/main --yaml \
			--tag "$release" --outdir $releasedir || return 1
	fi

	if $use_network; then
		log "uploading $release release"
		ssh $upload_host mkdir -p "${upload_iso#${upload_host}:}"
		rsync -ruva $rsync_opts "$releasedir"/* "$upload_iso" || return 1
	fi
	)
	local rc=$?
	abuild-apk del .alpine-release-deps
	return $rc
}

build() {
	( $buildrepo "$@" || 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

use_network=true
force_release=false
skip_build=false

# parse opts
while getopts "fFu:ns" opt; do
	case $opt in
	'f') force=true;;
	'F') force_release=true;;
	'u') logurl=" $OPTARG";;
	'n') use_network=false
	     status_msg="echo status_msg:"
	     upload_msg="echo upload_msg:"
	     ;;
	's') skip_build=true;;
	esac
done
shift $(( $OPTIND - 1 ))

while true; do
	do_release=false
	cd $aports || return 1
	# check if we need to rebuild
	_old=$(git describe)
	_old_tag=$(git describe --abbrev=0)
	if $use_network; then
		log "pulling git"
		git checkout $git_branch
		git pull || return 1
	fi
	_current=$(git describe)
	_current_tag=$(git describe --abbrev=0)
	if [ "$_old_tag" != "$_current_tag" ]; then
		log "$_old_tag -> $_current_tag"
		do_release=true
	fi

	# don't create release candidates on edge
	if [ "$rel" = "edge" ] && [ "${_current_tag%_rc*}" != "$_current_tag" ]; then
		do_release=false
	fi

	if [ "$_old" = "$_current" ] && [ -f /tmp/uploaded ] && [ -z "$force" ] && [ "$_current_tag" = "$_old_tag" ]; then
		break
	fi

	if $force_release; then
		do_release=true
	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"
	rm -f "$logdir"/$hostname.log
	rc=0
	! $skip_build && for repo in $repos; do
		if ! build $repo >>"$logdir"/$hostname.log 2>&1 ; then
			errlog=$hostname.$_current.log
			cp "$logdir"/$hostname.log "$logdir"/$errlog
			# todo: revert last commit?
			log "failed"
			exit 1
		fi

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

		# copy for distribution
		cd "$packages" || return 1
		if [ -z "$upload_pkg" ]; then
			continue
		fi

		log "uploading packages to $repo"
		$use_network && for i in $upload_pkg; do
			rsync -rui --delete-delay --delay-updates $rsync_opts \
				$repo/$arch $i/$repo/ > /tmp/upload-$repo
			if [ $? -ne 0 ]; then
				rc=1
			elif [ -s /tmp/upload-$repo ]; then
				$upload_msg "$rel/$repo/$arch"
			fi
		done
	done
	[ $rc -eq 0 ] && touch /tmp/uploaded && abuild-apk update

	if $do_release && create_release ${_current_tag#v}; then
		$upload_msg "$rel/releases/$arch"
	fi

done

# cleanup
log "idle"
rm -f $lockfile