Class: VagrantPlugins::SshConfigManager::Config

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

Overview

Configuration for SSH Config Manager

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Set initial state, call parent constructor



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vagrant_ssh_config_manager/config.rb', line 22

def initialize
  super
  @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.



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

def auto_create_dir
  @auto_create_dir
end

#auto_remove_on_destroyObject

Additional configuration options



19
20
21
# File 'lib/vagrant_ssh_config_manager/config.rb', line 19

def auto_remove_on_destroy
  @auto_remove_on_destroy
end

#cleanup_empty_dirObject

Returns the value of attribute cleanup_empty_dir.



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

def cleanup_empty_dir
  @cleanup_empty_dir
end

#enabledObject

Plugin enabled/disabled flag



11
12
13
# File 'lib/vagrant_ssh_config_manager/config.rb', line 11

def enabled
  @enabled
end

#keep_config_on_haltObject

Returns the value of attribute keep_config_on_halt.



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

def keep_config_on_halt
  @keep_config_on_halt
end

#manage_includesObject

Returns the value of attribute manage_includes.



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

def manage_includes
  @manage_includes
end

#project_isolationObject

Returns the value of attribute project_isolation.



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

def project_isolation
  @project_isolation
end

#refresh_on_provisionObject

Returns the value of attribute refresh_on_provision.



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

def refresh_on_provision
  @refresh_on_provision
end

#ssh_config_dirObject

SSH config directory configuration



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

def ssh_config_dir
  @ssh_config_dir
end

#update_on_reloadObject

Returns the value of attribute update_on_reload.



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

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)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/vagrant_ssh_config_manager/config.rb', line 131

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



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/vagrant_ssh_config_manager/config.rb', line 170

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: 0o700)
    true
  rescue StandardError
    false
  end
end

#finalize!Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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
  if @ssh_config_dir == Vagrant::Plugin::V2::Config::UNSET_VALUE
    @ssh_config_dir = File.expand_path('~/.ssh/config.d/vagrant')
  end
  @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

#merge(other) ⇒ Object

Merge configuration from another config object (for inheritance)



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/vagrant_ssh_config_manager/config.rb', line 151

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

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

  result
end

#ssh_manager_instance(_machine) ⇒ Object Also known as: get_ssh_manager_instance

Retrieve the SSH config manager for a given machine



183
184
185
186
# File 'lib/vagrant_ssh_config_manager/config.rb', line 183

def ssh_manager_instance(_machine)
  require_relative 'file_manager'
  FileManager.new(self)
end

#to_hashObject

Get configuration summary for debugging



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/vagrant_ssh_config_manager/config.rb', line 115

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



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
# File 'lib/vagrant_ssh_config_manager/config.rb', line 58

def validate(_machine)
  errors = _detected_errors

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

  # Validate SSH config directory
  if @ssh_config_dir
    if @ssh_config_dir.is_a?(String)
      # 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
      if File.directory?(@ssh_config_dir)
        # 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
      elsif @auto_create_dir
        begin
          # Try to create the directory to validate the path
          FileUtils.mkdir_p(@ssh_config_dir, mode: 0o700)
        rescue StandardError => 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
      errors << 'sshconfigmanager.ssh_config_dir must be a string path'
    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|
    errors << "sshconfigmanager.#{option_name} must be true or false" unless [true, false].include?(value)
  end

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