#!/usr/bin/env bash
# Copyright (c) 2023 Tigera, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e
set -x

zone=$1
vm_name=$2
disk_size=$3
project=${GCP_PROJECT:-unique-caldron-775}
gcp_secret_key=${GCP_SECRET_KEY:-$HOME/secrets/secret.google-service-account-key.json}

gcloud config set project $project
gcloud auth activate-service-account --key-file=$gcp_secret_key

function create-vm() {
  gcloud --quiet compute instances create "${vm_name}" \
           --zone=${zone} \
           --machine-type=n1-standard-4 \
           --image=ubuntu-2004-focal-v20211102 \
           --image-project=ubuntu-os-cloud \
           --boot-disk-size=$disk_size \
           --boot-disk-type=pd-standard && \
  ssh_cmd="gcloud --quiet compute ssh --zone=${zone} ubuntu@${vm_name}"
  for ssh_try in $(seq 1 10); do
    echo "Trying to SSH in: $ssh_try"
    ${ssh_cmd} -- echo "Success" && break
    sleep 1
  done  && \
  ${ssh_cmd} -- sudo apt install apt-transport-https ca-certificates curl software-properties-common && \
  ${ssh_cmd} -- "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --yes --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg" && \
  ${ssh_cmd} -- "echo 'deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu focal stable' | sudo tee /etc/apt/sources.list.d/docker.list" && \
  ${ssh_cmd} -- sudo apt update -y && \
  ${ssh_cmd} -- sudo apt install -y --no-install-recommends git docker-ce=5:20.10.9~3-0~ubuntu-focal docker-ce-cli=5:20.10.9~3-0~ubuntu-focal containerd.io make iproute2 wireguard && \
  ${ssh_cmd} -- sudo usermod -a -G docker ubuntu && \
  ${ssh_cmd} -- sudo modprobe ipip && \
  ${ssh_cmd} -- 'if [ -s /etc/docker/daemon.json ] ; then cat /etc/docker/daemon.json | sed "\$d" | sed "\$s/\$/,/" > /tmp/daemon.json ; else echo -en {\\n > /tmp/daemon.json ; fi' && \
  ${ssh_cmd} -- 'cat >> /tmp/daemon.json << EOF
  "ipv6": true,
  "fixed-cidr-v6": "2001:db8:1::/64"
}
EOF' && \
  ${ssh_cmd} --  sudo mv /tmp/daemon.json /etc/docker/daemon.json && \
  ${ssh_cmd} --  sudo systemctl restart docker && \
  set +x && \
  echo "$DOCKERHUB_PASSWORD" | gcloud --quiet compute ssh --zone=${zone} "ubuntu@${vm_name}" -- docker login --username "$DOCKERHUB_USERNAME" --password-stdin && \
  set -x && \
  gcloud --quiet compute scp --zone=${zone} --recurse --compress "$(dirname $(pwd))" "ubuntu@${vm_name}:/home/ubuntu/calico" && \
  gcloud --quiet compute scp --zone=${zone} --recurse --compress "$HOME/secrets" "ubuntu@${vm_name}:/home/ubuntu/secrets"
}

function delete-vm() {
  gcloud --quiet compute instances delete "${vm_name}" --zone=${zone}
}

for attempt in $(seq 1 5); do
  echo "Trying to create text VM, attempt ${attempt}"
  if create-vm; then
    echo "Success!"
    exit 0
  else
    echo "Failed to create VM.  Tearing it down."
    delete-vm || true
  fi
done

echo "Out of retries"
exit 1
