Class: VagrantPlugins::SshConfigManager::IncludeManager

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant_ssh_config_manager/include_manager.rb

Constant Summary collapse

PLUGIN_MARKER_START =
'# BEGIN vagrant-ssh-config-manager'
PLUGIN_MARKER_END =
'# END vagrant-ssh-config-manager'

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ IncludeManager

Initialize IncludeManager with configuration



14
15
16
17
# File 'lib/vagrant_ssh_config_manager/include_manager.rb', line 14

def initialize(config)
  @config = config
  @logger = Log4r::Logger.new('vagrant::plugins::sshconfigmanager::includemanager')
end

Instance Method Details

#add_include_directiveObject

Add Include directive to main SSH config file



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vagrant_ssh_config_manager/include_manager.rb', line 34

def add_include_directive
  return false unless @config.manage_includes
  return true if include_directive_exists?

  begin
    # Create backup before modifying
    create_backup

    # Add Include directive at the beginning of file
    add_include_to_config

    @logger.info("Added Include directive for #{@config.ssh_config_dir} to ~/.ssh/config")
    true
  rescue StandardError => e
    @logger.error("Failed to add Include directive: #{e.message}")
    restore_backup
    false
  end
end

#find_include_location(content) ⇒ Object

Parse SSH config file and find optimal location for Include



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/vagrant_ssh_config_manager/include_manager.rb', line 100

def find_include_location(content)
  lines = content.lines

  # Look for existing Include directives to place ours with them
  include_line_index = lines.find_index { |line| line.strip.start_with?('Include ') }

  if include_line_index
    # Place before other Include directives
    include_line_index
  else
    # Place at the beginning of file, after any initial comments
    comment_end = 0
    lines.each_with_index do |line, index|
      break unless line.strip.empty? || line.strip.start_with?('#')

      comment_end = index + 1
    end
    comment_end
  end
end

#handle_empty_main_configObject

Handle edge case: empty main config file



129
130
131
132
133
134
135
136
137
# File 'lib/vagrant_ssh_config_manager/include_manager.rb', line 129

def handle_empty_main_config
  return if File.exist?(ssh_config_file)

  # Create empty SSH config file with proper permissions
  FileUtils.mkdir_p(File.dirname(ssh_config_file), mode: 0o700)
  File.write(ssh_config_file, '')
  File.chmod(0o600, ssh_config_file)
  @logger.info("Created empty SSH config file: #{ssh_config_file}")
end

#include_directive_exists?Boolean

Check if Include directive exists in main SSH config

Returns:

  • (Boolean)


25
26
27
28
29
30
31
# File 'lib/vagrant_ssh_config_manager/include_manager.rb', line 25

def include_directive_exists?
  return false unless File.exist?(ssh_config_file)

  content = File.read(ssh_config_file)
  include_pattern = /^Include\s+#{Regexp.escape(@config.ssh_config_dir)}/
  content.match?(include_pattern)
end

#main_config_writable?Boolean

Check if main SSH config file is write-protected

Returns:

  • (Boolean)


122
123
124
125
126
# File 'lib/vagrant_ssh_config_manager/include_manager.rb', line 122

def main_config_writable?
  return false unless File.exist?(ssh_config_file)

  File.writable?(ssh_config_file)
end

#manage_include_directiveObject

Manage Include directive based on current state



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/vagrant_ssh_config_manager/include_manager.rb', line 87

def manage_include_directive
  return unless @config.manage_includes

  if should_remove_include_directive?
    remove_include_directive
  elsif Dir.exist?(@config.ssh_config_dir) && Dir.entries(@config.ssh_config_dir).any? do |f|
    File.file?(File.join(@config.ssh_config_dir, f)) && f.end_with?('.conf')
  end
    add_include_directive
  end
end

#remove_include_directiveObject

Remove Include directive from main SSH config file



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/vagrant_ssh_config_manager/include_manager.rb', line 55

def remove_include_directive
  return false unless @config.manage_includes
  return true unless include_directive_exists?

  begin
    # Create backup before modifying
    create_backup

    # Remove Include directive
    remove_include_from_config

    @logger.info("Removed Include directive for #{@config.ssh_config_dir} from ~/.ssh/config")
    true
  rescue StandardError => e
    @logger.error("Failed to remove Include directive: #{e.message}")
    restore_backup
    false
  end
end

#should_remove_include_directive?Boolean

Check if Include directive should be removed (no config files exist)

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
# File 'lib/vagrant_ssh_config_manager/include_manager.rb', line 76

def should_remove_include_directive?
  return false unless @config.cleanup_empty_dir
  return false unless Dir.exist?(@config.ssh_config_dir)

  # Check if directory is empty (excluding . and ..)
  entries = Dir.entries(@config.ssh_config_dir) - %w[. ..]
  config_files = entries.select { |file| file.end_with?('.conf') }
  config_files.empty?
end

#ssh_config_fileObject

Get the SSH config file path (always ~/.ssh/config)



20
21
22
# File 'lib/vagrant_ssh_config_manager/include_manager.rb', line 20

def ssh_config_file
  @ssh_config_file ||= File.expand_path('~/.ssh/config')
end

#validate_main_configObject

Validate SSH config file format



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/vagrant_ssh_config_manager/include_manager.rb', line 140

def validate_main_config
  return true unless File.exist?(ssh_config_file)

  begin
    content = File.read(ssh_config_file)
    # Basic validation - check for severely malformed config
    lines = content.lines
    lines.each_with_index do |line, index|
      stripped = line.strip
      next if stripped.empty? || stripped.start_with?('#')

      # Check for basic SSH config format issues
      if stripped.include?("\t") && stripped.start_with?('Host ')
        @logger.warn("Potential SSH config format issue at line #{index + 1}: tabs in Host directive")
      end
    end
    true
  rescue StandardError => e
    @logger.error("SSH config validation failed: #{e.message}")
    false
  end
end