module Stax::Ssh

Public Class Methods

included(thor) click to toggle source
# 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

Public Instance Methods

ssh(*cmd) click to toggle source
# File lib/stax/mixin/ssh.rb, line 42
def ssh(*cmd)
  try(:before_ssh)
  ssh_cmd(ssh_instances, cmd)
ensure
  try(:after_ssh)
end
ssh_cmd(instances, cmd = [], opt = {}) click to toggle source
# 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_instances() click to toggle source

IP address to ssh

# 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) click to toggle source
# 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