kafo_parsers 0.0.6 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc259a5db6ee3563620d111d1775e2c85f4d6968
4
- data.tar.gz: 656f94ca96aa36bbebf37c126083e18aa7a4e546
3
+ metadata.gz: 7317ced24d434141968dbe2d6a7587562c1315ee
4
+ data.tar.gz: fad877075b7f49f78959329081cb69649b20f1d3
5
5
  SHA512:
6
- metadata.gz: 17c14657902bde419c774515ff6a43969cd9eea1d887320f1f66cc0b4df64bd0b4f65d53e39426c1c5e2f03b0f679249a4f1d3f2bf0a110852451a3750b95436
7
- data.tar.gz: 30f393c3208da33b04dacceb0b8db24d71b2773365efcc4fb017da8ad26c6c8b2c8835fbbe1699af8414b45972ab33f23a7f56ca4c6db0766820bb652f0c3fed
6
+ metadata.gz: 7eef567e4e770c4d00a2f12ef2cc3a7bc71554d647695f9d286fc07f463798836d61572fb434c781bcff3a729f7244d10ee7ffd68389b25de1f7524aa3b27e50
7
+ data.tar.gz: 7df4289db02589fc951d7f421be6995fc97e788420d2de4878ef547d3f9816b398b8f3142fc99439c80d6ac3291b43c71e7653ea0ce02bd028805577e1ea1643
data/README.md CHANGED
@@ -25,13 +25,51 @@ Or install it yourself as:
25
25
 
26
26
  ## Usage
27
27
 
28
- To parse file and see parsed information
28
+ To parse file using the best available parser, and see parsed information:
29
+ ```ruby
30
+ require 'kafo_parsers/parsers'
31
+ parser = KafoParsers::Parsers.find_available or fail('No parser available')
32
+ hash = parser.parse('/puppet/module/manifests/init.pp')
33
+ p hash
34
+ ```
35
+
36
+ `find_available` can take a logger object that responds to `#debug` to log
37
+ detailed reasons why each parser isn't available.
38
+
39
+ ```ruby
40
+ logger = Logging.logger(STDOUT)
41
+ logger.level = :debug
42
+ KafoParsers::Parsers.find_available(:logger => logger)
43
+ ```
44
+
45
+ To load a specific parser:
29
46
  ```ruby
30
47
  require 'kafo_parsers/puppet_module_parser'
31
48
  hash = KafoParsers::PuppetModuleParser.parse('/puppet/module/manifests/init.pp')
32
- p hash
33
49
  ```
34
50
 
51
+ #### PuppetModuleParser
52
+
53
+ The standard PuppetModuleParser loads Puppet as a regular library or gem, so it
54
+ must be installed in the same Ruby that's running kafo_parsers.
55
+
56
+ Only Puppet versions 2.6.x, 2.7.x and 3.x are supported.
57
+
58
+ Add `gem 'puppet', '< 4'` to your application's Gemfile to use this.
59
+
60
+ #### PuppetStringsModuleParser
61
+
62
+ If you use Puppet 4 you prefer PuppetStringsModuleParser which leverages
63
+ puppet-strings to parse puppet manifests.
64
+
65
+ ```ruby
66
+ require 'kafo_parsers/puppet_strings_module_parser'
67
+ hash = KafoParsers::PuppetStringsModuleParser.parse('/puppet/module/manifests/init.pp')
68
+ ```
69
+
70
+ Output will be similar to PuppetModuleParser, only validations are not supported,
71
+ since they are not parsed by puppet-strings.
72
+
35
73
  # License
36
74
 
37
75
  This project is licensed under the GPLv3+.
@@ -6,4 +6,20 @@ module KafoParsers
6
6
  class ModuleName < StandardError
7
7
  end
8
8
 
9
+ class ParserNotAvailable < StandardError
10
+ def initialize(wrapped)
11
+ if wrapped.is_a?(Exception)
12
+ @wrapped = wrapped
13
+ else
14
+ @message = wrapped
15
+ end
16
+ end
17
+
18
+ def message
19
+ @message || @wrapped.message
20
+ end
21
+ end
22
+
23
+ class ParseError < StandardError
24
+ end
9
25
  end
