Class: Fulmar::Infrastructure::Service::SSHConfigService

Inherits:
Object
  • Object
show all
Defined in:
lib/fulmar/infrastructure/service/ssh_config_service.rb

Overview

Adds entries to the ssh config and checks for existing ones

Constant Summary collapse

DEFAULT_CONFIG_FILE =
"#{ENV['HOME']}/.ssh/config".freeze
DEFAULT_KNOWN_HOSTS_FILE =
"#{ENV['HOME']}/.ssh/known_hosts".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, config_file = DEFAULT_CONFIG_FILE, known_hosts_file = DEFAULT_KNOWN_HOSTS_FILE) ⇒ SSHConfigService

Returns a new instance of SSHConfigService.



16
17
18
19
20
21
# File 'lib/fulmar/infrastructure/service/ssh_config_service.rb', line 16

def initialize(config, config_file = DEFAULT_CONFIG_FILE, known_hosts_file = DEFAULT_KNOWN_HOSTS_FILE)
  @config = config
  @config_file = config_file
  @known_hosts_file = known_hosts_file
  @quiet = false
end

Instance Attribute Details

#config_fileObject

Returns the value of attribute config_file.



14
15
16
# File 'lib/fulmar/infrastructure/service/ssh_config_service.rb', line 14

def config_file
  @config_file
end

#known_host_fileObject

Returns the value of attribute known_host_file.



14
15
16
# File 'lib/fulmar/infrastructure/service/ssh_config_service.rb', line 14

def known_host_file
  @known_host_file
end

#quietObject

Returns the value of attribute quiet.



14
15
16
# File 'lib/fulmar/infrastructure/service/ssh_config_service.rb', line 14

def quiet
  @quiet
end

Instance Method Details

#add_host(hostname, ssh_config = {}) ⇒ Object

Adds a host to the ssh config file



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/fulmar/infrastructure/service/ssh_config_service.rb', line 90

def add_host(hostname, ssh_config = {})
  puts "Adding host #{hostname}..." if @config[:debug]
  config_file = File.open(@config_file, 'a')

  unless ssh_config[:IdentityFile].blank? || ssh_config[:IdentityFile][0, 1] == '/'
    ssh_config[:IdentityFile] = @config.base_path + '/' + ssh_config[:IdentityFile]
  end

  config_file.puts host_entry(hostname, ssh_config)

  config_file.close
end

#add_hostsObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fulmar/infrastructure/service/ssh_config_service.rb', line 42

def add_hosts
  backup_file
  @config.hosts.values.each do |data|
    unless config_valid?(data)
      puts "Skipping #{data[:hostname]}, config not sufficient." if @config[:debug]
      next
    end
    edit_host(data[:hostname], data[:ssh_config])
  end
  show_diff
end

#backup_fileObject



103
104
105
106
# File 'lib/fulmar/infrastructure/service/ssh_config_service.rb', line 103

def backup_file
  backup_filename = "#{@config_file}.bak"
  FileUtils.cp @config_file, backup_filename
end

#changed?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/fulmar/infrastructure/service/ssh_config_service.rb', line 23

def changed?
  File.read(@config_file) != File.read("#{@config_file}.bak")
end

#diff(type = :color) ⇒ Object



27
28
29
30
31
# File 'lib/fulmar/infrastructure/service/ssh_config_service.rb', line 27

def diff(type = :color)
  before = File.read("#{@config_file}.bak")
  after = File.read(@config_file)
  Diffy::Diff.new(before, after, context: 3).to_s(type)
end

#edit_host(hostname, ssh_config) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/fulmar/infrastructure/service/ssh_config_service.rb', line 65

def edit_host(hostname, ssh_config)
  data = read_file
  new_data = block_before(data.clone, hostname) +
             host_entry(hostname, ssh_config) +
             block_after(data, hostname)

  File.open(@config_file, 'w') do |file|
    file.puts new_data.join("\n")
  end
end

#host_exists?(hostname) ⇒ Boolean

Parses the users ssh config for an existing hostname

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fulmar/infrastructure/service/ssh_config_service.rb', line 77

def host_exists?(hostname)
  config_file = File.open(@config_file, 'r')
  while (line = config_file.gets)
    if /\s*Host #{Regexp.escape(hostname)}\s*$/ =~ line
      config_file.close
      return true
    end
  end
  config_file.close
  false
end

#remove_known_host(hostname) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/fulmar/infrastructure/service/ssh_config_service.rb', line 54

def remove_known_host(hostname)
  input_file = File.open(@known_hosts_file, 'r')
  output_file = File.open(@known_hosts_file + '.temp', 'w')
  while (line = input_file.gets)
    output_file.puts(line) unless /^\[?#{Regexp.escape(hostname)}(?:\]:\d+)?[ ,]/ =~ line
  end
  input_file.close
  output_file.close
  FileUtils.mv(@known_hosts_file + '.temp', @known_hosts_file)
end

#show_diffObject



33
34
35
36
37
38
39
40
# File 'lib/fulmar/infrastructure/service/ssh_config_service.rb', line 33

def show_diff
  return if @quiet || !changed?
  puts 'You ssh host configuration changed: '
  puts '--------------- DIFF ---------------'
  puts diff
  puts '--------------- /DIFF --------------'
  puts 'You can revert these changes by running "fulmar revert:ssh_config"'
end