Class: AgentUtils::ConfFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/agentdispatcher.rb

Overview

Handles args parsing, configuration management

Constant Summary collapse

RESERVED_OPTS =
[:id, :cmd, :args, :config]
AGENTS_ROOT =
(ENV['AGENTS_ROOT'] ||  '.' )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(anAgentClass = nil) ⇒ ConfFactory

Returns a new instance of ConfFactory.



251
252
253
254
# File 'lib/agentdispatcher.rb', line 251

def initialize anAgentClass = nil 
    @agentClass = anAgentClass || AgentBase
    @allowed_cmds =  @agentClass.AllowedCommands 
end

Instance Attribute Details

#confObject (readonly)

Returns the value of attribute conf.



293
294
295
# File 'lib/agentdispatcher.rb', line 293

def conf
  @conf
end

Instance Method Details

#[](aSelector) ⇒ Object



295
296
297
# File 'lib/agentdispatcher.rb', line 295

def [] aSelector
    @conf[aSelector.to_sym]
end

#createFromArgv(*argv) ⇒ Object

create confign from parsed arguments

CONFIG
agentID command
arguments
CONFG
command
arguments
  • > agentID=class name

CONFIG
  • > agentID=class name, command=‘run’, no args



276
277
278
# File 'lib/agentdispatcher.rb', line 276

def createFromArgv *argv
    createFromMethod *parseArgv( *argv)
end

#createFromLine(aString) ⇒ Object

creates the config from a string (like what you type on command-line)



268
269
270
# File 'lib/agentdispatcher.rb', line 268

def createFromLine aString
    createFromArgv *fromLine(aString)
end

#createFromMethod(anAgentID = nil, aCmd = nil, anArgs = [], aConf = {}) ⇒ Object

create config by explicitely giving it the required componnents



281
282
283
# File 'lib/agentdispatcher.rb', line 281

def createFromMethod  anAgentID = nil, aCmd = nil, anArgs = [], aConf = {}
    createFromOpts semantize(anAgentID, aCmd, anArgs, aConf)
end

#createFromOpts(aConf) ⇒ Object

create config from options (Hash)



286
287
288
289
290
291
# File 'lib/agentdispatcher.rb', line 286

def createFromOpts aConf
    #puts "Class: #{@agentClass.name}, File: #{__FILE__}, execFile: #{$0}"
    raise "agentID, command, and args need to be defined" unless [:id, :cmd,:args].all? {|c| aConf.include? c}
    #puts "Execution --- conf:#{aConf.inspect}"
    @conf = aConf
end

#dumpHelpObject

genarates a help message



257
258
259
260
261
262
263
264
265
# File 'lib/agentdispatcher.rb', line 257

def dumpHelp
    puts %Q{Usage: #{@agentClass} [CONFIG] [ID] [COMMAND] [ARGUMENTS]
    CONFIG\tAny application specific configuration flags in the format --NAME VALUE[,VALUE]*
  \tSpecial flags: {help, id, config=[A,B,...],log_path, nopid, forcepid, pid_path, quiet, cmd}
    ID     \tInstance id of the agent (default: '#{class_to_id(@agentClass)}')
    COMMAND\tCommand to execute [#{@allowed_cmds.join(',')}] (default: 'run')
    ARGUMENTS\tSpace separated arguments (default:none)
}
end

#loadConfig(aPathOrName, aDefaults = {}) ⇒ Object

Load a yaml config file and merge it with defaults The aPathOrName can be a path (if it ends with .yml) or just a configuration file name (to be looked up in ‘runtime’ path, see AGENTS_ROOT) return the merged hash - all keys are symbolized



302
303
304
305
306
307
308
309
# File 'lib/agentdispatcher.rb', line 302

def loadConfig aPathOrName, aDefaults={}
    return aDefaults unless aPathOrName
    require 'yaml'
    aPathOrName = File.join(AGENTS_ROOT , "config", "#{aPathOrName}.yml") unless aPathOrName=~/\.yml$/
    cfg = YAML.load_file aPathOrName
    cfg = ProcTools.Intern cfg
    return cfg ?  (aDefaults.merge cfg)  : aDefaults
end

#loadConfigs(aPathsOrNames, aDefaults = {}) ⇒ Object

loads chain of configuration files



312
313
314
315
316
317
318
# File 'lib/agentdispatcher.rb', line 312

def loadConfigs aPathsOrNames, aDefaults={}
    aPathsOrNames = [aPathsOrNames] unless aPathsOrNames.kind_of? Array
    aPathsOrNames.each {|pathOrName|
        aDefaults = loadConfig pathOrName, aDefaults
    }
    aDefaults
end