Module: Pdfmdmethods

Included in:
Pdfmd, Pdfmdstat
Defined in:
lib/pdfmd/pdfmdmethods.rb

Overview

Module: pdfmdmethods

Method to extend functionalities

Instance Method Summary collapse

Instance Method Details

#determineValidSetting(manualSetting, key) ⇒ Object

Determine the valid setting

  1. Priority: manual setting

  2. Priority: Hiera setting

If there is no manual setting, the value of ‘manualSetting’

should be set to 'nil'


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pdfmd/pdfmdmethods.rb', line 14

def determineValidSetting(manualSetting,key)

  if !@hieradata.nil? 
    hieraKey    = '@hieradata'
    hieraValue  = ''

    key.split(':').each do |keyname|

      hieraKeyCheck = eval(hieraKey)
      if !hieraKeyCheck.nil? and hieraKeyCheck.has_key?(keyname)
        hieraKey = hieraKey + "['#{keyname}']"
      else
        # Key has not been found
        hieraKey = ''
        break
      end 
    end

    hieraValue = eval(hieraKey)

  else
    hieraValue = nil
  end

  if !manualSetting.nil?
    self.log('debug', "Chosing manual setting '#{key} = #{manualSetting}'.")

    # if manualSetting is date, the actual field meant is "'create date'", not 'date'.
    key = key.gsub('date:', 'createdate:')

    manualSetting
  elsif !hieraValue.nil? or
    !hieraValue == ''

    self.log('debug', "Chosing hiera setting '#{key} = #{hieraValue}'.")
    hieraValue

  else
    self.log('debug', "No setting chosen for '#{key}' in hiera.")
    false 
  end

end

#log(status = 'info', message) ⇒ Object

Logging stuff



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pdfmd/pdfmdmethods.rb', line 61

def log(status = 'info', message)

  # Setting the loglevel
  case @loglevel
  when /info/i
    level = 'Logger::INFO'
  when /warn/i
    level = 'Logger::WARN'
  when /error/i
    level = 'Logger::ERROR'
  when /debug/i
    level = 'Logger::DEBUG'
  else
    level = 'Logger::INFO'
  end
  logger = Logger.new(@logfile)
  logger.level = eval level
  logger.send(status, message)
  logger.close

end

#queryHiera(keyword, facts = 'UNSET') ⇒ Object

Query hiera for settings if available



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/pdfmd/pdfmdmethods.rb', line 87

def queryHiera(keyword, facts = 'UNSET')

  pathHieraConfig = [
    '/etc/hiera.yaml',
    '/etc/puppet/hiera.yaml',
    '/etc/puppetlabs/puppet/hiera.yaml',
  ]
  hieraConfig = ''
  pathHieraConfig.each do |path|
    if File.exist? path
      hieraConfig = path
      break
    end
  end

  # Set default facts
  facts == 'UNSET' ? facts = "fqdn=#{`hostname`}".chomp : ''.chomp

  # If Hiera is not found (damn cat, get of my keyboard!), return false,
  # otherwise return the hash from Hiera
  if !system('which hiera > /dev/null 2>&1')
    self.log('warn','Cannot find hiera command in $path.')
    puts 'Cannot find "hiera" command in $path.'
    return eval('{}')
  else

    self.log('debug', 'Reading hiera values for pdfmd::config.')
    commandreturn = ''
    commandreturn = `hiera -c #{hieraConfig} #{keyword} #{facts} 2>/dev/null`

    if $?.exitstatus == 1
      self.log('warn', 'Could not retrieve configuration from with hiera.')
      eval('{}')
    else
      self.log('debug', 'Could retrieve configuration from hiera.')
      eval(commandreturn)
    end

  end

end