class Etoro::Utility::MCollective::RPC

Attributes

current_host[R]
execute[R]
hosts[R]
post_execute[R]
pre_execute[R]

Public Class Methods

new(config={}) click to toggle source
# File lib/etoro/utility/mcollective/rpc.rb, line 15
def initialize (config={})

  default_config = {
      timeout: 600,
      log_file: 'deployment.log',
      log_level: Logger::INFO,
      sequential: false,
      wait_for_status: 30,
      wait_between_checks: 5
  }
  @config = default_config.merge(config)

  self.validate(config)

  @mc = rpcclient(self.class.name.downcase.split('::').last, :chomp => true)
  @mc.progress = false

  if @config.has_key?(:limit_targets)
    @mc.limit_targets = @config[:limit_targets]
    @mc.limit_method = :random
  end

  @mc.compound_filter @config[:compound_filter] if @config.has_key?(:compound_filter)

  log_descriptor = ""
  if (@config.has_key?(:fact_filter) && @config[:fact_filter].has_key?(:service) &&  @config[:fact_filter].has_key?(:application)) || (@config.has_key?(:application) && @config.has_key?(:service))
    if @config.has_key?(:fact_filter)
      service=@config[:fact_filter][:service]
      application=@config[:fact_filter][:application]
    else
      service=@config[:service]
      application=@config[:application]
    end
    log_descriptor="service=#{service}, application=#{application},"
  end

  self.logging(@config[:log_file],@config[:log_level],log_descriptor)
  @logger.info("Setting timeout to #{@config[:timeout]}")
  @logger.info("Setting log file to #{@config[:log_file]}")
  @logger.info("Setting log level to #{@config[:log_level]}")

  @config.each do |key,value|
    case key
      when :fact_filter
        self.fact_filter(value)
      when :identity_filter
        self.identity_filter(value)
      when :class_filter
        self.identity_filter(value)
    end
  end

end

Public Instance Methods

class_filter(class_array) click to toggle source
# File lib/etoro/utility/mcollective/rpc.rb, line 149
def class_filter(class_array)
  unless class_array.is_a?(Array)
    @logger.error('Class filter needs to be of type array')
  end
  class_array.each do |value|
    @logger.info("Settting class to  #{value}")
    @mc.class_filter value
  end
end
fact_filter(fact_hash) click to toggle source
# File lib/etoro/utility/mcollective/rpc.rb, line 129
def fact_filter(fact_hash)
  unless fact_hash.is_a?(Hash)
    @logger.error('Fact filter needs to be of type hash')
  end
  fact_hash.each do |key,value|
    @logger.info("Setting Fact #{key} to value #{value}")
    @mc.fact_filter key, value
  end
end
identity_filter(identity_array) click to toggle source
# File lib/etoro/utility/mcollective/rpc.rb, line 139
def identity_filter(identity_array)
  unless identity_array.is_a?(Array)
    @logger.error('Identity filter needs to be of type array')
  end
  identity_array.each do |value|
    @logger.info("Setting Identity to host #{value}")
    @mc.identity_filter value
  end
end
logging(log_file, log_level, description='') click to toggle source
# File lib/etoro/utility/mcollective/rpc.rb, line 159
def logging(log_file, log_level, description='')
  log = File.open(log_file,'a')
  @logger = Logger.new Etoro::Utility::MCollective::MultiDelegator.delegate(:write, :close).to(STDOUT, log)
  @logger.datetime_format = ('%Y-%m-%d_%H:%M:%S')
  @logger.formatter = proc do |severity, datetime, progname, msg|
    "#{datetime},level=#{severity},type=Deployment,#{description} message=#{msg}\n"
  end
  @logger.level = log_level
end
run() click to toggle source
# File lib/etoro/utility/mcollective/rpc.rb, line 94
def run
  # Check
  status
  @logger.info("#{self.class.name.split('::').last} run begin")

  if self.sequential? then
    hosts = self.hosts

    if hosts.length == 0
      @logger.error("No Hosts Defined")
      raise "No Hosts Defined"
    end

    @logger.info("Running deployment sequentially")

    hosts.each do |host|
      @current_host = host

      instance_eval &@config[:pre_execute] if @config.has_key?(:pre_execute)
      @mc.reset_filter
      @mc.identity_filter @current_host
      self.status
      self.execute
      instance_eval &@config[:post_execute] if @config.has_key?(:post_execute)
    end
  else
    instance_eval &@config[:pre_execute] if @config.has_key?(:pre_execute)
    self.execute
    instance_eval &@config[:post_execute] if @config.has_key?(:post_execute)
  end

  @logger.info("#{self.class.name.split('::').last} run complete")

end
sequential=(value) click to toggle source
# File lib/etoro/utility/mcollective/rpc.rb, line 78
def sequential=(value)
  @config[:sequential] = value
end
sequential?() click to toggle source
# File lib/etoro/utility/mcollective/rpc.rb, line 82
def sequential?
  return @config[:sequential] ? true : false
end
validate(config) click to toggle source
# File lib/etoro/utility/mcollective/rpc.rb, line 69
def validate(config)
  unless config.detect{|k| k.to_s =~ /:(fact|identity|class|compound)_filter/} then
    raise RuntimeError, "Filters need to be provided"
  end
  if self.class.name == 'Etoro::MCollective::RPC'
    raise RuntimeError, "Cannot instantiate this class directly - abstract"
  end
end