Class: Vagrant::Config::VMConfig

Inherits:
Base
  • Object
show all
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

Instance Method Summary collapse

Methods inherited from Base

#instance_variables_hash, json_create, #set_options, #to_hash, #to_json

Constructor Details

#initializeVMConfig

Returns a new instance of VMConfig.



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

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

Instance Attribute Details

#auto_port_rangeObject

Returns the value of attribute auto_port_range.



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

def auto_port_range
  @auto_port_range
end

#base_macObject

Returns the value of attribute base_mac.



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

def base_mac
  @base_mac
end

#boot_modeObject

Returns the value of attribute boot_mode.



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

def boot_mode
  @boot_mode
end

#boxObject

Returns the value of attribute box.



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

def box
  @box
end

#box_urlObject

Returns the value of attribute box_url.



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

def box_url
  @box_url
end

#customizationsObject (readonly)

Returns the value of attribute customizations.



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

def customizations
  @customizations
end

#forwarded_portsObject (readonly)

Returns the value of attribute forwarded_ports.



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

def forwarded_ports
  @forwarded_ports
end

#guestObject

Returns the value of attribute guest.



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

def guest
  @guest
end

#host_nameObject

Returns the value of attribute host_name.



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

def host_name
  @host_name
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/vagrant/config/vm.rb', line 9

def name
  @name
end

#networksObject (readonly)

Returns the value of attribute networks.



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

def networks
  @networks
end

#provisionersObject (readonly)

Returns the value of attribute provisioners.



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

def provisioners
  @provisioners
end

#shared_foldersObject (readonly)

Returns the value of attribute shared_folders.



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

def shared_folders
  @shared_folders
end

Instance Method Details

#customize(command = nil) ⇒ Object

TODO: This argument should not be nil in the future. It is only defaulted to nil so that the deprecation error can be properly shown.



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

def customize(command=nil)
  @customizations << command if command
end

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



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/vagrant/config/vm.rb', line 91

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) if block
end

#defined_vm_keysObject

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



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

def defined_vm_keys
  @defined_vm_keys ||= []
end

#defined_vmsObject



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

def defined_vms
  @defined_vms ||= {}
end

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



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

def forward_port(guestport, hostport, options=nil)
  @forwarded_ports << {
    :name       => "#{guestport.to_s(32)}-#{hostport.to_s(32)}",
    :guestport  => guestport,
    :hostport   => hostport,
    :protocol   => :tcp,
    :adapter    => 1,
    :auto       => false
  }.merge(options || {})
end

#merge(other) ⇒ Object

Custom merge method since some keys here are merged differently.



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

def merge(other)
  result = super
  result.instance_variable_set(:@forwarded_ports, @forwarded_ports + other.forwarded_ports)
  result.instance_variable_set(:@shared_folders, @shared_folders.merge(other.shared_folders))
  result.instance_variable_set(:@networks, @networks + other.networks)
  result.instance_variable_set(:@provisioners, @provisioners + other.provisioners)
  result.instance_variable_set(:@customizations, @customizations + other.customizations)
  result
end

#network(type, *args) ⇒ Object



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

def network(type, *args)
  @networks << [type, args]
end

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



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

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

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



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

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

#validate(env, errors) ⇒ Object



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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/vagrant/config/vm.rb', line 105

def validate(env, 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.boxes.find(box)
  errors.add(I18n.t("vagrant.config.vm.boot_mode_invalid")) if ![:headless, :gui].include?(boot_mode.to_sym)
  errors.add(I18n.t("vagrant.config.vm.base_mac_invalid")) if env.boxes.find(box) && !base_mac

  shared_folders.each do |name, options|
    hostpath = Pathname.new(options[:hostpath]).expand_path(env.root_path)

    if !hostpath.directory? && !options[:create]
      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
  #
  # TODO: One day we need to abstract this out, since in the future
  # providers other than VirtualBox will not be able to satisfy
  # all types of networks.
  networks.each do |type, args|
    if type == :hostonly && args[0] == :dhcp
      # Valid. There is no real way this can be invalid at the moment.
    elsif type == :hostonly
      # Validate the host-only network
      ip      = args[0]
      options = args[1] || {}

      if !ip
        errors.add(I18n.t("vagrant.config.vm.network_ip_required"))
      else
        ip_parts = ip.split(".")

        if ip_parts.length != 4
          errors.add(I18n.t("vagrant.config.vm.network_ip_invalid",
                            :ip => ip))
        elsif ip_parts.last == "1"
          errors.add(I18n.t("vagrant.config.vm.network_ip_ends_one",
                            :ip => ip))
        end
      end
    elsif type == :bridged
    else
      # Invalid network type
      errors.add(I18n.t("vagrant.config.vm.network_invalid",
                        :type => type.to_s))
    end
  end

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