#cloud-config

rsyslog:
   remotes:
     maas: "{{syslog_host_port}}"

power_state:
   delay: now
   mode: poweroff
   timeout: 1800
   condition: test ! -e /tmp/block-poweroff

misc_bucket:
 - &maas_enlist |
   # Bring up all interfaces.
   ip -o link | cut -d: -f2 | xargs -I{} ip link set dev {} up
   ####  IPMI setup  ######
   # If IPMI network settings have been configured statically, you can
   # make them DHCP. If 'true', the IPMI network source will be changed
   # to DHCP.
   IPMI_CHANGE_STATIC_TO_DHCP="false"

   # In certain hardware, the parameters for the ipmi_si kernel module
   # might need to be specified. If you wish to send parameters, uncomment
   # the following line.
   #IPMI_SI_PARAMS="type=kcs ports=0xca2"

   TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXX")
   IPMI_CONFIG_D="${TEMP_D}/ipmi.d"
   BIN_D="${TEMP_D}/bin"
   OUT_D="${TEMP_D}/out"
   PATH="$BIN_D:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

   mkdir -p "$BIN_D" "$OUT_D" "$IPMI_CONFIG_D"

   load_modules() {
      modprobe ipmi_msghandler
      modprobe ipmi_devintf
      modprobe ipmi_si ${IPMI_SI_PARAMS}
      modprobe ipmi_ssif
      udevadm settle
   }

   add_bin() {
      cat > "${BIN_D}/$1"
      chmod "${2:-755}" "${BIN_D}/$1"
   }
   add_ipmi_config() {
      cat > "${IPMI_CONFIG_D}/$1"
      chmod "${2:-644}" "${IPMI_CONFIG_D}/$1"
   }

   add_bin "maas-ipmi-autodetect-tool" <<"END_MAAS_IPMI_AUTODETECT_TOOL"
   {{for line in maas_ipmi_autodetect_tool_py.splitlines()}}
   {{line}}
   {{endfor}}
   END_MAAS_IPMI_AUTODETECT_TOOL

   add_bin "maas-ipmi-autodetect" <<"END_MAAS_IPMI_AUTODETECT"
   {{for line in maas_ipmi_autodetect_py.splitlines()}}
   {{line}}
   {{endfor}}
   END_MAAS_IPMI_AUTODETECT

   add_bin "maas-moonshot-autodetect" <<"END_MAAS_MOONSHOT_AUTODETECT"
   {{for line in maas_moonshot_autodetect_py.splitlines()}}
   {{line}}
   {{endfor}}
   END_MAAS_MOONSHOT_AUTODETECT

   add_bin "maas-wedge-autodetect" <<"END_MAAS_WEDGE_AUTODETECT"
   {{for line in maas_wedge_autodetect_sh.splitlines()}}
   {{line}}
   {{endfor}}
   END_MAAS_WEDGE_AUTODETECT

   add_bin "maas-enlist" <<"END_MAAS_ENLIST"
   {{for line in maas_enlist_sh.splitlines()}}
   {{line}}
   {{endfor}}
   END_MAAS_ENLIST

   # we could obtain the interface that booted from the kernel cmdline
   # thanks to 'IPAPPEND' (http://www.syslinux.org/wiki/index.php/SYSLINUX)
   url="{{server_url}}"

   # Early check to see if this machine already exists in MAAS. Already
   # existing machines just stop running and power off. We do not want to
   # update the power parameters of an existing machine.
   maas-enlist --serverurl "$url" --exists
   if [ $? -eq 1 ]; then
      msg="already registered on '$url'; skipping enlistment"
      echo
      echo "== $(date -R): $msg"
      maas-enlist --serverurl "$url" --in-action
      if [ $? -eq 1 ]; then
         msg="rebooting the machine to resume action"
         echo
         echo "=== $(date -R): $msg"
         reboot
      fi
      sleep 10
      exit 0
   fi

   # load ipmi modules
   load_modules
   pargs=""
   if $IPMI_CHANGE_STATIC_TO_DHCP; then
      pargs="--dhcp-if-static"
   fi
   set -x
   power_type=$(maas-ipmi-autodetect-tool)
   if [ -z $power_type ]; then
       power_type=$(maas-wedge-autodetect --check) || power_type=""
   fi
   case "$power_type" in
       ipmi)
           power_params=$(maas-ipmi-autodetect --configdir "$IPMI_CONFIG_D" ${pargs} --commission-creds) &&
             [ -n "${power_params}" ] && power_params=${power_params%.}
           ;;
       moonshot)
           power_params=$(maas-moonshot-autodetect --commission-creds) &&
             [ -n "${power_params}" ] && power_params=${power_params%.}
           ;;
       wedge)
           power_params=$(maas-wedge-autodetect --get-enlist-creds) || power_params=""
           ;;
   esac

   # Try maas-enlist without power parameters on failure for older versions of
   # maas-enlist without power parameter support
   maas-enlist --serverurl "$url" ${power_params:+--power-params "${power_params}" --power-type "${power_type}"}>/tmp/enlist.out ||\
      maas-enlist --serverurl "$url" >/tmp/enlist.out
   if [ $? -eq 0 ]; then
      msg="successfully enlisted to '$url'"
      echo
      echo "=== $(date -R): $msg"
      cat  /tmp/enlist.out
      echo =============================================
      sleep 10
      # Uncomment the following to allow troubleshooting for an hour.
      # echo "ubuntu:ubuntu" | chpasswd
      # bfile="/tmp/block-poweroff"
      # { echo "#!/bin/sh"; echo "touch $bfile"; } > /etc/profile.d/A01-block.sh
      # sleep 3600
      # [ -e $bfile ] && exit 0
   else
      user="ubuntu"
      pass="ubuntu"

      echo "$user:$pass" | chpasswd
      bfile="/tmp/block-poweroff"
      { echo "#!/bin/sh"; echo "touch $bfile"; } > /etc/profile.d/A01-block.sh
      chmod 755 /etc/profile.d/A01-block.sh
      echo
      echo =============================================
      echo "failed to enlist system maas server"
      echo "sleeping 60 seconds then poweroff"
      echo
      echo "login with '$user:$pass' to debug and disable poweroff"
      echo
      cat /tmp/enlist.out
      echo =============================================
      sleep 60
      [ -e $bfile ] && exit 0
   fi



packages: [ freeipmi-tools, openipmi, ipmitool, archdetect-deb, sshpass ]
output: {all: '| tee -a /var/log/cloud-init-output.log'}
runcmd:
 - [ sh, -c, *maas_enlist ]
