lib/vagrant-libvirt/provider.rb in vagrant-libvirt-0.0.30 vs lib/vagrant-libvirt/provider.rb in vagrant-libvirt-0.0.31

- old
+ new

@@ -1,9 +1,10 @@ require 'vagrant' module VagrantPlugins module ProviderLibvirt + autoload :Driver, 'vagrant-libvirt/driver' # This is the base class for a provider for the V2 API. A provider # is responsible for creating compute resources to match the # needs of a Vagrant-configured system. class Provider < Vagrant.plugin('2', :provider) @@ -20,10 +21,16 @@ action_method = "action_#{name}" return Action.send(action_method) if Action.respond_to?(action_method) nil end + def driver + return @driver if @driver + + @driver = Driver.new(@machine) + end + # This method is called if the underying machine ID changes. Providers # can use this method to load in new data for the actual backing # machine or to realize that the machine is now gone (the ID can # become `nil`). def machine_id_changed @@ -31,24 +38,47 @@ # This should return a hash of information that explains how to # SSH into the machine. If the machine is not at a point where # SSH is even possible, then `nil` should be returned. def ssh_info - # Run a custom action called "read_ssh_info" which does what it says - # and puts the resulting SSH info into the `:machine_ssh_info` key in - # the environment. + # Return the ssh_info if already retrieved otherwise call the driver + # and save the result. # # Ssh info has following format.. # #{ # :host => "1.2.3.4", # :port => "22", # :username => "mitchellh", # :private_key_path => "/path/to/my/key" #} - env = @machine.action('read_ssh_info') - env[:machine_ssh_info] + # note that modifing @machine.id or accessing @machine.state is not + # thread safe, so be careful to avoid these here as this method may + # be called from other threads of execution. + return nil if state.id != :running + + ip = driver.get_ipaddress(@machine) + + # if can't determine the IP, just return nil and let the core + # deal with it, similar to the docker provider + return nil if !ip + + ssh_info = { + :host => ip, + :port => @machine.config.ssh.guest_port, + :forward_agent => @machine.config.ssh.forward_agent, + :forward_x11 => @machine.config.ssh.forward_x11, + } + + ssh_info[:proxy_command] = ( + "ssh '#{@machine.provider_config.host}' " + + "-l '#{@machine.provider_config.username}' " + + "-i '#{@machine.provider_config.id_ssh_key_file}' " + + "nc %h %p" + ) if @machine.provider_config.connect_via_ssh + + ssh_info end def mac_addresses # Run a custom action called "read_mac_addresses" which will return # a list of mac addresses used by the machine. The returned data will @@ -62,19 +92,31 @@ end # This should return the state of the machine within this provider. # The state must be an instance of {MachineState}. def state - # Run a custom action we define called "read_state" which does - # what it says. It puts the state in the `:machine_state_id` - # key in the environment. - env = @machine.action('read_state') - state_id = env[:machine_state_id] + state_id = nil + state_id = :not_created if !@machine.id + state_id = :not_created if ( + !state_id && (!@machine.id || !driver.created?(@machine.id))) + # Query the driver for the current state of the machine + state_id = driver.state(@machine) if @machine.id && !state_id + state_id = :unknown if !state_id + # This is a special pseudo-state so that we don't set the + # NOT_CREATED_ID while we're setting up the machine. This avoids + # clearing the data dir. + state_id = :preparing if @machine.id == "preparing" + # Get the short and long description - short = I18n.t("vagrant_libvirt.states.short_#{state_id}") - long = I18n.t("vagrant_libvirt.states.long_#{state_id}") + short = state_id.to_s.gsub("_", " ") + long = I18n.t("vagrant_libvirt.states.#{state_id}") + + # If we're not created, then specify the special ID flag + if state_id == :not_created + state_id = Vagrant::MachineState::NOT_CREATED_ID + end # Return the MachineState object Vagrant::MachineState.new(state_id, short, long) end