Class: VagrantPlugins::SshConfigManager::Config

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vagrant-ssh-config-manager/config.rb', line 23

def initialize
  @enabled = Vagrant::Plugin::V2::Config::UNSET_VALUE
  @ssh_config_dir = Vagrant::Plugin::V2::Config::UNSET_VALUE
  @manage_includes = Vagrant::Plugin::V2::Config::UNSET_VALUE
  @auto_create_dir = Vagrant::Plugin::V2::Config::UNSET_VALUE
  @cleanup_empty_dir = Vagrant::Plugin::V2::Config::UNSET_VALUE
  @auto_remove_on_destroy = Vagrant::Plugin::V2::Config::UNSET_VALUE
  @update_on_reload = Vagrant::Plugin::V2::Config::UNSET_VALUE
  @refresh_on_provision = Vagrant::Plugin::V2::Config::UNSET_VALUE
  @keep_config_on_halt = Vagrant::Plugin::V2::Config::UNSET_VALUE
  @project_isolation = Vagrant::Plugin::V2::Config::UNSET_VALUE
end

Instance Attribute Details

#auto_create_dirObject

Returns the value of attribute auto_create_dir.



13
14
15
# File 'lib/vagrant-ssh-config-manager/config.rb', line 13

def auto_create_dir
  @auto_create_dir
end

#auto_remove_on_destroyObject

Additional configuration options



17
18
19
# File 'lib/vagrant-ssh-config-manager/config.rb', line 17

def auto_remove_on_destroy
  @auto_remove_on_destroy
end

#cleanup_empty_dirObject

Returns the value of attribute cleanup_empty_dir.



14
15
16
# File 'lib/vagrant-ssh-config-manager/config.rb', line 14

def cleanup_empty_dir
  @cleanup_empty_dir
end

#enabledObject

Plugin enabled/disabled flag



8
9
10
# File 'lib/vagrant-ssh-config-manager/config.rb', line 8

def enabled
  @enabled
end

#keep_config_on_haltObject

Returns the value of attribute keep_config_on_halt.



20
21
22
# File 'lib/vagrant-ssh-config-manager/config.rb', line 20

def keep_config_on_halt
  @keep_config_on_halt
end

#manage_includesObject

Returns the value of attribute manage_includes.



12
13
14
# File 'lib/vagrant-ssh-config-manager/config.rb', line 12

def manage_includes
  @manage_includes
end

#project_isolationObject

Returns the value of attribute project_isolation.



21
22
23
# File 'lib/vagrant-ssh-config-manager/config.rb', line 21

def project_isolation
  @project_isolation
end

#refresh_on_provisionObject

Returns the value of attribute refresh_on_provision.



19
20
21
# File 'lib/vagrant-ssh-config-manager/config.rb', line 19

def refresh_on_provision
  @refresh_on_provision
end

#ssh_config_dirObject

SSH config directory configuration



11
12
13
# File 'lib/vagrant-ssh-config-manager/config.rb', line 11

def ssh_config_dir
  @ssh_config_dir
end

#update_on_reloadObject

Returns the value of attribute update_on_reload.



18
19
20
# File 'lib/vagrant-ssh-config-manager/config.rb', line 18

def update_on_reload
  @update_on_reload
end

Instance Method Details

#enabled_for_action?(action_name) ⇒ Boolean

Check if the plugin should operate for a given action

Returns:

  • (Boolean)


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/vagrant-ssh-config-manager/config.rb', line 135

def enabled_for_action?(action_name)
  return false unless @enabled

  case action_name.to_sym
  when :up, :resume
    true
  when :destroy
    @auto_remove_on_destroy
  when :reload
    @update_on_reload
  when :provision
    @refresh_on_provision
  when :halt, :suspend
    @keep_config_on_halt
  else
    false
  end
end

#ensure_ssh_config_directoryObject

Create SSH config directory with proper permissions



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/vagrant-ssh-config-manager/config.rb', line 174

def ensure_ssh_config_directory
  return false unless @auto_create_dir
  return true if File.directory?(@ssh_config_dir)

  begin
    FileUtils.mkdir_p(@ssh_config_dir, mode: 0700)
    true
  rescue => e
    false
  end
end

#finalize!Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vagrant-ssh-config-manager/config.rb', line 36

def finalize!
  # Set default values for unset configuration options
  @enabled = true if @enabled == Vagrant::Plugin::V2::Config::UNSET_VALUE
  @ssh_config_dir = File.expand_path("~/.ssh/config.d/vagrant") if @ssh_config_dir == Vagrant::Plugin::V2::Config::UNSET_VALUE
  @manage_includes = false if @manage_includes == Vagrant::Plugin::V2::Config::UNSET_VALUE
  @auto_create_dir = true if @auto_create_dir == Vagrant::Plugin::V2::Config::UNSET_VALUE
  @cleanup_empty_dir = true if @cleanup_empty_dir == Vagrant::Plugin::V2::Config::UNSET_VALUE
  @auto_remove_on_destroy = true if @auto_remove_on_destroy == Vagrant::Plugin::V2::Config::UNSET_VALUE
  @update_on_reload = true if @update_on_reload == Vagrant::Plugin::V2::Config::UNSET_VALUE
  @refresh_on_provision = true if @refresh_on_provision == Vagrant::Plugin::V2::Config::UNSET_VALUE
  @keep_config_on_halt = true if @keep_config_on_halt == Vagrant::Plugin::V2::Config::UNSET_VALUE
  @project_isolation = true if @project_isolation == Vagrant::Plugin::V2::Config::UNSET_VALUE

  # Expand and validate file paths
  @ssh_config_dir = File.expand_path(@ssh_config_dir) if @ssh_config_dir.is_a?(String)
  
  # Ensure SSH config directory exists if auto_create_dir is enabled
  ensure_ssh_config_directory if @auto_create_dir && @ssh_config_dir
end

#get_ssh_manager_instance(machine) ⇒ Object

Get the appropriate manager instance



187
188
189
190
191
# File 'lib/vagrant-ssh-config-manager/config.rb', line 187

def get_ssh_manager_instance(machine)
  # Use separate file approach with FileManager
  require_relative 'file_manager'
  FileManager.new(self)
end

#merge(other) ⇒ Object

Merge configuration from another config object (for inheritance)



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/vagrant-ssh-config-manager/config.rb', line 155

def merge(other)
  result = self.class.new

  # Merge each attribute, preferring the other config's values if set
  result.enabled = other.enabled != UNSET_VALUE ? other.enabled : @enabled
  result.ssh_config_dir = other.ssh_config_dir != UNSET_VALUE ? other.ssh_config_dir : @ssh_config_dir
  result.manage_includes = other.manage_includes != UNSET_VALUE ? other.manage_includes : @manage_includes
  result.auto_create_dir = other.auto_create_dir != UNSET_VALUE ? other.auto_create_dir : @auto_create_dir
  result.cleanup_empty_dir = other.cleanup_empty_dir != UNSET_VALUE ? other.cleanup_empty_dir : @cleanup_empty_dir
  result.auto_remove_on_destroy = other.auto_remove_on_destroy != UNSET_VALUE ? other.auto_remove_on_destroy : @auto_remove_on_destroy
  result.update_on_reload = other.update_on_reload != UNSET_VALUE ? other.update_on_reload : @update_on_reload
  result.refresh_on_provision = other.refresh_on_provision != UNSET_VALUE ? other.refresh_on_provision : @refresh_on_provision
  result.keep_config_on_halt = other.keep_config_on_halt != UNSET_VALUE ? other.keep_config_on_halt : @keep_config_on_halt
  result.project_isolation = other.project_isolation != UNSET_VALUE ? other.project_isolation : @project_isolation

  result
end

#to_hashObject

Get configuration summary for debugging



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/vagrant-ssh-config-manager/config.rb', line 119

def to_hash
  {
    enabled: @enabled,
    ssh_config_dir: @ssh_config_dir,
    manage_includes: @manage_includes,
    auto_create_dir: @auto_create_dir,
    cleanup_empty_dir: @cleanup_empty_dir,
    auto_remove_on_destroy: @auto_remove_on_destroy,
    update_on_reload: @update_on_reload,
    refresh_on_provision: @refresh_on_provision,
    keep_config_on_halt: @keep_config_on_halt,
    project_isolation: @project_isolation
  }
end

#validate(machine) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/vagrant-ssh-config-manager/config.rb', line 56

def validate(machine)
  errors = _detected_errors

  # Validate enabled flag
  unless [true, false].include?(@enabled)
    errors << "sshconfigmanager.enabled must be true or false"
  end

  # Validate SSH config directory
  if @ssh_config_dir
    unless @ssh_config_dir.is_a?(String)
      errors << "sshconfigmanager.ssh_config_dir must be a string path"
    else
      # Validate directory path format
      expanded_path = File.expand_path(@ssh_config_dir)
      if expanded_path.include?("..") || expanded_path.include?("//")
        errors << "sshconfigmanager.ssh_config_dir contains invalid path components: #{@ssh_config_dir}"
      end

      # Check if the directory exists or can be created
      unless File.directory?(@ssh_config_dir)
        if @auto_create_dir
          begin
            # Try to create the directory to validate the path
            FileUtils.mkdir_p(@ssh_config_dir, mode: 0700)
          rescue => e
            errors << "sshconfigmanager.ssh_config_dir cannot be created: #{e.message}"
          end
        else
          errors << "sshconfigmanager.ssh_config_dir does not exist and auto_create_dir is disabled: #{@ssh_config_dir}"
        end
      else
        # Check directory permissions
        unless File.readable?(@ssh_config_dir) && File.writable?(@ssh_config_dir)
          errors << "sshconfigmanager.ssh_config_dir is not readable/writable: #{@ssh_config_dir}"
        end
      end
    end
  end

  # Validate boolean options
  boolean_options = {
    'auto_remove_on_destroy' => @auto_remove_on_destroy,
    'update_on_reload' => @update_on_reload,
    'refresh_on_provision' => @refresh_on_provision,
    'keep_config_on_halt' => @keep_config_on_halt,
    'project_isolation' => @project_isolation,
    'manage_includes' => @manage_includes,
    'auto_create_dir' => @auto_create_dir,
    'cleanup_empty_dir' => @cleanup_empty_dir
  }

  boolean_options.each do |option_name, value|
    unless [true, false].include?(value)
      errors << "sshconfigmanager.#{option_name} must be true or false"
    end
  end

  # Return validation results
  { "SSH Config Manager" => errors }
end