pdk 1.16.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +167 -11
  3. data/README.md +1 -1
  4. data/lib/pdk.rb +26 -19
  5. data/lib/pdk/answer_file.rb +2 -93
  6. data/lib/pdk/cli.rb +8 -6
  7. data/lib/pdk/cli/config.rb +3 -1
  8. data/lib/pdk/cli/config/get.rb +3 -1
  9. data/lib/pdk/cli/convert.rb +7 -9
  10. data/lib/pdk/cli/env.rb +52 -0
  11. data/lib/pdk/cli/exec/command.rb +13 -0
  12. data/lib/pdk/cli/exec_group.rb +78 -43
  13. data/lib/pdk/cli/get.rb +20 -0
  14. data/lib/pdk/cli/get/config.rb +24 -0
  15. data/lib/pdk/cli/new.rb +2 -0
  16. data/lib/pdk/cli/new/class.rb +2 -1
  17. data/lib/pdk/cli/new/defined_type.rb +2 -1
  18. data/lib/pdk/cli/new/fact.rb +29 -0
  19. data/lib/pdk/cli/new/function.rb +29 -0
  20. data/lib/pdk/cli/new/provider.rb +2 -1
  21. data/lib/pdk/cli/new/task.rb +2 -1
  22. data/lib/pdk/cli/new/test.rb +2 -1
  23. data/lib/pdk/cli/new/transport.rb +2 -1
  24. data/lib/pdk/cli/release/publish.rb +11 -1
  25. data/lib/pdk/cli/remove.rb +20 -0
  26. data/lib/pdk/cli/remove/config.rb +80 -0
  27. data/lib/pdk/cli/set.rb +20 -0
  28. data/lib/pdk/cli/set/config.rb +119 -0
  29. data/lib/pdk/cli/update.rb +6 -8
  30. data/lib/pdk/cli/util.rb +7 -3
  31. data/lib/pdk/cli/util/option_validator.rb +6 -0
  32. data/lib/pdk/cli/util/update_manager_printer.rb +82 -0
  33. data/lib/pdk/cli/validate.rb +26 -44
  34. data/lib/pdk/config.rb +264 -7
  35. data/lib/pdk/config/ini_file.rb +183 -0
  36. data/lib/pdk/config/ini_file_setting.rb +39 -0
  37. data/lib/pdk/config/namespace.rb +25 -5
  38. data/lib/pdk/config/setting.rb +3 -2
  39. data/lib/pdk/context.rb +99 -0
  40. data/lib/pdk/context/control_repo.rb +60 -0
  41. data/lib/pdk/context/module.rb +28 -0
  42. data/lib/pdk/context/none.rb +22 -0
  43. data/lib/pdk/control_repo.rb +40 -0
  44. data/lib/pdk/generate/defined_type.rb +25 -32
  45. data/lib/pdk/generate/fact.rb +25 -0
  46. data/lib/pdk/generate/function.rb +48 -0
  47. data/lib/pdk/generate/module.rb +14 -17
  48. data/lib/pdk/generate/provider.rb +15 -64
  49. data/lib/pdk/generate/puppet_class.rb +25 -31
  50. data/lib/pdk/generate/puppet_object.rb +83 -187
  51. data/lib/pdk/generate/task.rb +28 -46
  52. data/lib/pdk/generate/transport.rb +20 -74
  53. data/lib/pdk/module.rb +1 -1
  54. data/lib/pdk/module/convert.rb +43 -23
  55. data/lib/pdk/module/metadata.rb +6 -2
  56. data/lib/pdk/module/release.rb +3 -9
  57. data/lib/pdk/module/update.rb +7 -11
  58. data/lib/pdk/module/update_manager.rb +7 -0
  59. data/lib/pdk/report.rb +3 -3
  60. data/lib/pdk/report/event.rb +8 -2
  61. data/lib/pdk/template.rb +59 -0
  62. data/lib/pdk/template/fetcher.rb +98 -0
  63. data/lib/pdk/template/fetcher/git.rb +85 -0
  64. data/lib/pdk/template/fetcher/local.rb +28 -0
  65. data/lib/pdk/template/renderer.rb +96 -0
  66. data/lib/pdk/template/renderer/v1.rb +25 -0
  67. data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -0
  68. data/lib/pdk/template/renderer/v1/renderer.rb +132 -0
  69. data/lib/pdk/template/renderer/v1/template_file.rb +102 -0
  70. data/lib/pdk/template/template_dir.rb +67 -0
  71. data/lib/pdk/tests/unit.rb +8 -1
  72. data/lib/pdk/util.rb +38 -39
  73. data/lib/pdk/util/bundler.rb +2 -1
  74. data/lib/pdk/util/changelog_generator.rb +11 -2
  75. data/lib/pdk/util/json_finder.rb +84 -0
  76. data/lib/pdk/util/puppet_strings.rb +3 -3
  77. data/lib/pdk/util/puppet_version.rb +2 -2
  78. data/lib/pdk/util/ruby_version.rb +5 -1
  79. data/lib/pdk/util/template_uri.rb +13 -14
  80. data/lib/pdk/util/vendored_file.rb +1 -2
  81. data/lib/pdk/validate.rb +79 -25
  82. data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -0
  83. data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -0
  84. data/lib/pdk/validate/external_command_validator.rb +208 -0
  85. data/lib/pdk/validate/internal_ruby_validator.rb +100 -0
  86. data/lib/pdk/validate/invokable_validator.rb +220 -0
  87. data/lib/pdk/validate/metadata/metadata_json_lint_validator.rb +86 -0
  88. data/lib/pdk/validate/metadata/metadata_syntax_validator.rb +78 -0
  89. data/lib/pdk/validate/metadata/metadata_validator_group.rb +20 -0
  90. data/lib/pdk/validate/puppet/puppet_epp_validator.rb +133 -0
  91. data/lib/pdk/validate/puppet/puppet_lint_validator.rb +66 -0
  92. data/lib/pdk/validate/puppet/puppet_syntax_validator.rb +137 -0
  93. data/lib/pdk/validate/puppet/puppet_validator_group.rb +21 -0
  94. data/lib/pdk/validate/ruby/ruby_rubocop_validator.rb +80 -0
  95. data/lib/pdk/validate/ruby/ruby_validator_group.rb +19 -0
  96. data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +88 -0
  97. data/lib/pdk/validate/tasks/tasks_name_validator.rb +50 -0
  98. data/lib/pdk/validate/tasks/tasks_validator_group.rb +20 -0
  99. data/lib/pdk/validate/validator.rb +118 -0
  100. data/lib/pdk/validate/validator_group.rb +104 -0
  101. data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +95 -0
  102. data/lib/pdk/validate/yaml/yaml_validator_group.rb +19 -0
  103. data/lib/pdk/version.rb +1 -1
  104. data/locales/pdk.pot +477 -313
  105. metadata +77 -35
  106. data/lib/pdk/module/template_dir.rb +0 -115
  107. data/lib/pdk/module/template_dir/base.rb +0 -268
  108. data/lib/pdk/module/template_dir/git.rb +0 -91
  109. data/lib/pdk/module/template_dir/local.rb +0 -21
  110. data/lib/pdk/template_file.rb +0 -96
  111. data/lib/pdk/validate/base_validator.rb +0 -215
  112. data/lib/pdk/validate/metadata/metadata_json_lint.rb +0 -82
  113. data/lib/pdk/validate/metadata/metadata_syntax.rb +0 -111
  114. data/lib/pdk/validate/metadata_validator.rb +0 -26
  115. data/lib/pdk/validate/puppet/puppet_epp.rb +0 -135
  116. data/lib/pdk/validate/puppet/puppet_lint.rb +0 -64
  117. data/lib/pdk/validate/puppet/puppet_syntax.rb +0 -135
  118. data/lib/pdk/validate/puppet_validator.rb +0 -26
  119. data/lib/pdk/validate/ruby/rubocop.rb +0 -72
  120. data/lib/pdk/validate/ruby_validator.rb +0 -26
  121. data/lib/pdk/validate/tasks/metadata_lint.rb +0 -130
  122. data/lib/pdk/validate/tasks/name.rb +0 -90
  123. data/lib/pdk/validate/tasks_validator.rb +0 -29
  124. data/lib/pdk/validate/yaml/syntax.rb +0 -125
  125. data/lib/pdk/validate/yaml_validator.rb +0 -28
