Class: Vagrant::VM
Instance Attribute Summary collapse
-
#box ⇒ Object
readonly
Returns the value of attribute box.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#uuid ⇒ Object
Returns the value of attribute uuid.
-
#vm ⇒ Object
readonly
Returns the value of attribute vm.
Instance Method Summary collapse
-
#channel ⇒ Object
Returns a channel object to communicate with the virtual machine.
-
#created? ⇒ Boolean
Returns a boolean true if the VM has been created, otherwise returns false.
- #destroy ⇒ Object
-
#guest ⇒ Object
Returns the guest for this VM, loading the distro of the system if we can.
- #halt(options = nil) ⇒ Object
-
#initialize(name, env, config, opts = nil) ⇒ VM
constructor
A new instance of VM.
-
#load_guest!(guest = nil) ⇒ Object
Loads the guest associated with the VM.
- #package(options = nil) ⇒ Object
- #provision ⇒ Object
- #reload(options = nil) ⇒ Object
- #reload! ⇒ Object
- #resume ⇒ Object
- #run_action(name, options = nil) ⇒ Object
-
#ssh ⇒ Object
Access the SSH object associated with this VM, which is used to get SSH credentials with the virtual machine.
- #start(options = nil) ⇒ Object
-
#state ⇒ Symbol
Returns the state of the VM as a symbol.
- #suspend ⇒ Object
- #ui ⇒ Object
- #up(options = nil) ⇒ Object
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
#box ⇒ Object (readonly)
Returns the value of attribute box.
11 12 13 |
# File 'lib/vagrant/vm.rb', line 11 def box @box end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
12 13 14 |
# File 'lib/vagrant/vm.rb', line 12 def config @config end |
#driver ⇒ Object (readonly)
Returns the value of attribute driver.
13 14 15 |
# File 'lib/vagrant/vm.rb', line 13 def driver @driver end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
8 9 10 |
# File 'lib/vagrant/vm.rb', line 8 def env @env end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/vagrant/vm.rb', line 9 def name @name end |
#uuid ⇒ Object
Returns the value of attribute uuid.
7 8 9 |
# File 'lib/vagrant/vm.rb', line 7 def uuid @uuid end |
#vm ⇒ Object (readonly)
Returns the value of attribute vm.
10 11 12 |
# File 'lib/vagrant/vm.rb', line 10 def vm @vm end |
Instance Method Details
#channel ⇒ Object
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.
102 103 104 |
# File 'lib/vagrant/vm.rb', line 102 def created? state != :not_created end |
#destroy ⇒ Object
167 168 169 |
# File 'lib/vagrant/vm.rb', line 167 def destroy run_action(:destroy) end |
#guest ⇒ Object
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(=nil) run_action(:halt, ) 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(=nil) run_action(:package, { "validate" => false }.merge( || {})) end |
#provision ⇒ Object
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(=nil) run_action(:reload, ) 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 |
#resume ⇒ Object
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, =nil) = { :vm => self, :ui => ui }.merge( || {}) env.action_runner.run(name, ) end |
#ssh ⇒ Object
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(=nil) return if state == :running return resume if state == :saved run_action(:start, ) end |
#state ⇒ Symbol
Returns the state of the VM as a 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 |
#suspend ⇒ Object
171 172 173 |
# File 'lib/vagrant/vm.rb', line 171 def suspend run_action(:suspend) end |
#ui ⇒ Object
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(=nil) run_action(:up, ) end |