# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"

  # Expose ports to the host.
  config.vm.network "forwarded_port", guest: 4646, host: 4646, host_ip: "127.0.0.1" # Nomad HTTP API/UI
  config.vm.network "forwarded_port", guest: 8500, host: 8500, host_ip: "127.0.0.1" # Consul HTTP API/UI
  config.vm.network "forwarded_port", guest: 9090, host: 9090, host_ip: "127.0.0.1" # Prometheus HTTP API/UI

  # VM configuration.
  config.vm.provider "virtualbox" do |vb|
    vb.customize [ "modifyvm", :id, "--uartmode1", "file", File::NULL ] # https://bugs.launchpad.net/cloud-images/+bug/1874453
    vb.memory = "2048"
    vb.cpus = 2
  end

  # Provision demo dependencies.
  #   - Downloads and install Nomad, Consul and Docker
  # Only runs when the VM is created.
  config.vm.provision "deps", type: "shell", inline: <<-SHELL

    export nomad_version=1.0.0-beta2+ent

    export DEBIAN_FRONTEND=noninteractive
    mkdir /tmp/downloads

    # Install dependencies.
    apt-get update
    apt-get install -y \
      apt-transport-https \
      ca-certificates \
      curl \
      git \
      gnupg-agent \
      hey \
      redis-server \
      jq \
      software-properties-common \
      zip

    # Add External Dependency Repositories
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - 2>&1
    curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add - 2>&1

    add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
    apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

    # Update and install external deps.
    apt-get update
    apt-get install -y \
      consul \
      containerd.io \
      docker-ce \
      docker-ce-cli 

    usermod -aG docker vagrant


    pushd /tmp/downloads

    mkdir -p /opt/hashicorp/bin

    # Download and install Nomad.
    curl --silent --show-error --remote-name-all \
      https://releases.hashicorp.com/nomad/${nomad_version}/nomad_${nomad_version}_linux_amd64.zip
    unzip nomad_${nomad_version}_linux_amd64.zip
    mv nomad /opt/hashicorp/bin/nomad-${nomad_version}
    chmod +x /opt/hashicorp/bin/nomad-${nomad_version}
    ln -s /opt/hashicorp/bin/nomad-${nomad_version} /usr/local/bin/nomad

    # Download and install demo files
    git clone https://github.com/hashicorp/nomad-autoscaler nomad-autoscaler.git 2>&1 
    cd nomad-autoscaler.git/demo/vagrant/dynamic-app-sizing/
    mkdir -p /home/vagrant/nomad-autoscaler/
    mv files jobs ../shared /home/vagrant/nomad-autoscaler/
    
    chown -R vagrant:vagrant /home/vagrant/nomad-autoscaler/
    popd
    rm -fr /tmp/downloads
    ln -s /usr/bin/consul /usr/local/bin/consul
  SHELL

  # Setup demo dependencies.
  #   - Create daemons for Nomad and Consul
  # Runs everytime the VM starts.
  config.vm.provision "app:setup", type: "shell", run: "always", inline: <<-SHELL
    # create paths for Nomad host volumes
    mkdir -p /opt/nomad/volumes
    pushd /opt/nomad/volumes
    mkdir -p grafana
    chown 472:472 grafana
    popd

    # configure Nomad and Consul daemons
    cp /home/vagrant/nomad-autoscaler/files/* /home/vagrant/nomad-autoscaler/shared/
    pushd /home/vagrant/nomad-autoscaler/shared
    for t in consul nomad
    do
      mkdir -p /etc/${t}.d
      rm /etc/${t}.d/*
      cp ${t}.hcl /etc/${t}.d/
      cp ${t}.service /etc/systemd/system/
      systemctl enable $t 2>&1 
      systemctl start $t
    done
    popd
  SHELL

end