bolt 2.19.0 → 2.24.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of bolt might be problematic. Click here for more details.

Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/Puppetfile +3 -1
  3. data/bolt-modules/boltlib/lib/puppet/functions/download_file.rb +123 -0
  4. data/bolt-modules/boltlib/lib/puppet/functions/run_plan.rb +6 -0
  5. data/bolt-modules/ctrl/lib/puppet/functions/ctrl/do_until.rb +12 -6
  6. data/bolt-modules/dir/lib/puppet/functions/dir/children.rb +35 -0
  7. data/bolt-modules/out/lib/puppet/functions/out/message.rb +1 -1
  8. data/exe/bolt +1 -0
  9. data/guides/inventory.txt +19 -0
  10. data/guides/project.txt +22 -0
  11. data/lib/bolt/analytics.rb +5 -5
  12. data/lib/bolt/applicator.rb +4 -3
  13. data/lib/bolt/bolt_option_parser.rb +100 -27
  14. data/lib/bolt/catalog.rb +12 -3
  15. data/lib/bolt/cli.rb +356 -156
  16. data/lib/bolt/config.rb +2 -2
  17. data/lib/bolt/config/options.rb +18 -4
  18. data/lib/bolt/executor.rb +30 -7
  19. data/lib/bolt/inventory/group.rb +6 -5
  20. data/lib/bolt/inventory/inventory.rb +4 -3
  21. data/lib/bolt/logger.rb +3 -4
  22. data/lib/bolt/module.rb +2 -1
  23. data/lib/bolt/outputter.rb +56 -0
  24. data/lib/bolt/outputter/human.rb +10 -9
  25. data/lib/bolt/outputter/json.rb +11 -4
  26. data/lib/bolt/outputter/logger.rb +2 -2
  27. data/lib/bolt/outputter/rainbow.rb +18 -2
  28. data/lib/bolt/pal.rb +13 -11
  29. data/lib/bolt/pal/yaml_plan/evaluator.rb +22 -1
  30. data/lib/bolt/pal/yaml_plan/step.rb +24 -2
  31. data/lib/bolt/pal/yaml_plan/step/download.rb +38 -0
  32. data/lib/bolt/pal/yaml_plan/step/message.rb +30 -0
  33. data/lib/bolt/pal/yaml_plan/step/upload.rb +3 -3
  34. data/lib/bolt/pal/yaml_plan/transpiler.rb +11 -3
  35. data/lib/bolt/plugin/prompt.rb +3 -3
  36. data/lib/bolt/plugin/puppetdb.rb +3 -2
  37. data/lib/bolt/project.rb +7 -4
  38. data/lib/bolt/project_migrate.rb +138 -0
  39. data/lib/bolt/puppetdb/client.rb +2 -0
  40. data/lib/bolt/puppetdb/config.rb +16 -0
  41. data/lib/bolt/result.rb +7 -0
  42. data/lib/bolt/shell/bash.rb +31 -11
  43. data/lib/bolt/shell/powershell.rb +10 -4
  44. data/lib/bolt/transport/base.rb +24 -0
  45. data/lib/bolt/transport/docker.rb +8 -0
  46. data/lib/bolt/transport/docker/connection.rb +28 -10
  47. data/lib/bolt/transport/local/connection.rb +15 -2
  48. data/lib/bolt/transport/orch.rb +15 -3
  49. data/lib/bolt/transport/simple.rb +6 -0
  50. data/lib/bolt/transport/ssh/connection.rb +13 -5
  51. data/lib/bolt/transport/ssh/exec_connection.rb +24 -3
  52. data/lib/bolt/transport/winrm/connection.rb +125 -15
  53. data/lib/bolt/util.rb +27 -12
  54. data/lib/bolt/util/puppet_log_level.rb +4 -3
  55. data/lib/bolt/version.rb +1 -1
  56. data/lib/bolt_server/base_config.rb +1 -1
  57. data/lib/bolt_server/pe/pal.rb +1 -1
  58. data/lib/bolt_server/transport_app.rb +79 -2
  59. data/lib/bolt_spec/bolt_context.rb +7 -2
  60. data/lib/bolt_spec/plans.rb +16 -3
  61. data/lib/bolt_spec/plans/action_stubs.rb +3 -2
  62. data/lib/bolt_spec/plans/action_stubs/download_stub.rb +66 -0
  63. data/lib/bolt_spec/plans/mock_executor.rb +14 -1
  64. data/lib/bolt_spec/run.rb +22 -0
  65. data/libexec/apply_catalog.rb +2 -2
  66. data/libexec/bolt_catalog +4 -3
  67. data/libexec/custom_facts.rb +1 -1
  68. data/libexec/query_resources.rb +1 -1
  69. data/modules/secure_env_vars/plans/init.pp +20 -0
  70. metadata +11 -2
