~fginther/vmbuilder/new-artful-builder

20 by Ben Howard
Fix for missing serial number
1
#!/bin/bash
2
#
3
# Determine the build serial and place files into the build serial location
4
# Also, handle the unlikely race condition in case multiple builders arrive
5
# At the same point.
6
#!/bin/bash
7
# copies the files to their staging location
8
# Prevent race conditions for populating the aggregate build directory
9
10
DISTRO="${1}"
11
WORKSPACE="${2}"
12
BUILD_ID="${3}"
13
BTYPE="${4:-server}"
14
TEST_BUILD="${5:-0}"
15
SANDBOX_BUILD="${6:-0}"
69 by Ben Howard
Allow existing for publication retries
16
ALLOW_EXISTING="${7:-1}"
33 by Ben Howard
Better handling of build properties file
17
PUBLISH_IMAGE="${8:-0}"
20 by Ben Howard
Fix for missing serial number
18
19
base_d="/srv/ec2-images/${DISTRO}"
20
[ "${TEST_BUILD}" -eq 1 ] && base_d="/srv/ec2-images/test_builds/${DISTRO}"
21
[ "${SANDBOX_BUILD}" -eq 1 ] && base_d="/srv/ec2-images/sandbox/${DISTRO}" && TEST_BUILD=0
22
[ "${BTYPE}" = "desktop" ] && base_d="/srv/ec2-images/desktop/${DISTRO}"
23
70 by Ben Howard
Added time out to get_serial.sh during discovery of current serial caused by a race condition; increased potential random wait to reduce likelyhood of collision
24
let wait_time=$RANDOM%50
20 by Ben Howard
Fix for missing serial number
25
sleep $wait_time # Make build collisions a bit harder
26
27
make_meta() {
28
   # Write the property file for publishing. This used
29
   # to write trigger the EC2 publishing job
33 by Ben Howard
Better handling of build properties file
30
   cat << EOF > ${BUILD_PROPERTIES:-build_properties}
20 by Ben Howard
Fix for missing serial number
31
BUILD_TYPE=${BTYPE}
32
SERIAL=${1##*/}
33
SUITE=${DISTRO}
34
TEST_BUILD=${TEST_BUILD}
35
SANDBOX_BUILD=${SANDBOX_BUILD}
33 by Ben Howard
Better handling of build properties file
36
PUBLISH_IMAGE=${PUBLISH_IMAGE}
37
ALLOW_EXISTING=${ALLOW_EXISTING}
20 by Ben Howard
Fix for missing serial number
38
EOF
39
40
   # Write the build-info.txt file. This is used in
41
   # the publishing process
42
   [ -d "${1}/unpacked" ] || mkdir -p "${1}/unpacked"
43
   cat << EOF > "${1}/unpacked/build-info.txt"
44
serial=${1##*/}
45
orig_prefix=${DISTRO}-${BTYPE}-cloudimg
46
suite=${DISTRO}
47
build_name=${BTYPE}
48
EOF
49
50
   exit 0
51
}
52
53
$(stat /tmp/${DISTRO}-${BUILD_ID} > /dev/null 2>&1) && {
54
   echo "Another builder is/has reserved this part of the build. Deferring..."
55
   while [ -z "${destdir}" ]
56
   do
70 by Ben Howard
Added time out to get_serial.sh during discovery of current serial caused by a race condition; increased potential random wait to reduce likelyhood of collision
57
      sleep 5
20 by Ben Howard
Fix for missing serial number
58
      finaldir=""
59
60
      [ -e "${WORKSPACE}/serial.txt" ] && {
61
         read serial < "${WORKSPACE}/serial.txt"
62
         destdir="${base_d}/${serial}"
63
      }
64
65
      while read destdir
66
      do
67
         echo "Candidate serial found: ${destdir##*/}"
68
         finaldir="${destdir}"
69
      done < /tmp/${DISTRO}-${BUILD_ID}
70
71
      [ -n "${finaldir}" ] && {
72
         echo "Aggregation directory reported as ${finaldir}"
73
         echo "${finaldir##*/}" > "${WORKSPACE}/serial.txt"
74
         exit 0 
75
      } || {
76
         echo "destdir is not defined!" && exit 10
77
      }
78
79
   done
80
}
81
82
# if we get here, then know that the build dir hasn't been created yet
83
touch /tmp/${DISTRO}-$BUILD_ID
84
base_d="${base_d}/$(date +%Y%m%d)"
85
86
make_and_write() {
87
   echo "Creating aggregation directory ${1}"
88
   echo "${1##*/}" > "${WORKSPACE}/serial.txt"
89
   mkdir -p "${1}" &&
90
      echo "${1}" >> /tmp/${DISTRO}-$BUILD_ID ||
91
      exit 10
92
93
   # Copy stuff to where it should go
94
   make_meta "${1}"
95
}
96
97
if [ ! -d "${base_d}" ]; then
98
   make_and_write "${base_d}"
99
else
100
   for bs in {1..30}
101
   do
102
     base_nd="${base_d}.${bs}"
103
     echo "Checking on directory ${base_nd}"
104
     [ ! -d "${base_nd}" ] && make_and_write "${base_nd}"
105
   done
106
fi
107
108
exit 0