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

NODES = 2
MEMORY = 4096
CPUS = 2

Vagrant.configure("2") do |config|
    config.ssh.insert_key = false
    config.vm.box = "centos/7"

    # Override
    config.vm.provider :libvirt do |v,override|
        override.vm.synced_folder '.', '/home/vagrant/sync', disabled: true
        v.memory = MEMORY
        v.cpus = CPUS
        # Support remote libvirt
        $libvirt_host = ENV.fetch('LIBVIRT_HOST', '')
        $libvirt_user = ENV.fetch('LIBVIRT_USER', 'root')
        v.host = $libvirt_host
        if $libvirt_host.empty? || $libvirt_host.nil?
            v.connect_via_ssh = false
        else
            v.username = $libvirt_user
            v.connect_via_ssh = true
        end
    end

    # Make kub master
    config.vm.define :master do |master|
        master.vm.network :private_network, ip: "192.168.10.90"
        master.vm.host_name = "master"

        # View the documentation for the provider you're using for more
        # information on available options.
        master.vm.provision :ansible do |ansible|
            ansible.limit = "all"
            ansible.playbook = "site.yml"
            ansible.groups = {
                "master_node" => ["master"],
                "nodes" => (0..NODES-1).map {|j| "node#{j}"},
            }
            # Workaround for issue #644 on Vagrant < v1.8.6
            # Replace the ProxyCommand with the command specified by
            # vagrant ssh-config
            req = Gem::Requirement.new('<1.8.6')
            if req.satisfied_by?(Gem::Version.new(Vagrant::VERSION)) and not $libvirt_host.empty?
                ansible.raw_ssh_args = "-o 'ProxyCommand=ssh #{$libvirt_host} -l #{$libvirt_user} -i #{Dir.home}/.ssh/id_rsa nc %h %p'"
            end
        end
    end

    # Define the nodes' names and networks
    (0..NODES-1).each do |i|
        config.vm.define "node#{i}" do |node|
            node.vm.hostname = "node#{i}"
            node.vm.network :private_network, ip: "192.168.10.10#{i}"
        end
    end
end
