Module: AbideDevUtils::Ppt

Defined in:
lib/abide_dev_utils/ppt.rb,
lib/abide_dev_utils/ppt/api.rb,
lib/abide_dev_utils/ppt/hiera.rb,
lib/abide_dev_utils/ppt/new_obj.rb,
lib/abide_dev_utils/ppt/strings.rb,
lib/abide_dev_utils/ppt/code_gen.rb,
lib/abide_dev_utils/ppt/class_utils.rb,
lib/abide_dev_utils/ppt/facter_utils.rb,
lib/abide_dev_utils/ppt/score_module.rb,
lib/abide_dev_utils/ppt/puppet_module.rb,
lib/abide_dev_utils/ppt/code_gen/generate.rb,
lib/abide_dev_utils/ppt/code_gen/resource.rb,
lib/abide_dev_utils/ppt/code_introspection.rb,
lib/abide_dev_utils/ppt/code_gen/data_types.rb,
lib/abide_dev_utils/ppt/code_gen/resource_types/base.rb,
lib/abide_dev_utils/ppt/code_gen/resource_types/class.rb,
lib/abide_dev_utils/ppt/code_gen/resource_types/strings.rb,
lib/abide_dev_utils/ppt/code_gen/resource_types/manifest.rb,
lib/abide_dev_utils/ppt/code_gen/resource_types/parameter.rb

Defined Under Namespace

Modules: ClassUtils, CodeGen, CodeIntrospection, FacterUtils, Hiera Classes: ApiClient, NewObjectBuilder, PuppetModule, ScoreModule, Strings, YardObjectWrapper

Class Method Summary collapse

Class Method Details

.add_cis_comment(path, xccdf, number_format: false) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/abide_dev_utils/ppt.rb', line 82

def self.add_cis_comment(path, xccdf, number_format: false)
  require 'abide_dev_utils/xccdf'

  parsed_xccdf = AbideDevUtils::XCCDF::Benchmark.new(xccdf)
  return add_cis_comment_to_all(path, parsed_xccdf, number_format: number_format) if File.directory?(path)
  return add_cis_comment_to_single(path, parsed_xccdf, number_format: number_format) if File.file?(path)

  raise AbideDevUtils::Errors::FileNotFoundError, path
end

.add_cis_comment_to_all(path, xccdf, number_format: false) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/abide_dev_utils/ppt.rb', line 103

def self.add_cis_comment_to_all(path, xccdf, number_format: false)
  comments = {}
  Dir[File.join(path, '*.pp')].each do |puppet_file|
    comment = cis_recommendation_comment(puppet_file, xccdf, number_format)
    comments[puppet_file] = comment unless comment.nil?
  end
  comments.each do |key, value|
    write_cis_comment_to_file(key, value)
  end
  AbideDevUtils::Output.simple('Successfully added comments.')
end

.add_cis_comment_to_single(path, xccdf, number_format: false) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/abide_dev_utils/ppt.rb', line 92

def self.add_cis_comment_to_single(path, xccdf, number_format: false)
  write_cis_comment_to_file(
    path,
    cis_recommendation_comment(
      path,
      xccdf,
      number_format
    )
  )
end

.audit_class_names(dir, **kwargs) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/abide_dev_utils/ppt.rb', line 33

def self.audit_class_names(dir, **kwargs)
  mismatched = ClassUtils.find_all_mismatched_class_declarations(dir)
  outfile = kwargs.key?(:file) ? File.open(kwargs[:file], 'a') : nil
  quiet = kwargs.fetch(:quiet, false)
  mismatched.each do |class_file|
    AbideDevUtils::Output.simple("Mismatched class name in file #{class_file}") unless quiet
    outfile << "MISMATCHED_CLASS_NAME: #{class_file}\n" unless outfile.nil?
  end
  outfile&.close
  AbideDevUtils::Output.simple("Found #{mismatched.length} mismatched classes in #{dir}.") unless quiet
ensure
  outfile&.close
end

.build_new_object(type, name, opts) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/abide_dev_utils/ppt.rb', line 72

def self.build_new_object(type, name, opts)
  require 'abide_dev_utils/ppt/new_obj'
  AbideDevUtils::Ppt::NewObjectBuilder.new(
    type,
    name,
    opts: opts,
    vars: opts.fetch(:vars, '').split(',').map { |i| i.split('=') }.to_h # makes the str a hash
  ).build
end

.cis_recommendation_comment(puppet_file, xccdf, number_format) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/abide_dev_utils/ppt.rb', line 136

