Class: Ec2::Tracer::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2/tracer.rb

Constant Summary collapse

WRITE_MODE =
'a'.freeze
TEMP_IP =
'0.0.0.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name = 'ec2.d', host_prefix = 'welligence_ml', range = (2..10), user = 'ubuntu', identity_file = '~/.ssh/welligence.pem') ⇒ Writer

Returns a new instance of Writer.



12
13
14
15
16
17
18
# File 'lib/ec2/tracer.rb', line 12

def initialize(file_name = 'ec2.d', host_prefix = 'welligence_ml', range = (2..10), user = 'ubuntu', identity_file = '~/.ssh/welligence.pem' )
  @file_name = file_name
  @host_prefix = host_prefix
  @range = range
  @user = user
  @identity_file = identity_file
end

Instance Attribute Details

#file_nameObject (readonly)

Returns the value of attribute file_name.



10
11
12
# File 'lib/ec2/tracer.rb', line 10

def file_name
  @file_name
end

#host_prefixObject (readonly)

Returns the value of attribute host_prefix.



10
11
12
# File 'lib/ec2/tracer.rb', line 10

def host_prefix
  @host_prefix
end

#identity_fileObject (readonly)

Returns the value of attribute identity_file.



10
11
12
# File 'lib/ec2/tracer.rb', line 10

def identity_file
  @identity_file
end

#rangeObject (readonly)

Returns the value of attribute range.



10
11
12
# File 'lib/ec2/tracer.rb', line 10

def range
  @range
end

#userObject (readonly)

Returns the value of attribute user.



10
11
12
# File 'lib/ec2/tracer.rb', line 10

def user
  @user
end

Instance Method Details

#write_configObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ec2/tracer.rb', line 20

def write_config
  ec2 = Aws::EC2::Resource.new(region: 'us-west-2')
  filepath = "#{Dir.pwd}/#{file_name}"

  FileUtils.rm_rf filepath
  FileUtils.touch filepath

  ec2.instances({filters: [{name: 'tag:Name', values: range.map{|i| "#{host_prefix}_#{i}"} }]}).each do |i|
    instance_tag_name = i.tags.map(&:value).join()
    public_ip_address = i.public_ip_address

    puts "#{instance_tag_name} - #{public_ip_address}"
    File.open(filepath, WRITE_MODE) { |f| f << ssh_host(host: instance_tag_name, host_name: public_ip_address) }
  end
end