Class: VagrantPlugins::ProviderOpenStack::Driver::Base
- Inherits:
-
Object
- Object
- VagrantPlugins::ProviderOpenStack::Driver::Base
- Includes:
- Vagrant::Util::Retryable
- Defined in:
- lib/vagrant-openstack/driver/base.rb
Overview
Base class for all VirtualBox drivers.
This class provides useful tools for things such as executing VBoxManage and handling SIGINTs and so on.
Instance Method Summary collapse
-
#clear_forwarded_ports ⇒ Object
Clears the forwarded ports that have been set on the virtual machine.
-
#clear_shared_folders ⇒ Object
Clears the shared folders that have been set on the virtual machine.
-
#create_dhcp_server(network, options) ⇒ Object
Creates a DHCP server for a host only network.
-
#create_host_only_network(options) ⇒ Hash
Creates a host only network with the given options.
-
#delete ⇒ Object
Deletes the virtual machine references by this driver.
-
#delete_unused_host_only_networks ⇒ Object
Deletes any host only networks that aren’t being used for anything.
-
#discard_saved_state ⇒ Object
Discards any saved state associated with this VM.
-
#enable_adapters(adapters) ⇒ Object
Enables network adapters on the VM.
-
#execute(*command, &block) ⇒ Object
Execute the given subcommand for VBoxManage and return the output.
-
#execute_command(command) ⇒ Object
Execute a raw command straight through to VBoxManage.
-
#export(path) {|progress| ... } ⇒ Object
Exports the virtual machine to the given path.
-
#forward_ports(ports) ⇒ Object
Forwards a set of ports for a VM.
-
#halt ⇒ Object
Halts the virtual machine (pulls the plug).
-
#import(ovf) ⇒ String
Imports the VM from an OVF file.
-
#initialize ⇒ Base
constructor
A new instance of Base.
-
#raw(*command, &block) ⇒ Object
Executes a command and returns the raw result object.
-
#read_bridged_interfaces ⇒ Hash
Returns a list of bridged interfaces.
-
#read_forwarded_ports(uuid = nil, active_only = false) ⇒ Array<Array>
Returns a list of forwarded ports for a VM.
-
#read_guest_additions_version ⇒ String
Returns the guest additions version that is installed on this VM.
-
#read_host_only_interfaces ⇒ Hash
Returns a list of available host only interfaces.
-
#read_mac_address ⇒ String
Returns the MAC address of the first network interface.
-
#read_machine_folder ⇒ String
Returns the folder where VirtualBox places it’s VMs.
-
#read_network_interfaces ⇒ Hash
Returns a list of network interfaces of the VM.
-
#read_state ⇒ Symbol
Returns the current state of this VM.
-
#read_used_ports ⇒ Array
Returns a list of all forwarded ports in use by active virtual machines.
-
#read_vms ⇒ Array<String>
Returns a list of all UUIDs of virtual machines currently known by VirtualBox.
-
#set_mac_address(mac) ⇒ Object
Sets the MAC address of the first network adapter.
-
#share_folders(folders) ⇒ Object
Share a set of folders on this VM.
-
#ssh_port(expected) ⇒ Object
Reads the SSH port of this VM.
-
#start(mode) ⇒ Object
Starts the virtual machine.
-
#suspend ⇒ Object
Suspend the virtual machine.
-
#verify! ⇒ Object
Verifies that the driver is ready to accept work.
-
#verify_image(path) ⇒ Boolean
Verifies that an image can be imported properly.
-
#vm_exists?(uuid) ⇒ Boolean
Checks if a VM with the given UUID exists.
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
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 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/vagrant-openstack/driver/base.rb', line 19 def initialize @logger = Log4r::Logger.new("vagrant::provider::virtualbox::base") # This flag is used to keep track of interrupted state (SIGINT) @interrupted = false # Set the path to VBoxManage @vboxmanage_path = "VBoxManage" if Vagrant::Util::Platform.windows? @logger.debug("Windows. Trying VBOX_INSTALL_PATH for VBoxManage") # On Windows, we use the VBOX_INSTALL_PATH environmental # variable to find VBoxManage. if ENV.has_key?("VBOX_INSTALL_PATH") # Get the path. path = ENV["VBOX_INSTALL_PATH"] @logger.debug("VBOX_INSTALL_PATH value: #{path}") # There can actually be multiple paths in here, so we need to # split by the separator ";" and see which is a good one. path.split(";").each do |single| # Make sure it ends with a single += "\\" if !single.end_with?("\\") # If the executable exists, then set it as the main path # and break out vboxmanage = "#{path}VBoxManage.exe" if File.file?(vboxmanage) @vboxmanage_path = vboxmanage break end end end end @logger.info("VBoxManage path: #{@vboxmanage_path}") end |
Instance Method Details
#clear_forwarded_ports ⇒ Object
Clears the forwarded ports that have been set on the virtual machine.
59 60 |
# File 'lib/vagrant-openstack/driver/base.rb', line 59 def clear_forwarded_ports end |
#clear_shared_folders ⇒ Object
Clears the shared folders that have been set on the virtual machine.
63 64 |
# File 'lib/vagrant-openstack/driver/base.rb', line 63 def clear_shared_folders end |
#create_dhcp_server(network, options) ⇒ Object
Creates a DHCP server for a host only network.
70 71 |
# File 'lib/vagrant-openstack/driver/base.rb', line 70 def create_dhcp_server(network, ) end |
#create_host_only_network(options) ⇒ Hash
Creates a host only network with the given options.
78 79 |
# File 'lib/vagrant-openstack/driver/base.rb', line 78 def create_host_only_network() end |
#delete ⇒ Object
Deletes the virtual machine references by this driver.
82 83 |
# File 'lib/vagrant-openstack/driver/base.rb', line 82 def delete end |
#delete_unused_host_only_networks ⇒ Object
Deletes any host only networks that aren’t being used for anything.
86 87 |
# File 'lib/vagrant-openstack/driver/base.rb', line 86 def delete_unused_host_only_networks end |
#discard_saved_state ⇒ Object
Discards any saved state associated with this VM.
90 91 |
# File 'lib/vagrant-openstack/driver/base.rb', line 90 def discard_saved_state end |
#enable_adapters(adapters) ⇒ Object
Enables network adapters on the VM.
The format of each adapter specification should be like so:
:type => :hostonly,
:hostonly => "vboxnet0",
:mac_address => "tubes"
This must support setting up both host only and bridged networks.
106 107 |
# File 'lib/vagrant-openstack/driver/base.rb', line 106 def enable_adapters(adapters) end |
#execute(*command, &block) ⇒ Object
Execute the given subcommand for VBoxManage and return the output.
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/vagrant-openstack/driver/base.rb', line 271 def execute(*command, &block) # Get the options hash if it exists opts = {} opts = command.pop if command.last.is_a?(Hash) tries = 0 tries = 3 if opts[:retryable] # Variable to store our execution result r = nil retryable(:on => Vagrant::Errors::VBoxManageError, :tries => tries, :sleep => 1) do # Execute the command r = raw(*command, &block) # If the command was a failure, then raise an exception that is # nicely handled by Vagrant. if r.exit_code != 0 if @interrupted @logger.info("Exit code != 0, but interrupted. Ignoring.") else raise Vagrant::Errors::VBoxManageError, :command => command.inspect end else # Sometimes, VBoxManage fails but doesn't actual return a non-zero # exit code. For this we inspect the output and determine if an error # occurred. if r.stderr =~ /VBoxManage: error:/ @logger.info("VBoxManage error text found, assuming error.") raise Vagrant::Errors::VBoxManageError, :command => command.inspect end end end # Return the output, making sure to replace any Windows-style # newlines with Unix-style. r.stdout.gsub("\r\n", "\n") end |
#execute_command(command) ⇒ Object
Execute a raw command straight through to VBoxManage.
112 113 |
# File 'lib/vagrant-openstack/driver/base.rb', line 112 def execute_command(command) end |
#export(path) {|progress| ... } ⇒ Object
Exports the virtual machine to the given path.
119 120 |
# File 'lib/vagrant-openstack/driver/base.rb', line 119 def export(path) end |
#forward_ports(ports) ⇒ Object
Forwards a set of ports for a VM.
This will not affect any previously set forwarded ports, so be sure to delete those if you need to.
The format of each port hash should be the following:
{
:name => "foo",
:hostport => 8500,
:guestport => 80,
:adapter => 1,
:protocol => "tcp"
}
Note that “adapter” and “protocol” are optional and will default to 1 and “tcp” respectively.
142 143 |
# File 'lib/vagrant-openstack/driver/base.rb', line 142 def forward_ports(ports) end |
#halt ⇒ Object
Halts the virtual machine (pulls the plug).
146 147 |
# File 'lib/vagrant-openstack/driver/base.rb', line 146 def halt end |
#import(ovf) ⇒ String
Imports the VM from an OVF file.
153 154 |
# File 'lib/vagrant-openstack/driver/base.rb', line 153 def import(ovf) end |
#raw(*command, &block) ⇒ Object
Executes a command and returns the raw result object.
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/vagrant-openstack/driver/base.rb', line 311 def raw(*command, &block) int_callback = lambda do @interrupted = true @logger.info("Interrupted.") end # Append in the options for subprocess command << { :notify => [:stdout, :stderr] } # The following is a workaround for a combined VirtualBox and # Mac OS X 10.8 bug: # # Remove the DYLD_LIBRARY_PATH environmental variable on Mac. This # is to fix a bug in Mac OS X 10.8 where a warning is printed to the # console if this is set when executing certain programs, which # can cause some VBoxManage commands to break because they work # by just reading stdout and don't expect the OS to just inject # garbage into it. old_dyld_lib_path = ENV.delete("DYLD_LIBRARY_PATH") old_ld_lib_path = ENV.delete("LD_LIBRARY_PATH") Vagrant::Util::Busy.busy(int_callback) do Vagrant::Util::Subprocess.execute(@vboxmanage_path, *command, &block) end ensure # Reset the library path if it was set before. See above comments # for more information on why this was unset in the first place. ENV["DYLD_LIBRARY_PATH"] = old_dyld_lib_path if old_dyld_lib_path ENV["LD_LIBRARY_PATH"] = old_ld_lib_path if old_ld_lib_path end |
#read_bridged_interfaces ⇒ Hash
Returns a list of bridged interfaces.
169 170 |
# File 'lib/vagrant-openstack/driver/base.rb', line 169 def read_bridged_interfaces end |
#read_forwarded_ports(uuid = nil, active_only = false) ⇒ Array<Array>
Returns a list of forwarded ports for a VM.
163 164 |
# File 'lib/vagrant-openstack/driver/base.rb', line 163 def read_forwarded_ports(uuid=nil, active_only=false) end |
#read_guest_additions_version ⇒ String
Returns the guest additions version that is installed on this VM.
175 176 |
# File 'lib/vagrant-openstack/driver/base.rb', line 175 def read_guest_additions_version end |
#read_host_only_interfaces ⇒ Hash
Returns a list of available host only interfaces.
181 182 |
# File 'lib/vagrant-openstack/driver/base.rb', line 181 def read_host_only_interfaces end |
#read_mac_address ⇒ String
Returns the MAC address of the first network interface.
187 188 |
# File 'lib/vagrant-openstack/driver/base.rb', line 187 def read_mac_address end |
#read_machine_folder ⇒ String
Returns the folder where VirtualBox places it’s VMs.
193 194 |
# File 'lib/vagrant-openstack/driver/base.rb', line 193 def read_machine_folder end |
#read_network_interfaces ⇒ Hash
Returns a list of network interfaces of the VM.
199 200 |
# File 'lib/vagrant-openstack/driver/base.rb', line 199 def read_network_interfaces end |
#read_state ⇒ Symbol
Returns the current state of this VM.
205 206 |
# File 'lib/vagrant-openstack/driver/base.rb', line 205 def read_state end |
#read_used_ports ⇒ Array
Returns a list of all forwarded ports in use by active virtual machines.
212 213 |
# File 'lib/vagrant-openstack/driver/base.rb', line 212 def read_used_ports end |
#read_vms ⇒ Array<String>
Returns a list of all UUIDs of virtual machines currently known by VirtualBox.
219 220 |
# File 'lib/vagrant-openstack/driver/base.rb', line 219 def read_vms end |
#set_mac_address(mac) ⇒ Object
Sets the MAC address of the first network adapter.
225 226 |
# File 'lib/vagrant-openstack/driver/base.rb', line 225 def set_mac_address(mac) end |
#share_folders(folders) ⇒ Object
Share a set of folders on this VM.
231 232 |
# File 'lib/vagrant-openstack/driver/base.rb', line 231 def share_folders(folders) end |
#ssh_port(expected) ⇒ Object
Reads the SSH port of this VM.
237 238 |
# File 'lib/vagrant-openstack/driver/base.rb', line 237 def ssh_port(expected) end |
#start(mode) ⇒ Object
Starts the virtual machine.
244 245 |
# File 'lib/vagrant-openstack/driver/base.rb', line 244 def start(mode) end |
#suspend ⇒ Object
Suspend the virtual machine.
248 249 |
# File 'lib/vagrant-openstack/driver/base.rb', line 248 def suspend end |
#verify! ⇒ Object
Verifies that the driver is ready to accept work.
This should raise a VagrantError if things are not ready.
254 255 |
# File 'lib/vagrant-openstack/driver/base.rb', line 254 def verify! end |
#verify_image(path) ⇒ Boolean
Verifies that an image can be imported properly.
261 262 |
# File 'lib/vagrant-openstack/driver/base.rb', line 261 def verify_image(path) end |
#vm_exists?(uuid) ⇒ Boolean
Checks if a VM with the given UUID exists.
267 268 |
# File 'lib/vagrant-openstack/driver/base.rb', line 267 def vm_exists?(uuid) end |