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
|
|
|
|
|
2014-02-09 02:35:09 +00:00
|
|
|
# If true, will check if guest additions are installed and up to
|
|
|
|
# date. By default, this is true.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
attr_accessor :check_guest_additions
|
|
|
|
|
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
|
|
|
|
|
2018-11-07 23:53:46 +00:00
|
|
|
# Set the default type of NIC hardware to be used for network
|
2018-11-26 23:48:46 +00:00
|
|
|
# devices. By default this is `nil` and VirtualBox's default
|
|
|
|
# will be used.
|
2018-11-07 23:53:46 +00:00
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
attr_accessor :default_nic_type
|
|
|
|
|
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
|
|
|
|
|
2014-09-09 17:17:04 +00:00
|
|
|
# If set to `true`, then a linked clone is created from a master
|
|
|
|
# VM generated from the specified box.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
2015-10-06 18:11:41 +00:00
|
|
|
attr_accessor :linked_clone
|
|
|
|
|
2015-10-08 15:58:47 +00:00
|
|
|
# The snapshot to base the linked clone from. If this isn't set
|
|
|
|
# a snapshot will be made with the name of "base" which will be used.
|
|
|
|
#
|
|
|
|
# If this is set, then the snapshot must already exist.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
attr_accessor :linked_clone_snapshot
|
|
|
|
|
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
|
|
|
|
|
2014-04-11 01:26:19 +00:00
|
|
|
# Whether or not this VM has a functional vboxsf filesystem module.
|
|
|
|
# This defaults to true. If you set this to false, then the "virtualbox"
|
|
|
|
# synced folder type won't be valid.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
attr_accessor :functional_vboxsf
|
|
|
|
|
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
|
2014-02-09 02:35:09 +00:00
|
|
|
@check_guest_additions = UNSET_VALUE
|
2013-01-11 22:16:00 +00:00
|
|
|
@customizations = []
|
2018-11-07 23:53:46 +00:00
|
|
|
@default_nic_type = UNSET_VALUE
|
2013-07-23 20:13:11 +00:00
|
|
|
@destroy_unused_network_interfaces = UNSET_VALUE
|
2014-04-11 01:26:19 +00:00
|
|
|
@functional_vboxsf = 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
|
2015-10-06 18:11:41 +00:00
|
|
|
@linked_clone = UNSET_VALUE
|
2015-10-08 15:58:47 +00:00
|
|
|
@linked_clone_snapshot = UNSET_VALUE
|
2013-01-11 22:16:00 +00:00
|
|
|
|
|
|
|
# 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.
|
2014-01-17 17:53:02 +00:00
|
|
|
def network_adapter(slot, type, **opts)
|
|
|
|
@network_adapters[slot] = [type, opts]
|
2013-01-11 22:16:00 +00:00
|
|
|
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
|
|
|
|
|
2014-04-20 15:52:09 +00:00
|
|
|
def merge(other)
|
|
|
|
super.tap do |result|
|
|
|
|
c = customizations.dup
|
|
|
|
c += other.customizations
|
|
|
|
result.instance_variable_set(:@customizations, c)
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
2014-02-09 02:35:09 +00:00
|
|
|
if @check_guest_additions == UNSET_VALUE
|
|
|
|
@check_guest_additions = true
|
|
|
|
end
|
|
|
|
|
2013-07-23 20:13:11 +00:00
|
|
|
if @destroy_unused_network_interfaces == UNSET_VALUE
|
|
|
|
@destroy_unused_network_interfaces = false
|
|
|
|
end
|
|
|
|
|
2014-04-11 01:26:19 +00:00
|
|
|
if @functional_vboxsf == UNSET_VALUE
|
|
|
|
@functional_vboxsf = true
|
|
|
|
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
|
|
|
|
2014-09-09 17:17:04 +00:00
|
|
|
# Do not create linked clone by default
|
2015-10-06 18:11:41 +00:00
|
|
|
@linked_clone = false if @linked_clone == UNSET_VALUE
|
2015-10-08 15:58:47 +00:00
|
|
|
@linked_clone_snapshot = nil if @linked_clone_snapshot == UNSET_VALUE
|
2014-09-09 17:17:04 +00:00
|
|
|
|
2013-01-31 07:03:21 +00:00
|
|
|
# The default name is just nothing, and we default it
|
|
|
|
@name = nil if @name == UNSET_VALUE
|
2018-11-07 23:53:46 +00:00
|
|
|
|
2018-11-26 23:48:46 +00:00
|
|
|
@default_nic_type = nil if @default_nic_type == 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
|
|
|
|
2014-12-20 22:09:55 +00:00
|
|
|
valid_events = ["pre-import", "pre-boot", "post-boot", "post-comm"]
|
2013-07-23 19:56:40 +00:00
|
|
|
@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
|