kafo 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of kafo might be problematic. Click here for more details.

@@ -1,45 +1,43 @@
1
1
  # encoding: UTF-8
2
- class PuppetCommand
3
- def initialize(command, options = [])
4
- @command = command
5
-
6
- # Expand the modules_path to work around the fact that Puppet doesn't
7
- # allow modulepath to contain relative (i.e ..) directory references as
8
- # of 2.7.23.
9
- @options = options.push("--modulepath #{File.expand_path(modules_path)}")
10
- @logger = KafoConfigure.logger
11
- end
2
+ module Kafo
3
+ class PuppetCommand
4
+ def initialize(command, options = [])
5
+ @command = command
6
+ @options = options.push("--modulepath #{modules_path}")
7
+ @logger = KafoConfigure.logger
8
+ end
12
9
 
13
- def custom_answer_file
14
- KafoConfigure.temp_config_file.nil? ? '' : "$kafo_answer_file=\"#{KafoConfigure.temp_config_file}\""
15
- end
10
+ def custom_answer_file
11
+ KafoConfigure.temp_config_file.nil? ? '' : "$kafo_answer_file=\"#{KafoConfigure.temp_config_file}\""
12
+ end
16
13
 
17
- def add_progress
18
- KafoConfigure.verbose ? '' : "$kafo_add_progress=true"
19
- end
14
+ def add_progress
15
+ KafoConfigure.verbose ? '' : "$kafo_add_progress=true"
16
+ end
20
17
 
21
- def command
22
- result = [
23
- "echo '$kafo_config_file=\"#{KafoConfigure.config_file}\" #{custom_answer_file} #{add_progress} #{@command}'",
24
- '|',
25
- "RUBYLIB=#{["#{KafoConfigure.gem_root}/modules", ENV['RUBYLIB']].join(File::PATH_SEPARATOR)}",
26
- "puppet apply #{@options.join(' ')} #{@suffix}",
27
- ].join(' ')
28
- @logger.debug result
29
- result
30
- end
18
+ def command
19
+ result = [
20
+ "echo '$kafo_config_file=\"#{KafoConfigure.config_file}\" #{custom_answer_file} #{add_progress} #{@command}'",
21
+ '|',
22
+ "RUBYLIB=#{["#{KafoConfigure.gem_root}/modules", ENV['RUBYLIB']].join(File::PATH_SEPARATOR)}",
23
+ "puppet apply #{@options.join(' ')} #{@suffix}",
24
+ ].join(' ')
25
+ @logger.debug result
26
+ result
27
+ end
31
28
 
32
- def append(suffix)
33
- @suffix = suffix
34
- self
35
- end
29
+ def append(suffix)
30
+ @suffix = suffix
31
+ self
32
+ end
36
33
 
37
- private
34
+ private
38
35
 
39
- def modules_path
40
- [
41
- KafoConfigure.modules_dir,
42
- KafoConfigure.kafo_modules_dir,
43
- ].join(':')
36
+ def modules_path
37
+ [
38
+ KafoConfigure.modules_dir,
39
+ KafoConfigure.kafo_modules_dir,
40
+ ].join(':')
41
+ end
44
42
  end
45
43
  end
@@ -4,90 +4,114 @@ require 'kafo/param_builder'
4
4
  require 'kafo/puppet_module_parser'
5
5
  require 'kafo/validator'
6
6
 
7
- class PuppetModule
8
- attr_reader :name, :params, :dir_name, :class_name, :manifest_name, :manifest_path
9
-
10
- def initialize(name, parser = PuppetModuleParser)
11
- @name = name
12
- @dir_name = get_dir_name
13
- @manifest_name = get_manifest_name
14
- @class_name = get_class_name
15
- @params = []
16
- @manifest_path = File.join(KafoConfigure.root_dir, '/modules/', module_manifest_path)
17
- @parser = parser
18
- @validations = []
19
- @logger = KafoConfigure.logger
20
- end
7
+ module Kafo
8
+ class PuppetModule
9
+ PRIMARY_GROUP_NAME = 'Parameters'
10
+
11
+ attr_reader :name, :params, :dir_name, :class_name, :manifest_name, :manifest_path,
12
+ :groups
13
+
14
+ def initialize(name, parser = PuppetModuleParser)
15
+ @name = name
16
+ @dir_name = get_dir_name
17
+ @manifest_name = get_manifest_name
18
+ @class_name = get_class_name
19
+ @params = []
20
+ @manifest_path = File.join(KafoConfigure.root_dir, '/modules/', module_manifest_path)
21
+ @parser = parser
22
+ @validations = []
23
+ @logger = KafoConfigure.logger
24
+ @groups = {}
25
+ end
21
26
 
22
- def enabled?
23
- @enabled.nil? ? @enabled = KafoConfigure.config.module_enabled?(self) : @enabled
24
- end
27
+ def enabled?
28
+ @enabled.nil? ? @enabled = KafoConfigure.config.module_enabled?(self) : @enabled
29
+ end
25
30
 
26
- def disable
27
- @enabled = false
28
- end
31
+ def disable
32
+ @enabled = false
33
+ end
29
34
 
30
- def enable
31
- @enabled = true
32
- end
35
+ def enable
36
+ @enabled = true
37
+ end
33
38
 
34
- def parse(builder_klass = ParamBuilder)
35
- @params = []
36
- raw_data = @parser.parse(manifest_path)
37
- builder = builder_klass.new(self, raw_data)
38
- @validations = raw_data['validations']
39
+ def parse(builder_klass = ParamBuilder)
40
+ @params = []
41
+ raw_data = @parser.parse(manifest_path)
42
+ builder = builder_klass.new(self, raw_data)
43
+ @validations = raw_data[:validations]
39
44
 
40
- builder.validate
41
- @params = builder.build_params
45
+ builder.validate
46
+ @params = builder.build_params
47
+ @groups = builder.build_param_groups(@params)
42
48
 
43
- self
44
- rescue ConfigurationException => e
45
- puts "Unable to continue because of:"
46
- puts e.message
47
- KafoConfigure.exit(:manifest_error)
48
- end
49
+ self
50
+ rescue ConfigurationException => e
51
+ @logger.error "Unable to continue because of: #{e.message}"
52
+ KafoConfigure.exit(:manifest_error)
53
+ end
54
+
55
+ def primary_parameter_group
56
+ @groups.detect { |g| g.formatted_name == PRIMARY_GROUP_NAME } || dummy_primary_group
57
+ end
49
58
 
50
- def validations(param = nil)
51
- if param.nil?
52
- @validations
53
- else
54
- @validations.select do |validation|
55
- validation.arguments.map(&:to_s).include?("$#{param.name}")
59
+ def other_parameter_groups
60
+ @groups.select { |g| g.formatted_name != PRIMARY_GROUP_NAME }
61
+ end
62
+
63
+ def validations(param = nil)
64
+ if param.nil?
65
+ @validations
66
+ else
67
+ @validations.select do |validation|
68
+ validation.arguments.map(&:to_s).include?("$#{param.name}")
69
+ end
56
70
  end
57
71
  end
58
- end
59
72
 
60
- def params_hash
61
- Hash[params.map { |param| [param.name, param.value] }]
62
- end
73
+ def params_hash
74
+ Hash[params.map { |param| [param.name, param.value] }]
75
+ end
63
76
 
64
- def <=> o
65
- self.name <=> o.name
66
- end
77
+ def <=> o
78
+ self.name <=> o.name
79
+ end
67
80
 
68
- private
81
+ private
69
82
 
70
- # mapping from configuration with stringified keys
71
- def mapping
72
- @mapping ||= Hash[KafoConfigure.config.app[:mapping].map { |k, v| [k.to_s, v] }]
73
- end
83
+ # used when user haven't specified any primary group by name, we create a new group
84
+ # that holds all other groups as children, if we have no other groups (no children)
85
+ # we set all parameters that this module hold
86
+ def dummy_primary_group
87
+ group = ParamGroup.new(PRIMARY_GROUP_NAME)
88
+ other_parameter_groups.each { |child| group.add_child(child) }
89
+ @params.each { |p| group.add_param(p) } if group.children.empty?
90
+ group
91
+ end
74
92
 
75
- # custom module directory name
76
- def get_dir_name
77
- mapping[name].nil? ? name : mapping[name][:dir_name]
78
- end
93
+ # mapping from configuration with stringified keys
94
+ def mapping
95
+ @mapping ||= Hash[KafoConfigure.config.app[:mapping].map { |k, v| [k.to_s, v] }]
96
+ end
79
97
 
80
- # custom manifest filename without .pp extension
81
- def get_manifest_name
82
- mapping[name].nil? ? 'init' : mapping[name][:manifest_name]
83
- end
98
+ # custom module directory name
99
+ def get_dir_name
100
+ mapping[name].nil? ? name : mapping[name][:dir_name]
101
+ end
84
102
 
85
- def get_class_name
86
- manifest_name == 'init' ? name : "#{dir_name}::#{manifest_name}"
87
- end
103
+ # custom manifest filename without .pp extension
104
+ def get_manifest_name
105
+ mapping[name].nil? ? 'init' : mapping[name][:manifest_name]
106
+ end
88
107
 
89
- def module_manifest_path
90
- "#{dir_name}/manifests/#{manifest_name}.pp"
91
- end
108
+ def get_class_name
109
+ manifest_name == 'init' ? name : "#{dir_name}::#{manifest_name}"
110
+ end
111
+
112
+ def module_manifest_path
113
+ "#{dir_name}/manifests/#{manifest_name}.pp"
114
+ end
92
115
 
116
+ end
93
117
  end
@@ -1,74 +1,77 @@
1
1
  # encoding: UTF-8
2
2
  require 'puppet'
3
- require 'rdoc'
4
- require 'rdoc/markup' # required for RDoc < 0.9.5
5
- require 'rdoc/markup/parser' # required for RDoc < 0.9.5
3
+ require 'kafo/doc_parser'
6
4
 
7
- # Based on ideas from puppet-parse by Johan van den Dorpe
8
- class PuppetModuleParser
9
- def self.parse(file)
10
- content = new(file)
5
+ module Kafo
6
+ # Based on ideas from puppet-parse by Johan van den Dorpe
7
+ # we don't build any tree structure since e.g. params from doc does not
8
+ # have to be defined in puppet DSL and vice versa, we just gather all info
9
+ # we can read from the whole manifest
10
+ class PuppetModuleParser
11
+ def self.parse(file)
12
+ content = new(file)
13
+ docs = content.docs
11
14
 
12
- {
13
- 'parameters' => content.parameters,
14
- 'docs' => content.docs,
15
- 'validations' => content.validations
16
- }
17
- end
15
+ data = {
16
+ :values => content.values,
17
+ :validations => content.validations
18
+ }
19
+ data[:parameters] = data[:values].keys
20
+ data.merge!(docs)
21
+ data
22
+ end
18
23
 
19
- def initialize(file)
20
- raise ModuleName, "File not found #{file}, check you answer file" unless File.exists?(file)
21
- parser = Puppet::Parser::Parser.new('production')
22
- values = Puppet.settings.instance_variable_get('@values')
23
- values[:production][:confdir] ||= '/' # just some stubbing
24
- parser.import(file)
24
+ def initialize(file)
25
+ @file = file
26
+ raise ModuleName, "File not found #{file}, check you answer file" unless File.exists?(file)
27
+ parser = Puppet::Parser::Parser.new('production')
28
+ values = Puppet.settings.instance_variable_get('@values')
29
+ values[:production][:confdir] ||= '/' # just some stubbing
30
+ parser.import(@file)
25
31
 
26
- # Find object in list of hostclasses
27
- parser.environment.known_resource_types.hostclasses.each do |x|
28
- @object = x.last if x.last.file == file
29
- end
30
- # Find object in list of definitions
31
- parser.environment.known_resource_types.definitions.each do |x|
32
- @object = x.last if x.last.file == file
32
+ # Find object corresponding to class defined in init.pp in list of hostclasses
33
+ parser.environment.known_resource_types.hostclasses.each do |ast_objects|
34
+ ast_type = ast_objects.last
35
+ @object = ast_type if ast_type.file == file
36
+ end
37
+ # Find object in list of definitions
38
+ parser.environment.known_resource_types.definitions.each do |ast_objects|
39
+ ast_type = ast_objects.last
40
+ @object = ast_type.last if ast_type.last.file == file
41
+ end
33
42
  end
34
- end
35
43
 
36
- # TODO - store parsed object type (Puppet::Parser::AST::Variable must be dumped later)
37
- def parameters
38
- parameters = {}
39
- arguments = @object.respond_to?(:arguments) ? @object.arguments : {}
40
- arguments.each { |k, v| parameters[k] = v.respond_to?(:value) ? v.value : nil }
41
- parameters
42
- end
43
-
44
- def klass
45
- @object.name if @object.class.respond_to?(:name)
46
- end
44
+ # TODO - store parsed object type (Puppet::Parser::AST::Variable must be dumped later)
45
+ def values
46
+ parameters = {}
47
+ arguments = @object.respond_to?(:arguments) ? @object.arguments : {}
48
+ arguments.each { |k, v| parameters[k] = v.respond_to?(:value) ? v.value : nil }
49
+ parameters
50
+ end
47
51
 
48
- def validations(param = nil)
49
- @object.code.select { |stmt| stmt.is_a?(Puppet::Parser::AST::Function) && stmt.name =~ /^validate_/ }
50
- end
52
+ def validations(param = nil)
53
+ @object.code.select { |stmt| stmt.is_a?(Puppet::Parser::AST::Function) && stmt.name =~ /^validate_/ }
54
+ end
51
55
 
