Last active
August 29, 2015 14:03
-
-
Save raphink/b030d60fd686160184a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'puppetdb' | |
require 'puppet' | |
require 'json' | |
Puppet.initialize_settings | |
key = Puppet.settings['hostprivkey'] | |
cert = Puppet.settings['hostcert'] | |
ca_file = Puppet.settings['localcacert'] | |
server = 'puppetdb' # where do I get this? | |
client = PuppetDB::Client.new({ | |
:server => "https://#{server}:8081", | |
:pem => { | |
'key' => key, | |
'cert' => cert, | |
'ca_file' => ca_file | |
} | |
}) | |
response = client.request( | |
'resources', | |
[:and, [:'~', 'type', '[A-Za-z0-9_-]+']], | |
{ } | |
) | |
resources = response.data | |
# Initialize output | |
output = { 'modules' => {}, 'site-modules' => {} } | |
# Get list of environments | |
environment_paths = resources.map { |r| | |
if r['file'] | |
match = r['file'].match(%r{^(/.*)/(?:site-)?modules/}) | |
match.captures[0] if match | |
end | |
}.compact.uniq | |
resources.each do |r| | |
if r['file'] | |
f = r['file'] | |
match = f.match(%r{^(/.*)/((?:site-)?modules)/([^/]+)/(.*)$}) | |
if match | |
lead, type, mod, path = match.captures | |
# Add used files | |
output[type][mod] ||= {} | |
output[type][mod]['used'] ||= [] | |
output[type][mod]['used'] |= [path] | |
# Add unused files | |
mod_path = "#{lead}/#{type}/#{mod}/" | |
output[type][mod]['unused'] ||= environment_paths.map { |e| | |
env_mod_path = "#{e}/#{type}/#{mod}/" | |
Dir.glob("#{env_mod_path}manifests/**/*.pp").map { |p| p.gsub(env_mod_path, '') } | |
}.flatten.compact.uniq | |
output[type][mod]['unused'].delete(f.gsub(mod_path, '')) | |
end | |
end | |
end | |
puts JSON.dump output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment