Class: Vagrant::AutoSshConfig::Action::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/auto_ssh_config/action/base.rb

Direct Known Subclasses

CreateOrUpdate, Delete

Instance Method Summary collapse

Constructor Details

#initialize(app, env, options = {}) ⇒ Base

Returns a new instance of Base.



7
8
9
10
# File 'lib/vagrant/auto_ssh_config/action/base.rb', line 7

def initialize(app, env, options = {})
  @app = app
  @options = options
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
# File 'lib/vagrant/auto_ssh_config/action/base.rb', line 12

def call(env)
  @app.call(env)
  run(env)
end

#run(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vagrant/auto_ssh_config/action/base.rb', line 17

def run(env)
  env[:ui].info("#{doing_prefix} ssh_config for current box...")

  SSH_CONFIG_PATH.open(File::RDWR | File::CREAT) do |f|
    f.flock(File::LOCK_EX)
    old_ssh_config = f.read
    ssh_config_without_myself =
      old_ssh_config.gsub(TEMPLATE_REGEXP, "").
      sub(/\n?\n?\z/, "")
    new_ssh_config = create_new_ssh_config(ssh_config_without_myself)
    if old_ssh_config == new_ssh_config
      env[:ui].info("not changed.")
      return
    end
    begin
      f.rewind
      f.puts(new_ssh_config)
    rescue
      f.rewind
      f.write(old_ssh_config)
      raise
    ensure
      f.flush
      f.truncate(f.pos)
    end
  end
  env[:ui].detail("#{done_prefix} ssh_config for current box.")
end