Sha256: 3f8b44b63c6da3d1d5eb1123c96e959e99326e7d5eb9584dc80bee64a9e1cd23

Contents?: true

Size: 1.59 KB

Versions: 5

Compression:

Stored size: 1.59 KB

Contents

require 'thor'
require 'pathname'
require 'parallel'
require 'guignol'
require 'guignol/configuration'
require 'guignol/models/instance'
require 'core_ext/array/collect_key'

module Guignol::Commands
  class Base

    def initialize(patterns, options = {})
      @configs = select_configs(patterns)
      @options = options
    end

    # Run the block for each server in +configs+ (in parallel).
    def run
      before_run(@configs, @options) or return
      results = {}

      Parallel.each(@configs, parallel_options) do |name,config|
        instance = Guignol::Models::Instance.new(name, config)
        results[name] = run_on_server(instance, @options)
      end

      after_run(results, @options)
    end


    protected

    # Override in subclasses
    def before_run(configs, options) ; true ; end

    # Override in subclasses
    def after_run(data, options) ; true ; end


    def shell
      Guignol::Shell.shared_shell
    end


    def synchronize
      (@mutex ||= Mutex.new).synchronize do
        yield
      end
    end

    private

    def parallel_options
      if RUBY_VERSION >= '1.9.3'
        # 1.9.3 has bugs with Excon / SSL connections
        { :in_threads => 0 }
      else
        { :in_threads => @configs.size }
      end
    end

    # Put all the servers matching one of the +names+ in +configs+.
    def select_configs(patterns)
      patterns = patterns.map { |pattern| 
        pattern.kind_of?(String) ? Regexp.new(pattern) : pattern
      }
      Guignol.configuration.delete_if { |name,config| 
        patterns.none? { |pattern| name.to_s =~ pattern }
      }
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
guignol-0.3.7 lib/guignol/commands/base.rb
guignol-0.3.6.2 lib/guignol/commands/base.rb
guignol-0.3.6.1 lib/guignol/commands/base.rb
guignol-0.3.6 lib/guignol/commands/base.rb
guignol-0.3.5.1 lib/guignol/commands/base.rb