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