@@ -44,7 +44,7 @@ module PDK
44
44
  # If there are missing dependencies after updating the lockfile, let `bundle install`
45
45
  # go out and get them. If the specified puppet gem version points to a remote location
46
46
  # or local filepath, then run bundle install as well.
47
- if !bundle.installed? || (gem_overrides[:puppet] && gem_overrides[:puppet].start_with?('file://', 'git://', 'https://'))
47
+ if !bundle.installed?(gem_overrides) || (gem_overrides[:puppet] && gem_overrides[:puppet].start_with?('file://', 'git://', 'https://'))
48
48
  bundle.install!(gem_overrides)
49
49
  end
50
50
 
@@ -210,6 +210,7 @@ module PDK
210
210
  end
211
211
 
212
212
  def binstubs!(gems)
213
+ raise PDK::CLI::FatalError, _('Unable to install requested binstubs as the Gemfile is missing') if gemfile.nil?
213
214
  binstub_dir = File.join(File.dirname(gemfile), 'bin')
214
215
  return true if gems.all? { |gem| PDK::Util::Filesystem.file?(File.join(binstub_dir, gem)) }
215
216
 
@@ -5,11 +5,20 @@ module PDK
5
5
  module ChangelogGenerator
6
6
  # Taken from the version regex in https://forgeapi.puppet.com/schemas/module.json
