Class: Dropsonde::Metrics::Puppetfiles

Inherits:
Object
  • Object
show all
Defined in:
lib/dropsonde/metrics/puppetfiles.rb

Overview

puppetfiles plugin

Class Method Summary collapse

Class Method Details

.cleanupObject



92
93
94
# File 'lib/dropsonde/metrics/puppetfiles.rb', line 92

def self.cleanup
  # run just after generating this metric
end

.descriptionObject



11
12
13
14
15
16
# File 'lib/dropsonde/metrics/puppetfiles.rb', line 11

def self.description
  <<~DESCRIPTION
    This generates interesting stats about Puppetfiles used in your environments,
    including whether your Puppetfiles have Ruby code in them.
  DESCRIPTION
end

.exampleObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dropsonde/metrics/puppetfiles.rb', line 76

def self.example
  # this method is used to generate a table filled with randomized data to
  # make it easier to write data aggregation queries without access to the
  # actual private data that users have submitted.
  [
    puppetfile_ruby_methods: [
      { name: 'require', count: rand(200) },
      { name: 'each',    count: rand(200) },
      { name: 'puts',    count: rand(200) },
      { name: 'select',  count: rand(200) },
      { name: 'reject',  count: rand(200) },
      { name: 'read',    count: rand(200) },
    ].shuffle,
  ]
end

.initialize_puppetfilesObject



5
6
7
8
9
# File 'lib/dropsonde/metrics/puppetfiles.rb', line 5

def self.initialize_puppetfiles
  # require any libraries needed here -- no need to load puppet; it's already initialized
  # All plugins are initialized before any metrics are generated.
  require 'ripper'
end

.run(_puppetdb_session = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dropsonde/metrics/puppetfiles.rb', line 49

def self.run(_puppetdb_session = nil)
  methods = Dir.entries(Puppet.settings[:environmentpath]).map { |entry|
    puppetfile = File.join(Puppet.settings[:environmentpath], entry, 'Puppetfile')

    next if entry.start_with? '.'
    next unless File.file? puppetfile

    tokens  = Ripper.sexp(File.read(puppetfile)).flatten
    indices = tokens.map.with_index { |a, i| (a == :command) ? i : nil }.compact

    indices.map { |i| tokens[i + 2] }
  }.flatten.compact

  methods.reject! { |name| %w[mod forge moduledir].include? name }

  methods = methods.uniq.map do |name|
    {
      name: name,
      count: methods.count(name),
    }
  end

  [
    { puppetfile_ruby_methods: methods },
  ]
end

.schemaObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dropsonde/metrics/puppetfiles.rb', line 18

def self.schema
  # return an array of hashes of a partial schema to be merged into the complete schema
  # See https://cloud.google.com/bigquery/docs/schemas#specifying_a_json_schema_file
  [
    {
      "fields": [
        {
          "description": 'The method name',
          "mode": 'NULLABLE',
          "name": 'name',
          "type": 'STRING',
        },
        {
          "description": 'How many times is it used',
          "mode": 'NULLABLE',
          "name": 'count',
          "type": 'INTEGER',
        },
      ],
      "description": 'Ruby methods used in Puppetfiles.',
      "mode": 'REPEATED',
      "name": 'puppetfile_ruby_methods',
      "type": 'RECORD',
    },
  ]
end

.setupObject



45
46
47
# File 'lib/dropsonde/metrics/puppetfiles.rb', line 45

def self.setup
  # run just before generating this metric
end