pdk 1.17.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +148 -11
  3. data/README.md +1 -1
  4. data/lib/pdk.rb +1 -1
  5. data/lib/pdk/cli.rb +7 -1
  6. data/lib/pdk/cli/convert.rb +7 -9
  7. data/lib/pdk/cli/env.rb +52 -0
  8. data/lib/pdk/cli/exec/command.rb +11 -1
  9. data/lib/pdk/cli/new.rb +2 -0
  10. data/lib/pdk/cli/new/class.rb +2 -1
  11. data/lib/pdk/cli/new/defined_type.rb +2 -1
  12. data/lib/pdk/cli/new/fact.rb +29 -0
  13. data/lib/pdk/cli/new/function.rb +29 -0
  14. data/lib/pdk/cli/new/provider.rb +2 -1
  15. data/lib/pdk/cli/new/task.rb +2 -1
  16. data/lib/pdk/cli/new/test.rb +2 -1
  17. data/lib/pdk/cli/new/transport.rb +2 -1
  18. data/lib/pdk/cli/release.rb +1 -1
  19. data/lib/pdk/cli/release/publish.rb +11 -1
  20. data/lib/pdk/cli/remove.rb +20 -0
  21. data/lib/pdk/cli/remove/config.rb +80 -0
  22. data/lib/pdk/cli/set.rb +20 -0
  23. data/lib/pdk/cli/set/config.rb +119 -0
  24. data/lib/pdk/cli/update.rb +6 -8
  25. data/lib/pdk/cli/util.rb +1 -0
  26. data/lib/pdk/cli/util/option_validator.rb +6 -0
  27. data/lib/pdk/cli/util/update_manager_printer.rb +82 -0
  28. data/lib/pdk/config.rb +96 -13
  29. data/lib/pdk/context.rb +8 -5
  30. data/lib/pdk/generate/defined_type.rb +25 -32
  31. data/lib/pdk/generate/fact.rb +25 -0
  32. data/lib/pdk/generate/function.rb +48 -0
  33. data/lib/pdk/generate/module.rb +11 -10
  34. data/lib/pdk/generate/provider.rb +15 -64
  35. data/lib/pdk/generate/puppet_class.rb +25 -31
  36. data/lib/pdk/generate/puppet_object.rb +83 -187
  37. data/lib/pdk/generate/task.rb +28 -46
  38. data/lib/pdk/generate/transport.rb +20 -74
  39. data/lib/pdk/module.rb +1 -1
  40. data/lib/pdk/module/convert.rb +43 -23
  41. data/lib/pdk/module/metadata.rb +6 -2
  42. data/lib/pdk/module/release.rb +8 -2
  43. data/lib/pdk/module/update.rb +7 -11
  44. data/lib/pdk/module/update_manager.rb +7 -0
  45. data/lib/pdk/report.rb +3 -3
  46. data/lib/pdk/report/event.rb +8 -2
  47. data/lib/pdk/template.rb +59 -0
  48. data/lib/pdk/template/fetcher.rb +98 -0
  49. data/lib/pdk/template/fetcher/git.rb +85 -0
  50. data/lib/pdk/template/fetcher/local.rb +28 -0
  51. data/lib/pdk/template/renderer.rb +96 -0
  52. data/lib/pdk/template/renderer/v1.rb +25 -0
  53. data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -0
  54. data/lib/pdk/template/renderer/v1/renderer.rb +132 -0
  55. data/lib/pdk/template/renderer/v1/template_file.rb +102 -0
  56. data/lib/pdk/template/template_dir.rb +67 -0
  57. data/lib/pdk/tests/unit.rb +8 -1
  58. data/lib/pdk/util.rb +4 -35
  59. data/lib/pdk/util/bundler.rb +1 -1
  60. data/lib/pdk/util/changelog_generator.rb +20 -3
  61. data/lib/pdk/util/json_finder.rb +85 -0
  62. data/lib/pdk/util/puppet_strings.rb +3 -3
  63. data/lib/pdk/util/puppet_version.rb +2 -2
  64. data/lib/pdk/util/ruby_version.rb +5 -1
  65. data/lib/pdk/util/template_uri.rb +9 -11
  66. data/lib/pdk/util/vendored_file.rb +1 -2
  67. data/lib/pdk/validate.rb +17 -10
  68. data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -0
  69. data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -0
  70. data/lib/pdk/validate/invokable_validator.rb +8 -4
  71. data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +1 -1
  72. data/lib/pdk/validate/validator.rb +7 -0
  73. data/lib/pdk/validate/validator_group.rb +1 -0
  74. data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +2 -2
  75. data/lib/pdk/version.rb +1 -1
  76. data/locales/pdk.pot +356 -228
  77. metadata +65 -28
  78. data/lib/pdk/module/template_dir.rb +0 -115
  79. data/lib/pdk/module/template_dir/base.rb +0 -268
  80. data/lib/pdk/module/template_dir/git.rb +0 -91
  81. data/lib/pdk/module/template_dir/local.rb +0 -21
  82. data/lib/pdk/template_file.rb +0 -96
