Vagrant.require_version ">= 1.8.7"

VAGRANTFILE_API_VERSION = "2"

MEMORY = 4096
CPU_COUNT = 2

vm_guest_ip = "192.168.33.10"
if ENV["VAGRANT_GUEST_IP"]
  vm_guest_ip = ENV["VAGRANT_GUEST_IP"]
end

# These are versioning variables in the roles. Each can be overridden, first
# with OPENEDX_RELEASE, and then with a specific environment variable of the
# same name but upper-cased.
VERSION_VARS = [
    'edx_platform_version',
    'configuration_version',
    'certs_version',
    'forum_version',
    'xqueue_version',
    'demo_version',
    'NOTIFIER_VERSION',
    'ECOMMERCE_VERSION',
    'ECOMMERCE_WORKER_VERSION',
    'PROGRAMS_VERSION',
]

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  # Creates a machine from a base Ubuntu 16.04 image for virtualbox
  config.vm.box = "boxcutter/ubuntu1604"

  config.vm.network :private_network, ip: vm_guest_ip, nic_type: "virtio"

  config.ssh.insert_key = true
  config.vm.synced_folder ".", "/vagrant", disabled: true

  config.vm.provider :virtualbox do |vb|
    vb.customize ["modifyvm", :id, "--memory", MEMORY.to_s]
    vb.customize ["modifyvm", :id, "--cpus", CPU_COUNT.to_s]

    # Allow DNS to work for Ubuntu 12.10 host
    # http://askubuntu.com/questions/238040/how-do-i-fix-name-service-for-vagrant-client
    vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
  end

  # Make LC_ALL default to en_US.UTF-8 instead of en_US.
  # See: https://github.com/mitchellh/vagrant/issues/1188
  config.vm.provision "shell", inline: 'echo \'LC_ALL="en_US.UTF-8"\' > /etc/default/locale'

  config.vm.provision :ansible do |ansible|
    # point Vagrant at the location of your playbook you want to run
    ansible.playbook = "../../../playbooks/edx_sandbox.yml"
    ansible.verbose = "vv"

    # Set extra-vars here instead of in the vagrant play so that
    # they are written out to /edx/etc/server-vars.yml which can
    # be used later when running ansible locally.
    ansible.extra_vars = {}
    VERSION_VARS.each do |var|
      if ENV['OPENEDX_RELEASE']
        ansible.extra_vars[var] = ENV['OPENEDX_RELEASE']
      end
      env_var = var.upcase
      if ENV[env_var]
        ansible.extra_vars[var] = ENV[env_var]
      end
    end

  end
end