Module: Stax::Ssh

Defined in:
lib/stax/mixin/ssh.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(thor) ⇒ Object



7
8
9
10
11
12
13
14
15
16
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
# File 'lib/stax/mixin/ssh.rb', line 7

def self.included(thor)

  ## stack class can define this
  # def ssh_options
  #   {
  #     StrictHostKeyChecking: 'no',
  #     UserKnownHostsFile: '/dev/null',
  #   }
  # end

  ## IP address to ssh
  def ssh_instances
    Aws::Ec2.instances(stack_name).map(&:public_ip_address).reject(&:nil?)
  end

  def ssh_options_format(opt)
    opt.reject do |_,v|
      v.nil?
    end.map do |k,v|
      "-o #{k}=#{v}"
    end.join(' ')
  end

  def ssh_cmd(instances, cmd = [], opt = {})
    c = cmd.join(' ')
    o = ssh_options_format((try(:ssh_options) || {}).merge(opt))
    instances.each do |i|
      system "ssh #{o} #{i} #{c}"
    end
  end

  thor.class_eval do

    ## stack class can add before/after_ssh hooks
    desc 'ssh [CMD]', 'ssh to ec2 instances'
    def ssh(*cmd)
      try(:before_ssh)
      ssh_cmd(ssh_instances, cmd)
    ensure
      try(:after_ssh)
    end

  end
end

Instance Method Details

#ssh_cmd(instances, cmd = [], opt = {}) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/stax/mixin/ssh.rb', line 30

def ssh_cmd(instances, cmd = [], opt = {})
  c = cmd.join(' ')
  o = ssh_options_format((try(:ssh_options) || {}).merge(opt))
  instances.each do |i|
    system "ssh #{o} #{i} #{c}"
  end
end

#ssh_instancesObject

IP address to ssh



18
19
20
# File 'lib/stax/mixin/ssh.rb', line 18

def ssh_instances
  Aws::Ec2.instances(stack_name).map(&:public_ip_address).reject(&:nil?)
end

#ssh_options_format(opt) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/stax/mixin/ssh.rb', line 22

def ssh_options_format(opt)
  opt.reject do |_,v|
    v.nil?
  end.map do |k,v|
    "-o #{k}=#{v}"
  end.join(' ')
end