Sha256: 0115ea1b61cea78c7203967971d0badc4d49d9e1c0b8aadb4182c49a3e52dd37

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

require 'AWS/EC2'

# SafeAWS wraps the AWS module in exception catcher blocks, so that any
# exceptions that are thrown do not affect the caller.
#
# The SafeEC2, SafeSQS, SafeSimpleDB and SafeS3 log any errors that they encounter, so
# that they can be examined later.
module SafeAWS
  # Wrap EC2 functions that we use.
  # Catch errors and do something reasonable.
  class EC2
    def initialize(access_key, secret_key)
      @ec2 = AWS::EC2.new(access_key, secret_key)
      @@log = STDOUT
    end

    def logger=(logger)
      @@log = logger
    end

    private

    def report_error(res)
      @@log.puts "error #{$!}"
      $@.each {|line| @@log.puts "  #{line}"}
      res
    end

    public

    def describe_images(options={})
      begin
        @ec2.describe_images(options)
      rescue
        report_error []
      end
    end
    
    def describe_instances(instance_ids=[])
      begin
        @ec2.describe_instances(instance_ids)
      rescue
        report_error []
      end
    end

    def run_instances(image_id, min_count=1, max_count=min_count, options={})
      begin
        @ec2.run_instances(image_id, min_count, max_count, options)
      rescue
        report_error []
      end
    end

    def terminate_instances(instance_ids = [])
      begin
        @ec2.terminate_instances(instance_ids)
      rescue
        report_error []
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
staugaard-cloudmaster-0.1.1 lib/SafeAWS/EC2.rb
staugaard-cloudmaster-0.1.2 lib/SafeAWS/EC2.rb
staugaard-cloudmaster-0.1.3 lib/SafeAWS/EC2.rb
staugaard-cloudmaster-0.1.4 lib/SafeAWS/EC2.rb
staugaard-cloudmaster-0.1.5 lib/SafeAWS/EC2.rb