Sha256: 4f39e2a9ea9ba9b86bcb647dd1f7568990c9e6b2bfe67af64b61919e8016f7ba

Contents?: true

Size: 1.5 KB

Versions: 12

Compression:

Stored size: 1.5 KB

Contents

module Awsborn
  class ServerCluster
    def self.build (klass, &block)
      cluster = new(klass)
      block.bind(cluster, 'cluster').call
      cluster
    end

    def initialize (klass)
      @klass = klass
      @instances = []
    end
    
    def domain (*args)
      @domain = args.first unless args.empty?
      @domain
    end
    
    def server (name, options = {})
      options = add_domain_to_ip(options)
      instance = @klass.new name, options
      @instances << instance
    end

    def launch
      running, missing = @instances.partition { |e| e.running? }
      refresh_running(running) if running.any?
      start_missing_instances(missing) if missing.any?
    end

    def refresh_running (instances)
      instances.each { |e| e.refresh }
    end

    def start_missing_instances (instances)
      generate_key_pair(instances)
      instances.each { |e| e.start(@key_pair) }
      delete_key_pair(instances)
    end

    def generate_key_pair (instances)
      @key_pair = instances.first.ec2.generate_key_pair
    end

    def delete_key_pair (instances)
      instances.first.ec2.delete_key_pair(@key_pair)
    end
    
    def each (&block)
      @instances.each &block
    end

    def [] (name)
      @instances.detect { |i| i.name == name }
    end
    
    protected
    
    def add_domain_to_ip (hash)
      if @domain && hash.has_key?(:ip) && ! hash[:ip].include?('.')
        ip = [hash[:ip], @domain].join('.')
        hash.merge(:ip => ip)
      else
        hash
      end
    end
    
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
awsborn-0.3.9 lib/awsborn/server_cluster.rb
awsborn-0.3.8 lib/awsborn/server_cluster.rb
awsborn-0.3.7 lib/awsborn/server_cluster.rb
awsborn-0.3.6 lib/awsborn/server_cluster.rb
awsborn-0.3.5 lib/awsborn/server_cluster.rb
awsborn-0.3.4 lib/awsborn/server_cluster.rb
awsborn-0.3.3 lib/awsborn/server_cluster.rb
awsborn-0.3.2 lib/awsborn/server_cluster.rb
awsborn-0.3.1 lib/awsborn/server_cluster.rb
awsborn-0.3.0 lib/awsborn/server_cluster.rb
awsborn-0.2.1 lib/awsborn/server_cluster.rb
awsborn-0.2.0 lib/awsborn/server_cluster.rb