# -*- 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.
#

# Specify minimum Vagrant version and Vagrant API version
Vagrant.require_version ">= 1.8.1"
VAGRANTFILE_API_VERSION = "2"

# Update OS (Debian/RedHat based only)
UPDATE_OS_CMD = "(sudo apt-get update && sudo apt-get -y upgrade) || (sudo yum -y update)"

# Autocorrect Port Clashes
DEFAULT_AUTOCORRECT = false

# Require YAML module
require 'yaml'

# Read YAML file with box details
yaml_cfg = YAML.load_file(__dir__ + '/servers.yaml')

# Create boxes
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  # Iterate through server entries in YAML file
  yaml_cfg["servers"].each do |server|
    config.vm.define server["name"] do |server_config|

      server_config.vm.box = server["box"]

      server_config.vm.box_check_update = yaml_cfg["default_config"]["check_newer_vagrant_box"]

      if server.has_key?("ip")
        server_config.vm.network "private_network", ip: server["ip"]
      end

      if server.has_key?("forwarded_ports")
        server["forwarded_ports"].each do |port|
          if port.has_key?("autocorrect")
            autocorrect = true
          else
            autocorrect = DEFAULT_AUTOCORRECT
          end
          server_config.vm.network "forwarded_port", guest: port["guest"], host: port["host"], guest_ip: port["guest_ip"], autocorrect: autocorrect
        end
      end

      server_config.vm.hostname = server["name"]
      server_config.vm.provider :virtualbox do |vb|
        vb.name = server["name"]
        vb.memory = server["ram"]
        vb.cpus = server["cpus"]
      end

      if yaml_cfg["default_config"]["run_os_update"]
        server_config.vm.provision "shell", privileged: false, inline: UPDATE_OS_CMD
      end

      if server["shell"] && server["shell"]["cmd"]
        server["shell"]["cmd"].each do |cmd|
          server_config.vm.provision "shell", privileged: false, inline: cmd, env: server["shell"]["env"]
        end
      end

      server_config.vm.post_up_message = server["post_up_message"]
    end
  end
end

# ALTERING PORT FORWARDING
# If you are reading this you have likely been instructed by Vagrant to alter the example
# line below due to the forwarded port colliding with one alread in use on your system.
#
#   config.vm.network :forwarded_port, guest: 80, host: 1234
#
# This Vagrantfile does not define the port mapping here, instead you should alter
# the following line in the `servers.yaml` file in this directory.
#
#   host: 8081
#
# Change 8081 to a port that is not in use on your local machine before attempting
# to run vagrant up again.