Class BoxGrinder::EC2Helper
In: lib/boxgrinder-build/helpers/ec2-helper.rb
lib/boxgrinder-build/helpers/ec2-helper.rb
Parent: AWSHelper

Methods

Constants

DEF_POLL_FREQ = 1
DEF_TIMEOUT = 1000
HTTP_TIMEOUT = 10
SERVICES = { 'eu-west-1' => { :endpoint => 'ec2.eu-west-1.amazonaws.com', :location => 'EU', :kernel => { :i386 => {:aki => 'aki-4deec439'}, :x86_64 => {:aki => 'aki-4feec43b'}
DEF_POLL_FREQ = 1
DEF_TIMEOUT = 1000
HTTP_TIMEOUT = 10
SERVICES = { 'eu-west-1' => { :endpoint => 'ec2.eu-west-1.amazonaws.com', :location => 'EU', :kernel => { :i386 => {:aki => 'aki-4deec439'}, :x86_64 => {:aki => 'aki-4feec43b'}

Public Class methods

[Source]

     # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 104
104:     def self.availability_zone_to_region(availability_zone)
105:       availability_zone.scan(/((\w+)-(\w+)-(\d+))/).flatten.first
106:     end

[Source]

     # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 104
104:     def self.availability_zone_to_region(availability_zone)
105:       availability_zone.scan(/((\w+)-(\w+)-(\d+))/).flatten.first
106:     end

[Source]

    # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 96
96:     def self.current_availability_zone
97:       get_meta_data('/latest/meta-data/placement/availability-zone/')
98:     end

[Source]

    # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 96
96:     def self.current_availability_zone
97:       get_meta_data('/latest/meta-data/placement/availability-zone/')
98:     end

[Source]

     # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 100
100:     def self.current_instance_id
101:       get_meta_data('/latest/meta-data/instance-id')
102:     end

[Source]

     # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 100
100:     def self.current_instance_id
101:       get_meta_data('/latest/meta-data/instance-id')
102:     end

EC2 Endpoints

[Source]

     # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 131
131:     def self.endpoints
132:       SERVICES
133:     end

EC2 Endpoints

[Source]

     # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 131
131:     def self.endpoints
132:       SERVICES
133:     end

EC2 meta-data queries

[Source]

    # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 87
87:     def self.get_meta_data(path)
88:       timeout(HTTP_TIMEOUT) do
89:         req = Net::HTTP::Get.new(path)
90:         res = Net::HTTP.start('169.254.169.254', 80) {|http| http.request(req)}
91:         return res.body if  Net::HTTPSuccess
92:         res.error!
93:       end
94:     end

EC2 meta-data queries

[Source]

    # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 87
87:     def self.get_meta_data(path)
88:       timeout(HTTP_TIMEOUT) do
89:         req = Net::HTTP::Get.new(path)
90:         res = Net::HTTP.start('169.254.169.254', 80) {|http| http.request(req)}
91:         return res.body if  Net::HTTPSuccess
92:         res.error!
93:       end
94:     end

[Source]

    # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 30
30:     def initialize(ec2, opts={})
31:       raise ArgumentError, "ec2 argument must not be nil" if ec2.nil?
32:       @ec2 = ec2
33:       @log = opts[:log] || LogHelper.new
34:     end

[Source]

    # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 30
30:     def initialize(ec2, opts={})
31:       raise ArgumentError, "ec2 argument must not be nil" if ec2.nil?
32:       @ec2 = ec2
33:       @log = opts[:log] || LogHelper.new
34:     end

Public Instance methods

EC2 queries

[Source]

     # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 110
110:     def ami_by_name(name, account_number)
111:       q = @ec2.images.with_owner(account_number).
112:           filter("name", name)
113:       return nil unless q.any?
114:       q.first
115:     end

EC2 queries

[Source]

     # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 110
110:     def ami_by_name(name, account_number)
111:       q = @ec2.images.with_owner(account_number).
112:           filter("name", name)
113:       return nil unless q.any?
114:       q.first
115:     end

[Source]

     # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 123
123:     def live_instances(ami)
124:       q = @ec2.instances.filter('image-id', ami.id)
125:       return q.select{|a| a.status != :terminated} if q.any?
126:       nil
127:     end

[Source]

     # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 123
123:     def live_instances(ami)
124:       q = @ec2.instances.filter('image-id', ami.id)
125:       return q.select{|a| a.status != :terminated} if q.any?
126:       nil
127:     end

[Source]

     # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 117
117:     def snapshot_by_id(snapshot_id)
118:      q = @ec2.snapshots.filter('snapshot-id', snapshot_id)
119:      return nil unless q.any?
120:      q.first
121:     end

[Source]

     # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 117
117:     def snapshot_by_id(snapshot_id)
118:      q = @ec2.snapshots.filter('snapshot-id', snapshot_id)
119:      return nil unless q.any?
120:      q.first
121:     end

[Source]

    # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 45
45:     def wait_for_image_death(ami, opts={})
46:       opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT})
47:       wait_with_timeout(opts[:frequency], opts[:timeout]){ !ami.exists? }
48:     rescue AWS::EC2::Errors::InvalidImageID::NotFound
49:     end

[Source]

    # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 45
45:     def wait_for_image_death(ami, opts={})
46:       opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT})
47:       wait_with_timeout(opts[:frequency], opts[:timeout]){ !ami.exists? }
48:     rescue AWS::EC2::Errors::InvalidImageID::NotFound
49:     end

Wait cycles

[Source]

    # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 38
38:     def wait_for_image_state(state, ami, opts={})
39:       #First wait for the AMI to be confirmed to exist (after creating, an immediate query can cause an error)
40:       opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT})
41:       wait_with_timeout(opts[:frequency], opts[:timeout]){ ami.exists? }
42:       wait_with_timeout(opts[:frequency], opts[:timeout]){ ami.state == state }
43:     end

