Class: VagrantPlugins::SshConfigManager::SshInfoExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-ssh-config-manager/ssh_info_extractor.rb

Instance Method Summary collapse

Constructor Details

#initialize(machine) ⇒ SshInfoExtractor

Returns a new instance of SshInfoExtractor.



4
5
6
7
# File 'lib/vagrant-ssh-config-manager/ssh_info_extractor.rb', line 4

def initialize(machine)
  @machine = machine
  @logger = Log4r::Logger.new("vagrant::plugins::ssh_config_manager::ssh_info_extractor")
end

Instance Method Details

#extract_ssh_infoObject

Extract SSH information from Vagrant’s internal APIs This replicates what ‘vagrant ssh-config’ does but using internal methods



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vagrant-ssh-config-manager/ssh_info_extractor.rb', line 11

def extract_ssh_info
  begin
    ssh_info = @machine.ssh_info
    return nil if ssh_info.nil?

    # Additional validation for SSH info completeness
    return nil unless valid_ssh_info?(ssh_info)

    # Get the SSH configuration similar to what vagrant ssh-config provides
    config = build_ssh_config(ssh_info)
    
    @logger.info("Extracted SSH info for machine: #{@machine.name}") if @logger
    config
  rescue Vagrant::Errors::SSHNotReady => e
    @logger.debug("SSH not ready for machine #{@machine.name}: #{e.message}") if @logger
    nil
  rescue Vagrant::Errors::SSHUnavailable => e
    @logger.debug("SSH unavailable for machine #{@machine.name}: #{e.message}") if @logger
    nil
  rescue => e
    @logger.warn("Failed to extract SSH info for machine #{@machine.name}: #{e.message}") if @logger
    nil
  end
end

#machine_supports_ssh?Boolean

Comprehensive check for SSH support

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/vagrant-ssh-config-manager/ssh_info_extractor.rb', line 50

def machine_supports_ssh?
  # Basic checks
  return false unless @machine
  return false unless @machine.communicate_ready?
  
  # Check if machine state supports SSH
  return false unless machine_state_supports_ssh?
  
  # Check if communicator is SSH-based
  return false unless ssh_communicator?
  
  # Check if provider supports SSH
  return false unless provider_supports_ssh?
  
  true
rescue => e
  @logger.debug("Machine SSH support check failed: #{e.message}")
  false
end

#ssh_capable?Boolean

Check if the machine supports SSH with comprehensive validation

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
# File 'lib/vagrant-ssh-config-manager/ssh_info_extractor.rb', line 37

def ssh_capable?
  # Simplified check - if machine is running and has SSH info, assume SSH is available
  @machine && 
  @machine.state && 
  @machine.state.id == :running && 
  @machine.ssh_info &&
  ssh_communicator?
rescue => e
  @logger.debug("SSH capability check failed: #{e.message}") if @logger
  false
end