puppet-ghostbuster 0.0.6 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/puppet-ghostbuster/bin.rb +22 -1
- data/lib/puppet-ghostbuster/configuration.rb +95 -0
- data/lib/puppet-ghostbuster/optparser.rb +63 -0
- data/lib/puppet-ghostbuster/version.rb +1 -1
- data/lib/puppet-ghostbuster.rb +56 -20
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d56eb52cd4557da70d2d921969152ef7e8e49722
|
4
|
+
data.tar.gz: d4c72dd1fda2b98c096c7eb533959c431adc1aa9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 005cacf8fe81037d81b292974a9d0a06bd7baf17063ee23e50bf1e7188e462e7481cd20ffe3510d49ae14e9acf6fc5359bbf74e8c3d017b95629a1bb6c95c21b
|
7
|
+
data.tar.gz: 76beedef79101ed3cd1b270cffc756f8cdd9174ef281594cbf0cde2596d642326b7796fa85f68c30589147ebe28bbebcf006ebecf0c4cffc7bff26438009ca8c
|
@@ -1,10 +1,31 @@
|
|
1
|
+
require 'puppet-ghostbuster/optparser'
|
1
2
|
class PuppetGhostbuster::Bin
|
2
3
|
def initialize(args)
|
3
4
|
@args = args
|
4
5
|
end
|
5
6
|
|
6
7
|
def run
|
7
|
-
PuppetGhostbuster.
|
8
|
+
opts = PuppetGhostbuster::OptParser.build
|
9
|
+
|
10
|
+
begin
|
11
|
+
opts.parse!(@args)
|
12
|
+
rescue OptionParser::InvalidOption
|
13
|
+
puts "puppet-ghostbuster: #{$!.message}"
|
14
|
+
puts "puppet-ghostbuster: try 'puppet-ghostbuster --help' for more information"
|
15
|
+
return 1
|
16
|
+
end
|
17
|
+
|
18
|
+
if PuppetGhostbuster.configuration.display_version
|
19
|
+
puts "puppet-ghostbuster #{PuppetGhostbuster::VERSION}"
|
20
|
+
return 0
|
21
|
+
end
|
22
|
+
|
23
|
+
if @args[0].nil?
|
24
|
+
PuppetGhostbuster.new().run
|
25
|
+
else
|
26
|
+
PuppetGhostbuster.new(@args[0]).run
|
27
|
+
end
|
28
|
+
|
8
29
|
exit 0
|
9
30
|
end
|
10
31
|
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
class PuppetGhostbuster
|
2
|
+
# Public: A singleton class to store the running configuration of
|
3
|
+
# puppet-ghostbuster.
|
4
|
+
class Configuration
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
Puppet.initialize_settings
|
8
|
+
end
|
9
|
+
|
10
|
+
# Public: Catch situations where options are being set for the first time
|
11
|
+
# and create the necessary methods to get & set the option in the future.
|
12
|
+
#
|
13
|
+
# args - An Array of values to set the option to.
|
14
|
+
# method - The String name of the option.
|
15
|
+
# block - Unused.
|
16
|
+
#
|
17
|
+
# Returns nothing.
|
18
|
+
#
|
19
|
+
# Signature
|
20
|
+
#
|
21
|
+
# <option>=(value)
|
22
|
+
def method_missing(method, *args, &block)
|
23
|
+
if method.to_s =~ /^(\w+)=$/
|
24
|
+
option = $1
|
25
|
+
add_option(option.to_s) if settings[option].nil?
|
26
|
+
settings[option] = args[0]
|
27
|
+
else
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Internal: Add options to the PuppetGhostbuster::Configuration object from inside
|
33
|
+
# the class.
|
34
|
+
#
|
35
|
+
# option - The String name of the option.
|
36
|
+
#
|
37
|
+
# Returns nothing.
|
38
|
+
#
|
39
|
+
# Signature
|
40
|
+
#
|
41
|
+
# <option>
|
42
|
+
# <option>=(value)
|
43
|
+
def add_option(option)
|
44
|
+
self.class.add_option(option)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Public: Add an option to the PuppetGhostbuster::Configuration object from
|
48
|
+
# outside the class.
|
49
|
+
#
|
50
|
+
# option - The String name of the option.
|
51
|
+
#
|
52
|
+
# Returns nothing.
|
53
|
+
#
|
54
|
+
# Signature
|
55
|
+
#
|
56
|
+
# <option>
|
57
|
+
# <option>=(value)
|
58
|
+
def self.add_option(option)
|
59
|
+
# Public: Set the value of the named option.
|
60
|
+
#
|
61
|
+
# value - The value to set the option to.
|
62
|
+
#
|
63
|
+
# Returns nothing.
|
64
|
+
define_method("#{option}=") do |value|
|
65
|
+
settings[option] = value
|
66
|
+
end
|
67
|
+
|
68
|
+
# Public: Get the value of the named option.
|
69
|
+
#
|
70
|
+
# Returns the value of the option.
|
71
|
+
define_method(option) do
|
72
|
+
settings[option]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Internal: Access the internal storage for settings.
|
77
|
+
#
|
78
|
+
# Returns a Hash containing all the settings.
|
79
|
+
def settings
|
80
|
+
@settings ||= {}
|
81
|
+
end
|
82
|
+
|
83
|
+
# Public: Clear the PuppetGhostbuster::Configuration storage and set some sane
|
84
|
+
# default values.
|
85
|
+
#
|
86
|
+
# Returns nothing.
|
87
|
+
def defaults
|
88
|
+
settings.clear
|
89
|
+
self.puppetdbserverurl = "https://#{Puppet[:server]}:8081"
|
90
|
+
self.hostprivkey = Puppet[:hostprivkey]
|
91
|
+
self.hostcert = Puppet[:hostcert]
|
92
|
+
self.localcacert = Puppet[:localcacert]
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
|
3
|
+
# Public: Contains the puppet-ghostbuster option parser so that it can be used easily
|
4
|
+
# in multiple places.
|
5
|
+
class PuppetGhostbuster::OptParser
|
6
|
+
HELP_TEXT = <<-EOF
|
7
|
+
puppet-ghostbuster
|
8
|
+
|
9
|
+
Basic Command Line Usage:
|
10
|
+
puppet-ghostbuster [OPTIONS] [PATH]
|
11
|
+
|
12
|
+
PATH Path to the Root directory of puppet code. Current dir by default.
|
13
|
+
|
14
|
+
Option:
|
15
|
+
EOF
|
16
|
+
|
17
|
+
# Public: Initialise a new puppet-ghostbuster OptionParser.
|
18
|
+
#
|
19
|
+
# Returns an OptionParser object.
|
20
|
+
def self.build
|
21
|
+
OptionParser.new do |opts|
|
22
|
+
opts.banner = HELP_TEXT
|
23
|
+
|
24
|
+
opts.on('--version', 'Display the current version.') do
|
25
|
+
PuppetGhostbuster.configuration.display_version = true
|
26
|
+
end
|
27
|
+
|
28
|
+
opts.on('-c', '--config FILE', 'Load puppet-ghostbuster options from file.') do |file|
|
29
|
+
opts.load(file)
|
30
|
+
end
|
31
|
+
|
32
|
+
opts.on('--error-level LEVEL', [:all, :warning, :error],
|
33
|
+
'The level of error to return (warning, error or all).') do |el|
|
34
|
+
PuppetGhostbuster.configuration.error_level = el
|
35
|
+
end
|
36
|
+
|
37
|
+
opts.on('-s', '--puppetdburl SERVER', 'puppet db server url to connect to.') do |s|
|
38
|
+
PuppetGhostbuster.configuration.puppetdbserverurl = s
|
39
|
+
end
|
40
|
+
|
41
|
+
opts.on('--key FILE', 'Load private key from the given file.') do |file|
|
42
|
+
PuppetGhostbuster.configuration.hostprivkey
|
43
|
+
end
|
44
|
+
|
45
|
+
opts.on('--cert FILE', 'Load cert from the given file.') do |file|
|
46
|
+
PuppetGhostbuster.configuration.hostcert
|
47
|
+
end
|
48
|
+
|
49
|
+
opts.on('--ca FILE', 'Load local ca cert from the given file.') do |file|
|
50
|
+
PuppetGhostbuster.configuration.localcacert
|
51
|
+
end
|
52
|
+
|
53
|
+
opts.load('/etc/puppet-ghostbuster.rc')
|
54
|
+
begin
|
55
|
+
opts.load(File.expand_path('~/.puppet-ghostbuster.rc')) if ENV['HOME']
|
56
|
+
rescue Errno::EACCES
|
57
|
+
# silently skip loading this file if HOME is set to a directory that
|
58
|
+
# the user doesn't have read access to.
|
59
|
+
end
|
60
|
+
opts.load('.puppet-ghostbuster.rc')
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/lib/puppet-ghostbuster.rb
CHANGED
@@ -1,14 +1,43 @@
|
|
1
1
|
require 'json'
|
2
2
|
require 'puppet'
|
3
3
|
require 'puppetdb'
|
4
|
+
require 'optparse'
|
4
5
|
|
5
6
|
require 'puppet-ghostbuster/version'
|
6
7
|
require 'puppet-ghostbuster/bin'
|
8
|
+
require 'puppet-ghostbuster/configuration'
|
7
9
|
|
8
10
|
class PuppetGhostbuster
|
9
11
|
|
12
|
+
attr_accessor :path
|
13
|
+
|
14
|
+
def manifests
|
15
|
+
Dir["#{path}/**/manifests/*.pp"]
|
16
|
+
end
|
17
|
+
|
18
|
+
def templates
|
19
|
+
Dir["#{path}/**/templates/*"]
|
20
|
+
end
|
21
|
+
|
22
|
+
def files
|
23
|
+
Dir["#{path}/**/files/*"]
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.configuration
|
27
|
+
@configuration ||= PuppetGhostbuster::Configuration.new
|
28
|
+
end
|
29
|
+
|
30
|
+
def configuration
|
31
|
+
self.class.configuration
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
def self.puppetdbserverfilename
|
36
|
+
return configuration.puppetdbserverurl.gsub(/[:\/]/,'_')
|
37
|
+
end
|
38
|
+
|
10
39
|
def self.cache
|
11
|
-
"/var/tmp/puppet-ghostbuster.cache"
|
40
|
+
"/var/tmp/puppet-ghostbuster.#{puppetdbserverfilename}.cache"
|
12
41
|
end
|
13
42
|
|
14
43
|
def self.update_cache(value)
|
@@ -28,16 +57,15 @@ class PuppetGhostbuster
|
|
28
57
|
|
29
58
|
def self.client
|
30
59
|
PuppetDB::Client.new({
|
31
|
-
:server =>
|
60
|
+
:server => configuration.puppetdbserverurl,
|
32
61
|
:pem => {
|
33
|
-
'key' =>
|
34
|
-
'cert' =>
|
35
|
-
'ca_file' =>
|
62
|
+
'key' => configuration.hostprivkey,
|
63
|
+
'cert' => configuration.hostcert,
|
64
|
+
'ca_file' => configuration.localcacert,
|
36
65
|
}
|
37
66
|
})
|
38
67
|
end
|
39
68
|
|
40
|
-
|
41
69
|
def self.used_classes
|
42
70
|
return get_cache || update_cache(
|
43
71
|
client.request(
|
@@ -50,7 +78,7 @@ class PuppetGhostbuster
|
|
50
78
|
end
|
51
79
|
|
52
80
|
def find_unused_classes
|
53
|
-
|
81
|
+
manifests.each do |file|
|
54
82
|
if c = File.readlines(file).grep(/^class\s+([^\s\(\{]+)/){$1}[0]
|
55
83
|
class_name = c.split('::').map(&:capitalize).join('::')
|
56
84
|
count = self.class.used_classes.select { |klass| klass == class_name }.size
|
@@ -60,7 +88,7 @@ class PuppetGhostbuster
|
|
60
88
|
end
|
61
89
|
|
62
90
|
def find_unused_defines
|
63
|
-
|
91
|
+
manifests.each do |file|
|
64
92
|
if d = File.readlines(file).grep(/^define\s+([^\s\(\{]+)/){$1}[0]
|
65
93
|
define_name = d.split('::').map(&:capitalize).join('::')
|
66
94
|
count = self.class.client.request('resources', [:'=', 'type', define_name]).data.size
|
@@ -70,11 +98,11 @@ class PuppetGhostbuster
|
|
70
98
|
end
|
71
99
|
|
72
100
|
def find_unused_templates
|
73
|
-
|
101
|
+
templates.each do |template|
|
74
102
|
next unless File.file?(template)
|
75
103
|
module_name, template_name = template.match(/.*\/([^\/]+)\/templates\/(.+)$/).captures
|
76
104
|
count = 0
|
77
|
-
|
105
|
+
manifests.each do |manifest|
|
78
106
|
if match = manifest.match(/.*\/([^\/]+)\/manifests\/.+$/)
|
79
107
|
manifest_module_name = match.captures[0]
|
80
108
|
count += File.readlines(manifest).grep(/["']\$\{module_name\}\/#{template_name}["']/).size if manifest_module_name == module_name
|
@@ -86,28 +114,34 @@ class PuppetGhostbuster
|
|
86
114
|
end
|
87
115
|
|
88
116
|
def find_unused_files
|
89
|
-
|
117
|
+
files.each do |file|
|
90
118
|
next unless File.file?(file)
|
91
119
|
module_name, file_name = file.match(/.*\/([^\/]+)\/files\/(.+)$/).captures
|
92
120
|
count = 0
|
93
|
-
Dir["
|
121
|
+
Dir["#{path}"].each do |caller_file|
|
94
122
|
next unless File.file?(caller_file)
|
95
|
-
|
96
|
-
if
|
97
|
-
|
98
|
-
|
99
|
-
|
123
|
+
begin
|
124
|
+
if caller_file =~ /\.pp$/
|
125
|
+
if match = caller_file.match(/.*\/([^\/]+)\/manifests\/.+$/)
|
126
|
+
manifest_module_name = match.captures[0]
|
127
|
+
if manifest_module_name == module_name
|
128
|
+
count += File.readlines(caller_file).grep(/["']\$\{module_name\}\/#{file_name}["']/).size
|
129
|
+
end
|
100
130
|
end
|
101
131
|
end
|
132
|
+
count += File.readlines(caller_file).grep(/#{module_name}\/#{file_name}/).size
|
133
|
+
rescue ArgumentError
|
102
134
|
end
|
103
|
-
count += File.readlines(caller_file).grep(/#{module_name}\/#{file_name}/).size
|
104
135
|
end
|
105
136
|
puts "File #{file} not used" if count == 0
|
106
137
|
end
|
107
138
|
end
|
108
139
|
|
109
|
-
def initialize
|
110
|
-
|
140
|
+
def initialize(path = '.')
|
141
|
+
self.path = path
|
142
|
+
end
|
143
|
+
|
144
|
+
def run
|
111
145
|
find_unused_classes
|
112
146
|
find_unused_defines
|
113
147
|
find_unused_templates
|
@@ -115,3 +149,5 @@ class PuppetGhostbuster
|
|
115
149
|
end
|
116
150
|
|
117
151
|
end
|
152
|
+
|
153
|
+
PuppetGhostbuster.configuration.defaults
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: puppet-ghostbuster
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Camptocamp
|
@@ -66,6 +66,8 @@ files:
|
|
66
66
|
- bin/puppet-ghostbuster
|
67
67
|
- lib/puppet-ghostbuster.rb
|
68
68
|
- lib/puppet-ghostbuster/bin.rb
|
69
|
+
- lib/puppet-ghostbuster/configuration.rb
|
70
|
+
- lib/puppet-ghostbuster/optparser.rb
|
69
71
|
- lib/puppet-ghostbuster/version.rb
|
70
72
|
- puppet-ghostbuster.gemspec
|
71
73
|
homepage: http://github.com/camptocamp/puppet-ghostbuster
|