<%
cloud_config = node['dcos-grid']['cloud-config']
dcos_config = node['dcos-grid']['bootstrap']['config']
-%>
#cloud-config

hostname: <%= cloud_config['hostname'] %>

# include one or more SSH public keys for the core user
ssh_authorized_keys:
<%
cloud_config['ssh_authorized_keys'].each {|key|
-%>
  - "<%= key %>"
<%
}
-%>

<%
case cloud_config['target_platform']
when 'coreos' then
-%> 
coreos:
  units:
    - name: systemd-networkd.service
      command: stop
    - name: 10-static.network
      runtime: true
      content: |
        [Match]
        Name=enp0s8

        [Network]
        Address=<ip_address>/24
        #Gateway=192.168.56.1
    - name: 20-dhcp.network
      runtime: true
      content: |
        [Match]
        Name=en*

        [Network]
        DHCP=yes
    - name: down-interfaces.service
      command: start
      content: |
        [Service]
        Type=oneshot
        ExecStart=/usr/bin/ip link set enp0s8 down
        ExecStart=/usr/bin/ip addr flush dev enp0s8
    - name: systemd-networkd.service
      command: restart
<%
end
-%>

write_files:
  - path: "/etc/resolv.conf"
    permissions: "0644"
    owner: "root"
    content: |
<%
dcos_config['resolvers'].each {|resolv|
-%>
      nameserver <%= resolv %>
<%
}
-%>
  - path: "<%= cloud_config['target_dir'] %>/genconf/config.yaml"
    permissions: "0644"
    owner: "<%= cloud_config['target_owner'] %>"
    content: |
      ---
<%
dcos_config.each {|key, value|
  if !@config_params.include?(key) || value.nil? || value.empty? then
    Chef::Log.warn("invalid config key: #{key}")
    next
  end

  if value.is_a?(String) then
-%>
      <%= key %>: <%= value %>
<%
  elsif value.is_a?(Array) then
-%>
      <%= key %>:
<%
    value.each {|val|
-%>
      - <%= val %>
<%
    }
  end
}
-%>
  - path: "<%= cloud_config['target_dir'] %>/genconf/ip-detect"
    permissions: "0755"
    owner: "<%= cloud_config['target_owner'] %>"
    content: |
      #!/usr/bin/env bash
      set -o nounset -o errexit
      export PATH=/usr/sbin:/usr/bin:$PATH
      iface=<%= node['dcos-grid']['bootstrap']['ip-detect']['interface'] %>  # or eth0,...
      echo $(ip addr show $iface | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -1)
  - path: "<%= cloud_config['target_dir'] %>/bootstrap_setup.sh"
    permissions: "0755"
    owner: "<%= cloud_config['target_owner'] %>"
    content: |
      #!/usr/bin/env bash
      PATH=.:/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin:`dirname $0`
      cd <%= cloud_config['target_dir'] %>
      echo 'Downloading DC/OS release ...'
      curl -O <%= node['dcos-grid']['dcos_release_url'] %>
      echo 'Generating DC/OS configurations ...'
      sudo bash dcos_generate_config.sh
      echo 'Launching Docker container for the bootstrap service ...'
      docker run -d -p <%= node['dcos-grid']['bootstrap']['port'] %>:80 -v $PWD/genconf/serve:/usr/share/nginx/html:ro nginx
  - path: "<%= cloud_config['target_dir'] %>/node_setup.sh"
    permissions: "0755"
    owner: "<%= cloud_config['target_owner'] %>"
    content: |
      #!/usr/bin/env bash
      PATH=.:/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin:`dirname $0`
      
      help() {
        echo 'Usage: node_setup.sh {master,slave,slave_public}'
      }
      
      ARGS="$@"
      ROLE=$1
      if [ $# != 1 ]; then
        help
        exit 1
      elif [ $ROLE = '-h' ]; then
        help
        exit 0
      fi
      
      case "$ROLE" in
        'master' | 'slave' | 'slave_public' )
          echo "Node Role: $ROLE"
          ;;
        * )
          help
          exit 1
          ;;
      esac
      
      if [ -d /opt/mesosphere ]; then
        echo -e '[ERROR] DC/OS is already installed.'
        exit 1
      fi
      if [ ! -d /tmp/dcos ]; then
        mkdir /tmp/dcos
      fi
      cd /tmp/dcos
      echo 'Downloading dcos_install.sh ...'
      curl -O <%= dcos_config['bootstrap_url'] %>/dcos_install.sh
      echo "Installing DC/OS $ROLE node ..."
      sudo bash dcos_install.sh $ROLE
  - path: "<%= cloud_config['target_dir'] %>/uninstall.sh"
    permissions: "0755"
    owner: "<%= cloud_config['target_owner'] %>"
    content: |
      #!/usr/bin/env bash
      PATH=.:/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin:`dirname $0`
      
      pkgpanda=/opt/mesosphere/bin/pkgpanda
      if [ -e "$pkgpanda" ]; then
        echo 'Uninstalling DC/OS packages...'
        sudo -i $pkgpanda uninstall
      fi
      if [ -e /opt/mesosphere ]; then
        echo 'Remove /opt/mesosphere and /etc/mesosphere ...'
        sudo rm -rf /opt/mesosphere /etc/mesosphere
      fi

