Vagrant.configure(2) do |config|
  # create a new volume (vdi file)
  def attach_media(vb, controller_name, device, sizeMB, port)
    vdi_file = vb.name + device + ".vdi"
    unless File.exist?(vdi_file)
       vb.customize ["createhd", "--filename", vdi_file, "--size", sizeMB * 1024]
    end
    vb.customize ["storageattach", :id, "--storagectl", controller_name, "--port", port, 
                  "--device", 0, "--type", "hdd", "--medium", vdi_file]
  end

  # mount a volume using the specified filesystem on the given device
  def mount_volume(vm, fs, volume, device)
    vm.provision "shell", inline: <<-SHELL
      fs_type=$(blkid -o value -s TYPE /dev/sdb)
      if [ "$fs_type" != "#{fs}" ]; then
          echo mounting #{fs}...
          mkfs.#{fs} -f /dev/#{device}
          mkdir -p #{volume}
          rm -rf #{volume}/*
          mount /dev/#{device} #{volume}
          if ! grep -q "#{fs}" /etc/fstab ; then 
             echo "/dev/#{device} #{volume}  #{fs} defaults   0 0" >> /etc/fstab
          fi
      fi
  SHELL
  end

  def create_node(vm, node_name, cpus, ramMB)
    vm.define node_name do |node|
      node.vm.hostname = node_name
      node.vm.provider "virtualbox" do |vb|
        vb.name = node.vm.hostname
        vb.cpus = cpus
        vb.memory = ramMB
        attach_media(vb, "SATA Controller", "sdb", 10, 1) # 10Gb, /var/lib/gravity
        attach_media(vb, "SATA Controller", "sdc", 10, 2) # 10Gb, direct-lvm partition, no fs
      end
      # mount_volume(node.vm, "xfs", "/var/lib/gravity", "sdb")
    end
  end

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "boxcutter/centos72"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a private network
  config.vm.network "private_network", type: "dhcp"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.


  # fix grub to turn on memory cgroup
  config.vm.provision "shell", inline: <<-SHELL
    echo $(netstat -rn | grep UG | awk '{print $2}') opscenter.localhost.localdomain >> /etc/hosts
    echo "[SITE] Set up /etc/hosts "
  SHELL

  create_node(config.vm, "master", 1, 2200)
  create_node(config.vm, "node-1", 1, 800)
  create_node(config.vm, "node-2", 1, 800)
  # create_node(config.vm, "node-3", 1, 800)
end
