-
Couldn't load subscription status.
- Fork 612
Extend postgresql_conf to support multiple config files using ParsedFile #1542
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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, | ||
| #} |
| 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.
| 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 | ||
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_patternsworks 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: