## This file is used as input to deployment script, which amends it as needed.
## More specifically, we need to add environment files for as many nodes as we
## are going to deploy.

write_files:
  - path: /opt/bin/curl-retry.sh
    permissions: '0755'
    owner: root
    content: |
      #!/bin/sh -x
      until curl $@
      do sleep 1
      done

coreos:
  update:
    group: stable
    reboot-strategy: off
  units:
    - name: systemd-networkd-wait-online.service
      drop-ins:
        - name: 50-check-github-is-reachable.conf
          content: |
            [Service]
            ExecStart=/bin/sh -x -c \
              'until curl --silent --fail https://status.github.com/api/status.json | grep -q \"good\"; do sleep 2; done'

    - name: weave-network.target
      enable: true
      content: |
        [Unit]
        Description=Weave Network Setup Complete
        Documentation=man:systemd.special(7)
        RefuseManualStart=no
        After=network-online.target
        [Install]
        WantedBy=multi-user.target
        WantedBy=kubernetes-master.target
        WantedBy=kubernetes-node.target

    - name: kubernetes-master.target
      enable: true
      command: start
      content: |
        [Unit]
        Description=Kubernetes Cluster Master
        Documentation=http://kubernetes.io/
        RefuseManualStart=no
        After=weave-network.target
        Requires=weave-network.target
        ConditionHost=kube-00
        Wants=kube-apiserver.service
        Wants=kube-scheduler.service
        Wants=kube-controller-manager.service
        Wants=kube-proxy.service
        [Install]
        WantedBy=multi-user.target

    - name: kubernetes-node.target
      enable: true
      command: start
      content: |
        [Unit]
        Description=Kubernetes Cluster Node
        Documentation=http://kubernetes.io/
        RefuseManualStart=no
        After=weave-network.target
        Requires=weave-network.target
        ConditionHost=!kube-00
        Wants=kube-proxy.service
        Wants=kubelet.service
        [Install]
        WantedBy=multi-user.target

    - name: 10-weave.network
      runtime: false
      content: |
        [Match]
        Type=bridge
        Name=weave*
        [Network]

    - name: install-weave.service
      enable: true
      content: |
        [Unit]
        After=network-online.target
        After=docker.service
        Before=weave.service
        Description=Install Weave
        Documentation=http://docs.weave.works/
        Requires=network-online.target
        [Service]
        EnvironmentFile=-/etc/weave.%H.env
        EnvironmentFile=-/etc/weave.env
        Type=oneshot
        RemainAfterExit=yes
        TimeoutStartSec=0
        ExecStartPre=/bin/mkdir -p /opt/bin/
        ExecStartPre=/opt/bin/curl-retry.sh \
          --silent \
          --location \
          git.io/weave \
          --output /opt/bin/weave
        ExecStartPre=/usr/bin/chmod +x /opt/bin/weave
        ExecStart=/opt/bin/weave setup
        [Install]
        WantedBy=weave-network.target
        WantedBy=weave.service

    - name: weaveproxy.service
      enable: true
      content: |
        [Unit]
        After=install-weave.service
        After=docker.service
        Description=Weave proxy for Docker API
        Documentation=http://docs.weave.works/
        Requires=docker.service
        Requires=install-weave.service
        [Service]
        EnvironmentFile=-/etc/weave.%H.env
        EnvironmentFile=-/etc/weave.env
        ExecStartPre=/opt/bin/weave launch-proxy --rewrite-inspect --without-dns
        ExecStart=/usr/bin/docker attach weaveproxy
        Restart=on-failure
        ExecStop=/opt/bin/weave stop-proxy
        [Install]
        WantedBy=weave-network.target

    - name: weave.service
      enable: true
      content: |
        [Unit]
        After=install-weave.service
        After=docker.service
        Description=Weave Network Router
        Documentation=http://docs.weave.works/
        Requires=docker.service
        Requires=install-weave.service
        [Service]
        TimeoutStartSec=0
        EnvironmentFile=-/etc/weave.%H.env
        EnvironmentFile=-/etc/weave.env
        ExecStartPre=/opt/bin/weave launch-router $WEAVE_PEERS
        ExecStart=/usr/bin/docker attach weave
        Restart=on-failure
        ExecStop=/opt/bin/weave stop-router
        [Install]
        WantedBy=weave-network.target

    - name: weave-expose.service
      enable: true
      content: |
        [Unit]
        After=install-weave.service
        After=weave.service
        After=docker.service
        Documentation=http://docs.weave.works/
        Requires=docker.service
        Requires=install-weave.service
        Requires=weave.service
        [Service]
        Type=oneshot
        RemainAfterExit=yes
        TimeoutStartSec=0
        EnvironmentFile=-/etc/weave.%H.env
        EnvironmentFile=-/etc/weave.env
        ExecStart=/opt/bin/weave expose
        ExecStop=/opt/bin/weave hide
        [Install]
        WantedBy=weave-network.target

    - name: install-kubernetes.service
      enable: true
      content: |
        [Unit]
        After=network-online.target
        Before=kube-apiserver.service
        Before=kube-controller-manager.service
        Before=kubelet.service
        Before=kube-proxy.service
        Description=Download Kubernetes Binaries
        Documentation=http://kubernetes.io/
        Requires=network-online.target
        [Service]
        Environment=KUBE_RELEASE_TARBALL=https://github.com/kubernetes/kubernetes/releases/download/v1.1.2/kubernetes.tar.gz
        ExecStartPre=/bin/mkdir -p /opt/
        ExecStart=/opt/bin/curl-retry.sh --silent --location $KUBE_RELEASE_TARBALL --output /tmp/kubernetes.tgz
        ExecStart=/bin/tar xzvf /tmp/kubernetes.tgz -C /tmp/
        ExecStart=/bin/tar xzvf /tmp/kubernetes/server/kubernetes-server-linux-amd64.tar.gz -C /opt
        ExecStartPost=/bin/chmod o+rx -R /opt/kubernetes
        ExecStartPost=/bin/ln -s /opt/kubernetes/server/bin/kubectl /opt/bin/
        ExecStartPost=/bin/mv /tmp/kubernetes/examples/guestbook /home/core/guestbook-example
        ExecStartPost=/bin/chown core. -R /home/core/guestbook-example
        ExecStartPost=/bin/rm -rf /tmp/kubernetes
        ExecStartPost=/bin/sed 's/# type: LoadBalancer/type: NodePort/' -i /home/core/guestbook-example/frontend-service.yaml
        RemainAfterExit=yes
        Type=oneshot
        [Install]
        WantedBy=kubernetes-master.target
        WantedBy=kubernetes-node.target

    - name: kube-apiserver.service
      enable: true
      content: |
        [Unit]
        After=install-kubernetes.service
        Before=kube-controller-manager.service
        Before=kube-scheduler.service
        ConditionFileIsExecutable=/opt/kubernetes/server/bin/kube-apiserver
        Description=Kubernetes API Server
        Documentation=http://kubernetes.io/
        Wants=install-kubernetes.service
        ConditionHost=kube-00
        [Service]
        ExecStart=/opt/kubernetes/server/bin/kube-apiserver \
            --insecure-bind-address=0.0.0.0 \
            --advertise-address=$public_ipv4 \
            --port=8080 \
            $ETCD_SERVERS \
            --service-cluster-ip-range=10.16.0.0/12 \
            --cloud-provider=vagrant \
            --logtostderr=true
        Restart=always
        RestartSec=10
        [Install]
        WantedBy=kubernetes-master.target

    - name: kube-scheduler.service
      enable: true
      content: |
        [Unit]
        After=kube-apiserver.service
        After=install-kubernetes.service
        ConditionFileIsExecutable=/opt/kubernetes/server/bin/kube-scheduler
        Description=Kubernetes Scheduler
        Documentation=http://kubernetes.io/
        Wants=kube-apiserver.service
        ConditionHost=kube-00
        [Service]
        ExecStart=/opt/kubernetes/server/bin/kube-scheduler \
            --logtostderr=true \
            --master=127.0.0.1:8080
        Restart=always
        RestartSec=10
        [Install]
        WantedBy=kubernetes-master.target

    - name: kube-controller-manager.service
      enable: true
      content: |
        [Unit]
        After=install-kubernetes.service
        After=kube-apiserver.service
        ConditionFileIsExecutable=/opt/kubernetes/server/bin/kube-controller-manager
        Description=Kubernetes Controller Manager
        Documentation=http://kubernetes.io/
        Wants=kube-apiserver.service
        Wants=install-kubernetes.service
        ConditionHost=kube-00
        [Service]
        ExecStart=/opt/kubernetes/server/bin/kube-controller-manager \
            --master=127.0.0.1:8080 \
            --logtostderr=true
        Restart=always
        RestartSec=10
        [Install]
        WantedBy=kubernetes-master.target

    - name: kubelet.service
      enable: true
      content: |
        [Unit]
        After=install-kubernetes.service
        ConditionFileIsExecutable=/opt/kubernetes/server/bin/kubelet
        Description=Kubernetes Kubelet
        Documentation=http://kubernetes.io/
        Wants=install-kubernetes.service
        ConditionHost=!kube-00
        [Service]
        ExecStartPre=/bin/mkdir -p /etc/kubernetes/manifests/
        ExecStart=/opt/kubernetes/server/bin/kubelet \
            --docker-endpoint=unix:/var/run/weave/weave.sock \
            --address=0.0.0.0 \
            --port=10250 \
            --hostname-override=%H \
            --api-servers=http://kube-00:8080 \
            --logtostderr=true \
            --cluster-dns=10.16.0.3 \
            --cluster-domain=kube.local \
            --config=/etc/kubernetes/manifests/
        Restart=always
        RestartSec=10
        [Install]
        WantedBy=kubernetes-node.target

    - name: kube-proxy.service
      enable: true
      content: |
        [Unit]
        After=install-kubernetes.service
        ConditionFileIsExecutable=/opt/kubernetes/server/bin/kube-proxy
        Description=Kubernetes Proxy
        Documentation=http://kubernetes.io/
        Wants=install-kubernetes.service
        [Service]
        ExecStart=/opt/kubernetes/server/bin/kube-proxy \
             --master=http://kube-00:8080 \
             --logtostderr=true
        Restart=always
        RestartSec=10
        [Install]
        WantedBy=kubernetes-master.target
        WantedBy=kubernetes-node.target

    - name: kube-create-addons.service
      enable: true
      content: |
        [Unit]
        After=install-kubernetes.service
        ConditionFileIsExecutable=/opt/kubernetes/server/bin/kubectl
        ConditionPathIsDirectory=/etc/kubernetes/addons/
        ConditionHost=kube-00
        Description=Kubernetes Addons
        Documentation=http://kubernetes.io/
        Wants=install-kubernetes.service
        Wants=kube-apiserver.service
        [Service]
        Type=oneshot
        RemainAfterExit=no
        ExecStart=/bin/bash -c 'until /opt/kubernetes/server/bin/kubectl create -f /etc/kubernetes/addons/; do sleep 2; done'
        SuccessExitStatus=1
        [Install]
        WantedBy=kubernetes-master.target
