class AWS_SSH::EC2

Attributes

LogLevel[RW]
ProxyCommand[RW]
StrictHostKeyChecking[RW]
User[RW]

ssh config file values

UserKnownHostsFile[RW]
api[RW]
class values

aws api object

seperator[RW]

value used as seperater in the tags

ssh_config_file[RW]

auto generated ssh config_file

ssh_key_suffixes[RW]

suffix of the key to use

Public Class Methods

new() click to toggle source
# File lib/aws_ssh/ec2.rb, line 22
def initialize
  self.env_error_check
  self.default_values
end

Public Instance Methods

default_values() click to toggle source
# File lib/aws_ssh/ec2.rb, line 33
def default_values
  @User = "deploy"
  @StrictHostKeyChecking = "no"
  @UserKnownHostsFile = "/dev/null"
  @LogLevel = "quiet"
  @ProxyCommand = AWS_SSH::SSH.proxy

  @seperator = "."
  @ssh_key_suffixes = [".pem", ""]
  @api = AWS::EC2.new(access_key_id: ENV['AWS_ACCESS_KEY_ID'], secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], region: ENV['AWS_REGION'])
end
env_error_check() click to toggle source
# File lib/aws_ssh/ec2.rb, line 27
def env_error_check
  if ENV['AWS_ACCESS_KEY_ID'].nil? || ENV['AWS_SECRET_ACCESS_KEY'].nil? || ENV['AWS_REGION'].nil?
    raise MissingAWS_CONFIG, "Missing AWS config data from the ENV."
  end
end
hosts() click to toggle source
# File lib/aws_ssh/ec2.rb, line 86
def hosts
  hosts = {}
  self.instances.select!{|i| i.status.to_s == "running"}.each do |i|
    info = self.instance_data(i)
    if !info[:key_file].nil?
      self.instance_names(info).each{ |h| hosts[h] = info}
    end
  end
  return hosts
end
instance_data(instance) click to toggle source
# File lib/aws_ssh/ec2.rb, line 56
def instance_data(instance)
  tags = instance.tags.to_h
  key_file = nil
  base = ENV['HOME']+"/.ssh/"+instance.key_name
  @ssh_key_suffixes.each{|k| if key_file.nil? && File.exists?(base+k) then key_file = base+k end }
  return {
    :name => if tags["Name"].nil? then nil else tags["Name"].to_s.strip end,
    :user => if tags["user"].nil? then @User else tags["user"].to_s.strip end,
    :envs => if tags["environment"].nil? then nil else tags["environment"].split(@seperator) end,
    :services => if tags["service"].nil? then nil else tags["service"].split(@seperator) end,
    :ip => instance.private_ip_address,
    :key => instance.key_name,
    :key_file => key_file
  }
end
instance_names(info) click to toggle source
# File lib/aws_ssh/ec2.rb, line 72
def instance_names(info)
  names = []
  if ! info[:name].nil? then names.push(info[:name]) end
  if ! info[:envs].nil? && ! info[:services].nil?
    info[:envs].each do |e|
      info[:services].each do |s|
        names.push("#{e}_#{s}")
        names.push("#{s}_#{e}")
      end
    end
  end
  return names
end
instances() click to toggle source
# File lib/aws_ssh/ec2.rb, line 46
def instances
  instances = []
  # normal instances
  @api.instances.each{|i| instances.push(i)}
  # vpc based insatnces
  @api.vpcs.each{|v| v.instances.each{|vi| instances.push(vi)} }
  return instances
end
write() click to toggle source
# File lib/aws_ssh/ec2.rb, line 98
def write
  # loop over hash and generate string
  config = "\n\n# Custom generated list from AWS on #{Time.now}\n\n"
  dir = ENV['HOME']+"/.ssh/"
  sshfile = dir+AWS_SSH::HOSTS_FILE
  File.open(sshfile, "w"){ |file| file.write(config)}
  self.hosts.each do |name, host|
    config += "Host #{name}\n"
    config += "  HostName #{host[:ip].to_s}\n"
    config += "  User #{host[:user].to_s}\n"
    config += "  IdentityFile #{host[:key_file]}\n"
    config += "  StrictHostKeyChecking #{@StrictHostKeyChecking}\n"
    config += "  UserKnownHostsFile #{@UserKnownHostsFile}\n"
    config += "  LogLevel #{@LogLevel}\n"
    config += if ! @ProxyCommand.nil? then "  ProxyCommand #{@ProxyCommand}\n" else "" end
    config += "\n\n"
  end
  # write to file
  File.open(sshfile, "w"){ |file| file.write(config)}
end