@@ -0,0 +1,21 @@
1
+ require 'kafo_parsers/puppet_module_parser.rb'
2
+ require 'kafo_parsers/puppet_strings_module_parser.rb'
3
+
4
+ module KafoParsers
5
+ module Parsers
6
+ def self.all
7
+ [ PuppetModuleParser, PuppetStringsModuleParser ]
8
+ end
9
+
10
+ def self.find_available(options = {})
11
+ all.find do |provider|
12
+ begin
13
+ provider.available?
14
+ rescue ParserNotAvailable => e
15
+ options[:logger].debug "Provider #{provider} not available: #{e.message}" if options[:logger]
16
+ false
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,5 +1,4 @@
1
1
  # encoding: UTF-8
2
- require 'puppet'
3
2
  require 'kafo_parsers/doc_parser'
4
3
 
5
4
  module KafoParsers
@@ -10,6 +9,13 @@ module KafoParsers
10
9
  class PuppetModuleParser
11
10
  @@puppet_initialized = false
12
11
 
12
+ def self.available?
13
+ require 'puppet'
14
+ [2, 3].include?(Puppet::PUPPETVERSION.to_i)
15
+ rescue LoadError => e
16
+ raise KafoParsers::ParserNotAvailable.new(e)
17
+ end
18
+
13
19
  # You can call this method to get all supported information from a given manifest
14
20
  #
15
21
  # @param [ String ] manifest file path to parse
@@ -32,11 +38,13 @@ module KafoParsers
32
38
  raise KafoParsers::ModuleName, "File not found #{file}, check your answer file" unless File.exists?(file)
33
39
 
34
40
  unless @@puppet_initialized
41
+ require 'puppet'
35
42
  if Puppet::PUPPETVERSION.to_i >= 3
36
43
  Puppet.initialize_settings
37
44
  else
38
45
  Puppet.parse_config
39
46
  end
47
+ Encoding.default_external = Encoding::UTF_8 if defined?(Encoding) && Encoding.respond_to?(:default_external=)
40
48
  @@puppet_initialized = true
41
49
  end
42
50
 
