Class: Bolt::PAL::YamlPlan::Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/pal/yaml_plan/evaluator.rb

Instance Method Summary collapse

Constructor Details

#initialize(analytics = Bolt::Analytics::NoopClient.new) ⇒ Evaluator



9
10
11
12
13
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 9

def initialize(analytics = Bolt::Analytics::NoopClient.new)
  @logger = Logging.logger[self]
  @analytics = analytics
  @evaluator = Puppet::Pops::Parser::EvaluatingParser.new
end

Instance Method Details

#command_step(scope, step) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 75

def command_step(scope, step)
  command = step['command']
  target = step['target']
  description = step['description']

  args = [command, target]
  args << description if description
  scope.call_function('run_command', args)
end

#dispatch_step(scope, step) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 15

def dispatch_step(scope, step)
  step_type = step.type
  step_body = evaluate_code_blocks(scope, step.body)

  case step_type
  when 'task'
    task_step(scope, step_body)
  when 'command'
    command_step(scope, step_body)
  when 'plan'
    plan_step(scope, step_body)
  when 'script'
    script_step(scope, step_body)
  when 'source'
    upload_file_step(scope, step_body)
  when 'eval'
    eval_step(scope, step_body)
  end
end

#eval_step(_scope, step) ⇒ Object



96
97
98
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 96

def eval_step(_scope, step)
  step['eval']
end

#evaluate(value, _scope) ⇒ Object

Occasionally the Closure will ask us to evaluate what it assumes are AST objects. Because we’ve sidestepped the AST, they aren’t, so just return the values as already evaluated.



137
138
139
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 137

def evaluate(value, _scope)
  value
end

#evaluate_block_with_bindings(closure_scope, args_hash, plan) ⇒ Object

This is the method that Puppet calls to evaluate the plan. The name makes more sense for .pp plans.



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 102

def evaluate_block_with_bindings(closure_scope, args_hash, plan)
  plan_result = closure_scope.with_local_scope(args_hash) do |scope|
    plan.steps.each do |step|
      step_result = dispatch_step(scope, step)

      scope.setvar(step.name, step_result) if step.name
    end

    evaluate_code_blocks(scope, plan.return)
  end

  throw :return, Puppet::Pops::Evaluator::Return.new(plan_result, nil, nil)
end

#evaluate_code_blocks(scope, value) ⇒ Object

Recursively evaluate any EvaluableString instances in the object.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 117

def evaluate_code_blocks(scope, value)
  # XXX We should establish a local scope here probably
  case value
  when Array
    value.map { |element| evaluate_code_blocks(scope, element) }
  when Hash
    value.each_with_object({}) do |(k, v), o|
      key = k.is_a?(EvaluableString) ? k.value : k
      o[key] = evaluate_code_blocks(scope, v)
    end
  when EvaluableString
    value.evaluate(scope, @evaluator)
  else
    value
  end
end

#plan_step(scope, step) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 50

def plan_step(scope, step)
  plan = step['plan']
  parameters = step['parameters'] || {}

  args = [plan, parameters]

  scope.call_function('run_plan', args)
end

#script_step(scope, step) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 59

def script_step(scope, step)
  script = step['script']
  target = step['target']
  description = step['description']
  arguments = step['arguments'] || []

  options = { 'arguments' => arguments }
  args = if description
           [script, target, description, options]
         else
           [script, target, options]
         end

  scope.call_function('run_script', args)
end

#task_step(scope, step) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 35

def task_step(scope, step)
  task = step['task']
  target = step['target']
  description = step['description']
  params = step['parameters'] || {}

  args = if description
           [task, target, description, params]
         else
           [task, target, params]
         end

  scope.call_function('run_task', args)
end

#upload_file_step(scope, step) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 85

def upload_file_step(scope, step)
  source = step['source']
  destination = step['destination']
  target = step['target']
  description = step['description']

  args = [source, destination, target]
  args << description if description
  scope.call_function('upload_file', args)
end