Class: Connection

Inherits:
Object show all
Defined in:
lib/maws/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Connection

Returns a new instance of Connection.



8
9
10
11
12
13
14
15
# File 'lib/maws/connection.rb', line 8

def initialize(config)
  @config = config

  @access_key_id = @config.aws_key.key_id
  @secret_key = @config.aws_key.secret_key

  @params = {:region => @config.region, :logger => $right_aws_logger}
end

Instance Attribute Details

#ec2Object (readonly)

Returns the value of attribute ec2.



6
7
8
# File 'lib/maws/connection.rb', line 6

def ec2
  @ec2
end

#elbObject (readonly)

Returns the value of attribute elb.



6
7
8
# File 'lib/maws/connection.rb', line 6

def elb
  @elb
end

#rdsObject (readonly)

Returns the value of attribute rds.



6
7
8
# File 'lib/maws/connection.rb', line 6

def rds
  @rds
end

Instance Method Details

#available_zonesObject



35
36
37
38
39
# File 'lib/maws/connection.rb', line 35

def available_zones
  ec2.describe_availability_zones.
      find_all{ |zone_description| zone_description[:zone_state] == "available"}.
      map { |zone_description| zone_description[:zone_name][/\w$/]}
end

#connect(services) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/maws/connection.rb', line 17

def connect(services)
  # The right_aws gem parses the EC2_URL environment variable if it is set. The EC2 CLI tools also use that variable
  # but expect the hostname to be region-specific (e.g., us-east-1.ec2.amazonaws.com) instead of generic
  # (e.g., ec2.amazonaws.com). To avoid conflicts, unset the variable here and use the right_aws default value.
  ENV["EC2_URL"] = nil

  # always connect to ec2
  @ec2 = RightAws::Ec2.new(@access_key_id, @secret_key, @params.dup)

  if services.include?(:rds)
    @rds = RightAws::RdsInterface.new(@access_key_id, @secret_key, @params.dup)
  end

  if services.include?(:elb)
    @elb = RightAws::ElbInterface.new(@access_key_id, @secret_key, @params.dup)
  end
end

#descriptions(services = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/maws/connection.rb', line 53

def descriptions(services = nil)
  services ||= :all
  descriptions = []

  descriptions += ec2_descriptions if services == :all or services.include?(:ec2)
  descriptions += rds_descriptions if services == :all or services.include?(:rds)
  descriptions += elb_descriptions if services == :all or services.include?(:elb)

  descriptions
end

#ebs_descriptionsObject



107
108
109
110
111
112
113
114
# File 'lib/maws/connection.rb', line 107

def ebs_descriptions
  descriptions = ec2.describe_volumes.map { |description|
    description[:service] = :ebs
    Description.create(description)
  }

  filter_current_profile_prefix(descriptions)
end

#ec2_descriptionsObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/maws/connection.rb', line 64

def ec2_descriptions
  # convert aws description to Description
  descriptions =  descriptions = ec2.describe_instances.map {|description|
    description[:service] = :ec2
    Description.create(description)
  }

  # filter out terminated when same name exists as a living one and terminated
  by_name = descriptions.group_by { |d| d.name }

  # if there is one than more for the same name: delete terminated descriptions, take the last one (trust AWS sorting)
  by_name.map {|name, descriptions|
    if descriptions.count > 1
      descriptions.delete_if {|d| d.status == "terminated"}
      descriptions.replace([descriptions.last]).compact!
    end
  }

  filter_current_profile_prefix(by_name.values.flatten)
end

#elb_descriptionsObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/maws/connection.rb', line 96

def elb_descriptions
  return [] unless elb

  descriptions = elb.describe_load_balancers.map { |description|
    description[:service] = :elb
    Description.create(description)
  }

  filter_current_profile_prefix(descriptions)
end

#image_id_for_image_name(image_name) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/maws/connection.rb', line 41

def image_id_for_image_name(image_name)
  return if image_name.nil? || image_name.empty?
  images = @ec2.describe_images(:filters => { 'tag:Name' => image_name})
  if images.empty?
    error "No AMI with name '#{image_name}'"
  elsif images.count > 1
    error "Ambigous AMI name: '#{image_name}'. Several AMIs match it #{images.collect{|i| i[:aws_id]}.join(', ')}"
  else
    images.first[:aws_id]
  end
end

#rds_descriptionsObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/maws/connection.rb', line 85

def rds_descriptions
  return [] unless rds

  descriptions = rds.describe_db_instances.map { |description|
    description[:service] = :rds
    Description.create(description)
  }

  filter_current_profile_prefix(descriptions)
end