Class: Vagrant::VM

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, env, config, opts = nil) ⇒ VM

Returns a new instance of VM.



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

def initialize(name, env, config, opts=nil)
  @logger = Log4r::Logger.new("vagrant::vm")

  @name   = name
  @vm     = nil
  @env    = env
  @config = config
  @box    = env.boxes.find(config.vm.box)

  opts ||= {}
  if opts[:base]
    # The name is the ID we use.
    @uuid = name
  else
    # Load the UUID if its saved.
    active = env.local_data[:active] || {}
    @uuid = active[@name.to_s]
  end

  # Reload ourselves to get the state
  reload!

  # Load the associated guest.
  load_guest!
  @loaded_guest_distro = false
end

Instance Attribute Details

#boxObject (readonly)

Returns the value of attribute box.



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

def box
  @box
end

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#driverObject (readonly)

Returns the value of attribute driver.



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

def driver
  @driver
end

#envObject (readonly)

Returns the value of attribute env.



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

def env
  @env
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#uuidObject

Returns the value of attribute uuid.



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

def uuid
  @uuid
end

#vmObject (readonly)

Returns the value of attribute vm.



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

def vm
  @vm
end

Instance Method Details

#channelObject

Returns a channel object to communicate with the virtual machine.



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

def channel
  @channel ||= Communication::SSH.new(self)
end

#created?Boolean

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

Returns:

  • (Boolean)


102
103
104
# File 'lib/vagrant/vm.rb', line 102

def created?
  state != :not_created
end

#destroyObject



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

def destroy
  run_action(:destroy)
end

#guestObject

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



71
72
73
74
75
76
77
78
79
80
# File 'lib/vagrant/vm.rb', line 71

def guest
  if !@loaded_guest_distro && state == :running
    # Load the guest distro for the first time
    result = @guest.distro_dispatch
    load_guest!(result)
    @loaded_guest_distro = true
  end

  @guest
end

#halt(options = nil) ⇒ Object



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

def halt(options=nil)
  run_action(:halt, options)
end

#load_guest!(guest = nil) ⇒ Object

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

This method should never be called manually.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vagrant/vm.rb', line 47

def load_guest!(guest=nil)
  guest ||= config.vm.guest
  @logger.info("Loading guest: #{guest}")

  if guest.is_a?(Class)
    raise Errors::VMGuestError, :_key => :invalid_class, :guest => guest.to_s if !(guest <= Guest::Base)
    @guest = guest.new(self)
  elsif guest.is_a?(Symbol)
    guest_klass = Vagrant.guests.get(guest)
    raise Errors::VMGuestError, :_key => :unknown_type, :guest => guest.to_s if !guest_klass
    @guest = guest_klass.new(self)
  else
    raise Errors::VMGuestError, :unspecified
  end
end

#package(options = nil) ⇒ Object



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

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

#provisionObject



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

def provision
  run_action(:provision)
end

#reload(options = nil) ⇒ Object



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

def reload(options=nil)
  run_action(:reload, options)
end

#reload!Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/vagrant/vm.rb', line 127

def reload!
  begin
    @driver = Driver::VirtualBox.new(@uuid)
  rescue Driver::VirtualBox::VMNotFound
    # Clear the UUID since this VM doesn't exist.
    @uuid = nil

    # Reset the driver. This shouldn't raise a VMNotFound since we won't
    # feed it a UUID.
    @driver = Driver::VirtualBox.new
  end
end

#resumeObject



175
176
177
# File 'lib/vagrant/vm.rb', line 175

def resume
  run_action(:resume)
end

#run_action(name, options = nil) ⇒ Object



186
187
188
189
190
191
192
193
# File 'lib/vagrant/vm.rb', line 186

def run_action(name, options=nil)
  options = {
    :vm => self,
    :ui => ui
  }.merge(options || {})

  env.action_runner.run(name, options)
end

#sshObject

Access the SSH object associated with this VM, which is used to get SSH credentials with the virtual machine.



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

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

#start(options = nil) ⇒ Object



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

def start(options=nil)
  return if state == :running
  return resume if state == :saved

  run_action(:start, options)
end

#stateSymbol

Returns the state of the VM as a symbol.

Returns:

  • (Symbol)


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

def state
  return :not_created if !@uuid
  state = @driver.read_state
  return :not_created if !state
  return state
end

#suspendObject



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

def suspend
  run_action(:suspend)
end

#uiObject



179
180
181
182
183
184
# File 'lib/vagrant/vm.rb', line 179

def ui
  return @_ui if defined?(@_ui)
  @_ui = @env.ui.dup
  @_ui.resource = @name
  @_ui
end

#up(options = nil) ⇒ Object



144
145
146
# File 'lib/vagrant/vm.rb', line 144

def up(options=nil)
  run_action(:up, options)
end