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.



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

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

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



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

def system
  @system
end

Instance Method Details

#customize(&block) ⇒ Object



69
70
71
# File 'lib/vagrant/config/vm.rb', line 69

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

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



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/vagrant/config/vm.rb', line 87

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.



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

def defined_vm_keys
  @defined_vm_keys ||= []
end

#defined_vmsObject



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

def defined_vms
  @defined_vms ||= {}
end

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



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vagrant/config/vm.rb', line 31

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)


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

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

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



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

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

#validate(errors) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/vagrant/config/vm.rb', line 101

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

    if options[:nfs] && (options[:owner] || options[:group])
      # Owner/group don't work with NFS
      errors.add(I18n.t("vagrant.config.vm.shared_folder_nfs_owner_group",
                        :name => name))
    end
  end

  # Validate some basic networking
  network_options.each do |options|
    next if !options

    ip = options[:ip].split(".")

    if ip.length != 4
      errors.add(I18n.t("vagrant.config.vm.network_ip_invalid",
                        :ip => options[:ip]))
    elsif ip.last == "1"
      errors.add(I18n.t("vagrant.config.vm.network_ip_ends_one",
                        :ip => options[:ip]))
    end
  end

  # Each provisioner can validate itself
  provisioners.each do |prov|
    # TODO: Remove at some point
    if prov.shortcut == :chef_server
      errors.add(I18n.t("vagrant.config.vm.provisioner_chef_server_changed"))
    else
      prov.validate(errors)
    end
  end
end