Class: Wukong::Runner

Inherits:
Object
  • Object
show all
Includes:
Logging, BootSequence, CodeLoader, CommandRunner, DeployPackLoader
Defined in:
lib/wukong/runner.rb,
lib/wukong/runner/code_loader.rb,
lib/wukong/runner/help_message.rb,
lib/wukong/runner/boot_sequence.rb,
lib/wukong/runner/command_runner.rb,
lib/wukong/runner/deploy_pack_loader.rb

Overview

A base class which handles

  • requiring any necessary code like deploy packs or code from command-line arguments
  • having all plugins configure settings as necessary
  • resolving settings
  • having all plugins boot from now resolved settings
  • parsing command-line arguments
  • instantiating and handing over control to a driver which runs the actual code

Direct Known Subclasses

Local::LocalRunner

Defined Under Namespace

Modules: BootSequence, CodeLoader, CommandRunner, DeployPackLoader, HelpMessage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BootSequence

#boot!

Methods included from HelpMessage

#dump_help, #dump_help_and_exit!, #help_given?, #strip_help_param!

Methods included from DeployPackLoader

#deploy_pack_dir, #environment_file, #in_deploy_pack?, #load_deploy_pack, #loaded_deploy_pack?

Methods included from CodeLoader

#load_args

Methods included from Logging

included

Constructor Details

#initialize(settings = Configliere::Param.new) ⇒ Runner

Create a new Runner with the given +settings+.

Uses an empty Configliere::Param object if no +settings+ are given.

Parameters:

  • settings (Configliere::Param) (defaults to: Configliere::Param.new)


34
35
36
# File 'lib/wukong/runner.rb', line 34

def initialize settings=Configliere::Param.new
  self.settings = settings
end

Instance Attribute Details

#settingsObject

The settings object that will be configured and booted from. All plugins will configure this object.



26
27
28
# File 'lib/wukong/runner.rb', line 26

def settings
  @settings
end

Class Method Details

.description(msg = nil) ⇒ String

Convenience method for setting the description message of a Runner.

Parameters:

  • msg (String, nil) (defaults to: nil)

    set the description message

Returns:

  • (String)

    the description message



83
84
85
86
# File 'lib/wukong/runner.rb', line 83

def self.description msg=nil
  return @description unless msg
  @description = msg
end

.die(message = nil, code = 127) ⇒ Object

Kill this process with the given error message and exit code.

Parameters:

  • message (String) (defaults to: nil)
  • code. (Integer)


93
94
95
96
# File 'lib/wukong/runner.rb', line 93

def self.die(message=nil, code=127)
  log.error(message) if message
  exit(code)
end

.run(settings = Configliere::Param.new) ⇒ Object

Instantiates a new Runner and boot it up.

Will rescue any Wukong::Error with a logged error message and exit.



42
43
44
45
46
47
48
# File 'lib/wukong/runner.rb', line 42

def self.run(settings=Configliere::Param.new)
  begin
    new(settings).boot!
  rescue Wukong::Error => e
    die(e.message, 127)
  end
end

.usage(msg = nil) ⇒ String

Convenience method for setting the usage message of a Runner.

Parameters:

  • msg (String, nil) (defaults to: nil)

    set the usage message

Returns:

  • (String)

    the usage message



74
75
76
77
# File 'lib/wukong/runner.rb', line 74

def self.usage msg=nil
  return @usage unless msg
  @usage = msg
end

Instance Method Details

#argsArray<String>

The parsed command-line arguments.

Will raise an error if +boot+ hasn't been called yet.

Returns:



55
56
57
# File 'lib/wukong/runner.rb', line 55

def args
  settings.rest
end

#dataflow?(name) ⇒ true, false

Is the given name a registered as a dataflow?

Parameters:

Returns:

  • (true, false)


164
165
166
# File 'lib/wukong/runner.rb', line 164

def dataflow?(name)
  registered?(name) && dataflow_class_for(name).ancestors.include?(Wukong::Dataflow)
end

#dataflow_class_for(name) ⇒ Wukong::Processor, ...

Retrieve the dataflow registered under a given name.

Parameters:

Returns:



147
148
149
150
# File 'lib/wukong/runner.rb', line 147

def dataflow_class_for(name)
  builder = (Wukong.registry.retrieve(name.to_sym) or return)
  builder.for_class
end

#descriptionString

Return the description text for this runner.

Returns:

  • (String)

    the description text



130
131
132
# File 'lib/wukong/runner.rb', line 130

def description
  self.class.description
end

#processor?(name) ⇒ true, false

Is the given name a registered as a processor?

Parameters:

Returns:

  • (true, false)


156
157
158
# File 'lib/wukong/runner.rb', line 156

def processor?(name)
  registered?(name) && dataflow_class_for(name).ancestors.include?(Wukong::Processor)
end

#program_nameString

Return the name of the program this Runner is running.

This is passed to plugins which can configure settings appropriately. Defaults to the name of the currently running process.

Returns:



105
106
107
# File 'lib/wukong/runner.rb', line 105

def program_name
  @program_name || File.basename($0)
end

#program_name=(name) ⇒ Object

Explicitly set the name of the program this Runner is running.

This is useful for unit tests in which the name of the currently running process may be different from the runner command being tested (rspec vs. wu-local).

Parameters:



116
117
118
# File 'lib/wukong/runner.rb', line 116

def program_name= name
  @program_name = name
end

#registered?(name) ⇒ true, false

Is there a processor or dataflow registered with the given name?

Parameters:

Returns:

  • (true, false)


139
140
141
# File 'lib/wukong/runner.rb', line 139

def registered? name
  name && Wukong.registry.registered?(name.to_sym)
end

#rootString

The root directory we should consider ourselves to be running in.

Defaults to the root directory of a deploy pack if we're running inside one, else just returns Dir.pwd.

Returns:



66
67
68
# File 'lib/wukong/runner.rb', line 66

def root
  in_deploy_pack? ? deploy_pack_dir : Dir.pwd
end

#usageString

Return the usage message for this runner.

Returns:

  • (String)

    the usage message



123
124
125
# File 'lib/wukong/runner.rb', line 123

def usage
  ["usage: #{program_name} [ --param=val | --param | -p val | -p ]", self.class.usage].compact.join(' ')
end