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=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



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

def add_shared_option(name, options = {})
  @shared_options = {} if @shared_options.nil?
  @shared_options[name] =  options
end

.shared_options(*option_names) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/cli/aws-ssh-resolver-cli.rb', line 37

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

#awsObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cli/aws-ssh-resolver-cli.rb', line 143

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



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/cli/aws-ssh-resolver-cli.rb', line 181

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/cli/aws-ssh-resolver-cli.rb', line 101

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