Class: Laborantin::Environment

Inherits:
Object
  • Object
show all
Extended by:
Metaprog::Describable, Metaprog::Hookable, Metaprog::MultiName
Includes:
Metaprog::Configurable, Metaprog::Datable
Defined in:
lib/laborantin/core/environment.rb

Overview

An Environment represents the surrounding of an experiment. Basically, it should only contains things that are hard to change during an experiment. Let’s say you have two computers, A and B. A can be a server and B a client or vice-versa. Hence, this is a good choice for two differents environments.

As a normal user, you should only subclass Environment, and let the script creates it for you. But it is easy to monkey-patch or to subclass by hand. If you want to do that, you must know that Environment @@all class variable holds a reference to every child class from Environment.

Constant Summary collapse

@@all =
[]

Constants included from Metaprog::MultiName

Metaprog::MultiName::AVAILABLE_NAMES

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Metaprog::Describable

#description

Attributes included from Metaprog::Hookable

#hooks

Attributes included from Metaprog::MultiName

#cli_name, #fs_name

Attributes included from Metaprog::Configurable

#config

Attributes included from Metaprog::Datable

#date

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Metaprog::Hookable

setup, teardown

Methods included from Metaprog::MultiName

set_name

Methods included from Metaprog::Configurable

#load_config!, #save_config

Methods included from Metaprog::Datable

#date_str

Constructor Details

#initialize(command = nil) ⇒ Environment

Initializes a new instance: the date is Time.now the rundir is the Environment.envdir followed by the date_str the loggers contains an empty Array Does NOT create any directory, so the accessors can be overwritten if needed.



127
128
129
130
131
132
133
# File 'lib/laborantin/core/environment.rb', line 127

def initialize(command=nil)
  @command = command
  @date = Time.now
  @rundir = File.join(self.class.envdir, date_str)
  @loggers = []
  @config = {}
end

Class Attribute Details

.verificationsObject

An array of methods called to ensure that the environment run is the wanted one (e.g. a way to specify a RUBY_PLATFORM). CURRENTLY NOT HERITED



85
86
87
# File 'lib/laborantin/core/environment.rb', line 85

def verifications
  @verifications
end

Instance Attribute Details

#commandObject

The (optional) commmand instanciating this environment



120
121
122
# File 'lib/laborantin/core/environment.rb', line 120

def command
  @command
end

#loggersObject

An array of loggers objects.



117
118
119
# File 'lib/laborantin/core/environment.rb', line 117

def loggers
  @loggers
end

#rundirObject

An attribute that holds the directory where the logfile and the scenarii results are stored. Can be overridden (e.g. Environment.scan_resdir does that).



114
115
116
# File 'lib/laborantin/core/environment.rb', line 114

def rundir
  @rundir
end

Class Method Details

.allObject

Returns all the known subklasses of Environment.



101
102
103
# File 'lib/laborantin/core/environment.rb', line 101

def all
  @@all
end

.envdirObject

The path where the results for instance of a subklass of Environment are stored (needs the Runner’s resultdir).



107
108
109
# File 'lib/laborantin/core/environment.rb', line 107

def envdir
  File.join(Runner.instance.resultdir, self.fs_name)
end

.inherited(klass) ⇒ Object

Prepares attributes’ default values whenever a subclass is created.



88
89
90
91
92
93
# File 'lib/laborantin/core/environment.rb', line 88

def inherited(klass)
  klass.verifications = []
  klass.description = ''
  klass.hooks = {:setup => [], :teardown => []}
  @@all << klass
end

.new_loading_from_dir(path) ⇒ Object



73
74
75
76
77
78
# File 'lib/laborantin/core/environment.rb', line 73

def self.new_loading_from_dir(path)
  obj = self.new
  obj.load_config!
  obj.rundir = path
  obj
end

.scan_resdir(dir) ⇒ Object

Populates loaded (i.e. put in @@all class variable when self.inherited is called) environment classes from existing results that are stored in the dir parameter.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/laborantin/core/environment.rb', line 57

