#cloud-config
write_files:
  - path: /etc/systemd/system/configure-docker.service
    owner: "root:root"
    permissions: "0644"
    content: |
      [Unit]
      Description=Configure Docker

      [Service]
      Type=oneshot
      EnvironmentFile=/etc/sysconfig/heat-params
      ExecStart=/etc/sysconfig/configure-docker.sh

      [Install]
      WantedBy=multi-user.target

  - path: /etc/sysconfig/configure-docker.sh
    owner: "root:root"
    permissions: "0755"
    content: |
      #!/bin/sh

      if [ -n "${INSECURE_REGISTRY_URL}" ]; then
        DOCKER_OPTS="--insecure-registry ${INSECURE_REGISTRY_URL}"
      fi

      TEMPLATE=/etc/systemd/system/docker.service.d/docker-opts.conf
      mkdir -p $(dirname ${TEMPLATE})
      cat << EOF > $TEMPLATE
      [Service]
      Environment=DOCKER_OPTS=$DOCKER_OPTS
      EOF

      systemctl daemon-reload
      systemctl --no-block restart docker.service
