Class: Sfp::Runtime

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Runtime

Returns a new instance of Runtime.



6
7
8
9
10
11
12
# File 'lib/sfpagent/runtime.rb', line 6

def initialize(model)
  @mutex_procedure = Mutex.new
  @mutex_get_state = Mutex.new
  @root = nil
  @cloudfinder = Sfp::Helper::CloudFinder.new
  set_model(model)
end

Instance Attribute Details

#cloudfinderObject (readonly)

Returns the value of attribute cloudfinder.



4
5
6
# File 'lib/sfpagent/runtime.rb', line 4

def cloudfinder
  @cloudfinder
end

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/sfpagent/runtime.rb', line 4

def model
  @model
end

#rootObject (readonly)

Returns the value of attribute root.



4
5
6
# File 'lib/sfpagent/runtime.rb', line 4

def root
  @root
end

Instance Method Details

#execute_action(action) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sfpagent/runtime.rb', line 19

def execute_action(action)
  return false if !defined?(@root) or @root.nil?

  def normalise_parameters(params)
    p = {}
    params.each { |k,v| p[k[2,k.length-2]] = v }
    p
  end

  module_path, method_name = action['name'].pop_ref
  mod = @root.at?(module_path)[:_self]

  if mod.nil?
    raise Exception, "Module #{module_path} cannot be found!"

  elsif mod.is_a?(Sfp::Module::Shell)
    params = normalise_parameters(action['parameters'])
    mod.execute method_name, params

  elsif not mod.respond_to?(method_name)
    raise Exception, "Cannot execute #{action['name']}!"

  else
    params = normalise_parameters(action['parameters'])
    if mod.synchronized.rindex(method_name)
      @mutex_procedure.synchronize {
        mod.send method_name.to_sym, params
      }
    else
      mod.send method_name.to_sym, params
    end

  end

  # TODO - check post-execution state for verification
end

#get_state(as_sfp = false) ⇒ Object



72
73
74
75
76
77
# File 'lib/sfpagent/runtime.rb', line 72

def get_state(as_sfp=false)
  @mutex_get_state.synchronize {
    update_state(@root)
    get_object_state(@root, @model)
  }
end

#normalise_parameters(params) ⇒ Object



22
23
24
25
26
# File 'lib/sfpagent/runtime.rb', line 22

def normalise_parameters(params)
  p = {}
  params.each { |k,v| p[k[2,k.length-2]] = v }
  p
end

#set_model(model) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sfpagent/runtime.rb', line 56

def set_model(model)
  @mutex_get_state.synchronize {
    @model = model
    if @model.is_a?(Hash)
      root_model = Sfp::Helper.deep_clone(@model)
      root_model.accept(SFPtoRubyValueConverter)
      root_model.accept(ParentEliminator)
      @root = update_model(root_model, root_model, '$')
      @root.accept(ParentGenerator)

      @cloudfinder.clouds = []
      @model.accept(@cloudfinder.reset)
    end
  }
end

#whoami?Boolean

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/sfpagent/runtime.rb', line 14

def whoami?
  @model.each { |key,value| return key if key[0,1] != '_' and value['_context'] == 'object' } if !@model.nil?
  nil
end