~ubuntu-on-ec2/vmbuilder/automated-ec2-builds

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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/bin/bash
# vi: ts=4 noexpandtab

# these environment variables must be set
env_vars="START_D PUBLISH_BASE"

base_d=$(dirname $(readlink -f "${0}"))
PATH="${base_d}:${PATH}"
SKIP_PUBLISH=0
WORK_D=""
PUB_D=""
OUT_D=""
BUILDING_LOCK=""
ARM_IMAGES=0
ARCHS="amd64 i386"
LOG_D=${LOG_D:-${START_D}/logs}
LOG_OUT=""
LOG_ERR=""
FAIL=0
DEBUG=0
STD_OUT=1
STD_ERR=2

umask 022

Usage() {
	cat <<EOF
Usage: ${0##*/} [ options ] config

   run an ec2 build according to settings in config

   -d | --debug           debug mode (output to stdout/stderr)
   -s | --skip-publish    do not publish
        --working-dir d   put build-ec2-image output in d (default tmpdir)
                          if specified, it will not be deleted
        --fake-only       quick-testing, only fake the long running commands
EOF
}
bad_Usage() { Usage >&2; [ $# -eq 0 ] || fail "$@"; }
error() { echo "$@" >&2; };
debug() { 
	local date=$(date -R)
	error "${date}:" "$@"; 
	# if stderr is a terminal and not --debug, send this there
	[ "${DEBUG}" = "0" -a -t "${STD_ERR}" ] &&
		echo "${date}" "${@}" >&${STD_ERR}
}
fail() { [ $# -eq 0 ] || error "$(date -R)" "$@"; FAIL=1; exit 1; }
summary_file() {
	local file=${1} head=${2:-20} tail=${2:-40}
    local lines="" remain=""
	lines=$(wc -l < "${file}")
	if [ $lines -le $(($head+$tail)) ]; then
		cat "${file}"
	else
		head -n "${head}" "${file}"
		echo "lines=${lines} head=${head} tail=${tail}"
		echo "... <<<< $((${lines}-${head}-${tail})) lines >>>>..."
		tail -n "${tail}" "${file}"
	fi
}

do_exit() {
	[ -z "${WORK_D}" -a -n "${OUT_D}" ] && rm -Rf "${OUT_D}"
	[ -z "${BUILDING_LOCK}" ] || rm -f "${BUILDING_LOCK}"

	[ "${DEBUG}" = "0" ] || return
	[ "${FAIL}" = "0" ] && return

	[ "${STD_ERR}" = "2" ] || exec 2>&${STD_ERR}
	[ "${STD_OUT}" = "1" ] || exec 1>&${STD_OUT}
	if [ -n "${LOG_OUT}" -a -f "${LOG_OUT}" ]; then
		local label="stdout"
		[ "${LOG_OUT}" = "${LOG_ERR}" ] && label="stdout.stderr"
		echo "==== [${label}] ${LOG_OUT} ====";
		summary_file "${LOG_OUT}";
		[ -n "${PUB_D}" ] && cp "${LOG_OUT}" "${PUB_D}/log.${label}"
	fi
	if [ -n "${LOG_ERR}" -a -f "${LOG_OUT}" ] &&
	   [ "${LOG_ERR}" != "${LOG_OUT}" ]; then
		echo "==== [stderr] ${LOG_ERR} ===="
		summary_file "${LOG_ERR}"
		[ -n "${PUB_D}" ] && cp "${LOG_OUT}" "${PUB_D}/log.stderr"
	fi
}

do_mkdir() {
	local p=""
	[ "$1" = "-p" ] && { p=${1}; shift; }
	[ -d "${1}" ] || mkdir ${p} "${1}"
}

push_build() {
	trigger-sync
}

trap do_exit EXIT

short_opts="d:hsa"
long_opts="debug,help,fake-only,no-publish-ec2,skip-publish,working-dir"
getopt_out=$(getopt --name "${0##*/}" \
	--options "${short_opts}" --long "${long_opts}" -- "$@") &&
	eval set -- "${getopt_out}" ||
	bad_Usage

publish_ec2=1
fake_only=0

while [ $# -ne 0 ]; do
	cur=${1}; next=${2};
	case "$cur" in
		-d|--debug) DEBUG=1;;
		-h|--help) Usage; exit 0;;
		-s|--skip-publish) SKIP_PUBLISH=1;;
		   --no-publish-ec2) publish_ec2=0;;
		   --fake-only) fake_only=1;;
		--working-dir)
			case "${next}" in
				/*) WORK_D=${next};;
				*) WORK_D="$PWD/${next}";;
			esac
			shift;;
		--) shift; break;;
	esac
	shift
done

[ $# -eq 1 ] || bad_Usage "must supply config file"

conf_in="$1"

conf=""
if [ -f "${conf_in}" ]; then
	conf=$(readlink -f "${conf_in}")
else
	[ "${conf_in#/}" = "${conf_in}" ] || fail "${conf_in}: not a file"
	for f in "${base_d}/${conf_in}" "${base_d}/${conf_in}.conf" \
	         "${base_d}/conf/${conf_in}" "${base_d}/conf/${conf_in}.conf"; do
		[ -f "$f" ] && conf=$(readlink -f "${f}") && break
	done
fi

[ -n "${conf}" -a -f "${conf}" ] ||
	fail "${conf_in}: unable to find config"

conf_base=$(dirname ${conf})

for x in ${env_vars}; do
	[ -n "${!x}" ] || fail "must set ${env_vars} (${x} not set)"
done

publish_base=${PUBLISH_BASE}

unset suite build_name;
out=$(bash -c '. "$1" && echo "${suite}:${build_name}"' "${0}" "${conf}") &&
	[ -n "${out}" ] ||
	fail "failed to get suite, build_name from conf";

do_mkdir -p "${publish_base}" ||
	fail "failed to make publish dir ${publish_base}"

publish_base=$(readlink -f "${publish_base}")

if [ "${fake_only}" = "1" ]; then
	build-ec2-image() {
		while [ "${1#-}" != "${1}" ]; do shift; done
		local conf=${1} out=${2} x pre d
		for x in "${ARCHS}"; do
			pre="${suite}-${build_name}-cloudimg-${x}"
			for d in "${out}" "${out}/unpacked"; do
				do_mkdir "${d}" || return 1;
				: > "${d}/${pre}.tar.gz"
				: > "${d}/${pre}.manifest"
			done
		done
	}

	publish-build() {
		[ "$1" = "--verbose" ] && shift;
		local out_d=${4}
		echo "${*}" > "${out_d}/published-ec2-daily.txt"
	}

	make-web-indices() {
		local out_d=${1}
		echo "$*" > "${out_d}/HEADER.html"
		echo "$*" > "${out_d}/FOOTER.html"
	}

	checksum-directory() {
		: > "${1}/MD5SUMS"
	}

	push_build() {
		:
	}
fi

[ -d "${START_D}" ] || fail "${START_D} does not exist"

if [ "${DEBUG}" = "0" ]; then
	do_mkdir -p "${LOG_D}" ||
		fail "failed to make logdir ${LOG_D}"
	LOG_D=$(cd "${LOG_D}" && pwd) || fail "failed to get full path for log dir"
fi

cd "${START_D}" || fail "failed cd ${START_D}"

suite=${out%%:*}
build_name=${out#*:}

case "${build_name}" in
	server) base_d="${publish_base}/${suite}";;
	desktop) base_d="${publish_base}/desktop/${suite}";;
	*) fail "unknown build_name: ${build_name}";;
esac

do_mkdir -p "${base_d}" ||
	fail "failed to mkdir ${base_d}"

source "${conf}"
ds=$(date +"%Y%m%d") || fail "failed to run date"

serial=""
pub_d=""
for p in "" .1 .2 .3 .4 .5 .6 .7 .8 .9; do
	mkdir "${base_d}/${ds}${p}" 2>/dev/null || continue
	pub_d="${base_d}/${ds}${p}"
	serial="${ds}${p}"
	break
done
[ -n "${pub_d}" -a -d "${pub_d}" ] ||
	fail "failed to make output dir under ${base_d}"

echo "$$" > "${pub_d}/.building.pid" &&
	BUILDING_LOCK="${pub_d}/.building.pid" ||
	fail "failed to lock ${pub_d}"
PUB_D="${pub_d}"

if [ "$DEBUG" = "0" ]; then
	log_out="${LOG_D}/${suite}-${build_name}-cloudimg-${serial}.log"
	# redirect output to log file. keep file descriptors for
	# sending output at end on failure (ie, so cron output gets mailed)
	LOG_OUT=${log_out}
	LOG_ERR=${log_out}
	exec 3>&1 && STD_OUT=3 || fail "failed stdout dupe"
	exec 4>&2 && STD_ERR=4 || fail "failed stderr dupe"
	exec 1> "${log_out}" 2>&1  # stderr, stdout same location
	# exec 1> "${log_out}"   # for separate stderr
	# exec 2> "${log_err}"   # for separate stderr
	[ $? -eq 0 ] || fail "failed to write to ${log_out}"
fi

debug "building ${conf_in} to ${pub_d}.${log_out:+ log in ${log_out} .}"

if [ -z "${WORK_D}" ]; then
	OUT_D=$(mktemp -d "${START_D}/${0##*/}.${serial}.XXXXXX") ||
		fail "failed to make tempdir"
