-
Notifications
You must be signed in to change notification settings - Fork 611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend postgresql_conf to support multiple config files using ParsedFile #1542
Draft
ekohl
wants to merge
2
commits into
puppetlabs:main
Choose a base branch
from
ekohl:reuse-parsed-file
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
postgresql_conf { '/tmp/first-postgresql.conf:other': | ||
value => 'bla', | ||
} | ||
|
||
postgresql_conf { '/tmp/first-postgresql.conf:port': | ||
value => 5432, | ||
} | ||
|
||
postgresql_conf { '/tmp/second-postgresql.conf:other': | ||
value => 'bla', | ||
} | ||
|
||
postgresql_conf { '/tmp/second-postgresql.conf:port': | ||
value => 5433, | ||
} | ||
|
||
# TODO: make target optional | ||
#postgresql_conf { 'port': | ||
# value => 5434, | ||
#} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'puppet/provider/parsedfile' | ||
|
||
Puppet::Type.type(:postgresql_conf).provide( | ||
:parsed, | ||
parent: Puppet::Provider::ParsedFile, | ||
default_target: '/etc/postgresql.conf', | ||
filetype: :flat, | ||
) do | ||
desc 'Set key/values in postgresql.conf.' | ||
|
||
text_line :comment, match: %r{^\s*#} | ||
text_line :blank, match: %r{^\s*$} | ||
|
||
record_line :parsed, | ||
fields: ['key', 'value', 'comment'], | ||
optional: ['comment'], | ||
match: %r{^\s*([\w.]+)\s*=?\s*(.*?)(?:\s*#\s*(.*))?\s*$}, | ||
to_line: proc { |h| | ||
# simple string and numeric values don't need to be enclosed in quotes | ||
val = if h[:value].is_a?(Numeric) | ||
h[:value].to_s | ||
elsif h[:value].is_a?(Array) | ||
# multiple listen_addresses specified as a string containing a comma-speparated list | ||
h[:value].join(', ') | ||
else | ||
h[:value] | ||
end | ||
dontneedquote = val.match(%r{^(\d+.?\d+|\w+)$}) | ||
dontneedequal = h[:key].match(%r{^(include|include_if_exists)$}i) | ||
|
||
str = h[:key].downcase # normalize case | ||
str += dontneedequal ? ' ' : ' = ' | ||
str += "'" unless dontneedquote && !dontneedequal | ||
str += val | ||
str += "'" unless dontneedquote && !dontneedequal | ||
str += " # #{h[:comment]}" unless h[:comment].nil? || h[:comment] == :absent | ||
str | ||
}, | ||
post_parse: proc { |h| | ||
h[:key].downcase! # normalize case | ||
h[:value].gsub!(%r{(^'|'$)}, '') # strip out quotes | ||
} | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,40 +2,36 @@ | |
|
||
Puppet::Type.newtype(:postgresql_conf) do | ||
@doc = 'This type allows puppet to manage postgresql.conf parameters.' | ||
|
||
ensurable | ||
|
||
newparam(:name) do | ||
desc 'A unique title for the resource.' | ||
newvalues(%r{^[\w.]+$}) | ||
def self.title_patterns | ||
[ | ||
[ /^(.+\.conf):([\w.]+)$/m, [ [:target], [:key] ] ], | ||
# TODO: make target optional | ||
#[ /^([\w.]+)$/m, [ [:key] ] ], | ||
] | ||
end | ||
|
||
newparam(:key) do | ||
desc 'The Postgresql parameter to manage.' | ||
|
||
newparam(:key, namevar: true) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept |
||
desc 'The postgresql parameter name to manage.' | ||
isrequired | ||
newvalues(%r{^[\w.]+$}) | ||
end | ||
|
||
newproperty(:value) do | ||
desc 'The value to set for this parameter.' | ||
newvalues(%r{^\S(.*\S)?$}) | ||
end | ||
|
||
munge do |value| | ||
if value.to_i.to_s == value | ||
value.to_i | ||
elsif value.to_f.to_s == value | ||
value.to_f | ||
newparam(:target, namevar: true) do | ||
desc 'The path to postgresql.conf' | ||
defaultto do | ||
if @resource.class.defaultprovider.ancestors.include?(Puppet::Provider::ParsedFile) | ||
@resource.class.defaultprovider.default_target | ||
else | ||
value | ||
nil | ||
end | ||
end | ||
end | ||
|
||
newproperty(:comment) do | ||
desc 'The comment to set for this parameter.' | ||
newvalues(%r{^[\w\W]+$}) | ||
end | ||
|
||
newparam(:target) do | ||
desc 'The path to the postgresql config file' | ||
newvalues(%r{^/\S+[a-z0-9(/)-]*\w+.conf$}) | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't have time to figure out how
title_patterns
works with matching, but it probably isn't hard.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that really depends on the point of view :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It comes down to understanding https://github.com/puppetlabs/puppet/blob/945beb6ef0200b1bc7879b62131282a61fc9a935/lib/puppet/resource.rb#L625-L663 and finding the right combination.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, couldn't help myself. I think the commented code should work. The reason it's commented is that I didn't know if it would really work. But this part suggests the patterns are correct: