#cloud-config
password: passw0rd
chpasswd: { expire: False }
ssh_pwauth: True

#apt_proxy: "http://local-proxy:3128/"
#apt_mirror: "http://us.archive.ubuntu.com/ubuntu"
#ssh_import_id: smoser

bucket:
 - &setup |
   cd /root
   (
   #ONE_TIME_PROXY=http://local-proxy:3128/
   PROXY_COPY_SYS_TO_COBBLER=1

   echo === $(date) ====
   debconf-set-selections <<EOF
   cobbler  cobbler/password  password xcobbler
   cloud-init	cloud-init/datasources	multiselect	NoCloud
   EOF

   [ -n "$ONE_TIME_PROXY" ] && export http_proxy="$ONE_TIME_PROXY"
   export DEBIAN_FRONTEND=noninteractive;
   dpkg-reconfigure cloud-init

   read oldhost < /etc/hostname
   sed -i "/$oldhost/d;/zimmer/d" /etc/hosts
   echo zimmer > /etc/hostname
   hostname zimmer

   echo "127.0.1.2 zimmer-server" >> /etc/hosts
   
   echo === $(date): starting apt ====
   apt_get() { 
     DEBIAN_FRONTEND=noninteractive apt-get \
        --option "Dpkg::Options::=--force-confold" --assume-yes "$@"
   }
   pkgs=""
   pkgs="$pkgs distro-info" # (LP: #960142)
   pkgs="$pkgs libvirt-bin" # for power control via virsh
   pkgs="$pkgs cobbler-web" # for debugging and ease of use of cobbler
   pkgs="$pkgs maas" # main maas package
   apt_get update
   apt_get install ${pkgs}
   
   # if local apt is configured to use a proxy, modify
   # cobbler to tell nodes to use that proxy. (LP: #914202)
   if [ "${PROXY_COPY_SYS_TO_COBBLER:-1}" != "0" ] &&
      apt_proxy="" && out=$(apt-config shell apt_proxy Acquire::HTTP::Proxy) &&
      eval $out && [ -n "$apt_proxy" ]; then
      fname=/var/lib/cobbler/snippets/maas_proxy
      [ "${fname}.dist" -ef "$fname" ] || cp "$fname" "$fname.dist"
      sed "s,\(.*mirror/http/proxy string \).*,\1${apt_proxy}," \
         "$fname" > "$fname.zimmer"
      ln -sf ${fname##*/}.zimmer "$fname"
   fi

   # hack /etc/init/cobbler.conf to always update its IP address on start
   cat >> /etc/init/cobbler.conf <<"ENDCOB"
   #### added by zimmer-build ######
   pre-start script
     #set -x; exec >/tmp/cobbler-pre.out 2>/tmp/cobbler-pre.err
     found=""
     fn="/etc/cobbler/cobbler-server"
   
     # if user wants to manage this, then just remove that file
     # it is expected to either have an IP address or 'auto'
     [ -f "$fn" ] || exit 0
     read found < "$fn" || :
     [ -n "$found" -a "$found" != "auto" ] ||
       found=$(ifconfig eth0 2>/dev/null |
         awk '$0 ~ /inet addr:/ { sub(/.*:/,"",$2); print $2; }')
   
     [ -z "$found" ] && exit 0
   
     sed -i.start -e "s/^next_server: .*$/next_server: $found/" \
        -e "s/^server: *..*..*..*$/server: $found/" \
        /etc/cobbler/settings
   
     # if the above sed did something, then we leave the orig
     # file around for post-start to cleanup.
     cmp /etc/cobbler/settings.start /etc/cobbler/settings >/dev/null &&
       rm -f /etc/cobbler/settings.start || :
   end script
   
   post-start script
     #set -x; exec >/tmp/cobbler-start.out 2>/tmp/cobbler-start.err
     set +e  # upstart uses 'set -e' by default
     [ -f /etc/cobbler/settings.start ] || exit 0
     rm -f /etc/cobbler/settings.start

     # now basically wait around until cobbler is available
     while : ; do
       # will exit 155 if "cobblerd does not appear to be running"
       cobbler system list >/dev/null
       [ $? -eq 155 ] || break
       sleep 1
     done

     # we're allowing cobbler to start even if these fail
     cobbler sync
     maas-import-isos --update
     exit 0
   end script
   ENDCOB

   echo "auto" >> /etc/cobbler/cobbler-server

   cat >> /etc/maas/import_isos <<END
   RELEASES="precise"
   ARCHES="i386 amd64"
   END

   echo === $(date): starting import ====
   maas-import-isos

   sed -i '/zimmer-server/d' /etc/hosts

   echo === $(date): starting cleanup ====
   apt_get clean
   time sh -c 'dd if=/dev/zero of=/out.img; rm /out.img'

   echo === $(date): poweroff ===
   echo === ZIMMER BUILD FINISHED ===
   ) 2>&1 | tee out.log

runcmd:
 - [ sh, -c, *setup ]
 - [ /sbin/poweroff ]