def self.scan_resdir(dir)
  list = []
  Dir.entries(dir).each do |f| 
    envklass = Laborantin::Environment.all.find{|e| e.fs_name == f} 
    if envklass
      Dir.entries(envklass.envdir).each do |e|
        if e =~ /\d+-\w+-\d+_\d+-\d+-\d+/
          env = envklass.new_loading_from_dir(File.join(envklass.envdir, e))
          list << env
        end
      end
    end
  end
  list
end

.verify(*args) ⇒ Object

Registers new verifiers methods that will be verified at beginning.



96
97
98
# File 'lib/laborantin/core/environment.rb', line 96

def verify(*args)
  self.verifications = [*args].flatten
end

Instance Method Details

#config_pathObject

complete path to the config.yaml file



151
152
153
# File 'lib/laborantin/core/environment.rb', line 151

def config_path
  File.join(rundir, 'config.yaml')
end

#failed?Boolean

returns if the environment exited with error

Returns:

  • (Boolean)


182
183
184
# File 'lib/laborantin/core/environment.rb', line 182

def failed?
  config[:state] == :error
end

#log(str, lvl = :debug) ⇒ Object

Send str log message at the levele lvl to every loggers.



210
211
212
# File 'lib/laborantin/core/environment.rb', line 210

def log(str, lvl=:debug)
  @loggers.each{|l| l.send(lvl, str)}
end

#logfile_pathObject

complete path to the environment.log file



146
147
148
# File 'lib/laborantin/core/environment.rb', line 146

def logfile_path
  File.join(rundir, 'environment.log')
end

#populateObject

Returns an array of Scenario objects from the results in the envdir. The scenarii classes must be loaded before, else, some results might be ignored.



216
217
218
# File 'lib/laborantin/core/environment.rb', line 216

def populate
  Laborantin::Scenario.scan_env(self)
end

#prepare!Object

In the following order:

  • Creates the envdir if needed.

  • Adds some loggers.

  • Calls the setup hooks methods

BEWARE : currently does not ensure unicity of envdir, so wait one sec between several runs of same env



193
194
195
196
197
198
199
200
201
202
# File 'lib/laborantin/core/environment.rb', line 193

def prepare!
  FileUtils.mkdir_p(rundir) #TODO: ensure unicity
  @loggers << Logger.new(logfile_path)
  @loggers << Logger.new(STDOUT)
  log(self.class.description, :info) unless self.class.description.empty?
  log "Directories prepared"
  log "Writing config file"
  save_config
  call_hooks :setup
end

#record_scenario_dir(dir, save = false) ⇒ Object



159
160
161
162
163
# File 'lib/laborantin/core/environment.rb', line 159

def record_scenario_dir(dir, save=false)
  config[:scenarii_dirs] ||= []
  config[:scenarii_dirs] << dir
  save_config if save
end

#runnerObject



135
136
137
# File 'lib/laborantin/core/environment.rb', line 135

def runner
  command.runner if command
end

#scenarii_dirsObject



155
156
157
# File 'lib/laborantin/core/environment.rb', line 155

def scenarii_dirs
  config[:scenarii_dirs]
end

#stateObject

gets the state of the environment



166
167
168
# File 'lib/laborantin/core/environment.rb', line 166

def state
  config[:state]
end

#state=(val, save = true) ⇒ Object

changes the state of the environment



171
172
173
174
# File 'lib/laborantin/core/environment.rb', line 171

def state=(val, save=true)
  config[:state] = val
  save_config if save
end

#successful?Boolean

returns if the environment succeeded

Returns:

  • (Boolean)


177
178
179
# File 'lib/laborantin/core/environment.rb', line 177

def successful?
  config[:state] == :success
end

#teardown!Object

Calls the teardown hooks methods.



205
206
207
# File 'lib/laborantin/core/environment.rb', line 205

def teardown!
  call_hooks :teardown
end

#valid?Boolean

sends all the methods registered in Environment.verify returns the method symbol of the failed verification if any

Returns:

  • (Boolean)


141
142
143
# File 'lib/laborantin/core/environment.rb', line 141

def valid?
  self.class.verifications.find{|v| not send(v)}.nil?
end