Class: Inspec::Resources::MysqlConf

Inherits:
Object
  • Object
show all
Includes:
FindFiles
Defined in:
lib/resources/mysql_conf.rb

Constant Summary

Constants included from FindFiles

FindFiles::TYPES

Instance Method Summary collapse

Methods included from FindFiles

#find_files, #find_files_or_error

Constructor Details

#initialize(conf_path = nil) ⇒ MysqlConf

Returns a new instance of MysqlConf.



41
42
43
44
45
46
47
# File 'lib/resources/mysql_conf.rb', line 41

def initialize(conf_path = nil)
  @conf_path = conf_path || inspec.mysql.conf_path
  @files_contents = {}
  @content = nil
  @params = nil
  read_content
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



62
63
64
65
# File 'lib/resources/mysql_conf.rb', line 62

def method_missing(name)
  @params || read_content
  @params[name.to_s]
end

Instance Method Details

#abs_path(dir, f) ⇒ Object



111
112
113
114
# File 'lib/resources/mysql_conf.rb', line 111

def abs_path(dir, f)
  return f if f.start_with? '/'
  File.join(dir, f)
end

#contentObject



49
50
51
# File 'lib/resources/mysql_conf.rb', line 49

def content
  @content ||= read_content
end

#include_files(reldir, conf) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/resources/mysql_conf.rb', line 101

def include_files(reldir, conf)
  files = conf.scan(/^!include\s+(.*)\s*/).flatten.compact.map { |x| abs_path(reldir, x) }
  dirs = conf.scan(/^!includedir\s+(.*)\s*/).flatten.compact.map { |x| abs_path(reldir, x) }
  dirs.map do |dir|
    # @TODO: non local glob
    files += find_files(dir, depth: 1, type: 'file')
  end
  files
end

#params(*opts) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/resources/mysql_conf.rb', line 53

def params(*opts)
  @params || read_content
  res = @params
  opts.each do |opt|
    res = res[opt] unless res.nil?
  end
  MysqlConfEntry.new(opts, res)
end

#read_contentObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/resources/mysql_conf.rb', line 67

def read_content
  @content = ''
  @params = {}

  # skip if the main configuration file doesn't exist
  if !inspec.file(@conf_path).file?
    return skip_resource "Can't find file \"#{@conf_path}\""
  end
  raw_conf = read_file(@conf_path)
  if raw_conf.empty? && !inspec.file(@conf_path).empty?
    return skip_resource("Can't read file \"#{@conf_path}\"")
  end

  to_read = [@conf_path]
  until to_read.empty?
    cur_file = to_read[0]
    raw_conf = read_file(cur_file)
    @content += raw_conf

    params = SimpleConfig.new(raw_conf).params
    @params = @params.deep_merge(params)

    to_read = to_read.drop(1)
    # see if there is more stuff to include

    dir = File.dirname(cur_file)
    to_read += include_files(dir, raw_conf).find_all do |fp|
      not @files_contents.key? fp
    end
  end
  #
  @content
end

#read_file(path) ⇒ Object



116
117
118
# File 'lib/resources/mysql_conf.rb', line 116

def read_file(path)
  @files_contents[path] ||= inspec.file(path).content
end

#to_sObject



120
121
122
# File 'lib/resources/mysql_conf.rb', line 120

def to_s
  'MySQL Configuration'
end