~fginther/vmbuilder/new-artful-builder

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
#
# 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.
#!/bin/bash
# copies the files to their staging location
# Prevent race conditions for populating the aggregate build directory

DISTRO="${1}"
WORKSPACE="${2}"
BUILD_ID="${3}"
BTYPE="${4:-server}"
TEST_BUILD="${5:-0}"
SANDBOX_BUILD="${6:-0}"
ALLOW_EXISTING="${7:-1}"
PUBLISH_IMAGE="${8:-0}"

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

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

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

   # 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=${1##*/}
orig_prefix=${DISTRO}-${BTYPE}-cloudimg
suite=${DISTRO}
build_name=${BTYPE}
EOF

   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}

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

   done
}

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

make_and_write() {
   echo "Creating aggregation directory ${1}"
   echo "${1##*/}" > "${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 "${base_d}" ]; then
   make_and_write "${base_d}"
else
   for bs in {1..30}
   do
     base_nd="${base_d}.${bs}"
     echo "Checking on directory ${base_nd}"
     [ ! -d "${base_nd}" ] && make_and_write "${base_nd}"
   done
fi

exit 0