52
- def docs
53
- docs = {}
54
- if !@object.doc.nil?
55
- if RDoc::Markup.respond_to?(:parse)
56
- rdoc = RDoc::Markup.parse(@object.doc)
57
- else # RDoc < 3.10.0
58
- rdoc = RDoc::Markup::Parser.parse(@object.doc)
59
- end
60
- items = rdoc.parts.select { |part| part.respond_to?(:items) }.map(&:items).flatten
61
- items.each do |item|
62
- # Skip rdoc items that aren't paragraphs
63
- next unless (item.parts.to_s.scan("RDoc::Markup::Paragraph") == ["RDoc::Markup::Paragraph"])
64
- # RDoc (>= 4) makes label an array
65
- label = item.label.is_a?(Array) ? item.label.first : item.label
66
- # Documentation must be a list - if there's no label then skip
67
- next if label.nil?
68
- key = label.tr('^A-Za-z0-9_-', '')
69
- docs[key] = item.parts.first.parts.map!(&:strip)
56
+ # returns data in following form
57
+ # {
58
+ # :docs => { $param1 => 'documentation without types and conditions'}
59
+ # :types => { $param1 => 'boolean'},
60
+ # :groups => { $param1 => ['Parameters', 'Advanced']},
61
+ # :conditions => { $param1 => '$db_type == "mysql"'},
62
+ # }
63
+ def docs
64
+ data = { :docs => {}, :types => {}, :groups => {}, :conditions => {} }
65
+ if @object.nil?
66
+ raise DocParseError, "no documentation found for manifest #{@file}, parsing error?"
67
+ elsif !@object.doc.nil?
68
+ parser = DocParser.new(@object.doc).parse
69
+ data[:docs] = parser.docs
70
+ data[:groups] = parser.groups
71
+ data[:types] = parser.types
72
+ data[:conditions] = parser.conditions
70
73
  end
74
+ data
71
75
  end
72
- docs
73
76
  end
74
77
  end
@@ -1,21 +1,25 @@
1
1
  # encoding: UTF-8
2
- module StringHelper
3
- def dashize(string)
4
- string.tr('_', '-')
5
- end
6
- alias :d :dashize
2
+ module Kafo
3
+ module StringHelper
4
+ def dashize(string)
5
+ string.tr('_', '-')
6
+ end
7
7
 
8
- def underscore(string)
9
- string.tr('-', '_')
10
- end
11
- alias :u :underscore
8
+ alias :d :dashize
12
9
 
13
- def with_prefix(param)
14
- prefix = KafoConfigure.config.app[:no_prefix] ? '' : "#{d(param.module_name)}-"
15
- "#{prefix}#{d(param.name)}"
16
- end
10
+ def underscore(string)
11
+ string.tr('-', '_')
12
+ end
13
+
14
+ alias :u :underscore
15
+
16
+ def with_prefix(param)
17
+ prefix = KafoConfigure.config.app[:no_prefix] ? '' : "#{d(param.module_name)}-"
18
+ "#{prefix}#{d(param.name)}"
19
+ end
17
20
 
18
- def parametrize(param)
19
- "--#{with_prefix(param)}"
21
+ def parametrize(param)
22
+ "--#{with_prefix(param)}"
23
+ end
20
24
  end
21
25
  end
@@ -1,26 +1,28 @@
1
1
  # encoding: UTF-8
2
2
 
3
- class SystemChecker
4
- def self.check
5
- new(File.join(KafoConfigure.root_dir, 'checks', '*')).check
6
- end
7
-
8
- def initialize(path)
9
- @checkers = Dir.glob(path)
10
- end
3
+ module Kafo
4
+ class SystemChecker
5
+ def self.check
6
+ new(File.join(KafoConfigure.root_dir, 'checks', '*')).check
7
+ end
11
8
 
12
- def logger
13
- Logging::logger['checks']
14
- end
9
+ def initialize(path)
10
+ @checkers = Dir.glob(path)
11
+ end
15
12
 
16
- def check
17
- @checkers.map! do |checker|
18
- logger.debug "Executing checker: #{checker}"
19
- stdout = `#{checker}`
20
- logger.error stdout unless stdout.empty?
21
- $?.exitstatus == 0
13
+ def logger
14
+ Logging::logger['checks']
22
15
  end
23
16
 
24
- @checkers.all?
17
+ def check
18
+ @checkers.map! do |checker|
19
+ logger.debug "Executing checker: #{checker}"
20
+ stdout = `#{checker}`
21
+ logger.error stdout unless stdout.empty?
22
+ $?.exitstatus == 0
23
+ end
24
+
25
+ @checkers.all?
26
+ end
25
27
  end
26
28
  end