else
	OUT_D="${WORK_D}"
fi

build-ec2-image --serial=${serial} "${conf}" "${OUT_D}" ||
	fail "failed: build-ec2-image '${conf}' '${OUT_D}'"

source "${conf}"
[ -n "${extra_arches}" ] && {
    for nested_conf in "${extra_arches}" 
    do
        debug "Processing nested configuration file ${nested_conf}"
        build-ec2-image --serial=${serial} "${conf_base}/${nested_conf}" "${OUT_D}" ||
           fail "failed nested configuration: build-ec2-image ${nested_conf} ${OUT_D}"
    done
}

[ "${SKIP_PUBLISH}" = "0" ] || exit 0

debug "finalizing working dir ${OUT_D}"

U_OUT_D="${OUT_D}/unpacked"

printf "serial=%s\norig_prefix=%s\nsuite=%s\nbuild_name=%s\n" \
	"${serial}" "${suite}-${build_name}-cloudimg" "${suite}" "${build_name}" \
	> "${U_OUT_D}/build-info.txt"

{
for tool in "${0}" "$(which vmbuilder)" "${LIVE_BUILD_PATH}/live-build"; do
	dir=$(dirname "${tool}")
	echo "====== ${tool##*/} [${tool}] ======"
	if ( cd "${dir}" && bzr root ) >/dev/null 2>&1; then
		( cd "${dir}" && bzr info && bzr version-info && 
		  { bzr diff || :; } ) ||
			fail "getting bzr info failed"
	elif dpkg_out=$(dpkg -S "${tool}" 2>/dev/null); then
		pkg=${dpkg_out%%:*}
		dpkg-query --show "${pkg}"
		if debsum_out=$(debsums "${pkg}" >/dev/null 2>&1); then
			echo "package unmodified"
		else
			echo "package modified"
			echo "${out}"
		fi
	else
		echo "unversioned"
		md5sum "${tool}"
	fi
done
} > "${U_OUT_D}/tool-version-info.txt" 2>&1

