puppet-parse 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.8.7
5
+ env:
6
+ - PUPPET_VERSION=2.7.6
7
+ - PUPPET_VERSION=2.7.13
8
+ - PUPPET_VERSION=2.7.20
9
+ - PUPPET_VERSION=3.0.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in puppet-parse.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Johan van den Dorpe
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # puppet-parse
2
+
3
+ [![Build Status](https://travis-ci.org/johanek/puppet-parse.png)](http://travis-ci.org/johanek/puppet-parse)
4
+
5
+ Analyse puppet manifests and report what classes and defines are specified, and their parameters and parameter documentation.
6
+
7
+ ## Installation
8
+
9
+ gem install puppet-parse
10
+
11
+ ## Requirements
12
+
13
+ puppet >2.7, <3.0
14
+ rdoc
15
+
16
+ I think there's a bug in Puppet 3.0 which is breaking things. So don't use with Puppet 3.0 yet.
17
+
18
+ ## Usage
19
+
20
+ ### By hand
21
+
22
+ You can test one or more manifests by running
23
+
24
+ puppet-parse <path(s) to file(s)>
25
+
26
+
27
+ ### Rake tast
28
+
29
+ If you want to parse your entire modules directory, you can add
30
+ `require 'puppet-parse/puppet-parse' to your Rakefile and then run
31
+
32
+ rake parse
33
+
34
+ ## Contributing
35
+
36
+ You can do any of these:
37
+
38
+ 1. Create new Pull Request
39
+ 2. Create an issue
40
+ 3. Write me an email
41
+ 4. Complain about how useless my code is on twitter
42
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'rake'
2
+ require 'rspec/core/rake_task'
3
+
4
+ task :default => :test
5
+
6
+ RSpec::Core::RakeTask.new(:test)
data/bin/puppet-parse ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
4
+ require 'puppet-parse'
5
+ require 'yaml'
6
+
7
+ run = PuppetParse::Runner.new
8
+ puts run.run(ARGV).to_yaml
9
+
@@ -0,0 +1,13 @@
1
+ class Hash
2
+
3
+ def paramflat
4
+ result = {}
5
+
6
+ self.each do |key, val|
7
+ result[key] = (defined? val.value) ? val.value : nil
8
+ end
9
+
10
+ result
11
+ end
12
+
13
+ end
@@ -0,0 +1,63 @@
1
+ module PuppetParse
2
+ class Parser
3
+
4
+ def initialize(file)
5
+ # Read file and return parsed object
6
+ pparser = Puppet::Parser::Parser.new('production')
7
+ if File.exists?(file)
8
+ @file = File.expand_path(file)
9
+ pparser.import(@file)
10
+
11
+ # Find object in list of hostclasses
12
+ pparser.environment.known_resource_types.hostclasses.each do |x|
13
+ @object = x.last if x.last.file == @file
14
+ end
15
+ # Find object in list of definitions
16
+ pparser.environment.known_resource_types.definitions.each do |x|
17
+ @object = x.last if x.last.file == @file
18
+ end
19
+
20
+ else
21
+ 'File does not exist'
22
+ end
23
+ end
24
+
25
+ # Read parameters from parsed object, returns hash of parameters and default
26
+ # values
27
+ def parameters
28
+ result = (defined? @object.arguments) ? @object.arguments : {}
29
+ result
30
+ end
31
+
32
+ # Read class from parsed object, returns string containing class
33
+ def klass
34
+ @object.name if (defined? @object.class.name)
35
+ end
36
+
37
+ # Read RDOC contents from parsed object, returns hash of paragraph headings
38
+ # and the following paragraph contents
39
+ #(i.e. parameter and parameter documentation)
40
+ def docs
41
+ if !@object.doc.nil?
42
+ rdoc = RDoc::Markup.parse(@object.doc)
43
+ docs = {}
44
+
45
+ rdoc.parts.each do |part|
46
+ if part.respond_to?(:items)
47
+ part.items.each do |item|
48
+ # Skip rdoc items that aren't paragraphs
49
+ next unless (item.parts.to_s.scan("RDoc::Markup::Paragraph") == ["RDoc::Markup::Paragraph"])
50
+ # Documentation must be a list - if there's no label then skip
51
+ next if item.label.nil?
52
+ key = item.label.tr('^A-Za-z0-9_-', '')
53
+ docs[key] = item.parts.first.parts
54
+ end # do item
55
+ end # endif
56
+ end # do parm
57
+
58
+ docs
59
+ end # if nil?
60
+ end # def docs
61
+
62
+ end # class Parser
63
+ end # module PuppetParse
@@ -0,0 +1,13 @@
1
+
2
+ $LOAD_PATH.unshift File.expand_path('/vagrant/puppet-parser/lib')
3
+
4
+ require 'puppet-parse'
5
+ require 'rake'
6
+ require 'rake/tasklib'
7
+
8
+ desc 'Run puppet-parse'
9
+ task :parse do
10
+ matched_files = FileList['**/*.pp']
11
+ run = PuppetParse::Runner.new
12
+ puts run.run(matched_files.to_a).to_yaml
13
+ end
@@ -0,0 +1,26 @@
1
+ module PuppetParse
2
+ class Runner
3
+
4
+ def initialize
5
+ end
6
+
7
+ def run(files)
8
+ output = {}
9
+ files.each do |file|
10
+ content = PuppetParse::Parser.new(file)
11
+ next if content.instance_variable_get('@object').nil?
12
+ parameters = (defined? content.parameters) ? content.parameters.paramflat : nil
13
+ result = {
14
+ content.klass => {
15
+ 'parameters' => parameters,
16
+ 'docs' => content.docs
17
+ }
18
+ }
19
+ output = output.merge(result)
20
+ end
21
+ output
22
+ end
23
+
24
+
25
+ end #class Runner
26
+ end #module PuppetParse
@@ -0,0 +1,3 @@
1
+ module PuppetParse
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,8 @@
1
+ require 'puppet-parse/version'
2
+ require 'puppet-parse/parser'
3
+ require 'puppet-parse/runner'
4
+ require 'puppet-parse/hash'
5
+ require 'rubygems'
6
+ require 'puppet'
7
+ require 'rdoc'
8
+
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/puppet-parse/version', __FILE__)
3
+
4
+ if ENV.key?('PUPPET_VERSION')
5
+ puppetversion = "= #{ENV['PUPPET_VERSION']}"
6
+ else
7
+ puppetversion = ['>= 2.7', '< 3.0']
8
+ end
9
+
10
+
11
+ Gem::Specification.new do |gem|
12
+ gem.authors = ["Johan van den Dorpe"]
13
+ gem.email = ["johan.vandendorpe@gmail.com"]
14
+ gem.description = %q{Parse Puppet modules for classes, defines, parameters and documentation}
15
+ gem.summary = %q{Parser for Puppet Modules. Returns Information about available classes and defines, their parameters, and documentation for those parameters.}
16
+ gem.homepage = "https://github.com/johanek/puppet-parse"
17
+
18
+ gem.files = `git ls-files`.split($\)
19
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
21
+ gem.name = "puppet-parse"
22
+ gem.require_paths = ["lib"]
23
+ gem.version = PuppetParse::VERSION
24
+
25
+ gem.add_development_dependency "rspec"
26
+ gem.add_development_dependency "rake"
27
+ gem.add_runtime_dependency "rdoc"
28
+ gem.add_runtime_dependency "puppet", puppetversion
29
+
30
+ end
@@ -0,0 +1,8 @@
1
+ define define_nordoc (
2
+ $param_one = true,
3
+ $param_two = '',
4
+ $param_three = $::fqdn,
5
+ $param_four
6
+ ) {
7
+ # no content
8
+ }
@@ -0,0 +1,20 @@
1
+ # = Define: define_rdoc
2
+ #
3
+ # A define for testing puppet-parse
4
+ #
5
+ # == Requirements
6
+ #
7
+ # None
8
+ #
9
+ # == Parameters
10
+ #
11
+ # [*param_one*]
12
+ # Param1 documentation text
13
+ define define_rdoc (
14
+ $param_one = true,
15
+ $param_two = '',
16
+ $param_three = $::fqdn,
17
+ $param_four
18
+ ) {
19
+ # no content
20
+ }
@@ -0,0 +1,4 @@
1
+ nrpe_command{'check_mysqldump':
2
+ command => 'check_file_age',
3
+ parameters => '-f /srv/backup/mysql/mysql.dump -w 86400 -c 90000 -W 8000 -C 8000'
4
+ }
@@ -0,0 +1,3 @@
1
+ class noparameters_nordoc {
2
+ # no content
3
+ }
@@ -0,0 +1,15 @@
1
+ # = Class: noparameters_rdoc
2
+ #
3
+ # A class for testing puppet-parse
4
+ #
5
+ # == Requirements
6
+ #
7
+ # None
8
+ #
9
+ # == Parameters
10
+ #
11
+ # [*param_one*]
12
+ # Param1 documentation text
13
+ class noparameters_rdoc {
14
+ # no content
15
+ }
@@ -0,0 +1,8 @@
1
+ class parameters_nordoc (
2
+ $param_one = true,
3
+ $param_two = '',
4
+ $param_three = $::fqdn,
5
+ $param_four
6
+ ) {
7
+ # no content
8
+ }
@@ -0,0 +1,20 @@
1
+ # = Class: parameters_rdoc
2
+ #
3
+ # A class for testing puppet-parse
4
+ #
5
+ # == Requirements
6
+ #
7
+ # None
8
+ #
9
+ # == Parameters
10
+ #
11
+ # [*param_one*]
12
+ # Param1 documentation text
13
+ class parameters_rdoc (
14
+ $param_one = true,
15
+ $param_two = '',
16
+ $param_three = $::fqdn,
17
+ $param_four
18
+ ) {
19
+ # no content
20
+ }
@@ -0,0 +1,122 @@
1
+ require 'spec_helper'
2
+
3
+ def setup(file)
4
+ run = PuppetParse::Runner.new
5
+ manifests = [ file ]
6
+ run.run(manifests)
7
+ end
8
+
9
+ shared_examples "standard tests" do |file, klass|
10
+
11
+ subject { setup(file) }
12
+
13
+ it 'should be a hash' do
14
+ subject.should be_a(Hash)
15
+ end
16
+
17
+ it 'class should have a name' do
18
+ subject.class.should_not be nil
19
+ end
20
+
21
+ it 'parameters and docs keys should be set' do
22
+ subject[klass].keys.should =~ ['parameters', 'docs']
23
+ end
24
+
25
+ it 'parameters should be a hash' do
26
+ subject[klass]['parameters'].should be_a(Hash)
27
+ end
28
+
29
+ it 'docs should be a hash' do
30
+ subject[klass]['docs'].should be_a(Hash)
31
+ end
32
+
33
+ it 'values should not be nil' do
34
+ subject[klass].values.should_not include(nil)
35
+ end
36
+
37
+ end
38
+
39
+ shared_examples "parameters" do |file, klass|
40
+ subject { setup(file) }
41
+ it 'parameters should have four keys' do
42
+ subject[klass]['parameters'].should have(4).keys
43
+ end
44
+ end
45
+
46
+ shared_examples "no parameters" do |file, klass|
47
+ subject { setup(file) }
48
+ it 'parameters should have no keys' do
49
+ subject[klass]['parameters'].should have(0).keys
50
+ end
51
+ end
52
+
53
+ shared_examples "rdoc" do |file, klass|
54
+ subject { setup(file) }
55
+ it 'docs should have one key' do
56
+ subject[klass]['docs'].should have(1).keys
57
+ end
58
+ end
59
+
60
+ shared_examples "no rdoc" do |file, klass|
61
+ subject { setup(file) }
62
+ it 'docs should have no keys' do
63
+ subject[klass]['docs'].should have(0).keys
64
+ end
65
+ end
66
+
67
+
68
+ describe "manifest with parameters and rdoc" do
69
+ file = 'spec/manifests/parameters_rdoc.pp'
70
+ klass = 'parameters_rdoc'
71
+ it_should_behave_like "standard tests", file, klass
72
+ it_should_behave_like "parameters", file, klass
73
+ it_should_behave_like "rdoc", file, klass
74
+ end
75
+
76
+ describe "manifest with parameters and no rdoc" do
77
+ file = 'spec/manifests/parameters_nordoc.pp'
78
+ klass = 'parameters_nordoc'
79
+ it_should_behave_like "standard tests", file, klass
80
+ it_should_behave_like "parameters", file, klass
81
+ it_should_behave_like "no rdoc", file, klass
82
+ end
83
+
84
+ describe "manifest with no parameters and rdoc" do
85
+ file = 'spec/manifests/noparameters_rdoc.pp'
86
+ klass = 'noparameters_rdoc'
87
+ it_should_behave_like "standard tests", file, klass
88
+ it_should_behave_like "no parameters", file, klass
89
+ it_should_behave_like "rdoc", file, klass
90
+ end
91
+
92
+ describe "manifest with no parameters and no rdoc" do
93
+ file = 'spec/manifests/noparameters_nordoc.pp'
94
+ klass = 'noparameters_nordoc'
95
+ it_should_behave_like "standard tests", file, klass
96
+ it_should_behave_like "no parameters", file, klass
97
+ it_should_behave_like "no rdoc", file, klass
98
+ end
99
+
100
+ describe "define with rdoc" do
101
+ file = 'spec/manifests/define_rdoc.pp'
102
+ klass = 'define_rdoc'
103
+ it_should_behave_like "standard tests", file, klass
104
+ it_should_behave_like "rdoc", file, klass
105
+ end
106
+
107
+ describe "define with no rdoc" do
108
+ file = 'spec/manifests/define_nordoc.pp'
109
+ klass = 'define_nordoc'
110
+ it_should_behave_like "standard tests", file, klass
111
+ it_should_behave_like "no rdoc", file, klass
112
+ end
113
+
114
+ describe "noclass" do
115
+ file = 'spec/manifests/noclass.pp'
116
+ subject { setup(file) }
117
+
118
+ it 'should be a hash' do
119
+ subject.should be_a(Hash)
120
+ end
121
+
122
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
2
+ require 'rspec/autorun'
3
+ require 'puppet-parse'
data/spec/tests ADDED
@@ -0,0 +1,13 @@
1
+ classes should have a name
2
+ parameters should be set
3
+ every parameter should have a value
4
+ docs should be set
5
+ every doc key should have a value
6
+
7
+ a sample file with parameters & rdoc should work
8
+ a sample file with parameters & no rdoc should work
9
+ a sample file with no parameters & rdoc should work
10
+ a sample file with no parameters & no rdoc should work
11
+ a sample file with no class should work
12
+ a sample file with define and rdoc should work
13
+ a sample file with define and no rdoc should work
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puppet-parse
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Johan van den Dorpe
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-11-22 00:00:00 +00:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rake
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: rdoc
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :runtime
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: puppet
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 13
72
+ segments:
73
+ - 2
74
+ - 7
75
+ version: "2.7"
76
+ - - <
77
+ - !ruby/object:Gem::Version
78
+ hash: 7
79
+ segments:
80
+ - 3
81
+ - 0
82
+ version: "3.0"
83
+ type: :runtime
84
+ version_requirements: *id004
85
+ description: Parse Puppet modules for classes, defines, parameters and documentation
86
+ email:
87
+ - johan.vandendorpe@gmail.com
88
+ executables:
89
+ - puppet-parse
90
+ extensions: []
91
+
92
+ extra_rdoc_files: []
93
+
94
+ files:
95
+ - .gitignore
96
+ - .travis.yml
97
+ - Gemfile
98
+ - LICENSE
99
+ - README.md
100
+ - Rakefile
101
+ - bin/puppet-parse
102
+ - lib/puppet-parse.rb
103
+ - lib/puppet-parse/hash.rb
104
+ - lib/puppet-parse/parser.rb
105
+ - lib/puppet-parse/puppet-parse.rb
106
+ - lib/puppet-parse/runner.rb
107
+ - lib/puppet-parse/version.rb
108
+ - puppet-parse.gemspec
109
+ - spec/manifests/define_nordoc.pp
110
+ - spec/manifests/define_rdoc.pp
111
+ - spec/manifests/noclass.pp
112
+ - spec/manifests/noparameters_nordoc.pp
113
+ - spec/manifests/noparameters_rdoc.pp
114
+ - spec/manifests/parameters_nordoc.pp
115
+ - spec/manifests/parameters_rdoc.pp
116
+ - spec/puppet-parse_spec.rb
117
+ - spec/spec_helper.rb
118
+ - spec/tests
119
+ has_rdoc: true
120
+ homepage: https://github.com/johanek/puppet-parse
121
+ licenses: []
122
+
123
+ post_install_message:
124
+ rdoc_options: []
125
+
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ hash: 3
134
+ segments:
135
+ - 0
136
+ version: "0"
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ hash: 3
143
+ segments:
144
+ - 0
145
+ version: "0"
146
+ requirements: []
147
+
148
+ rubyforge_project:
149
+ rubygems_version: 1.6.2
150
+ signing_key:
151
+ specification_version: 3
152
+ summary: Parser for Puppet Modules. Returns Information about available classes and defines, their parameters, and documentation for those parameters.
153
+ test_files:
154
+ - spec/manifests/define_nordoc.pp
155
+ - spec/manifests/define_rdoc.pp
156
+ - spec/manifests/noclass.pp
157
+ - spec/manifests/noparameters_nordoc.pp
158
+ - spec/manifests/noparameters_rdoc.pp
159
+ - spec/manifests/parameters_nordoc.pp
160
+ - spec/manifests/parameters_rdoc.pp
161
+ - spec/puppet-parse_spec.rb
162
+ - spec/spec_helper.rb
163
+ - spec/tests