# -*- mode: ruby -*-
# vi: set ft=ruby :
#
#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#  
#    http://www.apache.org/licenses/LICENSE-2.0
#  
#  Unless required by applicable law or agreed to in writing,
#  software distributed under the License is distributed on an
#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
#  specific language governing permissions and limitations
#  under the License.
#

VAGRANTFILE_API_VERSION = '2'

Vagrant.require_version '>= 1.5.0'

unless Vagrant.has_plugin?('vagrant-berkshelf')
  raise 'vagrant-berkshelf is not installed!'
end

unless Vagrant.has_plugin?('vagrant-omnibus')
  raise 'vagrant-omnibus is not installed!'
end

xenserver_networking_script = File.join(File.dirname(__FILE__), '../common/', 'configure-network.sh')

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.define 'xenserver' do |xenserver|
    xenserver.vm.box = 'duffy/xenserver'

    ## Map host only networks and the adapters
    xenserver.vm.provider 'virtualbox' do |v|
      v.customize ['modifyvm', :id, '--hostonlyadapter2', 'vboxnet0']
      v.customize ['modifyvm', :id, '--nicpromisc2', 'allow-all']
      v.customize ["modifyvm", :id, '--nictype2', 'Am79C973']
    end

    # Configure Interface
    ## Configure Management Interface
    xenserver.vm.provision 'shell' do |s|
      s.path = xenserver_networking_script
      s.args = %w(eth1 192.168.22.10 255.255.255.0 MGMT)
    end

  end

  config.vm.define 'management' do |management|
    management.vm.box = 'bento/centos-6.7'

    management.vm.network :private_network, :auto_config => true, :ip => '192.168.22.5'

    management.vm.network 'forwarded_port', guest: 3306, host: 3306

    management.vm.provider 'virtualbox' do |v|
      v.customize ['modifyvm', :id, '--memory', 512]
      v.customize ['modifyvm', :id, '--hostonlyadapter2', 'vboxnet0']
      v.customize ["modifyvm", :id, '--nictype2', 'Am79C973']
    end

    management.omnibus.chef_version = "11.16.4" 
    management.berkshelf.berksfile_path = File.join(File.dirname(__FILE__), 'Berksfile')
    management.berkshelf.enabled = true

    CHEF_CONFIGURATION = JSON.parse(Pathname(__FILE__).dirname.join('chef_configuration.json').read)

    management.vm.provision 'chef_solo' do |chef|
      chef.run_list = CHEF_CONFIGURATION.delete('run_list')
      chef.json = CHEF_CONFIGURATION
    end
  end
end
