#cloud-config
<%#
kind: snippet
name: coreos_cloudconfig
%>
      coreos:
        etcd2:
<% if @host.params['etcd_discovery_url'] -%>
          discovery: <%= @host.params['etcd_discovery_url'] %>
<% end -%>
          advertise-client-urls: http://<%= @host.ip %>:2379,http://<%= @host.ip %>:4001
          initial-advertise-peer-urls: http://<%= @host.ip %>:2380,http://<%= @host.ip %>:7001
          listen-client-urls: http://0.0.0.0:2379,http://0.0.0.0:4001
          listen-peer-urls: http://0.0.0.0:2380,http://0.0.0.0:7001
        units:
          - name: etcd2.service
            command: start
          - name: fleet.service
            command: start
          - name: docker-tcp.socket
            command: start
            enable: yes
            content: |
              [Unit]
              Description=Docker Socket for the API

              [Socket]
              ListenStream=2375
              BindIPv6Only=both
              Service=docker.service

              [Install]
              WantedBy=sockets.target
          - name: enable-docker-tcp.service
            command: start
            content: |
              [Unit]
              Description=Enable the Docker Socket for the API

              [Service]
              Type=oneshot
              ExecStart=/usr/bin/systemctl enable docker-tcp.socket
<% if @host.subnet.respond_to?(:dhcp_boot_mode?) -%>
<% dhcp = @host.subnet.dhcp_boot_mode? && !@static -%>
<% else -%>
<% dhcp = !@static -%>
<% end -%>
<% unless dhcp -%>
          - name: systemd-networkd.service
            command: stop
          - name: static.network
            command: start
            content: |
              [Match]
              MACAddress=<%= @host.mac %>
              [Network]
              Gateway=<%= @host.subnet.gateway %>
              Address=<%= @host.ip %>/<%= @host.subnet.cidr %>
              DNS=<%= @host.subnet.dns_primary %>
              DNS=<%= @host.subnet.dns_secondary %>
          - name: down-interfaces.service
            command: start
            content: |
              [Service]
              Type=oneshot
              ExecStart=/usr/bin/bash -c 'for i in $(/usr/bin/ls /sys/class/net/); do [ $i == "lo" ] || (/usr/bin/ip link set $i down; /usr/bin/ip addr flush dev $i); done'
          - name: systemd-networkd.service
            command: restart
<% end -%>
<% if @host.params['ssh_authorized_keys'] -%>
      ssh_authorized_keys:
  <% @host.params['ssh_authorized_keys'].split(',').map(&:strip).each do |ssh_key| -%>
      - "<%= ssh_key %>"
  <% end -%>
<% else -%>
      users:
        - name: core
          passwd: <%= root_pass %>
<% end -%>
