Class: AwsRsync::Host

Inherits:
Object
  • Object
show all
Includes:
AwsServices
Defined in:
lib/aws_rsync/host.rb

Instance Method Summary collapse

Methods included from AwsServices

#ec2

Constructor Details

#initialize(options = {}) ⇒ Host

Returns a new instance of Host.



5
6
7
8
# File 'lib/aws_rsync/host.rb', line 5

def initialize(options={})
  @options = options
  @instance_id = options[:instance_id]
end

Instance Method Details

#find_instanceObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/aws_rsync/host.rb', line 19

def find_instance
  instances = ec2.describe_instances(instance_ids: [@instance_id]).
    reservations.first["instances"] || []
  instance = instances.first
rescue Aws::EC2::Errors::ServiceError => e
  puts "ERROR: Could not find the instance #{@instance_id}".colorize(:red)
  puts e.message
  puts "For the full internal backtrace re-run the command with DEBUG=1" unless ENV['DEBUG']
  puts e.backtrace if ENV['DEBUG']
  exit 1
end

#instanceObject



10
11
12
13
14
15
16
17
# File 'lib/aws_rsync/host.rb', line 10

def instance
  instance = find_instance
  unless instance
    puts "ERROR: unable to find instance '#{@instance_id}' in this aws account and region: #{region}"
    exit 1
  end
  instance
end

#ipObject



35
36
37
38
39
# File 'lib/aws_rsync/host.rb', line 35

def ip
  @options[:private_ip] ?
    instance.private_ip_address :
    instance.public_ip_address
end

#regionObject



31
32
33
# File 'lib/aws_rsync/host.rb', line 31

def region
  `aws configure get region`.strip rescue 'us-east-1'
end