Class: Automan::Ec2::Instance

Inherits:
Base
  • Object
show all
Defined in:
lib/automan/ec2/instance.rb

Constant Summary

Constants included from Mixins::AwsCaller

Mixins::AwsCaller::S3_PROTO

Instance Attribute Summary

Attributes inherited from Base

#logger, #wait

Attributes included from Mixins::AwsCaller

#as, #cfn, #eb, #ec, #ec2, #elb, #r53, #rds, #s3

Instance Method Summary collapse

Methods inherited from Base

add_option, #initialize, #log_options, #wait_until

Methods included from Mixins::AwsCaller

#account, #configure_aws, #looks_like_s3_path?, #parse_s3_path, #s3_object_exists?, #s3_read

Constructor Details

This class inherits a constructor from Automan::Base

Instance Method Details

#decrypt_password(encrypted, private_key) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/automan/ec2/instance.rb', line 18

def decrypt_password(encrypted, private_key)
  pk = OpenSSL::PKey::RSA.new(private_key)
  begin
    decoded = Base64.decode64(encrypted)
    password = pk.private_decrypt(decoded)
  rescue OpenSSL::PKey::RSAError => e
    logger.warn "Decrypt failed: #{e.message}"
    return nil
  end
  password
end

#password_data(instance_id) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/automan/ec2/instance.rb', line 7

def password_data(instance_id)
  opts = { instance_id: instance_id }
  response = ec2.client.get_password_data opts

  unless response.successful?
    raise RequestFailedError, "get_password_data failed: #{response.error}"
  end

  response.data[:password_data]
end

#show_envObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/automan/ec2/instance.rb', line 54

def show_env
  data = []
  ec2.instances.with_tag("Name", "*-#{environment}").each do |i|
    next unless i.status == :running

    tokens = []
    tokens << i.tags.Name
    tokens << i.private_ip_address
    tokens << windows_name(i.private_ip_address)
    if i.platform == "windows"
      tokens << windows_password(i.id, File.read(private_key_file))
    else
      tokens << ""
    end

    data << tokens
  end

  return if data.empty?

  # find longest name tag
  max_name_size = data.max_by {|i| i[0].size }.first.size
  data.map {|i| i[0] = i[0].ljust(max_name_size) }

  data.each do |i|
    puts i.join("\t")
  end
end

#windows_name(ip_address) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/automan/ec2/instance.rb', line 45

def windows_name(ip_address)
  return nil if ip_address.nil?
  return nil if ip_address.empty?

  quads = ip_address.split('.')
  quad_str = quads.map { |q| q.to_i.to_s(16).rjust(2,'0') }.join('')
  "ip-#{quad_str}"
end

#windows_password(instance_id, private_key) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/automan/ec2/instance.rb', line 30

def windows_password(instance_id, private_key)
  begin
    encrypted = password_data(instance_id)
  rescue RequestFailedError => e
    logger.warn e.message
    return nil
  end

  if encrypted.nil?
    return nil
  end

  decrypt_password(encrypted, private_key)
end