Class: Cli

Inherits:
Thor
  • Object
show all
Includes:
Utils::MyLogger
Defined in:
lib/cli/aws-ssh-resolver-cli.rb

Constant Summary collapse

PROGNAME =

logger progname

"main"
DEFAULT_SSH_CONFIG_FILE =

constanst

"ssh/config.aws"
DEFAULT_SSH_CONFIG_INIT =
"ssh/config.init"
MAGIC_START =
"# +++ aws-ssh-resolver-cli update start here +++"
MAGIC_END =
"# +++ aws-ssh-resolver-cli update end here +++"
DEFAULT_DESCRIBE_INSTANCES =
"aws ec2 describe-instances --filters 'Name=instance-state-name,Values=running' 'Name=tag-key,Values=Name'"
DEFAULT_HOST_TAG =
"Name"

Constants included from Utils::MyLogger

Utils::MyLogger::LOGFILE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::MyLogger

#getLogger

Constructor Details

#initialize(*args) ⇒ Cli


constructore



22
23
24
25
# File 'lib/cli/aws-ssh-resolver-cli.rb', line 22

def initialize(*args)
  super
  @logger = getLogger( PROGNAME, options )
end

Class Method Details

.add_shared_option(name, options = {}) ⇒ Object



43
44
45
46
# 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

.shared_options(*option_names) ⇒ Object



48
49
50
51
52
53
54
# 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

Instance Method Details

#__print_versionObject



34
35
36
# File 'lib/cli/aws-ssh-resolver-cli.rb', line 34

def __print_version
  puts File.readlines( File.join File.dirname(__FILE__), "../../VERSION" ).join( " " )
end

#awsObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# 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

#resetObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# 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 = "-") ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 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