@@ -0,0 +1,126 @@
1
+ # encoding: UTF-8
2
+ require 'json'
3
+ require 'kafo_parsers/doc_parser'
4
+
5
+ module KafoParsers
6
+ class PuppetStringsModuleParser
7
+ # You can call this method to get all supported information from a given manifest
8
+ #
9
+ # @param [ String ] manifest file path to parse
10
+ # @return [ Hash ] hash containing values, validations, documentation, types, groups and conditions
11
+ def self.parse(file)
12
+ content = new(file)
13
+ docs = content.docs
14
+
15
+ # data_type must be called before other validations
16
+ data = {
17
+ :object_type => content.data_type,
18
+ :values => content.values,
19
+ :validations => content.validations
20
+ }
21
+ data[:parameters] = data[:values].keys
22
+ data.merge!(docs)
23
+ data
24
+ end
25
+
26
+ def self.available?
27
+ `#{puppet_bin} help strings`
28
+ if $?.success?
29
+ return true
30
+ else
31
+ raise KafoParsers::ParserNotAvailable.new("#{puppet_bin} does not have strings module installed")
32
+ end
33
+ end
34
+
35
+ def initialize(file)
36
+ @file = file
37
+ raise KafoParsers::ModuleName, "File not found #{file}, check your answer file" unless File.exists?(file)
38
+
39
+ command = "#{self.class.puppet_bin} strings #{file} --emit-json-stdout"
40
+ @raw_json = `#{command}`
41
+ unless $?.success?
42
+ raise KafoParsers::ParseError, "'#{command}' returned error\n#{@raw_json}"
43
+ end
44
+
45
+ begin
46
+ @complete_hash = ::JSON.parse(@raw_json)
47
+ rescue ::JSON::ParserError => e
48
+ raise KafoParsers::ParseError, "'#{command}' returned invalid json output: #{e.message}\n#{@raw_json}"
49
+ end
50
+ self.data_type # we need to determine data_type before any further parsing
51
+
52
+ self
53
+ end
54
+
55
+ # AIO and system default puppet bins are tested for existence, fallback to just `puppet` otherwise
56
+ def self.puppet_bin
57
+ @puppet_bin ||= begin
58
+ found_puppet_path = (::ENV['PATH'].split(File::PATH_SEPARATOR) + ['/opt/puppetlabs/bin']).find do |path|
59
+ binary = File.join(path, 'puppet')
60
+ binary if File.executable?(binary)
61
+ end
62
+ File.join(found_puppet_path, 'puppet') || 'puppet'
63
+ end
64
+ end
65
+
66
+ def data_type
67
+ @data_type ||= begin
68
+ if (@parsed_hash = @complete_hash['puppet_classes'].find { |klass| klass['file'] == @file })
69
+ 'hostclass'
70
+ elsif (@parsed_hash = @complete_hash['defined_types'].find { |klass| klass['file'] == @file })
71
+ 'definition'
72
+ else
73
+ raise KafoParsers::ParseError, "unable to find manifest data, syntax error in manifest #{@file}?"
74
+ end
75
+ end
76
+ end
77
+
78
+ def values
79
+ Hash[@parsed_hash['parameters'].map { |name, value| [ name, sanitize(value) ] }]
80
+ end
81
+
82
+ # unsupported in puppet strings parser
83
+ def validations(param = nil)
84
+ []
85
+ end
86
+
87
+ # returns data in following form
88
+ # {
89
+ # :docs => { $param1 => 'documentation without types and conditions'}
90
+ # :types => { $param1 => 'boolean'},
91
+ # :groups => { $param1 => ['Parameters', 'Advanced']},
92
+ # :conditions => { $param1 => '$db_type == "mysql"'},
93
+ # }
94
+ def docs
95
+ data = { :docs => {}, :types => {}, :groups => {}, :conditions => {} }
96
+ if @parsed_hash.nil?
97
+ raise KafoParsers::DocParseError, "no documentation found for manifest #{@file}, parsing error?"
98
+ elsif !@parsed_hash['docstring'].nil?
99
+ parser = DocParser.new(@parsed_hash['docstring']).parse
100
+ data[:docs] = parser.docs
101
+ data[:groups] = parser.groups
102
+ data[:types] = parser.types
103
+ data[:conditions] = parser.conditions
104
+ end
105
+ data
106
+ end
107
+
108
+ private
109
+
110
+ # default values using puppet strings includes $ symbol, e.g. "$::foreman::params::ssl"
111
+ # to keep the same API we strip $ if it's present
112
+ #
113
+ # values are reported as strings which is issue especially for :under
114
+ # strings are double quoted
115
+ # others must be typecast manually according to reported type
116
+ def sanitize(value)
117
+ if (value.start_with?("'") && value.end_with?("'")) || (value.start_with?('"') && value.end_with?('"'))
118
+ value = value[1..-2]
119
+ end
120
+ value = value[1..-1] if value.start_with?('$')
121
+ value = :undef if value == 'undef'
122
+
123
+ value
124
+ end
125
+ end
126
+ end
@@ -1,3 +1,3 @@
1
1
  module KafoParsers
2
- VERSION = "0.0.6"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kafo_parsers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marek Hulan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-11 00:00:00.000000000 Z
11
+ date: 2016-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "<"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '11'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "<"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '11'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -86,14 +86,14 @@ dependencies:
86
86
  requirements:
87
87
  - - "<"
88
88
  - !ruby/object:Gem::Version
89
- version: 4.0.0
90
- type: :runtime
89
+ version: 5.0.0
90
+ type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "<"
95
95
  - !ruby/object:Gem::Version
96
- version: 4.0.0
96
+ version: 5.0.0
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rdoc
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: 3.9.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: json
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  description: This gem can parse values, validations, documentation, types, groups
112
126
  and conditions of parameters from your puppet modules
113
127
  email:
@@ -122,7 +136,9 @@ files:
122
136
  - lib/kafo_parsers.rb
123
137
  - lib/kafo_parsers/doc_parser.rb
124
138
  - lib/kafo_parsers/exceptions.rb
139
+ - lib/kafo_parsers/parsers.rb
125
140
  - lib/kafo_parsers/puppet_module_parser.rb
141
+ - lib/kafo_parsers/puppet_strings_module_parser.rb
126
142
  - lib/kafo_parsers/version.rb
127
143
  homepage: https://github.com/theforeman/kafo_parsers
128
144
  licenses:
@@ -149,3 +165,4 @@ signing_key:
149
165
  specification_version: 4
150
166
  summary: Puppet module parsers
151
167
  test_files: []
168
+ has_rdoc: