Sha256: 3c8a8a0f3eab7ca17d8f697f160356ba7ddb6b60cac9958b2c1e511e75aaa428

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 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
      start_missing_instances
    end

    def start_missing_instances
      to_start = find_missing_instances
      return if to_start.empty?
      generate_key_pair(to_start)
      to_start.each { |e| e.start(@key_pair) }
      delete_key_pair(to_start)
    end

    def find_missing_instances
      @instances.reject { |e| e.running? }
    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

2 entries across 2 versions & 1 rubygems

Version Path
awsborn-0.1.1 lib/awsborn/server_cluster.rb
awsborn-0.1.0 lib/awsborn/server_cluster.rb