Class: Cloudutil::AWS::EC2

Inherits:
Core
  • Object
show all
Defined in:
lib/cloudutil/aws/ec2.rb

Overview

A class of utility methods for the AWS EC2 service

Constant Summary

Constants inherited from Core

Core::DEFAULT_REGION

Instance Attribute Summary

Attributes inherited from Core

#access_key, #region, #secret_key

Instance Method Summary collapse

Methods inherited from Core

#config, #initialize

Constructor Details

This class inherits a constructor from Cloudutil::AWS::Core

Instance Method Details

#aws_ami_id?(ami) ⇒ Boolean

Returns true if the supplied value looks like an AWS AMI id

Returns:

  • (Boolean)


116
117
118
119
# File 'lib/cloudutil/aws/ec2.rb', line 116

def aws_ami_id?(ami)
  return true if ami.match(/^ami-\h{8,}$/)
  false
end

#aws_security_group_id?(group) ⇒ Boolean

Returns true if the supplied value looks like an AWS security group id

Returns:

  • (Boolean)


75
76
77
78
# File 'lib/cloudutil/aws/ec2.rb', line 75

def aws_security_group_id?(group)
  return true if group.match(/^sg-\h{8,}$/)
  false
end

#aws_subnet_id?(subnet) ⇒ Boolean

Returns true if the supplied value looks like an AWS subnet id

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/cloudutil/aws/ec2.rb', line 38

def aws_subnet_id?(subnet)
  return true if subnet.match(/^subnet-\h{8,}$/)
  false
end

#lookup_ami(ami, ignore_case = true, nap = 2, retries = 5) ⇒ Object

Resolve the provided value to an array of AWS AMI objects. By default, the search is not case-sensitive, but that can be overridden by supplying ‘false’ as a second argument to this method.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cloudutil/aws/ec2.rb', line 91

def lookup_ami(ami, ignore_case = true, nap = 2, retries = 5)
  ec2 = ::AWS::EC2.new
  amis = []

  begin
    if aws_ami_id? ami
      amis << ec2.images[ami]
    else
      # Don't bother memoizing, this is fast
      imgs = ec2.images.filter('image-type', 'machine').filter('state', 'available')

      amis = do_ami_lookup_by_name_tag(imgs, ami, ignore_case)
      amis = do_private_ami_lookup_by_name(imgs, ami, ignore_case) if amis.nil? || amis.empty?
      amis = do_ami_lookup_by_name(imgs, ami, ignore_case) if amis.nil? || amis.empty?
    end
  rescue ::AWS::EC2::Errors::RequestLimitExceeded, ::AWS::Errors::ServerError
    raise if retries < 1
    sleep nap
    lookup_ami(ami, ignore_case, (nap * 2), (retries - 1))
  end

  amis
end

#lookup_security_group(group, ignore_case = true, nap = 2, retries = 5) ⇒ Object

Resolve the provided value to an array of AWS sec group objects. By default, the search is not case-sensitive, but that can be overridden by supplying ‘false’ as a second argument to this method.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cloudutil/aws/ec2.rb', line 54

def lookup_security_group(group, ignore_case = true, nap = 2, retries = 5)
  ec2 = ::AWS::EC2.new
  groups = []

  begin
    if aws_security_group_id? group
      groups << ec2.security_groups[group]
    else
      groups = do_security_group_lookup_by_name(ec2, group, ignore_case)
      groups = do_security_group_lookup_by_name_tag(ec2, group, ignore_case) if groups.nil? || groups.empty?
    end
  rescue ::AWS::EC2::Errors::RequestLimitExceeded, ::AWS::Errors::ServerError
    raise if retries < 1
    sleep nap
    lookup_security_group(group, ignore_case, (nap * 2), (retries - 1))
  end

  groups
end

#lookup_subnet(subnet, ignore_case = true, nap = 2, retries = 5) ⇒ Object

Resolve the provided value to an array of AWS subnet objects. By default, the search is not case-sensitive, but that can be overridden by supplying ‘false’ as a second argument to this method.



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

def lookup_subnet(subnet, ignore_case = true, nap = 2, retries = 5)
  ec2 = ::AWS::EC2.new
  subnets = []

  begin
    if aws_subnet_id? subnet
      subnets << ec2.subnets[subnet]
    else
      subnets = do_subnet_lookup(ec2.subnets.filter('state', 'available'), subnet, ignore_case)
    end
  rescue ::AWS::EC2::Errors::RequestLimitExceeded, ::AWS::Errors::ServerError
    raise if retries < 1
    sleep nap
    lookup_subnet(subnet, ignore_case, (nap * 2), (retries - 1))
  end

  subnets
end

#resolve_ami_id(ami, ignore_case = true) ⇒ Object

Resolve the provided value to an AWS AMI id. By default, the search is not case-sensitive, but that can be overridden by supplying ‘false’ as a second argument to this method. Will return the 1st value in a set, if multiple values returned.



83
84
85
86
87
# File 'lib/cloudutil/aws/ec2.rb', line 83

def resolve_ami_id(ami, ignore_case = true)
  obj = lookup_ami(ami, ignore_case).first
  return obj.image_id unless obj.nil?
  nil
end

#resolve_security_group_id(group, ignore_case = true) ⇒ Object

Resolve the provided value to an AWS sec group id. By default, the search is not case-sensitive, but that can be overridden by supplying ‘false’ as a second argument to this method. Will return the 1st value in a set, if multiple values returned.



46
47
48
49
50
# File 'lib/cloudutil/aws/ec2.rb', line 46

def resolve_security_group_id(group, ignore_case = true)
  obj = lookup_security_group(group, ignore_case).first
  return obj.security_group_id unless obj.nil?
  nil
end

#resolve_subnet_id(subnet, ignore_case = true) ⇒ Object

Resolve the provided value to an AWS subnet id. By default, the search is not case-sensitive, but that can be overridden by supplying ‘false’ as a second argument to this method. Will return the 1st value in a set, if multiple values returned.



10
11
12
13
14
# File 'lib/cloudutil/aws/ec2.rb', line 10

def resolve_subnet_id(subnet, ignore_case = true)
  obj = lookup_subnet(subnet, ignore_case).first
  return obj.subnet_id unless obj.nil?
  nil
end