| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
# This provider is used to manage postgresql.conf files |
| 4 |
# It uses ruby to parse the config file and |
| 5 |
# to add, remove or modify settings. |
| 6 |
# |
| 7 |
# The provider is able to parse postgresql.conf files with the following format: |
| 8 |
# key = value # comment |
| 9 |
|
| 10 |
Puppet::Type.type(:postgresql_conf).provide(:ruby) do |
| 11 |
desc 'Set keys, values and comments in a postgresql config file.' |
| 12 |
|
| 13 |
# The function parses the postgresql.conf and figures out which active settings exist in a config file and returns an array of hashes |
| 14 |
# |
| 15 |
def parse_config |
| 16 |
# regex to match active keys, values and comments |
| 17 |
active_values_regex = %r{^\s*(?<key>[\w.]+)\s*=?\s*(?<value>.*?)(?:\s*#\s*(?<comment>.*))?\s*$} |
| 18 |
# empty array to be filled with hashes |
| 19 |
active_settings = [] |
| 20 |
# iterate the file and construct a hash for every matching/active setting |
| 21 |
# the hash is pushed to the array and the array is returned |
| 22 |
File.foreach(resource[:target]).with_index(1) do |line, line_number| |
| 23 |
matches = line.match(active_values_regex) |
| 24 |
if matches |
| 25 |
value = if matches[:value].to_i.to_s == matches[:value] |
| 26 |
matches[:value].to_i |
| 27 |
elsif matches[:value].to_f.to_s == matches[:value] |
| 28 |
matches[:value].to_f |
| 29 |
else |
| 30 |
matches[:value].delete("'") |
| 31 |
end |
| 32 |
attributes_hash = { line_number: line_number, key: matches[:key], ensure: 'present', value: value, comment: matches[:comment] } |
| 33 |
active_settings.push(attributes_hash) |
| 34 |
end |
| 35 |
end |
| 36 |
Puppet.debug("DEBUG: parse_config Active Settings found in PostgreSQL config file: #{active_settings}") |
| 37 |
active_settings |
| 38 |
end |
| 39 |
|
| 40 |
# Deletes an existing header from a parsed postgresql.conf configuration file |
| 41 |
# |
| 42 |
# @param [Array] lines of the parsed postgresql configuration file |
| 43 |
def delete_header(lines) |
| 44 |
header_regex = %r{^# HEADER:.*} |
| 45 |
lines.delete_if do |entry| |
| 46 |
entry.match?(header_regex) |
| 47 |
end |
| 48 |
end |
| 49 |
|
| 50 |
# Adds a header to a parsed postgresql.conf configuration file, after all other changes are made |
| 51 |
# |
| 52 |
# @param [Array] lines of the parsed postgresql configuration file |
| 53 |
def add_header(lines) |
| 54 |
timestamp = Time.now.strftime('%F %T %z') |
| 55 |
header = ["# HEADER: This file was autogenerated at #{timestamp}\n", |
| 56 |
"# HEADER: by puppet. While it can still be managed manually, it\n", |
| 57 |
"# HEADER: is definitely not recommended.\n"] |
| 58 |
header + lines |
| 59 |
end |
| 60 |
|
| 61 |
# This function writes the config file, it removes the old header, adds a new one and writes the file |
| 62 |
# |
| 63 |
# @param [Array] lines of the parsed postgresql configuration file |
| 64 |
def write_config(lines) |
| 65 |
lines = delete_header(lines) |
| 66 |
lines = add_header(lines) |
| 67 |
File.write(resource[:target], lines.join) |
| 68 |
end |
| 69 |
|
| 70 |
# check, if resource exists in postgresql.conf file |
| 71 |
def exists? |
| 72 |
select = parse_config.select { |hash| hash[:key] == resource[:key] } |
| 73 |
raise Puppet::Error, "found multiple config items of #{resource[:key]}, please fix this" if select.length > 1 |
| 74 |
return false if select.empty? |
| 75 |
|
| 76 |
@result = select.first |
| 77 |
Puppet.debug("DEBUG: exists? @result: #{@result}") |
| 78 |
true |
| 79 |
end |
| 80 |
|
| 81 |
# remove resource if exists and is set to absent |
| 82 |
def destroy |
| 83 |
entry_regex = %r{#{resource[:key]}.*=.*#{resource[:value]}} |
| 84 |
lines = File.readlines(resource[:target]) |
| 85 |
|
| 86 |
lines.delete_if do |entry| |
| 87 |
entry.match?(entry_regex) |
| 88 |
end |
| 89 |
write_config(lines) |
| 90 |
end |
| 91 |
|
| 92 |
# create resource if it does not exists |
| 93 |
def create |
| 94 |
lines = File.readlines(resource[:target]) |
| 95 |
new_line = line(key: resource[:key], value: resource[:value], comment: resource[:comment]) |
| 96 |
|
| 97 |
lines.push(new_line) |
| 98 |
write_config(lines) |
| 99 |
end |
| 100 |
|
| 101 |
# getter - get value of a resource |
| 102 |
def value |
| 103 |
@result[:value] |
| 104 |
end |
| 105 |
|
| 106 |
# getter - get comment of a resource |
| 107 |
def comment |
| 108 |
@result[:comment] |
| 109 |
end |
| 110 |
|
| 111 |
# setter - set value of a resource |
| 112 |
def value=(_value) |
| 113 |
lines = File.readlines(resource[:target]) |
| 114 |
active_values_regex = %r{^\s*(?<key>[\w.]+)\s*=?\s*(?<value>.*?)(?:\s*#\s*(?<comment>.*))?\s*$} |
| 115 |
new_line = line(key: resource[:key], value: resource[:value], comment: resource[:comment]) |
| 116 |
|
| 117 |
lines.each_with_index do |line, index| |
| 118 |
matches = line.to_s.match(active_values_regex) |
| 119 |
lines[index] = new_line if matches && (matches[:key] == resource[:key] && matches[:value] != resource[:value]) |
| 120 |
end |
| 121 |
write_config(lines) |
| 122 |
end |
| 123 |
|
| 124 |
# setter - set comment of a resource |
| 125 |
def comment=(_comment) |
| 126 |
lines = File.readlines(resource[:target]) |
| 127 |
active_values_regex = %r{^\s*(?<key>[\w.]+)\s*=?\s*(?<value>.*?)(?:\s*#\s*(?<comment>.*))?\s*$} |
| 128 |
new_line = line(key: resource[:key], value: resource[:value], comment: resource[:comment]) |
| 129 |
|
| 130 |
lines.each_with_index do |line, index| |
| 131 |
matches = line.to_s.match(active_values_regex) |
| 132 |
lines[index] = new_line if matches && (matches[:key] == resource[:key] && matches[:comment] != resource[:comment]) |
| 133 |
end |
| 134 |
write_config(lines) |
| 135 |
end |
| 136 |
|
| 137 |
private |
| 138 |
|
| 139 |
# Takes elements for a postgresql.conf configuration line and formats them properly |
| 140 |
# |
| 141 |
# @param [String] key postgresql.conf configuration option |
| 142 |
# @param [String] value the value for the configuration option |
| 143 |
# @param [String] comment optional comment that will be added at the end of the line |
| 144 |
# @return [String] line the whole line for the config file, with \n |
| 145 |
def line(key: '', value: '', comment: nil) |
| 146 |
value = value.to_s if value.is_a?(Numeric) |
| 147 |
dontneedquote = value.match(%r{^(\d+.?\d+|\w+)$}) |
| 148 |
dontneedequal = key.match(%r{^(include|include_if_exists)$}i) |
| 149 |
line = key.downcase # normalize case |
| 150 |
line += dontneedequal ? ' ' : ' = ' |
| 151 |
line += "'" unless dontneedquote && !dontneedequal |
| 152 |
line += value |
| 153 |
line += "'" unless dontneedquote && !dontneedequal |
| 154 |
line += " # #{comment}" unless comment.nil? || comment == :absent |
| 155 |
line += "\n" |
| 156 |
line |
| 157 |
end |
| 158 |
end |