Wait cycles

[Source]

    # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 38
38:     def wait_for_image_state(state, ami, opts={})
39:       #First wait for the AMI to be confirmed to exist (after creating, an immediate query can cause an error)
40:       opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT})
41:       wait_with_timeout(opts[:frequency], opts[:timeout]){ ami.exists? }
42:       wait_with_timeout(opts[:frequency], opts[:timeout]){ ami.state == state }
43:     end

Being serial shouldn‘t be much slower as we are blocked by the slowest stopper anyway

[Source]

    # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 57
57:     def wait_for_instance_death(instance, opts={})
58:       wait_for_instance_status(:terminated, instance, opts) if instance.exists?
59:     rescue AWS::EC2::Errors::InvalidInstanceID::NotFound
60:     end

Being serial shouldn‘t be much slower as we are blocked by the slowest stopper anyway

[Source]

    # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 57
57:     def wait_for_instance_death(instance, opts={})
58:       wait_for_instance_status(:terminated, instance, opts) if instance.exists?
59:     rescue AWS::EC2::Errors::InvalidInstanceID::NotFound
60:     end

[Source]

    # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 51
51:     def wait_for_instance_status(status, instance, opts={})
52:       opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT})
53:       wait_with_timeout(opts[:frequency], opts[:timeout]){ instance.status == status }
54:     end

[Source]

    # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 51
51:     def wait_for_instance_status(status, instance, opts={})
52:       opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT})
53:       wait_with_timeout(opts[:frequency], opts[:timeout]){ instance.status == status }
54:     end

[Source]

    # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 62
62:     def wait_for_snapshot_status(status, snapshot, opts={})
63:       opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT})
64:       progress = -1
65:       wait_with_timeout(opts[:frequency], opts[:timeout]) do
66:         current_progress = snapshot.progress || 0
67:           unless progress == current_progress
68:             @log.info "Progress: #{current_progress}%"
69:             progress = current_progress
70:           end
71:         snapshot.status == status
72:       end
73:     rescue Exception
74:       @log.debug "Polling of snapshot #{snapshot.id} for status '#{status}' failed" unless snapshot.nil?
75:       raise
76:     end

[Source]

    # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 62
62:     def wait_for_snapshot_status(status, snapshot, opts={})
63:       opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT})
64:       progress = -1
65:       wait_with_timeout(opts[:frequency], opts[:timeout]) do
66:         current_progress = snapshot.progress || 0
67:           unless progress == current_progress
68:             @log.info "Progress: #{current_progress}%"
69:             progress = current_progress
70:           end
71:         snapshot.status == status
72:       end
73:     rescue Exception
74:       @log.debug "Polling of snapshot #{snapshot.id} for status '#{status}' failed" unless snapshot.nil?
75:       raise
76:     end

[Source]

    # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 78
78:     def wait_for_volume_status(status, volume, opts={})
79:       opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT})
80:       wait_with_timeout(opts[:frequency], opts[:timeout]) do
81:         volume.status == status
82:       end
83:     end

[Source]

    # File lib/boxgrinder-build/helpers/ec2-helper.rb, line 78
78:     def wait_for_volume_status(status, volume, opts={})
79:       opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT})
80:       wait_with_timeout(opts[:frequency], opts[:timeout]) do
81:         volume.status == status
82:       end
83:     end

[Validate]