checksum-directory "${OUT_D}" &&
	checksum-directory "${U_OUT_D}" ||
	fail "failed to make checksums"

# from here out, we're working on the published directory
debug "publishing image ${suite}-${build_name}-cloudimg to ${pub_d}"
if [ ! -d "${pub_d}" ]; then
    error "WARNING: ${0##*/} found no publish dir (${pub_d}). creating it."
	mkdir -p "${pub_d}" || fail "failed to create ${pub_d}"
fi
mv "${OUT_D}"/* "${pub_d}/" || fail "failed to move files to ${pub_d}"

if [ ${publish_ec2} -ne 0 ]; then
	debug "publishing to ec2"
	publish-build --verbose "${suite}" "${build_name}" daily "${pub_d}" ||
		fail "failed publish-build ${suite} ${build_name} daily ${pub_d}"
else
	# publish-build above would do this, but in this case, do it here.
	DIST=${suite} make-web-indices "${pub_d}" "${suite}" daily &&
		DIST=${suite} make-web-indices "${pub_d}/unpacked" "${suite}" daily ||
		fail "failed to make web indices"
fi


current="${base_d}/current"
rm -f "${current}" && ln -sf "${serial}" "${current}" ||
	fail "failed to link current"

debug "built ${conf} to ${pub_d} ${SECONDS}"

push_build