~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
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
#!/bin/bash -xe
#
# Determine the build serial and place files into the build serial location
# Also, handle the unlikely race condition in case multiple builders arrive
# At the same point.
# copies the files to their staging location
# Prevent race conditions for populating the aggregate build directory
#
# OUTPUT:
# - serial.txt file in ${WORKSPACE}
# - build_properties (OR ${BUILD_PROPERTIES}) file in ${PWD}
# - build-info.txt in ${base_d}/unpacked (or ${base_nd}/unpacked})
# NOTE: see code for how base_d and base_nd are computed

# Required options to even do a build
DISTRO="${DISTRO:-$1}"
WORKSPACE="${WORKSPACE:-$2}" # where is the workspace
BUILD_ID="${BUILD_ID:-$3}"   # build id

# Convert hwe builds to regular for the sake of tooling
btype="${4:-server}"         # server or something else
if [[ "${btype}" =~ hwe ]]; then
    hwe_btype="${btype}"
    bytpe="server"; BTYPE="server"
fi

# Support the legacy broken positional stuff. This should have been
# done with environmental variables or flags
test_build="${5:-0}"         # test build?
sandbox_build="${6:-0}"      # should it be a sandbox build
allow_existing="${7:-1}"     # allow existing
publish_image="${8:-0}"      # publish the image
proposed_build="${9:-0}"     # build from proposed

# Make this less confusing by allowing someone to use environmental
# variables.
# TODO: utlemming: convert this --flags
BTYPE="${BTYPE:-$btype}"
TEST_BUILD="${TEST_BUILD:-$test_build}"
SANDBOX_BUILD="${SANDBOX_BUILD:-$sandbox_build}"
PUBLISH_IMAGE="${PUBLISH_IMAGE:-$publish_image}"
PROPOSED_BUILD="${PROPOSED_BUILD:-$proposed_build}"

ROOT_D="${ROOT_D:-/srv/ec2-images}"
base_d="${ROOT_D}/${DISTRO}"
[ "${TEST_BUILD}" -eq 1 ]     && base_d="${ROOT_D}/test_builds/${DISTRO}"
[ "${SANDBOX_BUILD}" -eq 1 ]  && base_d="${ROOT_D}/sandbox/${DISTRO}" && TEST_BUILD=0
[ "${BTYPE}" = "desktop" ]    && base_d="${ROOT_D}/desktop/${DISTRO}"
[ "${PROPOSED_BUILD}" -eq 1 ] && base_d="${ROOT_D}/proposed/${DISTRO}" &&
    TEST_BUILD=0 && SANDBOX_BUILD=0

let wait_time=$RANDOM%50
sleep $wait_time # Make build collisions a bit harder

make_hwe_meta() {
    # Create a sub build-info.txt for HWE builds
    serial="${1##*/}"
    hwe_unpacked="${base_d}/${serial}/${hwe_btype//$BTYPE-/}/unpacked"
    if [ -n "${hwe_btype}" ]; then
        [ -d "${hwe_unpacked}" ] || mkdir -p "${hwe_unpacked}"
        cat << EOF > "${hwe_unpacked}/build-info.txt"
serial=${serial}
orig_prefix=${DISTRO}-${hwe_btype}-cloudimg
suite=${DISTRO}
build_name=${hwe_btype}
EOF
    fi
}

make_meta() {
   # Write the property file for publishing. This used
   # to write trigger the EC2 publishing job
   serial=${1##*/}
   cat << EOM > "${BUILD_PROPERTIES:-$WORKSPACE/build_properties}"
BUILD_TYPE=${BTYPE}
SERIAL=${serial}
SUITE=${DISTRO}
TEST_BUILD=${TEST_BUILD}
SANDBOX_BUILD=${SANDBOX_BUILD}
PUBLISH_IMAGE=${PUBLISH_IMAGE}
ALLOW_EXISTING=${ALLOW_EXISTING}
PROPOSED_BUILD=${PROPOSED_BUILD}
EOM

   # Write the build-info.txt file. This is used in
   # the publishing process
    [ -d "${1}/unpacked" ] || mkdir -p "${1}/unpacked"
    cat << EOF > "${1}/unpacked/build-info.txt"
serial=${serial}
orig_prefix=${DISTRO}-${BTYPE}-cloudimg
suite=${DISTRO}
build_name=${BTYPE}
EOF
    make_hwe_meta ${serial}
    exit 0
}

$(stat /tmp/${DISTRO}-${BUILD_ID} > /dev/null 2>&1) && {
   echo "Another builder is/has reserved this part of the build. Deferring..."
   while [ -z "${destdir}" ]
   do
      sleep 5
      finaldir=""

      [ -e "${WORKSPACE}/serial.txt" ] && {
         read serial < "${WORKSPACE}/serial.txt"
         destdir="${base_d}/${serial}"
      }

      while read destdir
      do
         echo "Candidate serial found: ${destdir##*/}"
         finaldir="${destdir}"
      done < /tmp/${DISTRO}-${BUILD_ID}

      if [ -n "${finaldir}" ]; then
         echo "Aggregation directory reported as ${finaldir}"
         echo "${finaldir##*/}" > "${WORKSPACE}/serial.txt"
         make_hwe_meta "${finaldir##*/}"
         exit 0
      else
         echo "destdir is not defined!" && exit 10
      fi

   done
}

# if we get here, then know that the build dir hasn't been created yet
touch /tmp/${DISTRO}-$BUILD_ID
test_base_d="${base_d}/$(date +%Y%m%d)"

make_and_write() {
   serial="${1##*/}"
   echo "Creating aggregation directory ${1}"
   echo "${serial}" > "${WORKSPACE}/serial.txt"
   mkdir -p "${1}" &&
      echo "${1}" >> /tmp/${DISTRO}-$BUILD_ID ||
      exit 10

   # Copy stuff to where it should go
   make_meta "${1}"
}

if [ ! -d "${test_base_d}" ]; then
   make_and_write "${test_base_d}"
else
   for bs in {1..30}
   do
     base_nd="${test_base_d}.${bs}"
     serial="${base_nd##*/}"
     echo "Checking on directory ${base_nd}"
     [ ! -d "${base_nd}" ] && make_and_write "${base_nd}"
     make_hwe_meta "${serial}"
   done
fi

exit 0