Class: Flor::ConfExecutor

Inherits:
TransientExecutor show all
Defined in:
lib/flor/core/texecutor.rb

Instance Attribute Summary

Attributes inherited from Executor

#execution, #hooks, #traps, #unit

Class Method Summary collapse

Methods inherited from TransientExecutor

#archive, #clone, #initialize, #journal, #launch, #step, #walk

Methods inherited from Executor

#conf, #counter, #counter_add, #counter_next, #exid, #initialize, #node, #traps_and_hooks, #trigger_block, #trigger_hook, #trigger_trap, #vars

Constructor Details

This class inherits a constructor from Flor::TransientExecutor

Class Method Details

.determine_root(path) ⇒ Object



295
296
297
298
299
300
301
# File 'lib/flor/core/texecutor.rb', line 295

def determine_root(path)

  dir = File.absolute_path(File.dirname(path))
  ps = dir.split(File::SEPARATOR)

  ps.last == 'etc' ? File.absolute_path(File.join(dir, '..')) : dir
end

.interpret(path, source, context) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/flor/core/texecutor.rb', line 217

def interpret(path, source, context)

  path ||= '.'

  fs = context['payload'] || {}

  vs = Hash.new { |h, k| k }
    #
  vs.merge!(context['vars'] || {})
  vs['root'] = determine_root(path)
  vs['ruby_version'] = RUBY_VERSION
  vs['ruby_platform'] = RUBY_PLATFORM
    #
  class << vs
    def has_key?(k)
      prc = Flor::Procedure[k]
      ( ! prc) || ( ! prc.core?) # ignore non-core procedures
    end
  end

  c = ! (ENV['FLOR_DEBUG'] || '').match(/conf/)
  e = self.new('conf' => c)
  r = e.launch(source, payload: fs, vars: vs, path: path)

  unless r['point'] == 'terminated'
    ae = ArgumentError.new(
      "error while reading conf: #{r['error']['msg']}")
    ae.set_backtrace(r['error']['trc'])
    fail ae
  end

  o = Flor.dup(r['payload']['ret'])

  if o.is_a?(Hash)
    o['_path'] = path
    o['root'] ||= Flor.relativize_path(vs['root'])
  elsif o.is_a?(Array)
    o.each { |ee| ee['_path'] = path if ee.is_a?(Hash) }
  end

  rework_conf(o)
end

.interpret_line(s) ⇒ Object

Used by “flosh” the flor shell



287
288
289
290
291
292
293
# File 'lib/flor/core/texecutor.rb', line 287

def interpret_line(s)

  r = interpret_source(s)
  r.delete('root') if r.is_a?(Hash)

  r
end

.interpret_path(path, context = nil) ⇒ Object



260
261
262
263
264
265
266
267
268
269
# File 'lib/flor/core/texecutor.rb', line 260

def interpret_path(path, context=nil)

  path = File.join(path, 'etc/conf.json') if File.directory?(path)

  fail ArgumentError.new(
    "flor configuration file not found #{path.inspect}"
  ) unless File.exist?(path)

  interpret(path, load(path), context || {})
end

.interpret_path_or_source(s, context = nil) ⇒ Object



276
277
278
279
280
281
282
283
# File 'lib/flor/core/texecutor.rb', line 276

def interpret_path_or_source(s, context=nil)

  if s.index("\n")
    interpret_source(load(s), context)
  else
    interpret_path(s, context)
  end
end

.interpret_source(source, context = nil) ⇒ Object



271
272
273
274
# File 'lib/flor/core/texecutor.rb', line 271

def interpret_source(source, context=nil)

  interpret(nil, source, context || {})
end

.load(path) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/flor/core/texecutor.rb', line 194

def load(path)

  src =
    if path.match(/[\r\n]/)
      path.strip
    else
      File.readlines(path)
        .reject { |l| l.strip[0, 1] == '#' }
        .join("\n")
        .strip
    end

  az = "#{src[0, 1]}#{src[-1, 1]}"

  if az == '{}' || az == '[]'
    src
  elsif src.match(/[^\r\n{]+:/) || src == ''
    "{\n#{src}\n}"
  else
    "[\n#{src}\n]"
  end
end