Class: Vagrant::VM

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/vagrant/vm.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = nil) ⇒ VM

Returns a new instance of VM.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vagrant/vm.rb', line 18

def initialize(opts=nil)
  defaults = {
    :vm => nil,
    :env => nil,
    :name => nil
  }

  opts = defaults.merge(opts || {})

  @vm = opts[:vm]
  @name = opts[:name]

  if !opts[:env].nil?
    # We have an environment, so we create a new child environment
    # specifically for this VM. This step will load any custom
    # config and such.
    @env = Vagrant::Environment.new({
      :cwd => opts[:env].cwd,
      :parent => opts[:env],
      :vm => self
    }).load!

    # Load the associated system.
    load_system!
  end

  @loaded_system_distro = false
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



5
6
7
# File 'lib/vagrant/vm.rb', line 5

def env
  @env
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/vagrant/vm.rb', line 6

def name
  @name
end

#vmObject

Returns the value of attribute vm.



7
8
9
# File 'lib/vagrant/vm.rb', line 7

def vm
  @vm
end

Class Method Details

.find(uuid, env = nil, name = nil) ⇒ Object

Finds a virtual machine by a given UUID and either returns a Vagrant::VM object or returns nil.



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

def find(uuid, env=nil, name=nil)
  vm = VirtualBox::VM.find(uuid)
  new(:vm => vm, :env => env, :name => name)
end

Instance Method Details

#created?Boolean

Returns a boolean true if the VM has been created, otherwise returns false.

Returns:

  • (Boolean)


100
101
102
# File 'lib/vagrant/vm.rb', line 100

def created?
  !vm.nil?
end

#destroyObject



158
159
160
# File 'lib/vagrant/vm.rb', line 158

def destroy
  env.actions.run(:destroy)
end

#halt(options = nil) ⇒ Object



146
147
148
# File 'lib/vagrant/vm.rb', line 146

def halt(options=nil)
  env.actions.run(:halt, options)
end

#load_system!(system = nil) ⇒ Object

Loads the system associated with the VM. The system class is responsible for OS-specific functionality. More information can be found by reading the documentation on Systems::Base.

This method should never be called manually.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vagrant/vm.rb', line 52

def load_system!(system=nil)
  system ||= env.config.vm.system

  if system.is_a?(Class)
    raise Errors::VMSystemError, :_key => :invalid_class, :system => system.to_s if !(system <= Systems::Base)
    @system = system.new(self)
  elsif system.is_a?(Symbol)
    # Hard-coded internal systems
    mapping = {
      :debian  => Systems::Debian,
      :freebsd => Systems::FreeBSD,
      :gentoo  => Systems::Gentoo,
      :redhat  => Systems::Redhat,
      :linux   => Systems::Linux,
      :solaris => Systems::Solaris
    }

    raise Errors::VMSystemError, :_key => :unknown_type, :system => system.to_s if !mapping.has_key?(system)
    @system = mapping[system].new(self)
  else
    raise Errors::VMSystemError, :unspecified
  end
end

#package(options = nil) ⇒ Object



131
132
133
# File 'lib/vagrant/vm.rb', line 131

def package(options=nil)
  env.actions.run(:package, { "validate" => false }.merge(options || {}))
end

#powered_off?Boolean

Returns:

  • (Boolean)


174
# File 'lib/vagrant/vm.rb', line 174

def powered_off?; @vm.powered_off? end

#provisionObject



154
155
156
# File 'lib/vagrant/vm.rb', line 154

def provision
  env.actions.run(:provision)
end

#reloadObject



150
151
152
# File 'lib/vagrant/vm.rb', line 150

def reload
  env.actions.run(:reload)
end

#reload!Object



127
128
129
# File 'lib/vagrant/vm.rb', line 127

def reload!
  @vm = VirtualBox::VM.find(@vm.uuid)
end

#resumeObject



166
167
168
# File 'lib/vagrant/vm.rb', line 166

def resume
  env.actions.run(:resume)
end

#saved?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/vagrant/vm.rb', line 170

def saved?
  @vm.saved?
end

#sshObject

Access the SSH object associated with this VM. On the initial call, this will initialize the object. On subsequent calls it will reuse the existing object.



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

def ssh
  @ssh ||= SSH.new(env)
end

#start(options = nil) ⇒ Object



139
140
141
142
143
144
# File 'lib/vagrant/vm.rb', line 139

def start(options=nil)
  return if @vm.running?
  return resume if @vm.saved?

  env.actions.run(:start, options)
end

#suspendObject



162
163
164
# File 'lib/vagrant/vm.rb', line 162

def suspend
  env.actions.run(:suspend)
end

#systemObject

Returns the system for this VM, loading the distro of the system if we can.



78
79
80
81
82
83
84
85
86
87
# File 'lib/vagrant/vm.rb', line 78

def system
  if !@loaded_system_distro && created? && vm.running?
    # Load the system distro for the first time
    result = @system.distro_dispatch
    load_system!(result)
    @loaded_system_distro = true
  end

  @system
end

#up(options = nil) ⇒ Object



135
136
137
# File 'lib/vagrant/vm.rb', line 135

def up(options=nil)
  env.actions.run(:up, options)
end

#uuidObject



123
124
125
# File 'lib/vagrant/vm.rb', line 123

def uuid
  vm ? vm.uuid : nil
end