#cloud-config
users:
  - name: dev
    groups:
      - sudo
      - docker
    lock_passwd: false
    shell: /bin/bash
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
#    ssh-authorized-keys:
#      - ssh-rsa <SNIP>


pacakge_update: true
package_upgrade: true
packages:
  - jq
  - curl
  - software-properties-common

write_files:
- path: /config/provision-root.sh
  permissions: '0744'
  content: |
    #!/usr/bin/env bash
    set -ex
    # set -o errexit
    # set -o pipefail

    if [ -z $1 ]; then
      HLF_VERSION=2.2.0
    else
      HLF_VERSION=$1
    fi

    if [ ${HLF_VERSION:0:4} = '2.5.'  -o ${HLF_VERSION:0:4} = '2.4.']; then
      export GO_VERSION=1.18.10
    elif [ ${HLF_VERSION:0:4} = '2.2.' -o ${HLF_VERSION:0:4} = '2.3.' ]; then
      export GO_VERSION=1.14.11
    elif [ ${HLF_VERSION:0:4} = '2.0.' -o ${HLF_VERSION:0:4} = '2.1.' ]; then
      export GO_VERSION=1.13.15
    elif [ ${HLF_VERSION:0:4} = '1.2.' -o ${HLF_VERSION:0:4} = '1.3.' -o ${HLF_VERSION:0:4} = '1.4.' ]; then
      export GO_VERSION=1.10.4
    elif [ ${HLF_VERSION:0:4} = '1.1.' ]; then
      export GO_VERSION=1.9.7
    else
      >&2 echo "Unexpected HLF_VERSION ${HLF_VERSION}"
      >&2 echo "HLF_VERSION must be a 1.1.x, 1.2.x, 1.3.x, 1.4.x, 2.0.x, 2.1.x, 2.2.x, 2.3.x, 2.4.x, or 2.5.x version"
      exit 1
    fi


    # APT setup for kubectl
    curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
    apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"

    # Install kubectl
    apt-get -y --no-upgrade install kubectl

    # Install yq
    YQ_VERSION=4.23.1
    if [ ! -x "/usr/local/bin/yq" ]; then
      curl --fail --silent --show-error -L "https://github.com/mikefarah/yq/releases/download/v${YQ_VERSION}/yq_linux_amd64" -o /usr/local/bin/yq
      chmod 755 /usr/local/bin/yq
    fi

    # Install docker compose
    if [ ! -x /usr/local/bin/docker-compose ]; then
      curl --fail --silent --show-error -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
      chmod 755 /usr/local/bin/docker-compose
    fi

    # Install kind
    KIND_VERSION=0.14.0
    if [ ! -x "/usr/local/bin/kind" ]; then
      curl --fail --silent --show-error -L "https://kind.sigs.k8s.io/dl/v${KIND_VERSION}/kind-linux-amd64" -o /usr/local/bin/kind
      chmod 755 /usr/local/bin/kind
    fi

    # Install k9s
    K9S_VERSION=0.25.3
    if [ ! -x "/usr/local/bin/k9s" ]; then
      curl --fail --silent --show-error -L "https://github.com/derailed/k9s/releases/download/v${K9S_VERSION}/k9s_Linux_x86_64.tar.gz" -o "/tmp/k9s_Linux_x86_64.tar.gz"
      tar -zxf "/tmp/k9s_Linux_x86_64.tar.gz" -C /usr/local/bin k9s
      chown root:root /usr/local/bin/k9s
      chmod 755 /usr/local/bin/k9s
    fi

    # Install ccmetadata
    CCMETADATA_VERSION=0.2.0
    if [ ! -x "/usr/local/bin/ccmetadata" ]; then
      curl --fail --silent --show-error -L "https://github.com/hyperledgendary/ccmetadata/releases/download/v${CCMETADATA_VERSION}/ccmetadata-Linux-X64.tgz" -o "/tmp/ccmetadata-Linux-X64.tgz"
      tar -zxf "/tmp/ccmetadata-Linux-X64.tgz" -C /usr/local/bin ccmetadata
      chown root:root /usr/local/bin/ccmetadata
      chmod 755 /usr/local/bin/ccmetadata
    fi

    # Install just
    JUST_VERSION=1.2.0
    if [ ! -x "/usr/local/bin/just" ]; then
      curl --proto '=https' --tlsv1.2 -sSf https://just.systems/install.sh | bash -s -- --tag ${JUST_VERSION} --to /usr/local/bin
      chown root:root /usr/local/bin/just
      chmod 755 /usr/local/bin/just
    fi

- path: /config/provision-user.sh
  permissions: '0777'
  owner: ubuntu:ubuntu
  content: |
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] || curl --fail --silent --show-error -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.3/install.sh | bash
    . "$NVM_DIR/nvm.sh"

    # Install latest node v16.x, latest typescript, weft
    nvm install 16
    npm install -g typescript
    npm install -g @hyperledger-labs/weft

# Use Google DNS as the mac resolvers are not 100% reliable for the npm dependency builds in Docker
bootcmd:
  - printf "[Resolve]\nDNS=8.8.8.8" > /etc/systemd/resolved.conf
  - [systemctl, restart, systemd-resolved]

runcmd:
  - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
  - add-apt-repository "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  - apt-get update -y
  - apt-get install -y docker.io
  - usermod -a -G docker ubuntu
  - /config/provision-root.sh
  - su -c /config/provision-user.sh ubuntu

final_message: "The system is finally up, after $UPTIME seconds"
