lib/rodeo_clown/ec2.rb in rodeo_clown-0.0.1 vs lib/rodeo_clown/ec2.rb in rodeo_clown-0.1.0

- old
+ new

@@ -1,38 +1,71 @@ require "forwardable" module RodeoClown class EC2 < Struct.new(:ec2) + include InstanceBuilder + + STATUS = %w[pending running shutting-down terminated stopping stopped] + def self.instances ec2.instances end def self.ec2 @ec2 ||= AWS::EC2.new end + def self.create_instance(options) + new_instance = instances.create(options) + instances.wait_for_status(:running, 2, [*new_instance]) + new_instance + end + + def create_instance(options) + self.class.create_instance(options) + end + def self.by_name(name) new instances[name] end + # Filter by had of tag values. + # Keys and values as strings + # + # Examples + # RodeoClown::EC2.by_tags("app" => "rodeo", "version" = "2.1") + # # => [ instance-1, instanc-2 ] # - # options: keys: [ foo... , scope: :all], [:values scope: any] - # - def self.filter_instances(options = {}) - filtered = filter_by_tag_options(instances, options[:keys], :tagged) + # Returns an array of instances + def self.by_tags(options = {}) + return instances if options.nil? || options.empty? - filter_by_tag_options(filtered, options[:values], :tagged_values) + instances.tagged_values(options.values).select do |instance| + tags = instance.tags.to_h + (options.to_a - tags.to_a).empty? + end end - def self.filter_by_tag_options(instances, options, filter = :tagged_values) - return instances if options.nil? || options.empty? + def reboot + ec2.reboot + end - all = options.delete(:scope) == :all - vals = options.delete(:values) + def pending? + ec2.status == :pending + end - if all - vals.inject(instances) { |i, value| i.send(filter, value) } - else - instances.send filter, *vals - end + def running? + ec2.status == :running + end + + def stopped? + ec2.status == :stopped + end + + def terminated? + ec2.status == :terminated + end + + def dns_name + ec2.dns_name end end end