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

# To skip Docker pre-load of calico/node and busybox, run vagrant up with:
#    vagrant up --provision-with file,shell

# The version of the calico docker images to install.  This is used to pre-load
# the calico/node and calico/node-libnetwork images which slows down the
# install process, but speeds up the tutorial.
#
# This version should match the version required by calicoctl installed in the
# cloud config files.
calico_node_ver = "v0.22.0"
calico_libnetwork_ver = "v0.9.0"

# Size of the cluster created by Vagrant
num_instances=2

# Change basename of the VM
instance_name_prefix="calico"

# Official CoreOS channel from which updates should be downloaded
update_channel='stable'

Vagrant.configure("2") do |config|
  # always use Vagrants insecure key
  config.ssh.insert_key = false

  config.vm.box = "coreos-%s" % update_channel
  config.vm.box_version = ">= 1122.0.0"
  config.vm.box_url = "http://%s.release.core-os.net/amd64-usr/current/coreos_production_vagrant.json" % update_channel

  config.vm.provider :virtualbox do |v|
    # On VirtualBox, we don't have guest additions or a functional vboxsf
    # in CoreOS, so tell Vagrant that so it can be smarter.
    v.check_guest_additions = false
    v.functional_vboxsf     = false
  end

  # plugin conflict
  if Vagrant.has_plugin?("vagrant-vbguest") then
    config.vbguest.auto_update = false
  end

  # Set up each box
  (1..num_instances).each do |i|
    vm_name = "%s-%02d" % [instance_name_prefix, i]
    config.vm.define vm_name do |host|
      host.vm.hostname = vm_name

      ip = "172.17.8.#{i+100}"
      host.vm.network :private_network, ip: ip

      # Pre-load the calico/node image.  This slows down the vagrant up
      # command, but speeds up the actual tutorial.
      host.vm.provision :docker, images: ["calico/node-libnetwork:#{calico_libnetwork_ver}", "calico/node:#{calico_node_ver}", "busybox:latest"]

      # Use a different cloud-init on the first server.
      if i == 1
        host.vm.provision :file, :source => "../cloud-config/user-data-first", :destination => "/tmp/vagrantfile-user-data"
        host.vm.provision :shell, :inline => "mv /tmp/vagrantfile-user-data /var/lib/coreos-vagrant/", :privileged => true
      else
        host.vm.provision :file, :source => "../cloud-config/user-data-others", :destination => "/tmp/vagrantfile-user-data"
        host.vm.provision :shell, :inline => "mv /tmp/vagrantfile-user-data /var/lib/coreos-vagrant/", :privileged => true
      end

      config.vm.post_up_message = "Vagrant has finished but cloud-init might still be executing.
      Check the progress using systemctl status -f"
    end
  end
end
