Skip to content

Commit

Permalink
Merge pull request #1599 from ekohl/fix-postgresql_conf
Browse files Browse the repository at this point in the history
Avoid opening the file in postgresql_conf
  • Loading branch information
Ramesh7 authored Jul 12, 2024
2 parents 880a6f7 + f2b193d commit a72637b
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions lib/puppet/provider/postgresql_conf/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@
# The function parses the postgresql.conf and figures out which active settings exist in a config file and returns an array of hashes
#
def parse_config
# open the config file
file = File.open(resource[:target])
# regex to match active keys, values and comments
active_values_regex = %r{^\s*(?<key>[\w.]+)\s*=?\s*(?<value>.*?)(?:\s*#\s*(?<comment>.*))?\s*$}
# empty array to be filled with hashes
active_settings = []
# iterate the file and construct a hash for every matching/active setting
# the hash is pushed to the array and the array is returned
File.foreach(file).with_index do |line, index|
line_number = index + 1
File.foreach(resource[:target]).with_index(1) do |line, line_number|
matches = line.match(active_values_regex)
if matches
value = if matches[:value].to_i.to_s == matches[:value]
Expand All @@ -36,7 +33,7 @@ def parse_config
active_settings.push(attributes_hash)
end
end
Puppet.debug("DEBUG: parse_config Active Settings found in Postgreql config file: #{active_settings}")
Puppet.debug("DEBUG: parse_config Active Settings found in PostgreSQL config file: #{active_settings}")
active_settings
end

Expand All @@ -63,12 +60,11 @@ def add_header(lines)

# This function writes the config file, it removes the old header, adds a new one and writes the file
#
# @param [File] the file object of the postgresql configuration file
# @param [Array] lines of the parsed postgresql configuration file
def write_config(file, lines)
def write_config(lines)
lines = delete_header(lines)
lines = add_header(lines)
File.write(file, lines.join)
File.write(resource[:target], lines.join)
end

# check, if resource exists in postgresql.conf file
Expand All @@ -85,23 +81,21 @@ def exists?
# remove resource if exists and is set to absent
def destroy
entry_regex = %r{#{resource[:key]}.*=.*#{resource[:value]}}
file = File.open(resource[:target])
lines = File.readlines(file)
lines = File.readlines(resource[:target])

lines.delete_if do |entry|
entry.match?(entry_regex)
end
write_config(file, lines)
write_config(lines)
end

# create resource if it does not exists
def create
file = File.open(resource[:target])
lines = File.readlines(file)
lines = File.readlines(resource[:target])
new_line = line(key: resource[:key], value: resource[:value], comment: resource[:comment])

lines.push(new_line)
write_config(file, lines)
write_config(lines)
end

# getter - get value of a resource
Expand All @@ -116,30 +110,28 @@ def comment

# setter - set value of a resource
def value=(_value)
file = File.open(resource[:target])
lines = File.readlines(file)
lines = File.readlines(resource[:target])
active_values_regex = %r{^\s*(?<key>[\w.]+)\s*=?\s*(?<value>.*?)(?:\s*#\s*(?<comment>.*))?\s*$}
new_line = line(key: resource[:key], value: resource[:value], comment: resource[:comment])

lines.each_with_index do |line, index|
matches = line.to_s.match(active_values_regex)
lines[index] = new_line if matches && (matches[:key] == resource[:key] && matches[:value] != resource[:value])
end
write_config(file, lines)
write_config(lines)
end

# setter - set comment of a resource
def comment=(_comment)
file = File.open(resource[:target])
lines = File.readlines(file)
lines = File.readlines(resource[:target])
active_values_regex = %r{^\s*(?<key>[\w.]+)\s*=?\s*(?<value>.*?)(?:\s*#\s*(?<comment>.*))?\s*$}
new_line = line(key: resource[:key], value: resource[:value], comment: resource[:comment])

lines.each_with_index do |line, index|
matches = line.to_s.match(active_values_regex)
lines[index] = new_line if matches && (matches[:key] == resource[:key] && matches[:comment] != resource[:comment])
end
write_config(file, lines)
write_config(lines)
end

private
Expand Down

0 comments on commit a72637b

Please sign in to comment.