def self.cis_recommendation_comment(puppet_file, xccdf, number_format)
  _, control = xccdf.find_cis_recommendation(
    File.basename(puppet_file, '.pp'),
    number_format: number_format
  )
  if control.nil?
    AbideDevUtils::Output.simple("Could not find recommendation text for #{puppet_file}...")
    return nil
  end
  control_title = xccdf.resolve_control_reference(control).xpath('./xccdf:title').text
  "# #{control_title}"
end

.fix_class_names_class_rename(dir, **kwargs) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/abide_dev_utils/ppt.rb', line 59

def self.fix_class_names_class_rename(dir, **kwargs)
  mismatched = ClassUtils.find_all_mismatched_class_declarations(dir)
  progress = AbideDevUtils::Output.progress(title: 'Renaming classes', total: mismatched.length)
  mismatched.each do |class_path|
    current = ClassUtils.class_name_from_declaration(class_path)
    should = ClassUtils.class_name_from_path(class_path)
    ClassUtils.rename_puppet_class_declaration(current, should, class_path, **kwargs)
    progress.increment
    AbideDevUtils::Output.simple("Renamed #{from} to #{to} at #{file_path}...") if kwargs.fetch(:verbose, false)
  end
  AbideDevUtils::Output.simple('Successfully fixed all classes.')
end

.fix_class_names_file_rename(dir, **kwargs) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/abide_dev_utils/ppt.rb', line 47

def self.fix_class_names_file_rename(dir, **kwargs)
  mismatched = ClassUtils.find_all_mismatched_class_declarations(dir)
  progress = AbideDevUtils::Output.progress(title: 'Renaming files', total: mismatched.length)
  mismatched.each do |class_path|
    should = ClassUtils.path_from_class_name(class_name_from_declaration(class_path))
    ClassUtils.rename_class_file(class_path, should, **kwargs)
    progress.increment
    AbideDevUtils::Output.simple("Renamed file #{class_path} to #{should}...") if kwargs.fetch(:verbose, false)
  end
  AbideDevUtils::Output.simple('Successfully fixed all classes.')
end

.rename_puppet_class(from, to, **kwargs) ⇒ Object

Renames a Puppet class by renaming the class declaration and class file

Parameters:

  • from (String)

    fully-namespaced existing Puppet class name

  • to (String)

    fully-namespaced new Puppet class name

Raises:

  • (ClassFileNotFoundError)


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/abide_dev_utils/ppt.rb', line 19

def self.rename_puppet_class(from, to, **kwargs)
  from_path = ClassUtils.path_from_class_name(from)
  to_path = ClassUtils.path_from_class_name(to)
  file_path = kwargs.fetch(:declaration_in_to_file, false) ? to_path : from_path
  raise ClassFileNotFoundError, "Path:#{file_path}" if !File.file?(file_path) && kwargs.fetch(:validate_path, true)

  rename_puppet_class_declaration(from, to, file_path, **kwargs)
  AbideDevUtils::Output.simple("Renamed #{from} to #{to} at #{file_path}.")
  return unless kwargs.fetch(:declaration_only, false)

  rename_class_file(from_path, to_path, **kwargs)
  AbideDevUtils::Output.simple("Renamed file #{from_path} to #{to_path}.")
end

.score_module(module_path, outfile: nil, quiet: false, checks: ['all'], **_) ⇒ Object



149
150
151
152
153
154
# File 'lib/abide_dev_utils/ppt.rb', line 149

def self.score_module(module_path, outfile: nil, quiet: false, checks: ['all'], **_)
  AbideDevUtils::Output.simple 'This command is not currently implemented'
  # require 'abide_dev_utils/ppt/score_module'
  # score = {}
  # score[:lint_check] = ScoreModule.lint if checks.include?('all') || checks.include?('lint')
end

.write_cis_comment_to_file(path, comment) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/abide_dev_utils/ppt.rb', line 115

def self.write_cis_comment_to_file(path, comment)
  require 'tempfile'
  tempfile = Tempfile.new
  begin
    File.open(tempfile, 'w') do |nf|
      nf.write("#{comment}\n")
      File.foreach(path) do |line|
        nf.write(line) unless line == "#{comment}\n"
      end
    end
    File.rename(path, "#{path}.old")
    tempfile.close
    File.rename(tempfile.path, path)
    File.delete("#{path}.old")
    AbideDevUtils::Output.simple("Added CIS recomendation comment to #{path}...")
  ensure
    tempfile.close
    tempfile.unlink
  end
end