#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

   [ -n "$ONE_TIME_PROXY" ] &&
     export http_proxy="$ONE_TIME_PROXY" &&
     export https_proxy="$http_proxy"

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

   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 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

   ## set up rc.local.d functionality (LP: #915215)
   mkdir -p /etc/rc.local.d
   if [ ! -e /etc/rc.local.d/00-rc-local ]; then
     mv /etc/rc.local /etc/rc.local.d/00-rc-local
   fi
   printf "#!/bin/sh\nrun-parts /etc/rc.local.d\n" >> /etc/rc.local
   chmod 755 /etc/rc.local

   # add an rc.local.d job that sets MAAS_PROVISION_URL and cobbler
   # to have the right ip address.
   cat > /etc/rc.local.d/maas-ip-fixup <<"END_MAAS_IP_FIXUP"
   #!/bin/sh
   # reconfigure maas in case our IP address has changed
   # get the right value for maas/default-maas-url. it will take
   # care of restarting things that need to be
   /usr/local/bin/maas-set-ip set auto

   END_MAAS_IP_FIXUP
   chmod 755 /etc/rc.local.d/maas-ip-fixup

   cat > /usr/local/bin/maas-set-ip <<"END_MAAS_SET_IP"
   #!/bin/sh

   FIELDS="maas/default-maas-url"
   Usage() {
     cat <<EOF
   Usage ${0##*/} [set|show]
    Set or show the IP addresses for maas.
    for set, provide IP address or "auto"
   EOF
   }
   [ "$1" = "-h" -o "$1" = "--help" ] && { Usage; exit 0; }
   [ "$1" = "set" -o "$1" = "show" ] || { Usage 1>&2; exit 1; }

   get() {
     _RET=$(debconf-show ${1%%/*} 2>/dev/null |
       awk '{gsub("[*]","");} $1 == key { print $2 }' "key=$1:")
   }
   show() {
     while [ $# -ne 0 ]; do
       get $1; echo "$1: $_RET"
       shift
     done
   }
   get_ip() {
     # stolen from /var/lib/dpkg/info/maas.postinst
     local Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window \
     local IRTT interface ipaddr
     while read Iface Destination Gateway Flags RefCnt Use Metric Mask MTU \
                Window IRTT; do
       [ "$Mask" = "00000000" ] && break
     done < /proc/net/route
     interface="$Iface"
     ipaddr=$(LC_ALL=C /sbin/ip -4 addr list dev "$interface" scope global)
     ipaddr=${ipaddr#* inet }
     ipaddr=${ipaddr%%/*}
     _RET=${ipaddr}
   }
   if [ "$1" = "show" ]; then
     show $FIELDS
   else
     [ "$(id -u)" = "0" ] || { echo "must be root to set" 1>&2; exit 1; }
     url_val=$2
     url_key="maas/default-maas-url"
     if [ "${url_val:-auto}" = "auto" ]; then
       get_ip
       url_val=$_RET
     fi
     printf "%s\t%s\t%s\t%s\n" \
       "${url_key%%/*}" "${url_key}" "string" "$url_val" |
       debconf-set-selections
     DEBIAN_FRONTEND=${DEBIAN_FRONTEND:-noninteractive} \
       dpkg-reconfigure -plow ${url_key%%/*}
     sudo service apache2 restart
     show $FIELDS
     #grep MAAS_URL /etc/maas/maas_local_settings.py
     #egrep "^(server|next_server)" /etc/cobbler/settings
   fi
   END_MAAS_SET_IP
   chmod 755 /usr/local/bin/maas-set-ip

   my_release=$(lsb_release -c)
   cat >> /etc/maas/import_pxe_files <<END
   RELEASES="${my_release}"
   ARCHES="i386 amd64"
   END

   echo === $(date): starting import ====
   maas-import-pxe-files

   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 ]
