~ubuntu-on-ec2/vmbuilder/jenkins_kvm-philroche-trusty-supported

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
#!/bin/bash

# Read in the common files
myname=$(readlink -f ${0})
mydir=$(dirname ${myname})
mypdir=$(dirname ${mydir})

# Scope stuff locally here
# Create a temporary directory for the fun
tmp_dir=$(mktemp -d builder.XXXXX --tmpdir=${TMPDIR:-/tmp})
export TMPDIR=${tmp_dir}
export WORKSPACE=${mydir}
export HOME=${mydir}
export LOCAL_BUILD=1

clean() { [ -d ${tmp_dir} ] && rm -rf ${tmp_dir};
          [ -d "${mydir}/Virtualbox\ VMS" ] && rm -rf "${mydir}/Virtualbox\ VMS";
          exit "${@}"; }
error() { echo "$@"; }
debug() { error "$(date -R):" "$@"; }
fail()  { debug "${1:-Something bad happend}"; clean 1; }

# Fly with the safety on!
trap fail EXIT
trap fail SIGINT

test_cmd_exists() {
    which $1 >> /dev/null || fail "Command $1 does not exist! Please install $2"
}

if [ "$(lsb_release -r -s | sed 's/\.//')" -lt 1404 ]; then
    fail "This must be run on Ubuntu 14.04 or higher"
fi

test_cmd_exists qemu-nbd qemu-utils
test_cmd_exists vboxmanage virtualbox
test_cmd_exists bzr bzr
test_cmd_exists sstream-query simplestreams

# This defines what gets built
build_for=(${BUILD_FOR:-trusty:amd64 precise:amd64})
[ -n "${JUJU_CORE_PKG}" -o -n "${JUJU_LOCAL_PKG}" ] && \
    [ ${#build_for[@]} -ge 2 ] && \
    fail "JUJU_CORE_PKG and JUJU_LOCAL_PKG can be specified only for a single build target."

for build in ${build_for[@]};
do
    suite=${build%%:*}
    arch=${build##*:}
    builder_img="${mydir}/${suite}-builder-${arch}.img"
    results_d_arch="${mydir}/${suite}-${arch}"
    built_img="${suite}-server-cloudimg-${arch}-juju-vagrant-disk1.img"

    [ ! -e "${results_d_arch}" ] &&
        mkdir -p "${results_d_arch}"

    cmd=(
       "${mydir}/standalone.sh"
        "--cloud_cfg    ${mydir}/config/cloud-vps.cfg"
        "--template     ${mydir}/templates/img-juju.tmpl"
        "--suite        ${suite}"
        "--arch         ${arch}"
        "--use_img      ${builder_img}"
        "--final_img    ${built_img}"
        "--resize_final 40"
        )

    [ ! -e "${builder_img}" ] && cmd+=("--fetch_new")
    if [ -n "${JUJU_CORE_PKG}" -o -n "${JUJU_LOCAL_PKG}" ]; then
        cmd+=("--cloud-init-file ${mydir}/templates/handle-xdeb.py:text/part-handler")
        if [ -n "${JUJU_CORE_PKG}" ]; then
            cmd+=("--cloud-init-file ${JUJU_CORE_PKG}:application/x-deb")
            echo "JUJU_CORE_PKG=$(basename $JUJU_CORE_PKG)" > ${tmp_dir}/juju-sources.sh
        fi
        if [ -n "${JUJU_LOCAL_PKG}" ]; then
            cmd+=("--cloud-init-file ${JUJU_LOCAL_PKG}:application/x-deb")
            echo "JUJU_LOCAL_PKG=$(basename $JUJU_LOCAL_PKG)" >> ${tmp_dir}/juju-sources.sh
        fi
        cmd+=("--cloud-init-file ${tmp_dir}/juju-sources.sh:application/x-shellscript")
    fi

    [ -e "${results_d_arch}/${suite}-server-cloudimg-${arch}-juju-vagrant-disk1.img" ] ||
        ( cd ${results_d_arch} && ${cmd[@]} )

    # The following Vagrant-ifies the build
    SUITE=${suite} \
    	ARCH_TYPE=${arch} \
        SERIAL="current" \
        SRV_D="${mydir}/${suite}-${arch}" \
        OUTPUT_D="${mydir}/${suite}-${arch}" \
        WORKSPACE="${mydir}/${suite}-${arch}" \
    	${mydir}/jenkins/CloudImages_Juju.sh

    expected_box="${results_d_arch}/${suite}-server-cloudimg-${arch}-juju-vagrant-disk1.box"
    [ -f "${expected_box}" ] || fail "unable to find ${expected_box}; build failed!"
    results_out+=("${build} ${expected_box}")
done

# Clear the traps
trap - EXIT
trap - SIGINT
trap

debug "Results are in following locations"
echo -e "${results_out[@]}"

debug "Done with the build!"
clean 0