Class: Kafo::PuppetModule

Inherits:
Object
  • Object
show all
Defined in:
lib/kafo/puppet_module.rb

Constant Summary collapse

PRIMARY_GROUP_NAME =
'Parameters'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, parser = self.class.find_parser, configuration = KafoConfigure.config) ⇒ PuppetModule

Returns a new instance of PuppetModule.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kafo/puppet_module.rb', line 28

def initialize(identifier, parser = self.class.find_parser, configuration = KafoConfigure.config)
  @identifier        = identifier
  @configuration     = configuration
  @name              = get_name
  @dir_name          = get_dir_name
  @manifest_name     = get_manifest_name
  @class_name        = get_class_name
  @params            = []
  if @configuration.module_dirs.count == 1
    module_dir       = @configuration.module_dirs.first
  else
    module_dir         = @configuration.module_dirs.find { |dir| File.exists?(File.join(dir, module_manifest_path)) } ||
      warn("Manifest #{module_manifest_path} was not found in #{@configuration.module_dirs.join(', ')}")
  end
  @manifest_path     = File.join(module_dir, module_manifest_path)
  @parser            = parser
  @parser_cache      = @configuration.parser_cache
  @logger            = KafoConfigure.logger
  @groups            = {}
  @params_path       = get_params_path
  @params_class_name = get_params_class_name
  @raw_data          = nil
  @enabled           = nil
end

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



11
12
13
# File 'lib/kafo/puppet_module.rb', line 11

def class_name
  @class_name
end

#configurationObject (readonly)

Returns the value of attribute configuration.



11
12
13
# File 'lib/kafo/puppet_module.rb', line 11

def configuration
  @configuration
end

#dir_nameObject (readonly)

Returns the value of attribute dir_name.



11
12
13
# File 'lib/kafo/puppet_module.rb', line 11

def dir_name
  @dir_name
end

#groupsObject (readonly)

Returns the value of attribute groups.



11
12
13
# File 'lib/kafo/puppet_module.rb', line 11

def groups
  @groups
end

#identifierObject (readonly)

Returns the value of attribute identifier.



11
12
13
# File 'lib/kafo/puppet_module.rb', line 11

def identifier
  @identifier
end

#manifest_nameObject (readonly)

Returns the value of attribute manifest_name.



11
12
13
# File 'lib/kafo/puppet_module.rb', line 11

def manifest_name
  @manifest_name
end

#manifest_pathObject (readonly)

Returns the value of attribute manifest_path.



11
12
13
# File 'lib/kafo/puppet_module.rb', line 11

def manifest_path
  @manifest_path
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/kafo/puppet_module.rb', line 11

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



11
12
13
# File 'lib/kafo/puppet_module.rb', line 11

def params
  @params
end

#params_class_nameObject (readonly)

Returns the value of attribute params_class_name.



11
12
13
# File 'lib/kafo/puppet_module.rb', line 11

def params_class_name
  @params_class_name
end

#params_pathObject (readonly)

Returns the value of attribute params_path.



11
12
13
# File 'lib/kafo/puppet_module.rb', line 11

def params_path
  @params_path
end

#raw_dataObject (readonly)

Returns the value of attribute raw_data.



11
12
13
# File 'lib/kafo/puppet_module.rb', line 11

def raw_data
  @raw_data
end

Class Method Details

.find_parserObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kafo/puppet_module.rb', line 14

def self.find_parser
  @parser ||= begin
    logger = KafoConfigure.logger
    parser = KafoParsers::Parsers.find_available(:logger => logger)
    if parser
      logger.debug "Using Puppet module parser #{parser}"
      parser
    else
      logger.debug "No available Puppet module parser found"
      :none  # prevent continually re-checking
    end
  end
end

Instance Method Details

#<=>(o) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/kafo/puppet_module.rb', line 99

def <=> o
  @configuration.app[:low_priority_modules].each do |module_name|
    return 1 if self.name.include?(module_name) && !o.name.include?(module_name)
    return -1 if !self.name.include?(module_name) && o.name.include?(module_name)
    if self.name.include?(module_name) && o.name.include?(module_name)
      return self.name.sub(/.*#{module_name}/, '') <=> o.name.sub(/.*#{module_name}/, '')
    end
  end

  self.name <=> o.name
end

#disableObject



57
58
59
# File 'lib/kafo/puppet_module.rb', line 57

def disable
  @enabled = false
end

#enableObject



61
62
63
# File 'lib/kafo/puppet_module.rb', line 61

def enable
  @enabled = true
end

#enabled?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/kafo/puppet_module.rb', line 53

def enabled?
  @enabled.nil? ? @enabled = @configuration.module_enabled?(self) : @enabled
end

#other_parameter_groupsObject



91
92
93
# File 'lib/kafo/puppet_module.rb', line 91

def other_parameter_groups
  @groups.select { |g| g.formatted_name != PRIMARY_GROUP_NAME }
end

#params_hashObject



95
96
97
# File 'lib/kafo/puppet_module.rb', line 95

def params_hash
  Hash[params.map { |param| [param.name, param.value] }]
end

#parse(builder_klass = ParamBuilder) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/kafo/puppet_module.rb', line 65

def parse(builder_klass = ParamBuilder)
  @raw_data = @parser_cache.get(identifier, manifest_path) if @parser_cache
  if @raw_data.nil?
    if @parser.nil? || @parser == :none
      raise ParserError.new("No Puppet module parser is installed and no cache of the file #{manifest_path} is available. Please check debug logs and install optional dependencies for the parser.")
    else
      @raw_data = @parser.parse(manifest_path)
    end
  end

  builder      = builder_klass.new(self, @raw_data)

  builder.validate
  @params = builder.build_params
  @groups = builder.build_param_groups(@params)

  self
rescue ConfigurationException => e
  @logger.fatal "Unable to continue because of: #{e.message}"
  KafoConfigure.exit(:manifest_error)
end

#primary_parameter_groupObject



87
88
89
# File 'lib/kafo/puppet_module.rb', line 87

def primary_parameter_group
  @groups.detect { |g| g.formatted_name == PRIMARY_GROUP_NAME } || dummy_primary_group
end