Class: Bolt::PAL::YamlPlan::Step
- Inherits:
-
Object
- Object
- Bolt::PAL::YamlPlan::Step
- Defined in:
- lib/bolt/pal/yaml_plan/step.rb,
lib/bolt/pal/yaml_plan/step/eval.rb,
lib/bolt/pal/yaml_plan/step/plan.rb,
lib/bolt/pal/yaml_plan/step/task.rb,
lib/bolt/pal/yaml_plan/step/script.rb,
lib/bolt/pal/yaml_plan/step/upload.rb,
lib/bolt/pal/yaml_plan/step/command.rb,
lib/bolt/pal/yaml_plan/step/message.rb,
lib/bolt/pal/yaml_plan/step/verbose.rb,
lib/bolt/pal/yaml_plan/step/download.rb,
lib/bolt/pal/yaml_plan/step/resources.rb
Direct Known Subclasses
Command, Download, Eval, Message, Plan, Resources, Script, Task, Upload, Verbose
Defined Under Namespace
Classes: Command, Download, Eval, Message, Plan, Resources, Script, StepError, Task, Upload, Verbose
Constant Summary collapse
- STEP_KEYS =
%w[ command download eval message verbose plan resources script task upload ].freeze
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
Class Method Summary collapse
-
.allowed_keys ⇒ Object
Keys that are allowed for the step.
- .create(step_body, step_number) ⇒ Object
-
.option_keys ⇒ Object
Keys that translate to metaparameters for the plan step’s function call.
-
.parse_code_string(code, quote = false) ⇒ Object
Parses the an evaluable string, optionally quote it before parsing.
-
.required_keys ⇒ Object
Keys that are required for the step.
- .validate(body, step_number) ⇒ Object
-
.validate_puppet_code(step_key, value) ⇒ Object
Recursively ensure all puppet code can be parsed.
- .validate_step_keys(body, step_number) ⇒ Object
Instance Method Summary collapse
-
#evaluate(scope, evaluator) ⇒ Object
Evaluates the step.
-
#initialize(body) ⇒ Step
constructor
A new instance of Step.
-
#transpile ⇒ Object
Transpiles the step into the plan language.
Constructor Details
#initialize(body) ⇒ Step
Returns a new instance of Step.
67 68 69 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 67 def initialize(body) @body = body end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
9 10 11 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 9 def body @body end |
Class Method Details
.allowed_keys ⇒ Object
Keys that are allowed for the step
35 36 37 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 35 def self.allowed_keys required_keys + option_keys + Set['name', 'description', 'targets'] end |
.create(step_body, step_number) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 51 def self.create(step_body, step_number) type_keys = (STEP_KEYS & step_body.keys) case type_keys.length when 0 raise StepError.new("No valid action detected", step_body['name'], step_number) when 1 type = type_keys.first else raise StepError.new("Multiple action keys detected: #{type_keys.inspect}", step_body['name'], step_number) end step_class = const_get("Bolt::PAL::YamlPlan::Step::#{type.capitalize}") step_class.validate(step_body, step_number) step_class.new(step_body) end |
.option_keys ⇒ Object
Keys that translate to metaparameters for the plan step’s function call
41 42 43 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 41 def self.option_keys Set.new end |
.parse_code_string(code, quote = false) ⇒ Object
Parses the an evaluable string, optionally quote it before parsing
201 202 203 204 205 206 207 208 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 201 def self.parse_code_string(code, quote = false) if quote quoted = Puppet::Pops::Parser::EvaluatingParser.quote(code) Puppet::Pops::Parser::EvaluatingParser.new.parse_string(quoted) else Puppet::Pops::Parser::EvaluatingParser.new.parse_string(code) end end |
.required_keys ⇒ Object
Keys that are required for the step
47 48 49 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 47 def self.required_keys Set.new end |
.validate(body, step_number) ⇒ Object
112 113 114 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 146 147 148 149 150 151 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 112 def self.validate(body, step_number) validate_step_keys(body, step_number) begin body.each { |k, v| validate_puppet_code(k, v) } rescue Bolt::Error => e raise StepError.new(e.msg, body['name'], step_number) end if body.key?('parameters') unless body['parameters'].is_a?(Hash) raise StepError.new("Parameters key must be a hash", body['name'], step_number) end = body['parameters'].keys .select { |key| key.start_with?('_') } .map { |key| key.sub(/^_/, '') } if (dups = body.keys & ).any? raise StepError.new( "Cannot specify metaparameters when using top-level keys with same name: #{dups.join(', ')}", body['name'], step_number ) end end unless body.fetch('parameters', {}).is_a?(Hash) msg = "Parameters key must be a hash" raise StepError.new(msg, body['name'], step_number) end if body.key?('name') name = body['name'] unless name.is_a?(String) && name.match?(Bolt::PAL::YamlPlan::VAR_NAME_PATTERN) = "Invalid step name: #{name.inspect}" raise StepError.new(, body['name'], step_number) end end end |
.validate_puppet_code(step_key, value) ⇒ Object
Recursively ensure all puppet code can be parsed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 173 def self.validate_puppet_code(step_key, value) case value when Array value.map { |element| validate_puppet_code(step_key, element) } when Hash value.each_with_object({}) do |(k, v), o| key = k.is_a?(Bolt::PAL::YamlPlan::EvaluableString) ? k.value : k o[key] = validate_puppet_code(key, v) end # CodeLiterals can be parsed directly when Bolt::PAL::YamlPlan::CodeLiteral parse_code_string(value.value) # BareString is parsed directly if it starts with '$' when Bolt::PAL::YamlPlan::BareString if value.value.start_with?('$') parse_code_string(value.value) else parse_code_string(value.value, true) end when Bolt::PAL::YamlPlan::EvaluableString # Must quote parsed strings to evaluate them parse_code_string(value.value, true) end rescue Puppet::Error => e raise Bolt::Error.new("Error parsing #{step_key.inspect}: #{e.}", "bolt/invalid-plan") end |
.validate_step_keys(body, step_number) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 153 def self.validate_step_keys(body, step_number) step_type = name.split('::').last.downcase # For validated step action, ensure only valid keys illegal_keys = body.keys.to_set - allowed_keys if illegal_keys.any? = "The #{step_type.inspect} step does not support: #{illegal_keys.to_a.inspect} key(s)" raise StepError.new(, body['name'], step_number) end # Ensure all required keys are present missing_keys = required_keys - body.keys if missing_keys.any? = "The #{step_type.inspect} step requires: #{missing_keys.to_a.inspect} key(s)" raise StepError.new(, body['name'], step_number) end end |
Instance Method Details
#evaluate(scope, evaluator) ⇒ Object
Evaluates the step
82 83 84 85 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 82 def evaluate(scope, evaluator) evaluated = evaluator.evaluate_code_blocks(scope, body) scope.call_function(function, format_args(evaluated)) end |
#transpile ⇒ Object
Transpiles the step into the plan language
73 74 75 76 77 78 |
# File 'lib/bolt/pal/yaml_plan/step.rb', line 73 def transpile code = String.new(" ") code << "$#{body['name']} = " if body['name'] code << function_call(function, format_args(body)) code << "\n" end |