class Cli

Constants

DEFAULT_DESCRIBE_INSTANCES
DEFAULT_HOST_TAG
DEFAULT_SSH_CONFIG_FILE

constanst

DEFAULT_SSH_CONFIG_INIT
MAGIC_END
MAGIC_START
PROGNAME

Public Class Methods

add_shared_option(name, options = {}) click to toggle source
# File lib/cli/aws-ssh-resolver-cli.rb, line 43
def add_shared_option(name, options = {})
  @shared_options = {} if @shared_options.nil?
  @shared_options[name] =  options
end
new(*args) click to toggle source

constructore

Calls superclass method
# File lib/cli/aws-ssh-resolver-cli.rb, line 22
def initialize(*args)
  super
  @logger = getLogger( PROGNAME, options )
end
shared_options(*option_names) click to toggle source
# File lib/cli/aws-ssh-resolver-cli.rb, line 48
def shared_options(*option_names)
  option_names.each do |option_name|
    opt =  @shared_options[option_name]
    raise "Tried to access shared option '#{option_name}' but it was not previously defined" if opt.nil?
    option option_name, opt
  end
end

Public Instance Methods

__print_version() click to toggle source
# File lib/cli/aws-ssh-resolver-cli.rb, line 34
def __print_version
  puts File.readlines( File.join File.dirname(__FILE__), "../../VERSION" ).join( " " )
end
aws() click to toggle source
# File lib/cli/aws-ssh-resolver-cli.rb, line 154
def aws()

  host_tag        = options[:host_tag]
  ssh_config_file = options[:ssh_config_file]
  ssh_config_init = options[:ssh_config_init]
  describe_instances = options[:describe_instances]

  # run aws-cli query
  ec2_instances = aws_cli_ec2_instances( describe_instances )

  # hash with host => hostname
  host_hostname_mappings = create_host_hostname_mappings( ec2_instances, host_tag )

  # seed  'ssh_config_file' with 'ssh_config_init'
  init_ssh_config_file( ssh_config_file, ssh_config_init )

  # output to file
  output_to_file(  ssh_config_file, host_hostname_mappings )

end
aws_cli_ec2_instances( describe_instances ) click to toggle source

return raw ec2 describe-status JSON

