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