2012-12-23 06:47:06 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module ProviderVirtualBox
|
|
|
|
class Config < Vagrant.plugin("2", :config)
|
2013-01-31 05:03:02 +00:00
|
|
|
# Vagrant by default will make "smart" decisions to enable/disable
|
|
|
|
# the NAT DNS proxy. If this is set to `true`, then the DNS proxy
|
|
|
|
# will not be enabled, and it is up to the end user to do it.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
attr_accessor :auto_nat_dns_proxy
|
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
# An array of customizations to make on the VM prior to booting it.
|
|
|
|
#
|
|
|
|
# @return [Array]
|
2012-12-23 06:47:06 +00:00
|
|
|
attr_reader :customizations
|
|
|
|
|
2013-07-23 20:13:11 +00:00
|
|
|
# If true, unused network interfaces will automatically be deleted.
|
|
|
|
# This defaults to false because the detection does not work across
|
|
|
|
# multiple users, and because on Windows this operation requires
|
|
|
|
# administrative privileges.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
attr_accessor :destroy_unused_network_interfaces
|
|
|
|
|
2012-12-25 17:00:06 +00:00
|
|
|
# If set to `true`, then VirtualBox will be launched with a GUI.
|
2013-01-11 22:16:00 +00:00
|
|
|
#
|
|
|
|
# @return [Boolean]
|
2012-12-25 17:00:06 +00:00
|
|
|
attr_accessor :gui
|
|
|
|
|
2013-01-31 07:03:21 +00:00
|
|
|
# This should be set to the name of the machine in the VirtualBox
|
|
|
|
# GUI.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
attr_accessor :name
|
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
# The defined network adapters.
|
|
|
|
#
|
|
|
|
# @return [Hash]
|
|
|
|
attr_reader :network_adapters
|
|
|
|
|
2012-12-23 06:47:06 +00:00
|
|
|
def initialize
|
2013-01-31 05:03:02 +00:00
|
|
|
@auto_nat_dns_proxy = UNSET_VALUE
|
2013-01-11 22:16:00 +00:00
|
|
|
@customizations = []
|
2013-07-23 20:13:11 +00:00
|
|
|
@destroy_unused_network_interfaces = UNSET_VALUE
|
2013-01-31 07:03:21 +00:00
|
|
|
@name = UNSET_VALUE
|
2013-01-11 22:16:00 +00:00
|
|
|
@network_adapters = {}
|
|
|
|
@gui = UNSET_VALUE
|
|
|
|
|
|
|
|
# We require that network adapter 1 is a NAT device.
|
|
|
|
network_adapter(1, :nat)
|
2012-12-23 06:47:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Customize the VM by calling `VBoxManage` with the given
|
|
|
|
# arguments.
|
|
|
|
#
|
|
|
|
# When called multiple times, the customizations will be applied
|
|
|
|
# in the order given.
|
|
|
|
#
|
|
|
|
# The special `:name` parameter in the command will be replaced with
|
|
|
|
# the unique ID or name of the virtual machine. This is useful for
|
|
|
|
# parameters to `modifyvm` and the like.
|
|
|
|
#
|
|
|
|
# @param [Array] command An array of arguments to pass to
|
|
|
|
# VBoxManage.
|
2013-07-23 19:56:40 +00:00
|
|
|
def customize(*command)
|
|
|
|
event = command.first.is_a?(String) ? command.shift : "pre-boot"
|
|
|
|
command = command[0]
|
|
|
|
@customizations << [event, command]
|
2012-12-23 06:47:06 +00:00
|
|
|
end
|
2012-12-25 17:18:47 +00:00
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
# This defines a network adapter that will be added to the VirtualBox
|
|
|
|
# virtual machine in the given slot.
|
|
|
|
#
|
|
|
|
# @param [Integer] slot The slot for this network adapter.
|
|
|
|
# @param [Symbol] type The type of adapter.
|
|
|
|
def network_adapter(slot, type, *args)
|
|
|
|
@network_adapters[slot] = [type, args]
|
|
|
|
end
|
|
|
|
|
2013-10-28 03:43:33 +00:00
|
|
|
# Shortcut for setting memory size for the virtual machine.
|
|
|
|
# Calls #customize internally.
|
|
|
|
#
|
|
|
|
# @param size [Integer, String] the memory size in MB
|
|
|
|
def memory=(size)
|
2013-11-23 19:45:06 +00:00
|
|
|
customize("pre-boot", ["modifyvm", :id, "--memory", size.to_s])
|
2013-10-28 03:43:33 +00:00
|
|
|
end
|
|
|
|
|
2014-01-10 17:41:23 +00:00
|
|
|
# Shortcut for setting CPU count for the virtual machine.
|
|
|
|
# Calls #customize internally.
|
|
|
|
#
|
|
|
|
# @param count [Integer, String] the count of CPUs
|
|
|
|
def cpus=(count)
|
|
|
|
customize("pre-boot", ["modifyvm", :id, "--cpus", count.to_i])
|
|
|
|
end
|
|
|
|
|
2012-12-25 17:18:47 +00:00
|
|
|
# This is the hook that is called to finalize the object before it
|
|
|
|
# is put into use.
|
|
|
|
def finalize!
|
2013-01-31 05:03:02 +00:00
|
|
|
# Default is to auto the DNS proxy
|
|
|
|
@auto_nat_dns_proxy = true if @auto_nat_dns_proxy == UNSET_VALUE
|
|
|
|
|
2013-07-23 20:13:11 +00:00
|
|
|
if @destroy_unused_network_interfaces == UNSET_VALUE
|
|
|
|
@destroy_unused_network_interfaces = false
|
|
|
|
end
|
|
|
|
|
2012-12-25 17:18:47 +00:00
|
|
|
# Default is to not show a GUI
|
|
|
|
@gui = false if @gui == UNSET_VALUE
|
2013-01-31 07:03:21 +00:00
|
|
|
|
|
|
|
# The default name is just nothing, and we default it
|
|
|
|
@name = nil if @name == UNSET_VALUE
|
2012-12-25 17:18:47 +00:00
|
|
|
end
|
2013-04-03 23:18:37 +00:00
|
|
|
|
2013-07-23 19:56:40 +00:00
|
|
|
def validate(machine)
|
2014-01-16 17:43:31 +00:00
|
|
|
errors = _detected_errors
|
2013-07-23 19:56:40 +00:00
|
|
|
|
|
|
|
valid_events = ["pre-import", "pre-boot", "post-boot"]
|
|
|
|
@customizations.each do |event, _|
|
|
|
|
if !valid_events.include?(event)
|
|
|
|
errors << I18n.t(
|
|
|
|
"vagrant.virtualbox.config.invalid_event",
|
|
|
|
event: event.to_s,
|
|
|
|
valid_events: valid_events.join(", "))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-23 19:59:37 +00:00
|
|
|
@customizations.each do |event, command|
|
|
|
|
if event == "pre-import" && command.index(:id)
|
|
|
|
errors << I18n.t("vagrant.virtualbox.config.id_in_pre_import")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-25 19:25:10 +00:00
|
|
|
# Verify that internal networks are only on private networks.
|
|
|
|
machine.config.vm.networks.each do |type, data|
|
|
|
|
if data[:virtualbox__intnet] && type != :private_network
|
|
|
|
errors << I18n.t("vagrant.virtualbox.config.intnet_on_bad_type")
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-24 07:55:12 +00:00
|
|
|
{ "VirtualBox Provider" => errors }
|
2013-07-23 19:56:40 +00:00
|
|
|
end
|
|
|
|
|
2013-04-03 23:18:37 +00:00
|
|
|
def to_s
|
|
|
|
"VirtualBox"
|
|
|
|
end
|
2012-12-23 06:47:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|