7
7
  VERSION_REGEX = %r{^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(\+[0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*)?$}
8
+ GEM = 'github_changelog_generator'.freeze
8
9
 
9
10
  # Raises if the github_changelog_generator is not available
10
11
  def self.github_changelog_generator_available!
11
- require 'bundler'
12
- raise PDK::CLI::ExitWithError, _('Unable to generate the changelog as the github_changelog_generator gem is not installed') unless Bundler.rubygems.find_name('github_changelog_generator').any?
12
+ check_command = PDK::CLI::Exec::InteractiveCommand.new(PDK::CLI::Exec.bundle_bin, 'show', 'github_changelog_generator')
13
+ check_command.context = :module
14
+
15
+ result = check_command.execute!
16
+
17
+ return if result[:exit_code].zero?
18
+
19
+ raise PDK::CLI::ExitWithError, _(
20
+ 'Unable to generate the changelog as the %{gem} gem is not included in this module\'s Gemfile',
21
+ ) % { gem: GEM }
13
22
  end
14
23
 
15
24
  # Runs the Changelog Generator gem (in the module's context) to automatically create a CHANGLELOG.MD file
@@ -0,0 +1,84 @@
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
+ @objects = @objects.compact
43
+ end
44
+
45
+ private
46
+
47
+ # Recursively process the string to extract a complete JSON object.
48
+ #
49
+ # @param new_object [Boolean] Set to true if processing a new object to
50
+ # capture the opening brace. Set to false if being called recursively
51
+ # where the opening brace has already been captured.
52
+ #
53
+ # @return [String] The matched substring containing a JSON object.
54
+ def read_object(new_object = false)
55
+ matched_text = new_object ? @scanner.getch : ''
56
+
57
+ until @scanner.eos?
58
+ text = @scanner.scan_until(%r{(?:(?<!\\)"|\{|\})})
59
+ unless text
60
+ @scanner.terminate
61
+ return nil
62
+ end
63
+ matched_text += text
64
+
65
+ case @scanner.matched
66
+ when '}'
67
+ break
68
+ when '"'
69
+ text = @scanner.scan_until(%r{(?<!\\)"})
70
+ unless text
71
+ @scanner.terminate
72
+ return nil
73
+ end
74
+ matched_text += text
75
+ else
76
+ matched_text += read_object
77
+ end
78
+ end
79
+
80
+ matched_text
81
+ end
82
+ end
83
+ end
84
+ 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'
@@ -218,10 +217,11 @@ module PDK
218
217
  else
219
218
  nil
220
219
  end
221
- answers_uri = if [PACKAGED_TEMPLATE_KEYWORD, DEPRECATED_TEMPLATE_URL].include?(PDK.answers['template-url'])
220
+ default_template_url = PDK.config.get_within_scopes('module_defaults.template-url')
221
+ answers_uri = if [PACKAGED_TEMPLATE_KEYWORD, DEPRECATED_TEMPLATE_URL].include?(default_template_url)
222
222
  Addressable::URI.parse(default_template_uri)
223
- elsif PDK.answers['template-url']
224
- new(uri_safe(PDK.answers['template-url'])).uri
223
+ elsif default_template_url
224
+ new(uri_safe(default_template_url)).uri
225
225
  else
226
226
  nil
227
227
  end
@@ -241,11 +241,11 @@ module PDK
241
241
  require 'pdk/util'
242
242
  require 'pdk/version'
243
243
 
244
- return 'master' if PDK::Util.development_mode?
244
+ return 'main' if PDK::Util.development_mode?
245
245
  return PDK::TEMPLATE_REF if uri.nil?
246
246
 
247
247
  uri = new(uri) unless uri.is_a?(self)
248
- uri.default? ? PDK::TEMPLATE_REF : 'master'
248
+ uri.default? ? PDK::TEMPLATE_REF : 'main'
249
249
  end
250
250
 
251
251
  # @returns Addressable::URI
@@ -260,21 +260,20 @@ module PDK
260
260
  found_template[:uri]
261
261
  end
262
262
 
263
- def self.valid_template?(template)
263
+ def self.valid_template?(template, context = PDK.context)
264
264
  require 'addressable'
265
265
 
266
266
  return false if template.nil? || !template.is_a?(Hash)
267
267
  return false if template[:uri].nil? || !template[:uri].is_a?(Addressable::URI)
268
268
 
269
269
  return true if PDK::Util::Git.repo?(bare_uri(template[:uri]))
270
-
271
270
  path = human_readable(template[:uri].path)
272
271
  if PDK::Util::Filesystem.directory?(path)
273
272
  # We know that it's not a git repository, but it's a valid path on disk
274
273
  begin
275
- PDK::Module::TemplateDir.validate_module_template!(path)
276
- return true
277
- rescue ArgumentError
274
+ renderer = PDK::Template::Renderer.instance(path, template[:uri], context)
275
+ return !renderer.nil?
276
+ rescue StandardError
278
277
  nil
279
278
  end
280
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
@@ -2,37 +2,91 @@ require 'pdk'
2
2
 
3
3
  module PDK
4
4
  module Validate
5
- # TODO: Fix validator namespacing
6
- autoload :BaseValidator, 'pdk/validate/base_validator'
7
- autoload :MetadataJSONLint, 'pdk/validate/metadata/metadata_json_lint'
8
- autoload :MetadataSyntax, 'pdk/validate/metadata/metadata_syntax'
9
- autoload :MetadataValidator, 'pdk/validate/metadata_validator'
10
- autoload :PuppetEPP, 'pdk/validate/puppet/puppet_epp'
11
- autoload :PuppetLint, 'pdk/validate/puppet/puppet_lint'
12
- autoload :PuppetSyntax, 'pdk/validate/puppet/puppet_syntax'
13
- autoload :PuppetValidator, 'pdk/validate/puppet_validator'
14
- autoload :Rubocop, 'pdk/validate/ruby/rubocop'
15
- autoload :RubyValidator, 'pdk/validate/ruby_validator'
16
- autoload :TasksValidator, 'pdk/validate/tasks_validator'
17
- autoload :YAMLValidator, 'pdk/validate/yaml_validator'
5
+ autoload :ExternalCommandValidator, 'pdk/validate/external_command_validator'
6
+ autoload :InternalRubyValidator, 'pdk/validate/internal_ruby_validator'
7
+ autoload :InvokableValidator, 'pdk/validate/invokable_validator'
8
+ autoload :Validator, 'pdk/validate/validator'
9
+ autoload :ValidatorGroup, 'pdk/validate/validator_group'
18
10
 
19
- class Tasks
20
- autoload :Name, 'pdk/validate/tasks/name'
21
- autoload :MetadataLint, 'pdk/validate/tasks/metadata_lint'
11
+ module ControlRepo
12
+ autoload :ControlRepoValidatorGroup, 'pdk/validate/control_repo/control_repo_validator_group'
13
+ autoload :EnvironmentConfValidator, 'pdk/validate/control_repo/environment_conf_validator'
22
14
  end
23
15
 
24
- class YAML
25
- autoload :Syntax, 'pdk/validate/yaml/syntax'
16
+ module Metadata
17
+ autoload :MetadataJSONLintValidator, 'pdk/validate/metadata/metadata_json_lint_validator'
18
+ autoload :MetadataSyntaxValidator, 'pdk/validate/metadata/metadata_syntax_validator'
19
+ autoload :MetadataValidatorGroup, 'pdk/validate/metadata/metadata_validator_group'
20
+ end
21
+
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'
27
+ end
28
+
29
+ module Ruby
30
+ autoload :RubyRubocopValidator, 'pdk/validate/ruby/ruby_rubocop_validator'
31
+ autoload :RubyValidatorGroup, 'pdk/validate/ruby/ruby_validator_group'
32
+ end
33
+
34
+ module Tasks
35
+ autoload :TasksMetadataLintValidator, 'pdk/validate/tasks/tasks_metadata_lint_validator'
36
+ autoload :TasksNameValidator, 'pdk/validate/tasks/tasks_name_validator'
37
+ autoload :TasksValidatorGroup, 'pdk/validate/tasks/tasks_validator_group'
38
+ end
39
+
40
+ module YAML
41
+ autoload :YAMLSyntaxValidator, 'pdk/validate/yaml/yaml_syntax_validator'
42
+ autoload :YAMLValidatorGroup, 'pdk/validate/yaml/yaml_validator_group'
26
43
  end
27
44
 
28
45
  def self.validators
29
- @validators ||= [
30
- MetadataValidator,
31
- YAMLValidator,
32
- PuppetValidator,
33
- RubyValidator,
34
- TasksValidator,
35
- ].freeze
46
+ validator_hash.values
47
+ end
48
+
49
+ def self.validator_names
50
+ validator_hash.keys
51
+ end
52
+
53
+ # @api private
54
+ def self.validator_hash
55
+ # TODO: This isn't the most performant... But with only 6 items, it's fine
56
+ @validator_hash ||= [
57
+ ControlRepo::ControlRepoValidatorGroup,
58
+ Metadata::MetadataValidatorGroup,
59
+ Puppet::PuppetValidatorGroup,
60
+ Ruby::RubyValidatorGroup,
61
+ Tasks::TasksValidatorGroup,
62
+ YAML::YAMLValidatorGroup,
63
+ ].map { |klass| [klass.new.name, klass] }.to_h.freeze
64
+ end
65
+
66
+ def self.invoke_validators_by_name(context, names, parallel = false, options = {})
67
+ instances = names.select { |name| validator_names.include?(name) }
68
+ .map { |name| validator_hash[name].new(context, options) }
69
+ .select { |instance| instance.valid_in_context? }
70
+ .each { |instance| instance.prepare_invoke! }
71
+ report = PDK::Report.new
72
+
73
+ # Nothing to validate then nothing to do.
74
+ return [0, report] if instances.empty?
75
+
76
+ require 'pdk/cli/exec_group'
77
+ exec_group = PDK::CLI::ExecGroup.create(
78
+ _('Validating module using %{num_of_threads} threads' % { num_of_threads: instances.count }),
79
+ { parallel: parallel },
80
+ options,
81
+ )
82
+
83
+ instances.each do |validator|
84
+ exec_group.register do
85
+ validator.invoke(report)
86
+ end
87
+ end
88
+
89
+ [exec_group.exit_code, report]
36
90
  end
37
91
 
38
92
  class ParseOutputError < StandardError; end
@@ -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