@@ -73,7 +73,7 @@ module Bolt
73
73
  end
74
74
 
75
75
  def upload_step(scope, step)
76
- source = step['source']
76
+ source = step['upload'] || step['source']
77
77
  destination = step['destination']
78
78
  targets = step['targets'] || step['target']
79
79
  description = step['description']
@@ -83,6 +83,17 @@ module Bolt
83
83
  scope.call_function('upload_file', args)
84
84
  end
85
85
 
86
+ def download_step(scope, step)
87
+ source = step['download']
88
+ destination = step['destination']
89
+ targets = step['targets'] || step['target']
90
+ description = step['description']
91
+
92
+ args = [source, destination, targets]
93
+ args << description if description
94
+ scope.call_function('download_file', args)
95
+ end
96
+
86
97
  def eval_step(_scope, step)
87
98
  step['eval']
88
99
  end
@@ -97,6 +108,10 @@ module Bolt
97
108
  apply_manifest(scope, targets, manifest)
98
109
  end
99
110
 
111
+ def message_step(scope, step)
112
+ scope.call_function('out::message', [step['message']])
113
+ end
114
+
100
115
  def generate_manifest(resources)
101
116
  # inspect returns the Ruby representation of the resource hashes,
102
117
  # which happens to be the same as the Puppet representation
@@ -145,6 +160,12 @@ module Bolt
145
160
  Bolt::Logger.deprecation_warning("Using 'target' parameter for YAML plan steps, not 'targets'", msg)
146
161
  end
147
162
 
163
+ if plan.steps.any? { |step| step.body.key?('source') }
164
+ msg = "The 'source' parameter for YAML plan upload steps is deprecated and will be removed "\
165
+ "in a future version of Bolt. Use the 'upload' parameter instead."
166
+ Bolt::Logger.deprecation_warning("Using 'source' parameter for YAML upload steps, not 'upload'", msg)
167
+ end
168
+
148
169
  plan_result = closure_scope.with_local_scope(args_hash) do |scope|
149
170
  plan.steps.each do |step|
150
171
  step_result = dispatch_step(scope, step)
@@ -12,7 +12,19 @@ module Bolt
12
12
  Set['name', 'description', 'target', 'targets']
13
13
  end
14
14
 
15
- STEP_KEYS = %w[command script task plan source destination eval resources].freeze
15
+ STEP_KEYS = %w[
16
+ command
17
+ destination
18
+ download
19
+ eval
20
+ message
21
+ plan
22
+ resources
23
+ script
24
+ source
25
+ task
26
+ upload
27
+ ].freeze
16
28
 
17
29
  def self.create(step_body, step_number)
18
30
  type_keys = (STEP_KEYS & step_body.keys)
@@ -22,8 +34,10 @@ module Bolt
22
34
  when 1
23
35
  type = type_keys.first
24
36
  else
25
- if type_keys.to_set == Set['source', 'destination']
37
+ if [Set['source', 'destination'], Set['upload', 'destination']].include?(type_keys.to_set)
26
38
  type = 'upload'
39
+ elsif type_keys.to_set == Set['download', 'destination']
40
+ type = 'download'
27
41
  else
28
42
  raise step_error("Multiple action keys detected: #{type_keys.inspect}", step_body['name'], step_number)
29
43
  end
@@ -89,6 +103,12 @@ module Bolt
89
103
  missing_keys -= ['targets']
90
104
  end
91
105
 
106
+ # Handle cases where upload step uses deprecated 'source' key instead of 'upload'
107
+ # TODO: Remove when 'source' is removed
108
+ if body.include?('source')
109
+ missing_keys -= ['upload']
110
+ end
111
+
92
112
  if missing_keys.any?
93
113
  error_message = "The #{step_type.inspect} step requires: #{missing_keys.to_a.inspect} key(s)"
94
114
  err = step_error(error_message, body['name'], step_number)
@@ -156,3 +176,5 @@ require 'bolt/pal/yaml_plan/step/resources'
156
176
  require 'bolt/pal/yaml_plan/step/script'
157
177
  require 'bolt/pal/yaml_plan/step/task'
158
178
  require 'bolt/pal/yaml_plan/step/upload'
