Class: BoxGrinder::EC2Helper

Inherits:
AWSHelper show all
Defined in:
lib/boxgrinder-build/helpers/ec2-helper.rb

Constant Summary collapse

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'}
    }
  },

  'ap-southeast-1' => {
    :endpoint => 'ec2.ap-southeast-1.amazonaws.com',
    :location => 'ap-southeast-1',
    :kernel => {
      :i386 => {:aki => 'aki-13d5aa41'},
      :x86_64 => {:aki => 'aki-11d5aa43'}
    }
  },

  'ap-northeast-1' => {
    :endpoint => 'ec2.ap-northeast-1.amazonaws.com',
    :location => 'ap-northeast-1',
    :kernel => {
      :i386 => {:aki => 'aki-d209a2d3'},
      :x86_64 => {:aki => 'aki-d409a2d5'}
    }
  },

  'us-west-1' => {
    :endpoint => 'ec2.us-west-1.amazonaws.com',
    :location => 'us-west-1',
    :kernel => {
      :i386 => {:aki => 'aki-99a0f1dc'},
      :x86_64 => {:aki => 'aki-9ba0f1de'}
    }
  },

  'us-west-2' => {
    :endpoint => 'ec2.us-west-2.amazonaws.com',
    :location => 'us-west-2',
    :kernel => {
      :i386 => {:aki => 'aki-dce26fec'},
      :x86_64 => {:aki => 'aki-98e26fa8'}
    }
  },

  'us-east-1' => {
    :endpoint => 'ec2.amazonaws.com',
    :location => nil,
    :kernel => {
      :i386 => {:aki => 'aki-407d9529'},
      :x86_64 => {:aki => 'aki-427d952b'}
    }
  },

  'sa-east-1' => {
    :endpoint => 'ec2.sa-east-1.amazonaws.com',
    :location => 'sa-east-1',
    :kernel => {
      :i386 => {:aki => 'aki-863ce39b'},
      :x86_64 => {:aki => 'aki-d63ce3cb'}
  }
 }
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AWSHelper

block_device_mappings_validator, #parse_opts, #wait_with_timeout

Constructor Details

#initialize(ec2, opts = {}) ⇒ EC2Helper

Returns a new instance of EC2Helper.

Raises:



30
31
32
33
34
# File 'lib/boxgrinder-build/helpers/ec2-helper.rb', line 30

def initialize(ec2, opts={})
  raise ArgumentError, "ec2 argument must not be nil" if ec2.nil?
  @ec2 = ec2
  @log = opts[:log] || LogHelper.new
end

Class Method Details

.availability_zone_to_region(availability_zone) ⇒ Object



104
105
106
# File 'lib/boxgrinder-build/helpers/ec2-helper.rb', line 104

def self.availability_zone_to_region(availability_zone)
  availability_zone.scan(/((\w+)-(\w+)-(\d+))/).flatten.first
end

.current_availability_zoneObject



96
97
98
# File 'lib/boxgrinder-build/helpers/ec2-helper.rb', line 96

def self.current_availability_zone
  ('/latest/meta-data/placement/availability-zone/')
end

.current_instance_idObject



100
101
102
# File 'lib/boxgrinder-build/helpers/ec2-helper.rb', line 100

def self.current_instance_id
  ('/latest/meta-data/instance-id')
end

.endpointsObject

EC2 Endpoints



131
132
133
# File 'lib/boxgrinder-build/helpers/ec2-helper.rb', line 131

def self.endpoints
  SERVICES
end

.get_meta_data(path) ⇒ Object

EC2 meta-data queries



87
88
89
90
91
92
93
94
# File 'lib/boxgrinder-build/helpers/ec2-helper.rb', line 87

def self.(path)
  timeout(HTTP_TIMEOUT) do
    req = Net::HTTP::Get.new(path)
    res = Net::HTTP.start('169.254.169.254', 80) {|http| http.request(req)}
    return res.body if  Net::HTTPSuccess
    res.error!
  end
end

Instance Method Details

#ami_by_name(name, account_number) ⇒ Object

EC2 queries



110
111
112
113
114
115
# File 'lib/boxgrinder-build/helpers/ec2-helper.rb', line 110

def ami_by_name(name, )
  q = @ec2.images.with_owner().
      filter("name", name)
  return nil unless q.any?
  q.first
end

#live_instances(ami) ⇒ Object



123
124
125
126
127
# File 'lib/boxgrinder-build/helpers/ec2-helper.rb', line 123

def live_instances(ami)
  q = @ec2.instances.filter('image-id', ami.id)
  return q.select{|a| a.status != :terminated} if q.any?
  nil
end

#snapshot_by_id(snapshot_id) ⇒ Object



117
118
119
120
121
# File 'lib/boxgrinder-build/helpers/ec2-helper.rb', line 117

def snapshot_by_id(snapshot_id)
 q = @ec2.snapshots.filter('snapshot-id', snapshot_id)
 return nil unless q.any?
 q.first
end

#wait_for_image_death(ami, opts = {}) ⇒ Object



45
46
47
48
49
# File 'lib/boxgrinder-build/helpers/ec2-helper.rb', line 45

def wait_for_image_death(ami, opts={})
  opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT})
  wait_with_timeout(opts[:frequency], opts[:timeout]){ !ami.exists? }
rescue AWS::EC2::Errors::InvalidImageID::NotFound
end

#wait_for_image_state(state, ami, opts = {}) ⇒ Object

Wait cycles



38
39
40
41
42
43
# File 'lib/boxgrinder-build/helpers/ec2-helper.rb', line 38

def wait_for_image_state(state, ami, opts={})
  #First wait for the AMI to be confirmed to exist (after creating, an immediate query can cause an error)
  opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT})
  wait_with_timeout(opts[:frequency], opts[:timeout]){ ami.exists? }
  wait_with_timeout(opts[:frequency], opts[:timeout]){ ami.state == state }
end

#wait_for_instance_death(instance, opts = {}) ⇒ Object

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



57
58
59
60
# File 'lib/boxgrinder-build/helpers/ec2-helper.rb', line 57

def wait_for_instance_death(instance, opts={})
  wait_for_instance_status(:terminated, instance, opts) if instance.exists?
rescue AWS::EC2::Errors::InvalidInstanceID::NotFound
end

#wait_for_instance_status(status, instance, opts = {}) ⇒ Object



51
52
53
54
# File 'lib/boxgrinder-build/helpers/ec2-helper.rb', line 51

def wait_for_instance_status(status, instance, opts={})
  opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT})
  wait_with_timeout(opts[:frequency], opts[:timeout]){ instance.status == status }
end

#wait_for_snapshot_status(status, snapshot, opts = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/boxgrinder-build/helpers/ec2-helper.rb', line 62

def wait_for_snapshot_status(status, snapshot, opts={})
  opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT})
  progress = -1
  wait_with_timeout(opts[:frequency], opts[:timeout]) do
    current_progress = snapshot.progress || 0
      unless progress == current_progress
        @log.info "Progress: #{current_progress}%"
        progress = current_progress
      end
    snapshot.status == status
  end
rescue Exception
  @log.debug "Polling of snapshot #{snapshot.id} for status '#{status}' failed" unless snapshot.nil?
  raise
end

#wait_for_volume_status(status, volume, opts = {}) ⇒ Object



78
79
80
81
82
83
# File 'lib/boxgrinder-build/helpers/ec2-helper.rb', line 78

def wait_for_volume_status(status, volume, opts={})
  opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT})
  wait_with_timeout(opts[:frequency], opts[:timeout]) do
    volume.status == status
  end
end