# File lib/cli/aws-ssh-resolver-cli.rb, line 217
def aws_cli_ec2_instances( describe_instances ) 

  @logger.info( "#{__method__} describe_instances '#{describe_instances}'" )

  json_string = %x{ #{describe_instances} }
  ec2_instances = parse_json( json_string )

  @logger.debug( "#{__method__} describe_instances '#{describe_instances}' --> #{ec2_instances}" )

  return ec2_instances

end
create_host_hostname_mappings( ec2_instances, host_tag ) click to toggle source

map raw aws ec2-describe-status json to hash with Host/PublicDnsName props

# File lib/cli/aws-ssh-resolver-cli.rb, line 256
def create_host_hostname_mappings( ec2_instances, host_tag ) 

  @logger.info( "#{__method__} host_tag '#{host_tag}'" )

  host_hostname_mappings = ec2_instances['Reservations']
    .map{ |i| i['Instances'].first }
    .select{ |i|  i['Tags'].select{ |t| t['Key'] == host_tag }.any? }
    .map{ |i|   { 
      :Host => i['Tags'].select{ |t| t['Key'] == host_tag }.first['Value'],
      :HostName => i['PublicDnsName'] && !i['PublicDnsName'].empty? ? i['PublicDnsName'] : i['PrivateDnsName']
    } }

  @logger.info( "#{__method__} host_hostname_mappings '#{host_hostname_mappings}'" )
  return host_hostname_mappings
end
get_ec2_instances( file ) click to toggle source

return raw ec2 describe-status JSON

# File lib/cli/aws-ssh-resolver-cli.rb, line 232
def get_ec2_instances( file ) 

  @logger.info( "#{__method__} read file '#{file}'" )

  json_string =  ( file == "-" ? $stdin.readlines.join :  File.read(file) )
  ec2_instances = parse_json( json_string )

  @logger.debug( "#{__method__} file '#{file}' --> #{ec2_instances}" )
  return ec2_instances

end
init_ssh_config_file( ssh_config_file, ssh_config_init ) click to toggle source

copy ‘ssh_config_init’ to ‘ssh_config_file’ - if it does not exist && ‘ssh_config_init’ define

# File lib/cli/aws-ssh-resolver-cli.rb, line 310
def init_ssh_config_file( ssh_config_file, ssh_config_init ) 
  File.open( ssh_config_file, 'w') { |f| f.write(File.read(ssh_config_init )) } if !File.exist?( ssh_config_file ) && 
    ssh_config_init && !ssh_config_init.empty?
end
output_to_file( ssh_config_file, host_hostname_mappings ) click to toggle source

add ‘host_hostname_mappings’ to ‘ssh_config_file’

# File lib/cli/aws-ssh-resolver-cli.rb, line 273
    def output_to_file( ssh_config_file, host_hostname_mappings ) 

      # Read content of (without magic content) ssh_config_file into memory
      ssh_config_file_content = read_ssh_config_file_content_minus_magic( ssh_config_file )

      # write new magic with host entries
      File.open( ssh_config_file, 'w') do |f2|

        f2.puts MAGIC_START
        f2.puts <<-EOS

# Content generated #{Time.now.strftime("%Y-%m-%d-%H:%M:%S")}

        EOS

        host_hostname_mappings.each do |h|
          host_entry = <<EOS
host #{h[:Host]}
    HostName #{h[:HostName]}


EOS
          f2.puts  host_entry
        end

        f2.puts MAGIC_END
        
        ssh_config_file_content.each do |line|
          f2.puts line
        end

      end

    end
parse_json( json_string ) click to toggle source
# File lib/cli/aws-ssh-resolver-cli.rb, line 244
def parse_json( json_string ) 

  @logger.debug( "#{__method__} json_string '#{json_string}'" )

  ec2_instances = JSON.parse( json_string  )

  return ec2_instances

end
read_ssh_config_file_content_minus_magic( ssh_config_file ) click to toggle source

read ssh_config from file/$stdin, remove old magic

# File lib/cli/aws-ssh-resolver-cli.rb, line 316
def read_ssh_config_file_content_minus_magic( ssh_config_file )

  ssh_config_file_content = File.exist?( ssh_config_file ) ? File.readlines( ssh_config_file ) : []

  # remove old magic
  within_magic = false
  ssh_config_file_content = ssh_config_file_content.select do |line| 
    ret = case within_magic 
          when true
            if line.chomp == MAGIC_END then
              within_magic = false
            end
            false
          when false
            if line.chomp == MAGIC_START  then
              within_magic = true
            end
            (line.chomp == MAGIC_START ? false : true)
          end
    ret
  end

  return ssh_config_file_content

end
reset( ) click to toggle source
# File lib/cli/aws-ssh-resolver-cli.rb, line 192
def reset(  )

  ssh_config_file = options[:ssh_config_file]
  # Read content of (without magic content) ssh_config_file into memory
  ssh_config_file_content = read_ssh_config_file_content_minus_magic( ssh_config_file )
  if ssh_config_file_content.empty? 
    File.delete( ssh_config_file )
  else
    File.open( ssh_config_file, 'w') do |f2|
      ssh_config_file_content.each do |line|
        f2.puts line
      end
    end

  end


end
resolve( json_file="-" ) click to toggle source
# File lib/cli/aws-ssh-resolver-cli.rb, line 112
def resolve( json_file="-" )

  @logger.info( "#{__method__} starting, options '#{options}'" )

  host_tag        = options[:host_tag]
  ssh_config_init = options[:ssh_config_init]
  ssh_config_file = options[:ssh_config_file]
  # puts( "options=#{options}" )

  # raw data from aws
  ec2_instances = get_ec2_instances( json_file )
  
  # hash with host => hostname
  host_hostname_mappings = create_host_hostname_mappings( ec2_instances, host_tag )

  # seed  'ssh_config_file' with 'ssh_config_init'
  init_ssh_config_file( ssh_config_file, ssh_config_init )

  # output to file
  output_to_file(  ssh_config_file, host_hostname_mappings )

  
end