179
+ require 'bolt/pal/yaml_plan/step/download'
180
+ require 'bolt/pal/yaml_plan/step/message'
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bolt
4
+ class PAL
5
+ class YamlPlan
6
+ class Step
7
+ class Download < Step
8
+ def self.allowed_keys
9
+ super + Set['download', 'destination']
10
+ end
11
+
12
+ def self.required_keys
13
+ Set['download', 'destination', 'targets']
14
+ end
15
+
16
+ def initialize(step_body)
17
+ super
18
+ @source = step_body['download']
19
+ @destination = step_body['destination']
20
+ end
21
+
22
+ def transpile
23
+ code = String.new(" ")
24
+ code << "$#{@name} = " if @name
25
+
26
+ fn = 'download_file'
27
+ args = [@source, @destination, @targets]
28
+ args << @description if @description
29
+
30
+ code << function_call(fn, args)
31
+
32
+ code << "\n"
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bolt
4
+ class PAL
5
+ class YamlPlan
6
+ class Step
7
+ class Message < Step
8
+ def self.allowed_keys
9
+ super + Set['message']
10
+ end
11
+
12
+ def self.required_keys
13
+ Set['message']
14
+ end
15
+
16
+ def initialize(step_body)
17
+ super
18
+ @message = step_body['message']
19
+ end
20
+
21
+ def transpile
22
+ code = String.new(" ")
23
+ code << function_call('out::message', [@message])
24
+ code << "\n"
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -6,16 +6,16 @@ module Bolt
6
6
  class Step
7
7
  class Upload < Step
8
8
  def self.allowed_keys
9
- super + Set['source', 'destination']
9
+ super + Set['source', 'destination', 'upload']
10
10
  end
11
11
 
12
12
  def self.required_keys
13
- Set['destination', 'source', 'targets']
13
+ Set['upload', 'destination', 'targets']
14
14
  end
15
15
 
16
16
  def initialize(step_body)
17
17
  super
18
- @source = step_body['source']
18
+ @source = step_body['upload'] || step_body['source']
19
19
  @destination = step_body['destination']
20
20
  end
21
21
 
@@ -21,10 +21,18 @@ module Bolt
21
21
  validate_path
22
22
 
23
23
  plan_object = parse_plan
24
+ param_descriptions = plan_object.parameters.map do |param|
25
+ str = String.new("# @param #{param.name}")
26
+ str << " #{param.description}" if param.description
27
+ str
28
+ end.join("\n")
24
29
 
25
- plan_string = String.new("# WARNING: This is an autogenerated plan. " \
26
- "It may not behave as expected.\n" \
27
- "plan #{plan_object.name}(")
30
+ plan_string = String.new('')
31
+ plan_string << "# #{plan_object.description}\n" if plan_object.description
32
+ plan_string << "# WARNING: This is an autogenerated plan. It may not behave as expected.\n"
33
+ plan_string << "#{param_descriptions}\n" unless param_descriptions.empty?
34
+
35
+ plan_string << "plan #{plan_object.name}("
28
36
  # Parameters are Bolt::PAL::YamlPlan::Parameter
29
37
  plan_object.parameters&.each_with_index do |param, i|
30
38
  plan_string << param.transpile
@@ -18,9 +18,9 @@ module Bolt
18
18
  end
19
19
 
20
20
  def resolve_reference(opts)
21
- STDERR.print("#{opts['message']}: ")
22
- value = STDIN.noecho(&:gets).to_s.chomp
23
- STDERR.puts
21
+ $stderr.print("#{opts['message']}: ")
22
+ value = $stdin.noecho(&:gets).to_s.chomp
23
+ $stderr.puts
24
24
 
25
25
  value
26
26
  end
@@ -85,7 +85,8 @@ module Bolt
85
85
 
86
86
  def resolve_facts(config, certname, target_data)
87
87
  Bolt::Util.walk_vals(config) do |value|
88
- if value.is_a?(String)
88
+ case value
89
+ when String
89
90
  if value == 'certname'
90
91
  certname
91
92
  else
@@ -94,7 +95,7 @@ module Bolt
94
95
  # If there's no fact data this will be nil
95
96
  data&.fetch('value', nil)
96
97
  end
97
- elsif value.is_a?(Array) || value.is_a?(Hash)
98
+ when Array, Hash
98
99
  value
99
100
  else
100
101
  raise FactLookupError.new(value, "fact lookups must be a string")
@@ -3,6 +3,7 @@
3
3
  require 'pathname'
4
4
  require 'bolt/config'
5
5
  require 'bolt/pal'
6
+ require 'bolt/module'
6
7
 
7
8
  module Bolt
