Class: Vagrant::Config::VMConfig

Inherits:
Base
  • Object
show all
Includes:
Util::StackedProcRunner
Defined in:
lib/vagrant/config/vm.rb,
lib/vagrant/config/vm/sub_vm.rb,
lib/vagrant/config/vm/provisioner.rb

Defined Under Namespace

Classes: Provisioner, SubVM

Instance Attribute Summary collapse

Attributes inherited from Base

#top

Instance Method Summary collapse

Methods included from Util::StackedProcRunner

#proc_stack, #push_proc, #run_procs!

Methods inherited from Base

configures, #env, #instance_variables_hash, json_create, #set_options, #to_hash, #to_json

Constructor Details

#initializeVMConfig

Returns a new instance of VMConfig.



27
28
29
30
31
32
# File 'lib/vagrant/config/vm.rb', line 27

def initialize
  @forwarded_ports = {}
  @shared_folders = {}
  @network_options = []
  @provisioners = []
end

Instance Attribute Details

#auto_port_rangeObject

Returns the value of attribute auto_port_range.



11
12
13
# File 'lib/vagrant/config/vm.rb', line 11

def auto_port_range
  @auto_port_range
end

#base_macObject

Returns the value of attribute base_mac.



15
16
17
# File 'lib/vagrant/config/vm.rb', line 15

def base_mac
  @base_mac
end

#boot_modeObject

Returns the value of attribute boot_mode.



16
17
18
# File 'lib/vagrant/config/vm.rb', line 16

def boot_mode
  @boot_mode
end

#boxObject

Returns the value of attribute box.



12
13
14
# File 'lib/vagrant/config/vm.rb', line 12

def box
  @box
end

#box_ovfObject

Returns the value of attribute box_ovf.



14
15
16
# File 'lib/vagrant/config/vm.rb', line 14

def box_ovf
  @box_ovf
end

#box_urlObject

Returns the value of attribute box_url.



13
14
15
# File 'lib/vagrant/config/vm.rb', line 13

def box_url
  @box_url
end

#disk_image_formatObject

Returns the value of attribute disk_image_format.



22
23
24
# File 'lib/vagrant/config/vm.rb', line 22

def disk_image_format
  @disk_image_format
end

#forwarded_portsObject (readonly)

Returns the value of attribute forwarded_ports.



18
19
20
# File 'lib/vagrant/config/vm.rb', line 18

def forwarded_ports
  @forwarded_ports
end

#host_nameObject

Returns the value of attribute host_name.



17
18
19
# File 'lib/vagrant/config/vm.rb', line 17

def host_name
  @host_name
end

#network_optionsObject (readonly)

Returns the value of attribute network_options.



20
21
22
# File 'lib/vagrant/config/vm.rb', line 20

def network_options
  @network_options
end

#provisionersObject (readonly)

Returns the value of attribute provisioners.



21
22
23
# File 'lib/vagrant/config/vm.rb', line 21

def provisioners
  @provisioners
end

#shared_folder_gidObject



80
81
82
# File 'lib/vagrant/config/vm.rb', line 80

def shared_folder_gid
  @shared_folder_gid || env.config.ssh.username
end

#shared_folder_uidObject



76
77
78
# File 'lib/vagrant/config/vm.rb', line 76

def shared_folder_uid
  @shared_folder_uid || env.config.ssh.username
end

#shared_foldersObject (readonly)

Returns the value of attribute shared_folders.



19
20
21
# File 'lib/vagrant/config/vm.rb', line 19

def shared_folders
  @shared_folders
end

#systemObject

Returns the value of attribute system.



25
26
27
# File 'lib/vagrant/config/vm.rb', line 25

def system
  @system
end

Instance Method Details

#customize(&block) ⇒ Object



84
85
86
# File 'lib/vagrant/config/vm.rb', line 84

def customize(&block)
  push_proc(&block)
end

#define(name, options = nil, &block) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/vagrant/config/vm.rb', line 102

def define(name, options=nil, &block)
  name = name.to_sym
  options ||= {}

  # Add the name to the array of VM keys. This array is used to
  # preserve the order in which VMs are defined.
  defined_vm_keys << name

  # Add the SubVM to the hash of defined VMs
  defined_vms[name] ||= SubVM.new
  defined_vms[name].options.merge!(options)
  defined_vms[name].push_proc(&block)
end

#defined_vm_keysObject

This returns the keys of the sub-vms in the order they were defined.



98
99
100
# File 'lib/vagrant/config/vm.rb', line 98

def defined_vm_keys
  @defined_vm_keys ||= []
end

#defined_vmsObject



92
93
94
# File 'lib/vagrant/config/vm.rb', line 92

def defined_vms
  @defined_vms ||= {}
end

#forward_port(name, guestport, hostport, options = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vagrant/config/vm.rb', line 34

def forward_port(name, guestport, hostport, options=nil)
  options = {
    :guestport  => guestport,
    :hostport   => hostport,
    :protocol   => "TCP",
    :adapter    => 0,
    :auto       => false
  }.merge(options || {})

  forwarded_ports[name] = options
end

#has_multi_vms?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/vagrant/config/vm.rb', line 88

def has_multi_vms?
  !defined_vms.empty?
end

#network(ip, options = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vagrant/config/vm.rb', line 53

def network(ip, options=nil)
  options = {
    :ip => ip,
    :netmask => "255.255.255.0",
    :adapter => 1,
    :mac => nil,
    :name => nil
  }.merge(options || {})

  @network_options[options[:adapter]] = options
end

#provision(name, options = nil, &block) ⇒ Object



65
66
67
# File 'lib/vagrant/config/vm.rb', line 65

def provision(name, options=nil, &block)
  @provisioners << Provisioner.new(top, name, options, &block)
end

#provisioner=(_value) ⇒ Object

This shows an error message to smooth the transition for the backwards incompatible provisioner syntax change introduced in Vagrant 0.7.0.



72
73
74
# File 'lib/vagrant/config/vm.rb', line 72

def provisioner=(_value)
  raise Errors::VagrantError, :_key => :provisioner_equals_not_supported
end

#share_folder(name, guestpath, hostpath, opts = nil) ⇒ Object



46
47
48
49
50
51
# File 'lib/vagrant/config/vm.rb', line 46

def share_folder(name, guestpath, hostpath, opts=nil)
  @shared_folders[name] = {
    :guestpath => guestpath,
    :hostpath => hostpath
  }.merge(opts || {})
end

#validate(errors) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/vagrant/config/vm.rb', line 116

def validate(errors)
  errors.add(I18n.t("vagrant.config.vm.box_missing")) if !box
  errors.add(I18n.t("vagrant.config.vm.box_not_found", :name => box)) if box && !box_url && !env.box
  errors.add(I18n.t("vagrant.config.vm.boot_mode_invalid")) if ![:vrdp, :gui].include?(boot_mode.to_sym)
  errors.add(I18n.t("vagrant.config.vm.base_mac_invalid")) if env.box && !base_mac

  shared_folders.each do |name, options|
    if !File.directory?(File.expand_path(options[:hostpath].to_s, env.root_path))
      errors.add(I18n.t("vagrant.config.vm.shared_folder_hostpath_missing",
                 :name => name,
                 :path => options[:hostpath]))
    end
  end

  # Each provisioner can validate itself
  provisioners.each do |prov|
    prov.validate(errors)
  end
end