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

DISKS = [
  { "name" => "home",
    "size" => 50,
    "port" => 1
  },
  { "name" => "opt",
    "size" => 15,
    "port" => 2 },
  { "name" => "docker",
    "size" => 40,
    "port" => 3 },
]

BOX_NAME = "devbox"

Vagrant.configure("2") do |config|

  config.vm.box = "geerlingguy/debian10"
  config.vm.network "private_network", ip: ENV["DEVBOX_IP"] || "192.168.200.4", adapter: 2
  config.vm.hostname = "devbox.devops.detss.corpintra.net"

  # proxy configuration
  if Vagrant.has_plugin?("vagrant-proxyconf") and ENV.key?("http_proxy")
    config.proxy.http = ENV["http_proxy"]
    config.proxy.https = ENV["https_proxy"]
    config.proxy.no_proxy = ENV["no_proxy"]
  end

  # configure disksize
  if Vagrant.has_plugin?("vagrant-disksize")
    config.disksize.size = "20GB"
  end

  # configure VM settings and additional disks
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "4096"
    vb.cpus = 4
    vb.name = BOX_NAME

    vb.gui = ENV["SHOW_GUI"] ||= "false"

    # https://www.virtualbox.org/manual/ch08.html
    vb.customize ["modifyvm", :id, "--vram", "128"]
    vb.customize ["modifyvm", :id, "--graphicscontroller", "vboxvga"]
    vb.customize ["modifyvm", :id, "--accelerate3d", "on"]
    vb.customize ["modifyvm", :id, "--accelerate2dvideo", "on"]

    # Adding a SATA controller that allows 4 hard drives
    unless File.exist?(DISKS[0]["name"] + ".vmdk")
      vb.customize ['storagectl', :id, '--name', 'SATA Controller', '--add', 'sata', '--portcount', 4]
    end

    DISKS.each do |disk|
      datadisk = disk["name"] + ".vmdk"
      unless File.exist?(datadisk)
        vb.customize ['createmedium', '--filename', datadisk,'--format', 'VMDK', '--variant', 'Standard', '--size', disk["size"] * 1024]
      end
      vb.customize ['storageattach', "devbox",  '--storagectl', 'SATA Controller', '--port', disk["port"], '--device', 0, '--type', 'hdd', '--medium', datadisk]
    end
  end

  # add ssh key
  if !File.file?("#{Dir.home}/.ssh/id_rsa.pub")
    puts "No SSH key found."
  end
  config.vm.provision "shell" do |s|
    ssh_pub_key = File.readlines("#{Dir.home}/.ssh/id_rsa.pub").first.strip
    s.inline = <<-SHELL
      if grep -sq "#{ssh_pub_key}" /home/vagrant/.ssh/authorized_keys; then
        echo "SSH keys already provisioned."
        exit 0;
      else
        echo #{ssh_pub_key} >> /home/vagrant/.ssh/authorized_keys
        mkdir -p /root/.ssh
        echo #{ssh_pub_key} >> /root/.ssh/authorized_keys
      fi
    SHELL
  end

  # Set the name of the VM. See: http://stackoverflow.com/a/17864388/100134
  config.vm.define BOX_NAME do |box|
  end

  # Run Ansible from the Vagrant VM
  config.vm.provision "ansible_local" do |ansible|
    # ansible.compatibility_mode = "2.0"
    ansible.playbook = "playbook.yml"
    ansible.become = true
    ansible.galaxy_role_file = "requirements.yml"
    ansible.galaxy_roles_path = './local_roles/:./roles/'
    ansible.galaxy_command = 'ansible-galaxy install --role-file=%{role_file} --roles-path=./roles'

    ansible.tags = ENV["ANSIBLE_TAGS"] ||= "all"
    ansible.verbose = ENV["ANSIBLE_VERBOSE"] ||= ""     # do not enable verbose by default
    # ansible.verbose = ENV["ANSIBLE_VERBOSE"] ||= "v"  # enables verbose mode by default
  end
end
