Class: OpzWorks::Commands::SSH

Inherits:
Object
  • Object
show all
Defined in:
lib/opzworks/commands/ssh.rb

Class Method Summary collapse

Class Method Details



13
14
15
# File 'lib/opzworks/commands/ssh.rb', line 13

def self.banner
  'Generate and update SSH configuration files'
end

.runObject



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
45
46
47
48
49
50
51
52
53
54
55
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
# File 'lib/opzworks/commands/ssh.rb', line 17

def self.run
  options = Trollop.options do
    banner "      \#{SSH.banner}\n\n        opzworks ssh {stack1} {stack2} {...}\n\n      The stack name can be passed as any unique regex. If no\n      arguments are passed, the command will iterate over all stacks.\n\n      Options:\n    EOS\n    opt :update, 'Update ~/.ssh/config directly'\n    opt :backup, 'Backup old SSH config before updating'\n    opt :quiet, 'Use SSH LogLevel quiet', default: true\n    opt :private, 'Return private IPs, rather than the default of public', default: false\n    opt :raw, 'Return only raw IPs rather than .ssh/config format output', default: false\n  end\n\n  config = OpzWorks.config\n  client = Aws::OpsWorks::Client.new(region: config.aws_region, profile: config.aws_profile)\n\n  stacks     = []\n  stack_data = client.describe_stacks\n\n  if ARGV.empty?\n    stack_data[:stacks].each { |stack| stacks.push(stack) }\n  else\n    ARGV.each do |arg|\n      stack_data[:stacks].each do |stack|\n        stacks.push(stack) if stack[:name] =~ /\#{arg}/\n      end\n    end\n  end\n\n  stacks.each do |stack|\n    instances   = []\n    stack_name  = ''\n\n    stack_name = stack[:name].gsub('::', '-')\n\n    result = client.describe_instances(stack_id: stack[:stack_id])\n    instances += result.instances.select { |i| i[:status] != 'stopped' }\n\n    instances.map! do |instance|\n      ip = if options[:private]\n             instance[:private_ip]\n           else\n             instance[:elastic_ip].nil? ? instance[:public_ip] : instance[:elastic_ip]\n           end\n\n      if options[:raw]\n        puts ip\n      else\n        next if ip.nil?\n        parameters = {\n          'Host'     => \"\#{instance[:hostname]}-\#{stack_name}\",\n          'HostName' => ip,\n          'User'     => config.ssh_user_name\n        }\n        parameters['LogLevel'] = 'quiet' if options[:quiet]\n        parameters.map { |param| param.join(' ') }.join(\"\\n  \")\n      end\n    end\n\n    next if options[:raw]\n    new_contents = \"\#{instances.join(\"\\n\")}\\n\"\n\n    if options[:update]\n      ssh_config = \"\#{ENV['HOME']}/.ssh/config\"\n      old_contents = File.read(ssh_config)\n\n      if options[:backup]\n        backup_name = ssh_config + '.backup'\n        File.open(backup_name, 'w') { |file| file.puts old_contents }\n      end\n\n      File.open(ssh_config, 'w') do |file|\n        file.puts old_contents.gsub(\n          /\\n?\\n?\#{SSH_PREFIX}.*\#{SSH_POSTFIX}\\n?\\n?/m,\n          ''\n        )\n        file.puts new_contents\n      end\n\n      puts \"Successfully updated \#{ssh_config} with \#{instances.length} instances!\"\n    else\n      puts new_contents.strip\n    end\n  end\nend\n".unindent