pdk 1.15.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +168 -10
  3. data/README.md +1 -1
  4. data/lib/pdk.rb +28 -19
  5. data/lib/pdk/answer_file.rb +2 -93
  6. data/lib/pdk/bolt.rb +19 -0
  7. data/lib/pdk/cli.rb +7 -6
  8. data/lib/pdk/cli/bundle.rb +5 -1
  9. data/lib/pdk/cli/config.rb +3 -1
  10. data/lib/pdk/cli/config/get.rb +3 -1
  11. data/lib/pdk/cli/convert.rb +7 -9
  12. data/lib/pdk/cli/exec/command.rb +45 -4
  13. data/lib/pdk/cli/exec_group.rb +78 -43
  14. data/lib/pdk/cli/get.rb +20 -0
  15. data/lib/pdk/cli/get/config.rb +24 -0
  16. data/lib/pdk/cli/new.rb +2 -0
  17. data/lib/pdk/cli/new/class.rb +2 -2
  18. data/lib/pdk/cli/new/defined_type.rb +2 -2
  19. data/lib/pdk/cli/new/fact.rb +29 -0
  20. data/lib/pdk/cli/new/function.rb +29 -0
  21. data/lib/pdk/cli/new/provider.rb +2 -2
  22. data/lib/pdk/cli/new/task.rb +2 -2
  23. data/lib/pdk/cli/new/test.rb +2 -2
  24. data/lib/pdk/cli/new/transport.rb +2 -2
  25. data/lib/pdk/cli/release/publish.rb +11 -1
  26. data/lib/pdk/cli/remove.rb +20 -0
  27. data/lib/pdk/cli/remove/config.rb +80 -0
  28. data/lib/pdk/cli/set.rb +20 -0
  29. data/lib/pdk/cli/set/config.rb +119 -0
  30. data/lib/pdk/cli/update.rb +6 -8
  31. data/lib/pdk/cli/util.rb +7 -3
  32. data/lib/pdk/cli/util/option_validator.rb +6 -0
  33. data/lib/pdk/cli/util/update_manager_printer.rb +82 -0
  34. data/lib/pdk/cli/validate.rb +26 -44
  35. data/lib/pdk/config.rb +264 -7
  36. data/lib/pdk/config/ini_file.rb +183 -0
  37. data/lib/pdk/config/ini_file_setting.rb +39 -0
  38. data/lib/pdk/config/namespace.rb +25 -5
  39. data/lib/pdk/config/setting.rb +3 -2
  40. data/lib/pdk/context.rb +99 -0
  41. data/lib/pdk/context/control_repo.rb +60 -0
  42. data/lib/pdk/context/module.rb +28 -0
  43. data/lib/pdk/context/none.rb +22 -0
  44. data/lib/pdk/control_repo.rb +90 -0
  45. data/lib/pdk/generate/defined_type.rb +25 -32
  46. data/lib/pdk/generate/fact.rb +25 -0
  47. data/lib/pdk/generate/function.rb +48 -0
  48. data/lib/pdk/generate/module.rb +33 -20
  49. data/lib/pdk/generate/provider.rb +15 -64
  50. data/lib/pdk/generate/puppet_class.rb +25 -31
  51. data/lib/pdk/generate/puppet_object.rb +84 -188
  52. data/lib/pdk/generate/task.rb +28 -46
  53. data/lib/pdk/generate/transport.rb +20 -74
  54. data/lib/pdk/module.rb +1 -1
  55. data/lib/pdk/module/build.rb +23 -0
  56. data/lib/pdk/module/convert.rb +58 -34
  57. data/lib/pdk/module/metadata.rb +6 -2
  58. data/lib/pdk/module/release.rb +3 -9
  59. data/lib/pdk/module/update.rb +8 -12
  60. data/lib/pdk/module/update_manager.rb +7 -0
  61. data/lib/pdk/report.rb +3 -3
  62. data/lib/pdk/report/event.rb +8 -2
  63. data/lib/pdk/template.rb +59 -0
  64. data/lib/pdk/template/fetcher.rb +98 -0
  65. data/lib/pdk/template/fetcher/git.rb +85 -0
  66. data/lib/pdk/template/fetcher/local.rb +28 -0
  67. data/lib/pdk/template/renderer.rb +96 -0
  68. data/lib/pdk/template/renderer/v1.rb +25 -0
  69. data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -0
  70. data/lib/pdk/template/renderer/v1/renderer.rb +132 -0
  71. data/lib/pdk/template/renderer/v1/template_file.rb +102 -0
  72. data/lib/pdk/template/template_dir.rb +67 -0
  73. data/lib/pdk/tests/unit.rb +8 -1
  74. data/lib/pdk/util.rb +38 -39
  75. data/lib/pdk/util/bundler.rb +2 -1
  76. data/lib/pdk/util/changelog_generator.rb +11 -2
  77. data/lib/pdk/util/env.rb +28 -11
  78. data/lib/pdk/util/json_finder.rb +84 -0
  79. data/lib/pdk/util/puppet_strings.rb +3 -3
  80. data/lib/pdk/util/puppet_version.rb +2 -2
  81. data/lib/pdk/util/ruby_version.rb +11 -4
  82. data/lib/pdk/util/template_uri.rb +17 -14
  83. data/lib/pdk/util/vendored_file.rb +1 -2
  84. data/lib/pdk/validate.rb +79 -25
  85. data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -0
  86. data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -0
  87. data/lib/pdk/validate/external_command_validator.rb +208 -0
  88. data/lib/pdk/validate/internal_ruby_validator.rb +100 -0
  89. data/lib/pdk/validate/invokable_validator.rb +220 -0
  90. data/lib/pdk/validate/metadata/metadata_json_lint_validator.rb +86 -0
  91. data/lib/pdk/validate/metadata/metadata_syntax_validator.rb +78 -0
  92. data/lib/pdk/validate/metadata/metadata_validator_group.rb +20 -0
  93. data/lib/pdk/validate/puppet/puppet_epp_validator.rb +133 -0
  94. data/lib/pdk/validate/puppet/puppet_lint_validator.rb +66 -0
  95. data/lib/pdk/validate/puppet/puppet_syntax_validator.rb +137 -0
  96. data/lib/pdk/validate/puppet/puppet_validator_group.rb +21 -0
  97. data/lib/pdk/validate/ruby/ruby_rubocop_validator.rb +80 -0
  98. data/lib/pdk/validate/ruby/ruby_validator_group.rb +19 -0
  99. data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +88 -0
  100. data/lib/pdk/validate/tasks/tasks_name_validator.rb +50 -0
  101. data/lib/pdk/validate/tasks/tasks_validator_group.rb +20 -0
  102. data/lib/pdk/validate/validator.rb +118 -0
  103. data/lib/pdk/validate/validator_group.rb +104 -0
  104. data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +95 -0
  105. data/lib/pdk/validate/yaml/yaml_validator_group.rb +19 -0
  106. data/lib/pdk/version.rb +1 -1
  107. data/locales/pdk.pot +513 -329
  108. metadata +76 -32
  109. data/lib/pdk/module/template_dir.rb +0 -115
  110. data/lib/pdk/module/template_dir/base.rb +0 -268
  111. data/lib/pdk/module/template_dir/git.rb +0 -91
  112. data/lib/pdk/module/template_dir/local.rb +0 -21
  113. data/lib/pdk/template_file.rb +0 -96
  114. data/lib/pdk/validate/base_validator.rb +0 -215
  115. data/lib/pdk/validate/metadata/metadata_json_lint.rb +0 -82
  116. data/lib/pdk/validate/metadata/metadata_syntax.rb +0 -111
  117. data/lib/pdk/validate/metadata_validator.rb +0 -26
  118. data/lib/pdk/validate/puppet/puppet_epp.rb +0 -135
  119. data/lib/pdk/validate/puppet/puppet_lint.rb +0 -64
  120. data/lib/pdk/validate/puppet/puppet_syntax.rb +0 -135
  121. data/lib/pdk/validate/puppet_validator.rb +0 -26
  122. data/lib/pdk/validate/ruby/rubocop.rb +0 -72
  123. data/lib/pdk/validate/ruby_validator.rb +0 -26
  124. data/lib/pdk/validate/tasks/metadata_lint.rb +0 -130
  125. data/lib/pdk/validate/tasks/name.rb +0 -90
  126. data/lib/pdk/validate/tasks_validator.rb +0 -29
  127. data/lib/pdk/validate/yaml/syntax.rb +0 -125
  128. data/lib/pdk/validate/yaml_validator.rb +0 -28