8
9
  class Project
@@ -17,7 +18,7 @@ module Bolt
17
18
 
18
19
  attr_reader :path, :data, :config_file, :inventory_file, :modulepath, :hiera_config,
19
20
  :puppetfile, :rerunfile, :type, :resource_types, :warnings, :project_file,
20
- :deprecations
21
+ :deprecations, :downloads, :plans_path
21
22
 
22
23
  def self.default_project
23
24
  create_project(File.expand_path(File.join('~', '.puppetlabs', 'bolt')), 'user')
@@ -81,6 +82,8 @@ module Bolt
81
82
  @rerunfile = @path + '.rerun.json'
82
83
  @resource_types = @path + '.resource_types'
83
84
  @type = type
85
+ @downloads = @path + 'downloads'
86
+ @plans_path = @path + 'plans'
84
87
 
85
88
  tc = Bolt::Config::INVENTORY_OPTIONS.keys & raw_data.keys
86
89
  if tc.any?
@@ -137,10 +140,10 @@ module Bolt
137
140
 
138
141
  def validate
139
142
  if name
140
- name_regex = /^[a-z][a-z0-9_]*$/
141
- if name !~ name_regex
143
+ if name !~ Bolt::Module::MODULE_NAME_REGEX
142
144
  raise Bolt::ValidationError, <<~ERROR_STRING
143
- Invalid project name '#{name}' in bolt-project.yaml; project name must match #{name_regex.inspect}
145
+ Invalid project name '#{name}' in bolt-project.yaml; project name must begin with a lowercase letter
146
+ and can include lowercase letters, numbers, and underscores.
144
147
  ERROR_STRING
145
148
  elsif Dir.children(Bolt::PAL::BOLTLIB_PATH).include?(name)