@@ -0,0 +1,85 @@
1
+ module PDK
2
+ module Util
3
+ # Processes a given string, looking for JSON objects and parsing them.
4
+ #
5
+ # @example A string with a JSON object and some junk characters
6
+ # PDK::Util::JSONFinder.new('foo{"bar":1}').objects
7
+ # => [{ 'bar' => 1 }]
8
+ #
9
+ # @example A string with mulitple JSON objects
10
+ # PDK::Util::JSONFinder.new('foo{"bar":1}baz{"gronk":2}').objects
11
+ # => [{ 'bar' => 1 }, { 'gronk' => 2 }]
12
+ class JSONFinder
13
+ # Creates a new instance of PDK::Util::JSONFinder.
14
+ #
15
+ # @param string [String] the string to find JSON objects inside of.
16
+ #
17
+ # @return [PDK::Util::JSONFinder] a new PDK::Util::JSONFinder object.
18
+ def initialize(string)
19
+ require 'strscan'
20
+
21
+ @scanner = StringScanner.new(string)
22
+ end
23
+
24
+ # Returns the parsed JSON objects from the string.
25
+ #
26
+ # @return [Array[Hash]] the parsed JSON objects present in the string.
27
+ def objects
28
+ return @objects unless @objects.nil?
29
+
30
+ require 'json'
31
+
32
+ until @scanner.eos?
33
+ @scanner.getch until @scanner.peek(1) == '{' || @scanner.eos?
34
+
35
+ (@objects ||= []) << begin
36
+ JSON.parse(read_object(true) || '')
37
+ rescue JSON::ParserError
38
+ nil
39
+ end
40
+ end
41
+
42
+ return [] if @objects.nil?
43
+ @objects = @objects.compact
44
+ end
45
+
46
+ private
47
+
48
+ # Recursively process the string to extract a complete JSON object.
49
+ #
50
+ # @param new_object [Boolean] Set to true if processing a new object to
51
+ # capture the opening brace. Set to false if being called recursively
52
+ # where the opening brace has already been captured.
53
+ #
54
+ # @return [String] The matched substring containing a JSON object.
55
+ def read_object(new_object = false)
56
+ matched_text = new_object ? @scanner.getch : ''
57
+
58
+ until @scanner.eos?
59
+ text = @scanner.scan_until(%r{(?:(?<!\\)"|\{|\})})
60
+ unless text
61
+ @scanner.terminate
62
+ return nil
63
+ end
64
+ matched_text += text
65
+
66
+ case @scanner.matched
67
+ when '}'
68
+ break
69
+ when '"'
70
+ text = @scanner.scan_until(%r{(?<!\\)"})
71
+ unless text
72
+ @scanner.terminate
73
+ return nil
74
+ end
75
+ matched_text += text
76
+ else
77
+ matched_text += read_object
78
+ end
79
+ end
80
+
81
+ matched_text
82
+ end
83
+ end
84
+ end
85
+ end
@@ -92,12 +92,12 @@ module PDK
92
92
  require 'pdk/generate'
93
93
 
94
94
  generators = PDK::Generate.generators.select do |gen|
95
- gen.respond_to?(:puppet_strings_type) && !gen.puppet_strings_type.nil?
95
+ gen.const_defined?(:PUPPET_STRINGS_TYPE) && !gen::PUPPET_STRINGS_TYPE.nil?
96
96
  end
97
97
 
98
98
  known_objects = generate_hash
99
99
 
100
- generators.map { |gen| [gen, known_objects[gen.puppet_strings_type]] }.reject do |_, obj|
100
+ generators.map { |gen| [gen, known_objects[gen::PUPPET_STRINGS_TYPE]] }.reject do |_, obj|
101
101
  obj.nil? || obj.empty?
102
102
  end
103
103
  end
@@ -117,7 +117,7 @@ module PDK
117
117
  require 'pdk/generate'
118
118
 
119
119
  PDK::Generate.generators.find do |gen|
120
- gen.respond_to?(:puppet_strings_type) && gen.puppet_strings_type == type
120
+ gen.const_defined?(:PUPPET_STRINGS_TYPE) && gen::PUPPET_STRINGS_TYPE == type
121
121
  end
122
122
  end
123
123
  end
@@ -17,7 +17,7 @@ module PDK
17
17
 
18
18
  PE_VERSIONS_URL = 'https://forgeapi.puppet.com/private/versions/pe'.freeze
19
19
  DEFAULT_PUPPET_DEV_URL = 'https://github.com/puppetlabs/puppet'.freeze
20
- DEFAULT_PUPPET_DEV_BRANCH = 'master'.freeze
20
+ DEFAULT_PUPPET_DEV_BRANCH = 'main'.freeze
21
21
 
22
22
  def puppet_dev_env
23
23
  require 'pdk/util/ruby_version'
@@ -80,7 +80,7 @@ module PDK
80
80
  end
81
81
 
82
82
  # Reset local repo to latest
83
- reset_result = PDK::Util::Git.git('-C', puppet_dev_path, 'reset', '--hard', 'origin/master')
83
+ reset_result = PDK::Util::Git.git('-C', puppet_dev_path, 'reset', '--hard', "origin/#{DEFAULT_PUPPET_DEV_BRANCH}")
84
84
 
85
85
  @puppet_dev_fetched = true
86
86
  return if reset_result[:exit_code].zero?
@@ -60,7 +60,11 @@ module PDK
60
60
  #
61
61
  # PDK::Util::PuppetVersion.find_gem_for('5.5.10')[:ruby_version]
62
62
  #
63
- PDK::Util::PuppetVersion.latest_available[:ruby_version]
63
+ # For using the latest puppet gem:
64
+ # PDK::Util::PuppetVersion.latest_available[:ruby_version]
65
+ #
66
+ # Temporarily lock to Ruby 2.5.x as default until 2.7.x ecosystem is sorted
67
+ versions.keys.detect { |v| v =~ %r{^2\.5} }
64
68
  else
65
69
  # TODO: may not be a safe assumption that highest available version should be default
66
70
  # WARNING Do NOT use PDK::Util::PuppetVersion.*** methods as it can recurse into this
@@ -26,13 +26,13 @@ module PDK
26
26
  #
27
27
  # file:///c:/foo (git clone location)
28
28
  # c:/foo (shell paths)
29
- # file:///c:/foo#master (only for metadata)
30
- # c:/foo#master (only for metadata)
29
+ # file:///c:/foo#main (only for metadata)
30
+ # c:/foo#main (only for metadata)
31
31
  #
32
32
  # non output formats:
33
33
  #
34
34
  # /c:/foo (internal use only)
35
- # /c:/foo#master (internal use only)
35
+ # /c:/foo#main (internal use only)
36
36
  #
37
37
  def initialize(opts_or_uri)
38
38
  require 'addressable'
@@ -192,7 +192,6 @@ module PDK
192
192
  # directory, and :allow_fallback contains a Boolean that specifies if
193
193
  # the lookup process should proceed to the next template directory if
194
194
  # the template file is not in this template directory.
195
- #
196
195
  def self.templates(opts)
197
196
  require 'pdk/answer_file'
198
197
  require 'pdk/util'
@@ -242,11 +241,11 @@ module PDK
242
241
  require 'pdk/util'
243
242
  require 'pdk/version'
244
243
 
245
- return 'master' if PDK::Util.development_mode?
244
+ return 'main' if PDK::Util.development_mode?
246
245
  return PDK::TEMPLATE_REF if uri.nil?
247
246
 
248
247
  uri = new(uri) unless uri.is_a?(self)
249
- uri.default? ? PDK::TEMPLATE_REF : 'master'
248
+ uri.default? ? PDK::TEMPLATE_REF : 'main'
250
249
  end
251
250
 
252
251
  # @returns Addressable::URI
@@ -261,21 +260,20 @@ module PDK
261
260
  found_template[:uri]
262
261
  end
263
262
 
264
- def self.valid_template?(template)
263
+ def self.valid_template?(template, context = PDK.context)
265
264
  require 'addressable'
266
265
 
267
266
  return false if template.nil? || !template.is_a?(Hash)
268
267
  return false if template[:uri].nil? || !template[:uri].is_a?(Addressable::URI)
269
268
 
270
269
  return true if PDK::Util::Git.repo?(bare_uri(template[:uri]))
271
-
272
270
  path = human_readable(template[:uri].path)
273
271
  if PDK::Util::Filesystem.directory?(path)
274
272
  # We know that it's not a git repository, but it's a valid path on disk
275
273
  begin
276
- PDK::Module::TemplateDir.validate_module_template!(path)
277
- return true
278
- rescue ArgumentError
274
+ renderer = PDK::Template::Renderer.instance(path, template[:uri], context)
275
+ return !renderer.nil?
276
+ rescue StandardError
279
277
  nil
280
278
  end
281
279
  end
@@ -55,8 +55,7 @@ module PDK
55
55
  uri = URI.parse(url)
56
56
  http = Net::HTTP.new(uri.host, uri.port)
57
57
  http.use_ssl = true
58
- # TODO: Get rid of this, possible workaround:
59
- # https://github.com/glennsarti/dev-tools/blob/master/RubyCerts.ps1
58
+ # TODO: Get rid of this
60
59
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE if Gem.win_platform?
61
60
  request = Net::HTTP::Get.new(uri.request_uri)
62
61
  response = http.request(request)
data/lib/pdk/validate.rb CHANGED
@@ -8,15 +8,22 @@ module PDK
8
8
  autoload :Validator, 'pdk/validate/validator'
9
9
  autoload :ValidatorGroup, 'pdk/validate/validator_group'
10
10
 
11
+ module ControlRepo
12
+ autoload :ControlRepoValidatorGroup, 'pdk/validate/control_repo/control_repo_validator_group'
13
+ autoload :EnvironmentConfValidator, 'pdk/validate/control_repo/environment_conf_validator'
14
+ end
15
+
11
16
  module Metadata
12
17
  autoload :MetadataJSONLintValidator, 'pdk/validate/metadata/metadata_json_lint_validator'
13
18
  autoload :MetadataSyntaxValidator, 'pdk/validate/metadata/metadata_syntax_validator'
14
19
  autoload :MetadataValidatorGroup, 'pdk/validate/metadata/metadata_validator_group'
15
20
  end
16
21
 
17
- module YAML
18
- autoload :YAMLSyntaxValidator, 'pdk/validate/yaml/yaml_syntax_validator'
19
- autoload :YAMLValidatorGroup, 'pdk/validate/yaml/yaml_validator_group'
22
+ module Puppet
23
+ autoload :PuppetEPPValidator, 'pdk/validate/puppet/puppet_epp_validator'
24
+ autoload :PuppetLintValidator, 'pdk/validate/puppet/puppet_lint_validator'
25
+ autoload :PuppetSyntaxValidator, 'pdk/validate/puppet/puppet_syntax_validator'
26
+ autoload :PuppetValidatorGroup, 'pdk/validate/puppet/puppet_validator_group'
20
27
  end
21
28
 
22
29
  module Ruby
@@ -25,16 +32,14 @@ module PDK
25
32
  end
26
33
 
27
34
  module Tasks
28
- autoload :TasksNameValidator, 'pdk/validate/tasks/tasks_name_validator'
29
35
  autoload :TasksMetadataLintValidator, 'pdk/validate/tasks/tasks_metadata_lint_validator'
36
+ autoload :TasksNameValidator, 'pdk/validate/tasks/tasks_name_validator'
30
37
  autoload :TasksValidatorGroup, 'pdk/validate/tasks/tasks_validator_group'
31
38
  end
32
39
 
33
- module Puppet
34
- autoload :PuppetEPPValidator, 'pdk/validate/puppet/puppet_epp_validator'
35
- autoload :PuppetLintValidator, 'pdk/validate/puppet/puppet_lint_validator'
36
- autoload :PuppetSyntaxValidator, 'pdk/validate/puppet/puppet_syntax_validator'
37
- autoload :PuppetValidatorGroup, 'pdk/validate/puppet/puppet_validator_group'
40
+ module YAML
41
+ autoload :YAMLSyntaxValidator, 'pdk/validate/yaml/yaml_syntax_validator'
42
+ autoload :YAMLValidatorGroup, 'pdk/validate/yaml/yaml_validator_group'
38
43
  end
39
44
 
40
45
  def self.validators
@@ -47,8 +52,9 @@ module PDK
47
52
 
48
53
  # @api private
49
54
  def self.validator_hash
50
- # TODO: This isn't the most performant... But with only 5 items, it's fine
55
+ # TODO: This isn't the most performant... But with only 6 items, it's fine
51
56
  @validator_hash ||= [
57
+ ControlRepo::ControlRepoValidatorGroup,
52
58
  Metadata::MetadataValidatorGroup,
53
59
  Puppet::PuppetValidatorGroup,
54
60
  Ruby::RubyValidatorGroup,
@@ -60,6 +66,7 @@ module PDK
60
66
  def self.invoke_validators_by_name(context, names, parallel = false, options = {})
61
67
  instances = names.select { |name| validator_names.include?(name) }
62
68
  .map { |name| validator_hash[name].new(context, options) }
69
+ .select { |instance| instance.valid_in_context? }
63
70
  .each { |instance| instance.prepare_invoke! }
64
71
  report = PDK::Report.new
65
72
 
@@ -0,0 +1,23 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Validate
5
+ module ControlRepo
6
+ class ControlRepoValidatorGroup < ValidatorGroup
7
+ def name
8
+ 'control-repo'
9
+ end
10
+
11
+ def valid_in_context?
12
+ context.is_a?(PDK::Context::ControlRepo)
13
+ end
14
+
15
+ def validators
16
+ [
17
+ EnvironmentConfValidator,
18
+ ].freeze
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,98 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Validate
5
+ module ControlRepo
6
+ class EnvironmentConfValidator < InternalRubyValidator
7
+ ALLOWED_SETTINGS = %w[modulepath manifest config_version environment_timeout].freeze
8
+
9
+ def name
10
+ 'environment-conf'
11
+ end
12
+
13
+ def valid_in_context?
14
+ context.is_a?(PDK::Context::ControlRepo)
15
+ end
16
+
17
+ def pattern
18
+ ['environment.conf']
19
+ end
20
+
21
+ def spinner_text
22
+ _('Checking Puppet Environment settings (%{patterns}).') % {
23
+ patterns: pattern.join(' '),
24
+ }
25
+ end
26
+
27
+ def validate_target(report, target)
28
+ unless PDK::Util::Filesystem.readable?(target)
29
+ report.add_event(
30
+ file: target,
31
+ source: name,
32
+ state: :failure,
33
+ severity: 'error',
34
+ message: _('Could not be read.'),
35
+ )
36
+ return 1
37
+ end
38
+
39
+ is_valid = true
40
+ begin
41
+ env_conf = PDK::ControlRepo.environment_conf_as_config(target)
42
+
43
+ env_conf.resolve.each do |setting_name, setting_value|
44
+ # Remove the 'environment.' setting_name prefix
45
+ setting_name = setting_name.slice(12..-1)
46
+ next if ALLOWED_SETTINGS.include?(setting_name)
47
+ # A hash indicates that the ini file has a section in it.
48
+ message = if setting_value.is_a?(Hash)
49
+ _("Invalid section '%{name}'") % { name: setting_name }
50
+ else
51
+ _("Invalid setting '%{name}'") % { name: setting_name }
52
+ end
53
+
54
+ report.add_event(
55
+ file: target,
56
+ source: name,
57
+ state: :failure,
58
+ severity: 'error',
59
+ message: message,
60
+ )
61
+ is_valid = false
62
+ end
63
+
64
+ timeout = env_conf.fetch('environment_timeout', nil)
65
+ unless timeout.nil? || timeout == '0' || timeout == 'unlimited'
66
+ report.add_event(
67
+ file: target,
68
+ source: name,
69
+ state: :failure,
70
+ severity: 'error',
71
+ message: _("environment_timeout is set to '%{timeout}' but should be 0, 'unlimited' or not set.") % { timeout: timeout },
72
+ )
73
+ is_valid = false
74
+ end
75
+
76
+ return 1 unless is_valid
77
+ report.add_event(
78
+ file: target,
79
+ source: name,
80
+ state: :passed,
81
+ severity: 'ok',
82
+ )
83
+ return 0
84
+ rescue StandardError => e
85
+ report.add_event(
86
+ file: target,
87
+ source: name,
88
+ state: :failure,
89
+ severity: 'error',
90
+ message: e.message,
91
+ )
92
+ return 1
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -19,9 +19,8 @@ module PDK
19
19
  :once
20
20
  end
21
21
 
22
- # Whether this Validator can be invoked in this context. By default any Validator can work in any Context, except ::None
23
- # @return [Boolean]
24
- # @abstract
22
+ # Whether this Validator can be invoked in this context. By default any InvokableValidator can work in any Context, except ::None
23
+ # @see PDK::Validate::Validator
25
24
  def valid_in_context?
26
25
  !context.is_a?(PDK::Context::None)
27
26
  end
@@ -87,7 +86,12 @@ module PDK
87
86
  ignore_list = ignore_pathspec
88
87
  target_list = target_list.reject { |file| ignore_list.match(file) }
89
88
 
90
- skipped << target if target_list.flatten.empty?
89
+ if target_list.flatten.empty?
90
+ PDK.logger.info(_('Validator \'%{validator}\' skipped for \'%{target}\'. No files matching \'%{pattern}\' found to validate.') % { validator: name, target: target, pattern: pattern })
91
+
92
+ skipped << target
93
+ end
94
+
91
95
  target_list
92
96
  elsif PDK::Util::Filesystem.file?(target)
93
97
  if Array(pattern).include? target
@@ -49,7 +49,7 @@ module PDK
49
49
  # Need to set the JSON Parser and State Generator to the Native one to be
50
50
  # compatible with the multi_json adapter.
51
51
  JSON.parser = JSON::Ext::Parser if defined?(JSON::Ext::Parser)
52
- JSON.generator = JSON::Ext::Generator
52
+ JSON.generator = JSON::Ext::Generator if defined?(JSON::Ext::Generator)
53
53
 
54
54
  begin
55
55
  errors = JSON::Validator.fully_validate(schema_file, PDK::Util::Filesystem.read_file(target)) || []
@@ -40,6 +40,13 @@ module PDK
40
40
  @prepared = false
41
41
  end
42
42
 
43
+ # Whether this Validator can be invoked in this context. By default any Validator can work in any Context
44
+ # @return [Boolean]
45
+ # @abstract
46
+ def valid_in_context?
47
+ true
48
+ end
49
+
43
50
  # Returns the text used for the spinner to display to the user while invoking
44
51
  #
45
52
  # @return [String]