~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
109
110
111
112
113
#!/bin/bash
# Disable the cloud-init functions.


exec > >(tee /tmp/build-$(date +%s).log) 2>&1
echo "127.0.1.1 $HOSTNAME $HOSTNAME" >> /etc/hosts
error() { echo "$@" 1>&2; }
fail() {
    [ $# -eq 0 ] || error "$@";
    echo "Failed" > /tmp/failed;
    tar -C /tmp -cvf /dev/vdb failed build.log;
    shutdown -P now;
    exit 1;
}

debug() { error "$(date -R):" "$@"; }
sysfail() { fail "General failure!"; }

trap sysfail SIGINT SIGTERM
set -x

seed_d="/mnt/var/lib/cloud/seed/nocloud-net"
rhostid=$(uuidgen | cut -c -8)

# Open /dev/xvdb and write stuff
mount /dev/vdd1 /mnt ||
    fail "Unable to mount root file system"

mkdir -p "${seed_d}" ||
    fail "Unable to create seed directory"


# See whats mounted
blkid
df

# Create the smartcloud users
chroot /mnt groupadd -g 501 idcuser
chroot /mnt useradd -m -u 500 -g 501 idcuser
chroot /mnt passwd -l idcuser

# Make idcuser sudo-less
cat > /mnt/etc/sudoers.d/idcuser <<EOF
# Added by build system
idcuser ALL=(ALL) NOPASSWD:ALL
EOF

# Seed config files
cat << EOF > ${seed_d}/meta-data
instance-id: sc-${rhostid}
EOF

cat << EOF > ${seed_d}/user-data
#cloud-config
manage_etc_hosts: localhost
apt_mirror: http://ibm.clouds.archive.ubuntu.com
preserve_hostname: False
datasource_list: ["NoCloud"]

cloud_init_modules:
 - bootcmd
 - resizefs
 - ca-certs
 - rsyslog
 - ssh

cloud_config_modules:
 - mounts
 - locale
 - apt-pipelining
 - apt-update-upgrade
 - landscape
 - timezone
 - puppet
 - chef
 - salt-minion
 - mcollective
 - disable-ec2-metadata
 - runcmd
 - byobu

cloud_final_modules:
 - scripts-per-once
 - scripts-per-boot
 - scripts-per-instance
 - scripts-user
 - keys-to-console
 - phone-home
 - final-message

EOF


find /mnt/var/lib/cloud/

[ -e "${seed_d}/user-data" ] &&
    cat ${seed_d}/user-data ||
    fail "User-data was not written to disk"

[ -e "${seed_d}/meta-data" ] &&
    cat ${seed_d}/meta-data ||
    fail "Meta-data was not written to disk"

# Cleanup
umount /dev/vdd1


# Finish the work
echo "Taring up artifacts to /dev/vdb!"
touch /tmp/success
tar -C /tmp -cvf /dev/vdb maas_images success build*.log "${OUT_D}" >> /dev/null
sync
shutdown -P now