146
149
  raise Bolt::ValidationError, "The project '#{name}' will not be loaded. The project name conflicts "\
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bolt
4
+ class ProjectMigrate
5
+ attr_reader :path, :project_file, :backup_dir, :outputter, :inventory_file, :config_file
6
+
7
+ # This init mostly makes testing easier
8
+ def initialize(path, outputter, configured_inventory = nil)
9
+ @path = Pathname.new(path).expand_path
10
+ @project_file = @path + 'bolt-project.yaml'
11
+ @config_file = @path + 'bolt.yaml'
12
+ @backup_dir = @path + '.bolt-bak'
13
+ @inventory_file = configured_inventory || @path + 'inventory.yaml'
14
+ @outputter = outputter
15
+ end
16
+
17
+ def migrate_project
18
+ inv_ok = inventory_1_to_2(inventory_file, outputter) if inventory_file.file?
19
+ config_ok = bolt_yaml_to_bolt_project(inventory_file, outputter)
20
+ inv_ok && config_ok ? 0 : 1
21
+ end
22
+
23
+ # This could be made public and used elsewhere if the need arises
24
+ private def backup_file(origin_path)
25
+ unless File.exist?(origin_path)
26
+ outputter.print_message "Could not find file #{origin_path}, skipping backup."
27
+ return
28
+ end
29
+
30
+ date = Time.new.strftime("%Y%m%d_%H%M%S%L")
31
+ FileUtils.mkdir_p(backup_dir)
32
+
33
+ filename = File.basename(origin_path)
34
+ backup_path = File.join(backup_dir, "#{filename}.#{date}.bak")
35
+
36
+ outputter.print_message "Backing up #{filename} from #{origin_path} to #{backup_path}"
37
+
38
+ begin
39
+ FileUtils.cp(origin_path, backup_path)
40
+ rescue StandardError => e
41
+ raise Bolt::FileError.new("#{e.message}; unable to create backup of #{filename}.", origin_path)
42
+ end
43
+ end
44
+
45
+ private def bolt_yaml_to_bolt_project(inventory_file, outputter)
46
+ # If bolt-project.yaml already exists
47
+ if project_file.file?
48
+ outputter.print_message "bolt-project.yaml already exists in Bolt "\
49
+ "project at #{path}. Skipping project file update."
50
+
51
+ # If bolt.yaml doesn't exist
52
+ elsif !config_file.file?
53
+ outputter.print_message "Could not find bolt.yaml in project at "\
54
+ "#{path}. Skipping project file update."
55
+
56
+ else
57
+ config_data = Bolt::Util.read_optional_yaml_hash(config_file, 'config')
58
+ transport_data, project_data = config_data.partition do |k, _|
59
+ Bolt::Config::INVENTORY_OPTIONS.keys.include?(k)
60
+ end.map(&:to_h)
61
+
62
+ if transport_data.any?
63
+ if File.exist?(inventory_file)
64
+ inventory_data = Bolt::Util.read_yaml_hash(inventory_file, 'inventory')
65
+ merged = Bolt::Util.deep_merge(transport_data, inventory_data['config'] || {})
66
+ inventory_data['config'] = merged
67
+ backup_file(inventory_file)
68
+ else
69
+ FileUtils.touch(inventory_file)
70
+ inventory_data = { 'config' => transport_data }
71
+ end
72
+
73
+ backup_file(config_file)
74
+
75
+ begin
76
+ outputter.print_message "Moving transportation configuration options "\
77
+ "'#{transport_data.keys.join(', ')}' from bolt.yaml to inventory.yaml"
78
+ File.write(inventory_file, inventory_data.to_yaml)
79
+ File.write(config_file, project_data.to_yaml)
80
+ rescue StandardError => e
81
+ raise Bolt::FileError.new("#{e.message}; unable to write inventory.", inventory_file)
82
+ end
83
+ end
84
+
85
+ outputter.print_message "Renaming bolt.yaml to bolt-project.yaml"
86
+ FileUtils.mv(config_file, project_file)
87
+ outputter.print_message "Successfully updated project. Please add a "\
88
+ "'name' key to bolt-project.yaml to use project-level tasks and plans. "\
89
+ "Learn more about projects by running 'bolt guide project'."
90
+ # If nothing errored, this succeeded
91
+ true
92
+ end
93
+ end
94
+
95
+ private def inventory_1_to_2(inventory_file, outputter)
96
+ data = Bolt::Util.read_yaml_hash(inventory_file, 'inventory')
97
+ data.delete('version') if data['version'] != 2
98
+ migrated = migrate_group(data)
99
+
100
+ ok = if migrated
101
+ backup_file(inventory_file)
102
+ File.write(inventory_file, data.to_yaml)
103
+ end
104
+
105
+ result = if migrated && ok
106
+ "Successfully migrated Bolt inventory to the latest version."
107
+ elsif !migrated
108
+ "Bolt inventory is already on the latest version. Skipping inventory update."
109
+ else
110
+ "Could not migrate Bolt inventory to the latest version. See "\
111
+ "https://puppet.com/docs/bolt/latest/inventory_file_v2.html to manually update."
112
+ end
113
+ outputter.print_message(result)
114
+ ok
115
+ end
116
+
117
+ # Walks an inventory hash and replaces all 'nodes' keys with 'targets' keys
118
+ # and all 'name' keys nested in a 'targets' hash with 'uri' keys. Data is
119
+ # modified in place.
120
+ private def migrate_group(group)
121
+ migrated = false
122
+ if group.key?('nodes')
123
+ migrated = true
124
+ targets = group['nodes'].map do |target|
125
+ target['uri'] = target.delete('name') if target.is_a?(Hash)
126
+ target
127
+ end
128
+ group.delete('nodes')
129
+ group['targets'] = targets
130
+ end
131
+ (group['groups'] || []).each do |subgroup|
132
+ migrated_group = migrate_group(subgroup)
133
+ migrated ||= migrated_group
134
+ end
135
+ migrated
136
+ end
137
+ end
138
+ end
@@ -96,6 +96,8 @@ module Bolt
96
96
  @http = HTTPClient.new
97
97
  @http.ssl_config.set_client_cert_file(@config.cert, @config.key) if @config.cert
98
98
  @http.ssl_config.add_trust_ca(@config.cacert)
99
+ @http.connect_timeout = @config.connect_timeout if @config.connect_timeout
100
+ @http.receive_timeout = @config.read_timeout if @config.read_timeout
99
101
 
100
102
  @http
101
103
  end
@@ -132,6 +132,22 @@ module Bolt
132
132
  end
133
133
  end
134
134
 
135
+ def connect_timeout
136
+ validate_timeout('connect_timeout')
137
+ @settings['connect_timeout']
138
+ end
139
+
140
+ def read_timeout
141
+ validate_timeout('read_timeout')
142
+ @settings['read_timeout']
143
+ end
144
+
145
+ def validate_timeout(timeout)
146
+ unless @settings[timeout].nil? || (@settings[timeout].is_a?(Integer) && @settings[timeout] > 0)
147
+ raise Bolt::PuppetDBError, "#{timeout} must be a positive integer, received #{@settings[timeout]}"
148
+ end
149
+ end
150
+
135
151
  def to_hash
136
152
  @settings.dup
137
153
  end