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

Returns a new instance of Evaluator.



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

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

Instance Method Details

#apply_manifest(scope, targets, manifest) ⇒ Object



147
148
149
150
151
152
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 147

def apply_manifest(scope, targets, manifest)
  ast = @evaluator.parse_string(manifest)
  apply_block = ast.body.body
  applicator = Puppet.lookup(:apply_executor)
  applicator.apply([targets], apply_block, scope)
end

#command_step(scope, step) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 65

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

  args = [command, targets]
  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
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 15

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

  # Dispatch based on the step class name
  step_type = step.class.name.split('::').last.downcase
  method = "#{step_type}_step"

  send(method, scope, step_body)
end

#download_step(scope, step) ⇒ Object



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

def download_step(scope, step)
  source = step['download']
  destination = step['destination']
  targets = step['targets'] || step['target']
  description = step['description']

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

#eval_step(_scope, step) ⇒ Object



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

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.



207
208
209
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 207

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.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 156

def evaluate_block_with_bindings(closure_scope, args_hash, plan)
  if plan.steps.any? { |step| step.body.key?('target') }
    msg = "The 'target' parameter for YAML plan steps is deprecated and will be removed "\
          "in a future version of Bolt. Use the 'targets' parameter instead."
    Bolt::Logger.deprecate("yaml_plan_target", msg)
  end

  if plan.steps.any? { |step| step.body.key?('source') }
    msg = "The 'source' parameter for YAML plan upload steps is deprecated and will be removed "\
          "in a future version of Bolt. Use the 'upload' parameter instead."
    Bolt::Logger.deprecate("yaml_plan_source", msg)
  end

  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.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 183

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
    begin
      value.evaluate(scope, @evaluator)
    rescue StandardError => e
      raise format_evaluate_error(e, value)
    end
  else
    value
  end
end

#format_evaluate_error(error, value) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 211

def format_evaluate_error(error, value)
  # The Puppet::PreformattedError includes the line number of the
  # evaluable string that caused the error, while the value includes the
  # line number of the YAML plan that the string began on. To get the
  # actual line number of the error, add these two numbers together.
  line = error.line + value.line

  # If the evaluable string is not a scalar literal, correct for it
  # being on the same line as the step key.
  line -= 1 if value.is_a?(BareString)

  Bolt::PlanFailure.new(
    error.basic_message,
    'bolt/evaluation-error',
    { file: value.file, line: line }
  )
end

#generate_manifest(resources) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 115

def generate_manifest(resources)
  # inspect returns the Ruby representation of the resource hashes,
  # which happens to be the same as the Puppet representation
  puppet_resources = resources.inspect

  # Because the :tasks setting globally controls which mode the parser
  # is in, we need to make this snippet of non-tasks manifest code
  # parseable in tasks mode. The way to do that is by putting it in an
  # apply statement and taking the body.
  <<~MANIFEST
  apply('placeholder') {
    $resources = #{puppet_resources}
    $resources.each |$res| {
      Resource[$res['type']] { $res['title']:
        * => $res['parameters'],
      }
    }

    # Add relationships if there is more than one resource
    if $resources.length > 1 {
      ($resources.length - 1).each |$index| {
        $lhs = $resources[$index]
        $rhs = $resources[$index+1]
        $lhs_resource = Resource[$lhs['type'] , $lhs['title']]
        $rhs_resource = Resource[$rhs['type'] , $rhs['title']]
        $lhs_resource -> $rhs_resource
      }
    }
  }
  MANIFEST
end

#message_step(scope, step) ⇒ Object



111
112
113
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 111

def message_step(scope, step)
  scope.call_function('out::message', [step['message']])
end

#plan_step(scope, step) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 40

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

  args = [plan, parameters]

  scope.call_function('run_plan', args)
end

#resources_step(scope, step) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 101

def resources_step(scope, step)
  targets = step['targets'] || step['target']

  # TODO: Only call apply_prep when needed
  scope.call_function('apply_prep', targets)
  manifest = generate_manifest(step['resources'])

  apply_manifest(scope, targets, manifest)
end

#script_step(scope, step) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 49

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

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

  scope.call_function('run_script', args)
end

#task_step(scope, step) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bolt/pal/yaml_plan/evaluator.rb', line 25

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

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

  scope.call_function('run_task', args)
end

#upload_step(scope, step) ⇒ Object



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

def upload_step(scope, step)
  source = step['upload'] || step['source']
  destination = step['destination']
  targets = step['targets'] || step['target']
  description = step['description']

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