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

def get_mac(oui="28:b7:ad")
  "Generate a MAC address"
  nic = (1..3).map{"%0.2x"%rand(256)}.join(":")
  return "#{oui}:#{nic}"
end

def get_mac_cisco(oui="a0:00:00:00:00")
  "Generate a MAC address"
  nic = (1..1).map{"%0.2x"%rand(256)}.join(":")
  return "#{oui}:#{nic}"
end

cwd = Dir.pwd.split("/").last
username = ENV['USER']
domain_prefix = "#{username}_#{cwd}"

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

  config.vm.define "Router-1-vcp" do |node|
    guest_name = "Router-1-vcp"
    node.vm.box = "juniper/vmx-vcp"
    node.vm.box_version = "17.1R1.8"
    node.vm.guest = :tinycore
    node.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true

    node.ssh.insert_key = false

    node.vm.provider :libvirt do |domain|
      domain.default_prefix = "#{domain_prefix}"
      domain.cpus = 1
      domain.memory = 1024
      domain.disk_bus = "ide"
      domain.nic_adapter_count = 11
      domain.storage :file, :path => "#{username}-#{guest_name}-vmx-vcp-hdb-17.1R1.8-base.qcow2", :size => "196870144", :type => "qcow2", :bus => "ide", :device => "hdb", :allow_existing => true
      domain.storage :file, :path => "#{username}-#{guest_name}-vmx-vcp-hdc-17.1R1.8-base.img", :size => "16777216", :type => "raw", :bus => "ide", :device => "hdc", :allow_existing => true
    end
    add_volumes = [
      "virsh vol-create-as default #{username}-#{guest_name}-vmx-vcp-hdb-17.1R1.8-base.qcow2 196870144",
      "sleep 1",
      "virsh vol-upload --pool default #{username}-#{guest_name}-vmx-vcp-hdb-17.1R1.8-base.qcow2 /opt/vagrant/storage/vmx-vcp-hdb-17.1R1.8-base.qcow2",
      "sleep 1",
      "virsh vol-create-as default #{username}-#{guest_name}-vmx-vcp-hdc-17.1R1.8-base.img 16777216",
      "sleep 1",
      "virsh vol-upload --pool default #{username}-#{guest_name}-vmx-vcp-hdc-17.1R1.8-base.img /opt/vagrant/storage/vmx-vcp-hdc-17.1R1.8-base.img",
      "sleep 1"
    ]
    add_volumes.each do |i|
      node.trigger.before :up do |trigger|
        trigger.name = "add-volumes"
        trigger.info = "Adding Volumes"
        trigger.run = {inline: i}
      end
    end

    delete_volumes = [
      "virsh vol-delete #{username}-#{guest_name}-vmx-vcp-hdb-17.1R1.8-base.qcow2 default",
      "virsh vol-delete #{username}-#{guest_name}-vmx-vcp-hdc-17.1R1.8-base.img default"
    ]
    delete_volumes.each do |i|
      node.trigger.after :destroy do |trigger|
        trigger.name = "remove-volumes"
        trigger.info = "Removing Volumes"
        trigger.run = {inline: i}
      end
    end

    node.vm.network :private_network,
      # Link: Router-1-vcp-int1 <--> Router-1-vfp-int1
      :mac => "#{get_mac()}",
      :libvirt__tunnel_type => "udp",
      :libvirt__tunnel_local_ip => "127.255.255.1",
      :libvirt__tunnel_local_port => 10001,
      :libvirt__tunnel_ip => "127.255.255.2",
      :libvirt__tunnel_port => 10001,
      :libvirt__iface_name => "internal",
      auto_config: false

  end
  config.vm.define "Router-1-vfp" do |node|
    guest_name = "Router-1"
    node.vm.box = "juniper/vmx-vfp"
    node.vm.box_version = "17.1R1.8"
    node.vm.guest = :tinycore
    node.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true

    node.ssh.insert_key = false
    node.ssh.username = "root"

    node.vm.provider :libvirt do |domain|
      domain.default_prefix = "#{domain_prefix}"
      domain.cpus = 3
      domain.memory = 4096
      domain.disk_bus = "ide"
      domain.nic_adapter_count = 11
    end

    node.vm.network :private_network,
      # Link: Router-1-vfp-int1 <--> Router-1-vcp-int1
      :mac => "#{get_mac()}",
      :libvirt__tunnel_type => "udp",
      :libvirt__tunnel_local_ip => "127.255.255.2",
      :libvirt__tunnel_local_port => 10001,
      :libvirt__tunnel_ip => "127.255.255.1",
      :libvirt__tunnel_port => 10001,
      :libvirt__iface_name => "internal",
      auto_config: false

    node.vm.network :private_network,
      # Link: Router-1-vfp-ge-0/0/0
      :mac => "#{get_mac()}",
      :libvirt__tunnel_type => "udp",
      :libvirt__tunnel_local_ip => "127.255.255.2",
      :libvirt__tunnel_local_port => 10002,
      :libvirt__tunnel_ip => "127.255.253.2",
      :libvirt__tunnel_port => 10002,
      :libvirt__iface_name => "external",
      auto_config: false

      node.vm.network :private_network,
      # Link: Router-1-vfp-ge-0/0/1
      :mac => "#{get_mac()}",
      :libvirt__tunnel_type => "udp",
      :libvirt__tunnel_local_ip => "127.255.255.2",
      :libvirt__tunnel_local_port => 10077,
      :libvirt__tunnel_ip => "127.255.254.2",
      :libvirt__tunnel_port => 10077,
      :libvirt__iface_name => "external",
      auto_config: false

      node.vm.network :private_network,
      # Link: Router-1-vfp-ge-0/0/2
      :mac => "#{get_mac()}",
      :libvirt__tunnel_type => "udp",
      :libvirt__tunnel_local_ip => "127.255.255.2",
      :libvirt__tunnel_local_port => 10008,
      :libvirt__tunnel_ip => "127.255.252.2",
      :libvirt__tunnel_port => 10008,
      :libvirt__iface_name => "external",
      auto_config: false

      node.vm.network :private_network,
      # Link: Router-1-vfp-ge-0/0/3
      :mac => "#{get_mac()}",
      :libvirt__tunnel_type => "udp",
      :libvirt__tunnel_local_ip => "127.255.255.2",
      :libvirt__tunnel_local_port => 10082,
      :libvirt__tunnel_ip => "127.255.251.2",
      :libvirt__tunnel_port => 10082,
      :libvirt__iface_name => "external",
      auto_config: false

      node.vm.network :private_network,
      # Link: Router-1-vfp-ge-0/0/4
      :mac => "#{get_mac()}",
      :libvirt__tunnel_type => "udp",
      :libvirt__tunnel_local_ip => "127.255.255.2",
      :libvirt__tunnel_local_port => 10086,
      :libvirt__tunnel_ip => "127.255.246.2",
      :libvirt__tunnel_port => 10086,
      :libvirt__iface_name => "external",
      auto_config: false

      node.vm.network :private_network,
      # Link: Router-1-vfp-ge-0/0/5
      :mac => "#{get_mac()}",
      :libvirt__tunnel_type => "udp",
      :libvirt__tunnel_local_ip => "127.255.255.2",
      :libvirt__tunnel_local_port => 10087,
      :libvirt__tunnel_ip => "127.255.247.2",
      :libvirt__tunnel_port => 10087,
      :libvirt__iface_name => "external",
      auto_config: false

      node.vm.network :private_network,
      # Link: Router-1-vfp-ge-0/0/6
      :mac => "#{get_mac()}",
      :libvirt__tunnel_type => "udp",
      :libvirt__tunnel_local_ip => "127.255.255.2",
      :libvirt__tunnel_local_port => 10018,
      :libvirt__tunnel_ip => "127.255.254.2",
      :libvirt__tunnel_port => 10018,
      :libvirt__iface_name => "external",
      auto_config: false

      node.vm.network :private_network,
      # Link: Router-1-vfp-ge-0/0/7
      :mac => "#{get_mac()}",
      :libvirt__tunnel_type => "udp",
      :libvirt__tunnel_local_ip => "127.255.255.2",
      :libvirt__tunnel_local_port => 10006,
      :libvirt__tunnel_ip => "127.255.254.2",
      :libvirt__tunnel_port => 10006,
      :libvirt__iface_name => "external",
      auto_config: false

    end

# https://www.linkedin.com/pulse/network-automation-using-ansibleplaybookspart-4-ahnaf-shahriar/
# https://github.com/JNPRAutomate/ansible-junos-examples
    config.vm.provision "ansible" do |ansible|
      ansible.groups = {
          "all" => ["Router-1"]
      }
      ansible.playbook = "playbook.yml"
    end
end