@@ -1,111 +0,0 @@
1
- require 'pdk'
2
-
3
- module PDK
4
- module Validate
5
- class MetadataSyntax < BaseValidator
6
- def self.name
7
- 'metadata-syntax'
8
- end
9
-
10
- def self.pattern
11
- ['metadata.json', 'tasks/*.json']
12
- end
13
-
14
- def self.spinner_text(_targets = [])
15
- _('Checking metadata syntax (%{targets}).') % {
16
- targets: pattern.join(' '),
17
- }
18
- end
19
-
20
- def self.create_spinner(targets = [], options = {})
21
- require 'pdk/cli/util'
22
-
23
- return unless PDK::CLI::Util.interactive?
24
-
25
- options = options.merge(PDK::CLI::Util.spinner_opts_for_platform)
26
-
27
- exec_group = options[:exec_group]
28
- @spinner = if exec_group
29
- exec_group.add_spinner(spinner_text(targets), options)
30
- else
31
- require 'pdk/cli/util/spinner'
32
-
33
- TTY::Spinner.new("[:spinner] #{spinner_text(targets)}", options)
34
- end
35
- @spinner.auto_spin
36
- end
37
-
38
- def self.stop_spinner(exit_code)
39
- if exit_code.zero? && @spinner
40
- @spinner.success
41
- elsif @spinner
42
- @spinner.error
43
- end
44
- end
45
-
46
- def self.invoke(report, options = {})
47
- targets, skipped, invalid = parse_targets(options)
48
-
49
- process_skipped(report, skipped)
50
- process_invalid(report, invalid)
51
-
52
- return 0 if targets.empty?
53
-
54
- return_val = 0
55
- create_spinner(targets, options)
56
-
57
- # The pure ruby JSON parser gives much nicer parse error messages than
58
- # the C extension at the cost of slightly slower parsing. We require it
59
- # here and restore the C extension at the end of the method (if it was
60
- # being used).
61
- require 'json/pure'
62
- JSON.parser = JSON::Pure::Parser
63
-
64
- targets.each do |target|
65
- unless PDK::Util::Filesystem.readable?(target)
66
- report.add_event(
67
- file: target,
68
- source: name,
69
- state: :failure,
70
- severity: 'error',
71
- message: _('Could not be read.'),
72
- )
73
- return_val = 1
74
- next
75
- end
76
-
77
- begin
78
- JSON.parse(PDK::Util::Filesystem.read_file(target))
79
-
80
- report.add_event(
81
- file: target,
82
- source: name,
83
- state: :passed,
84
- severity: 'ok',
85
- )
86
- rescue JSON::ParserError => e
87
- # Because the message contains a raw segment of the file, we use
88
- # String#dump here to unescape any escape characters like newlines.
89
- # We then strip out the surrounding quotes and the exclaimation
90
- # point that json_pure likes to put in exception messages.
91
- sane_message = e.message.dump[%r{\A"(.+?)!?"\Z}, 1]
92
-
93
- report.add_event(
94
- file: target,
95
- source: name,
96
- state: :failure,
97
- severity: 'error',
98
- message: sane_message,
99
- )
100
- return_val = 1
101
- end
102
- end
103
-
104
- stop_spinner(return_val)
105
- return_val
106
- ensure
107
- JSON.parser = JSON::Ext::Parser if defined?(JSON::Ext::Parser)
108
- end
109
- end
110
- end
111
- end
@@ -1,26 +0,0 @@
1
- require 'pdk'
2
-
3
- module PDK
4
- module Validate
5
- class MetadataValidator < BaseValidator
6
- def self.name
7
- 'metadata'
8
- end
9
-
10
- def self.metadata_validators
11
- [MetadataSyntax, MetadataJSONLint]
12
- end
13
-
14
- def self.invoke(report, options = {})
15
- exit_code = 0
16
-
17
- metadata_validators.each do |validator|
18
- exit_code = validator.invoke(report, options)
19
- break if exit_code != 0
20
- end
21
-
22
- exit_code
23
- end
24
- end
25
- end
26
- end
@@ -1,135 +0,0 @@
1
- require 'pdk'
2
-
3
- module PDK
4
- module Validate
5
- class PuppetEPP < BaseValidator
6
- # In Puppet >= 5.3.4, the error context formatting was changed to facilitate localization
7
- ERROR_CONTEXT = %r{(?:file:\s(?<file>.+?)|line:\s(?<line>.+?)|column:\s(?<column>.+?))}
8
- # In Puppet < 5.3.3, the error context was formatted in these variations:
9
- # - "at file_path:line_num:col_num"
10
- # - "at file_path:line_num"
11
- # - "at line line_num"
12
- # - "in file_path"
13
- ERROR_CONTEXT_LEGACY = %r{(?:at\sline\s(?<line>\d+)|at\s(?<file>.+?):(?<line>\d+):(?<column>\d+)|at\s(?<file>.+?):(?<line>\d+)|in\s(?<file>.+?))}
14
-
15
- PUPPET_LOGGER_PREFIX = %r{^(debug|info|notice|warning|error|alert|critical):\s.+?$}i
16
- PUPPET_SYNTAX_PATTERN = %r{^
17
- (?<severity>.+?):\s
18
- (?<message>.+?)
19
- (?:
20
- \s\(#{ERROR_CONTEXT}(,\s#{ERROR_CONTEXT})*\)| # attempt to match the new localisation friendly location
21
- \s#{ERROR_CONTEXT_LEGACY}| # attempt to match the old " at file:line:column" location
22
- $ # handle cases where the output has no location
23
- )
24
- $}x
25
-
26
- def self.name
27
- 'puppet-epp'
28
- end
29
-
30
- def self.cmd
31
- 'puppet'
32
- end
33
-
34
- def self.pattern
35
- '**/*.epp'
36
- end
37
-
38
- def self.pattern_ignore
39
- ''
40
- end
41
-
42
- def self.spinner_text(_targets = nil)
43
- _('Checking Puppet EPP syntax (%{pattern}).') % { pattern: pattern }
44
- end
45
-
46
- def self.parse_options(_options, targets)
47
- # Due to PDK-1266 we need to run `puppet parser validate` with an empty
48
- # modulepath. On *nix, Ruby treats `/dev/null` as an empty directory
49
- # however it doesn't do so with `NUL` on Windows. The workaround for
50
- # this to ensure consistent behaviour is to create an empty temporary
51
- # directory and use that as the modulepath.
52
- ['epp', 'validate', '--config', null_file, '--modulepath', validate_tmpdir].concat(targets)
53
- end
54
-
55
- def self.invoke(report, options)
56
- super
57
- ensure
58
- remove_validate_tmpdir
59
- end
60
-
61
- def self.validate_tmpdir
62
- require 'tmpdir'
63
-
64
- @validate_tmpdir ||= Dir.mktmpdir('puppet-epp-validate')
65
- end
66
-
67
- def self.remove_validate_tmpdir
68
- return unless @validate_tmpdir
69
- return unless PDK::Util::Filesystem.directory?(@validate_tmpdir)
70
-
71
- PDK::Util::Filesystem.remove_entry_secure(@validate_tmpdir)
72
- @validate_tmpdir = nil
73
- end
74
-
75
- def self.null_file
76
- Gem.win_platform? ? 'NUL' : '/dev/null'
77
- end
78
-
79
- def self.parse_output(report, result, targets)
80
- # Due to PUP-7504, we will have to programmatically construct the json
81
- # object from the text output for now.
82
- output = result[:stderr].split(%r{\r?\n}).reject { |entry| entry.empty? }
83
-
84
- results_data = []
85
- output.each do |offense|
86
- offense_data = parse_offense(offense)
87
- results_data << offense_data
88
- end
89
-
90
- # puppet parser validate does not include files without problems in its
91
- # output, so we need to go through the list of targets and add passing
92
- # events to the report for any target not listed in the output.
93
- targets.reject { |target| results_data.any? { |j| j[:file] =~ %r{#{target}} } }.each do |target|
94
- report.add_event(
95
- file: target,
96
- source: name,
97
- severity: :ok,
98
- state: :passed,
99
- )
100
- end
101
-
102
- results_data.each do |offense|
103
- report.add_event(offense)
104
- end
105
- end
106
-
107
- def self.parse_offense(offense)
108
- sanitize_console_output(offense)
109
-
110
- offense_data = {
111
- source: name,
112
- state: :failure,
113
- }
114
-
115
- if offense.match(PUPPET_LOGGER_PREFIX)
116
- attributes = offense.match(PUPPET_SYNTAX_PATTERN)
117
-
118
- unless attributes.nil?
119
- attributes.names.each do |name|
120
- offense_data[name.to_sym] = attributes[name] unless attributes[name].nil?
121
- end
122
- end
123
- else
124
- offense_data[:message] = offense
125
- end
126
-
127
- offense_data
128
- end
129
-
130
- def self.sanitize_console_output(line)
131
- line.gsub!(%r{\e\[([;\d]+)?m}, '')
132
- end
133
- end
134
- end
135
- end
@@ -1,64 +0,0 @@
1
- require 'pdk'
2
-
3
- module PDK
4
- module Validate
5
- class PuppetLint < BaseValidator
6
- def self.name
7
- 'puppet-lint'
8
- end
9
-
10
- def self.cmd
11
- 'puppet-lint'
12
- end
13
-
14
- def self.pattern
15
- '**/*.pp'
16
- end
17
-
18
- def self.spinner_text(_targets = nil)
19
- _('Checking Puppet manifest style (%{pattern}).') % { pattern: pattern }
20
- end
21
-
22
- def self.parse_options(options, targets)
23
- cmd_options = ['--json', '--relative']
24
-
25
- cmd_options << '--fix' if options[:auto_correct]
26
-
27
- cmd_options.concat(targets)
28
- end
29
-
30
- def self.parse_output(report, result, targets)
31
- begin
32
- json_data = JSON.parse(result[:stdout]).flatten
33
- rescue JSON::ParserError
34
- raise PDK::Validate::ParseOutputError, result[:stdout]
35
- end
36
-
37
- # puppet-lint does not include files without problems in its JSON
38
- # output, so we need to go through the list of targets and add passing
39
- # events to the report for any target not listed in the JSON output.
40
- targets.reject { |target| json_data.any? { |j| j['path'] == target } }.each do |target|
41
- report.add_event(
42
- file: target,
43
- source: name,
44
- severity: 'ok',
45
- state: :passed,
46
- )
47
- end
48
-
49
- json_data.each do |offense|
50
- report.add_event(
51
- file: offense['path'],
52
- source: name,
53
- line: offense['line'],
54
- column: offense['column'],
55
- message: offense['message'],
56
- test: offense['check'],
57
- severity: (offense['kind'] == 'fixed') ? 'corrected' : offense['kind'],
58
- state: :failure,
59
- )
60
- end
61
- end
62
- end
63
- end
64
- end
@@ -1,135 +0,0 @@
1
- require 'pdk'
2
-
3
- module PDK
4
- module Validate
5
- class PuppetSyntax < BaseValidator
6
- # In Puppet >= 5.3.4, the error context formatting was changed to facilitate localization
7
- ERROR_CONTEXT = %r{(?:file:\s(?<file>.+?)|line:\s(?<line>.+?)|column:\s(?<column>.+?))}
8
- # In Puppet < 5.3.3, the error context was formatted in these variations:
9
- # - "at file_path:line_num:col_num"
10
- # - "at file_path:line_num"
11
- # - "at line line_num"
12
- # - "in file_path"
13
- ERROR_CONTEXT_LEGACY = %r{(?:at\sline\s(?<line>\d+)|at\s(?<file>.+?):(?<line>\d+):(?<column>\d+)|at\s(?<file>.+?):(?<line>\d+)|in\s(?<file>.+?))}
14
-
15
- PUPPET_LOGGER_PREFIX = %r{^(debug|info|notice|warning|error|alert|critical):\s.+?$}i
16
- PUPPET_SYNTAX_PATTERN = %r{^
17
- (?<severity>.+?):\s
18
- (?<message>.+?)
19
- (?:
20
- \s\(#{ERROR_CONTEXT}(,\s#{ERROR_CONTEXT})*\)| # attempt to match the new localisation friendly location
21
- \s#{ERROR_CONTEXT_LEGACY}| # attempt to match the old " at file:line:column" location
22
- $ # handle cases where the output has no location
23
- )
24
- $}x
25
-
26
- def self.name
27
- 'puppet-syntax'
28
- end
29
-
30
- def self.cmd
31
- 'puppet'
32
- end
33
-
34
- def self.pattern
35
- '**/*.pp'
36
- end
37
-
38
- def self.pattern_ignore
39
- '/plans/**/*.pp'
40
- end
41
-
42
- def self.spinner_text(_targets = nil)
43
- _('Checking Puppet manifest syntax (%{pattern}).') % { pattern: pattern }
44
- end
45
-
46
- def self.parse_options(_options, targets)
47
- # Due to PDK-1266 we need to run `puppet parser validate` with an empty
48
- # modulepath. On *nix, Ruby treats `/dev/null` as an empty directory
49
- # however it doesn't do so with `NUL` on Windows. The workaround for
50
- # this to ensure consistent behaviour is to create an empty temporary
51
- # directory and use that as the modulepath.
52
- ['parser', 'validate', '--config', null_file, '--modulepath', validate_tmpdir].concat(targets)
53
- end
54
-
55
- def self.invoke(report, options)
56
- super
57
- ensure
58
- remove_validate_tmpdir
59
- end
60
-
61
- def self.validate_tmpdir
62
- require 'tmpdir'
63
-
64
- @validate_tmpdir ||= Dir.mktmpdir('puppet-parser-validate')
65
- end
66
-
67
- def self.remove_validate_tmpdir
68
- return unless @validate_tmpdir
69
- return unless PDK::Util::Filesystem.directory?(@validate_tmpdir)
70
-
71
- PDK::Util::Filesystem.remove_entry_secure(@validate_tmpdir)
72
- @validate_tmpdir = nil
73
- end
74
-
75
- def self.null_file
76
- Gem.win_platform? ? 'NUL' : '/dev/null'
77
- end
78
-
79
- def self.parse_output(report, result, targets)
80
- # Due to PUP-7504, we will have to programmatically construct the json
81
- # object from the text output for now.
82
- output = result[:stderr].split(%r{\r?\n}).reject { |entry| entry.empty? }
83
-
84
- results_data = []
85
- output.each do |offense|
86
- offense_data = parse_offense(offense)
87
- results_data << offense_data
88
- end
89
-
90
- # puppet parser validate does not include files without problems in its
91
- # output, so we need to go through the list of targets and add passing
92
- # events to the report for any target not listed in the output.
93
- targets.reject { |target| results_data.any? { |j| j[:file] =~ %r{#{target}} } }.each do |target|
94
- report.add_event(
95
- file: target,
96
- source: name,
97
- severity: :ok,
98
- state: :passed,
99
- )
100
- end
101
-
102
- results_data.each do |offense|
103
- report.add_event(offense)
104
- end
105
- end
106
-
107
- def self.parse_offense(offense)
108
- sanitize_console_output(offense)
109
-
110
- offense_data = {
111
- source: name,
112
- state: :failure,
113
- }
114
-
115
- if offense.match(PUPPET_LOGGER_PREFIX)
116
- attributes = offense.match(PUPPET_SYNTAX_PATTERN)
117
-
118
- unless attributes.nil?
119
- attributes.names.each do |name|
120
- offense_data[name.to_sym] = attributes[name] unless attributes[name].nil?
121
- end
122
- end
123
- else
124
- offense_data[:message] = offense
125
- end
126
-
127
- offense_data
128
- end
129
-
130
- def self.sanitize_console_output(line)
131
- line.gsub!(%r{\e\[([;\d]+)?m}, '')
132
- end
133
- end
134
- end
135
- end