Skip to content

Commit

Permalink
puppet: Migrate Ruby functions from legacy Puppet 3.x API.
Browse files Browse the repository at this point in the history
https://www.puppet.com/docs/puppet/7/functions_refactor_legacy.html

This removes a bug in the 3.x API that was converting nil to the empty
string, so some templates need to be adjusted.

Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk committed May 13, 2023
1 parent cf8ae46 commit 16aa7c0
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 94 deletions.
19 changes: 19 additions & 0 deletions puppet/zulip/lib/puppet/functions/zulipconf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "shellwords"

Puppet::Functions.create_function(:zulipconf) do
def zulipconf(section, key, default)
zulip_conf_path = Facter.value("zulip_conf_path")
output = `/usr/bin/crudini --get -- #{[zulip_conf_path, section, key].shelljoin} 2>&1`; result = $?.success?
if result
if [true, false].include? default
# If the default is a bool, coerce into a bool. This list is also
# maintained in scripts/lib/zulip_tools.py
["1", "y", "t", "true", "yes", "enable", "enabled"].include? output.strip.downcase
else
output.strip
end
else
default
end
end
end
13 changes: 13 additions & 0 deletions puppet/zulip/lib/puppet/functions/zulipconf_keys.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "shellwords"

Puppet::Functions.create_function(:zulipconf_keys) do
def zulipconf_keys(section)
zulip_conf_path = Facter.value("zulip_conf_path")
output = `/usr/bin/crudini --get -- #{[zulip_conf_path, section].shelljoin} 2>&1`; result = $?.success?
if result
return output.lines.map { |l| l.strip }
else
return []
end
end
end
23 changes: 23 additions & 0 deletions puppet/zulip/lib/puppet/functions/zulipconf_nagios_hosts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "shellwords"

Puppet::Functions.create_function(:zulipconf_nagios_hosts) do
def zulipconf_nagios_hosts
section = "nagios"
prefix = "hosts_"
zulip_conf_path = Facter.value("zulip_conf_path")
keys = `/usr/bin/crudini --get -- #{[zulip_conf_path, section].shelljoin} 2>&1`; result = $?.success?
if result
filtered_keys = keys.lines.map { |l| l.strip }.select { |l| l.start_with?(prefix) }
all_values = []
filtered_keys.each do |key|
values = `/usr/bin/crudini --get -- #{[zulip_conf_path, section, key].shelljoin} 2>&1`; result = $?.success?
if result
all_values += values.strip.split(/,\s*/)
end
end
return all_values
else
return []
end
end
end
12 changes: 12 additions & 0 deletions puppet/zulip/lib/puppet/functions/zulipsecret.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "shellwords"

Puppet::Functions.create_function(:zulipsecret) do
def zulipsecret(section, key, default)
output = `/usr/bin/crudini --get -- /etc/zulip/zulip-secrets.conf #{[section, key].shelljoin} 2>&1`; result = $?.success?
if result
output.strip()
else
default
end
end
end
50 changes: 0 additions & 50 deletions puppet/zulip/lib/puppet/parser/functions/zulipconf.rb

This file was deleted.

11 changes: 0 additions & 11 deletions puppet/zulip/lib/puppet/parser/functions/zulipsecret.rb

This file was deleted.

2 changes: 1 addition & 1 deletion puppet/zulip/manifests/profile/postgresql.pp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
fail("PostgreSQL ${version} not supported")
}

if $replication_primary != '' and $replication_user != '' {
if $replication_primary != undef and $replication_user != undef {
if $s3_backups_bucket == '' {
$message = @(EOT/L)
Replication is enabled, but s3_backups_bucket is not set in zulip-secrets.conf! \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -776,14 +776,14 @@ work_mem = <%= scope["zulip::profile::postgresql::work_mem"] %>MB
shared_buffers = <%= scope["zulip::profile::postgresql::shared_buffers"] %>MB
wal_buffers = 4MB
checkpoint_completion_target = 0.7
<% if @random_page_cost != '' -%>
<% unless @random_page_cost.nil? -%>
random_page_cost = <%= @random_page_cost %>
<% end -%>
<% if @effective_io_concurrency != '' -%>
<% unless @effective_io_concurrency.nil? -%>
effective_io_concurrency = <%= @effective_io_concurrency %>
<% end -%>

<% if @listen_addresses != '' -%>
<% unless @listen_addresses.nil? -%>
listen_addresses = <%= @listen_addresses %>
<% end -%>

Expand All @@ -793,20 +793,20 @@ max_wal_senders = 5
archive_mode = on
archive_command = '/usr/bin/timeout 10m /usr/local/bin/env-wal-g wal-push %p'
restore_command = '/usr/local/bin/env-wal-g wal-fetch "%f" "%p"'
<% if @replication_primary != '' && @replication_user != '' -%>
<% unless @replication_primary.nil? || @replication_user.nil? -%>
primary_conninfo = 'host=<%= @replication_primary %> user=<%= @replication_user -%>
<% if @replication_password != '' %> password=<%= @replication_password %><% end -%>
<% if @ssl_mode != '' %> sslmode=<%= @ssl_mode %><% end -%>
<% unless @ssl_mode.nil? %> sslmode=<%= @ssl_mode %><% end -%>
'
<% end -%>
<% end -%>

<% if @ssl_cert_file != '' -%>
<% unless @ssl_cert_file.nil? -%>
ssl_cert_file = '<%= @ssl_cert_file %>' # (change requires restart)
<% end -%>
<% if @ssl_key_file != '' -%>
<% unless @ssl_key_file.nil? -%>
ssl_key_file = '<%= @ssl_key_file %>' # (change requires restart)
<% end -%>
<% if @ssl_ca_file != '' -%>
<% unless @ssl_ca_file.nil? -%>
ssl_ca_file = '<%= @ssl_ca_file %>' # (change requires restart)
<% end -%>
Original file line number Diff line number Diff line change
Expand Up @@ -807,14 +807,14 @@ work_mem = <%= scope["zulip::profile::postgresql::work_mem"] %>MB
shared_buffers = <%= scope["zulip::profile::postgresql::shared_buffers"] %>MB
wal_buffers = 4MB
checkpoint_completion_target = 0.7
<% if @random_page_cost != '' -%>
<% unless @random_page_cost.nil? -%>
random_page_cost = <%= @random_page_cost %>
<% end -%>
<% if @effective_io_concurrency != '' -%>
<% unless @effective_io_concurrency.nil? -%>
effective_io_concurrency = <%= @effective_io_concurrency %>
<% end -%>

<% if @listen_addresses != '' -%>
<% unless @listen_addresses.nil? -%>
listen_addresses = <%= @listen_addresses %>
<% end -%>

Expand All @@ -824,20 +824,20 @@ max_wal_senders = 5
archive_mode = on
archive_command = '/usr/bin/timeout 10m /usr/local/bin/env-wal-g wal-push %p'
restore_command = '/usr/local/bin/env-wal-g wal-fetch "%f" "%p"'
<% if @replication_primary != '' && @replication_user != '' -%>
<% unless @replication_primary.nil? || @replication_user.nil? -%>
primary_conninfo = 'host=<%= @replication_primary %> user=<%= @replication_user -%>
<% if @replication_password != '' %> password=<%= @replication_password %><% end -%>
<% if @ssl_mode != '' %> sslmode=<%= @ssl_mode %><% end -%>
<% unless @ssl_mode.nil? %> sslmode=<%= @ssl_mode %><% end -%>
'
<% end -%>
<% end -%>

<% if @ssl_cert_file != '' -%>
<% unless @ssl_cert_file.nil? -%>
ssl_cert_file = '<%= @ssl_cert_file %>' # (change requires restart)
<% end -%>
<% if @ssl_key_file != '' -%>
<% unless @ssl_key_file.nil? -%>
ssl_key_file = '<%= @ssl_key_file %>' # (change requires restart)
<% end -%>
<% if @ssl_ca_file != '' -%>
<% unless @ssl_ca_file.nil? -%>
ssl_ca_file = '<%= @ssl_ca_file %>' # (change requires restart)
<% end -%>
Original file line number Diff line number Diff line change
Expand Up @@ -828,14 +828,14 @@ work_mem = <%= scope["zulip::profile::postgresql::work_mem"] %>MB
shared_buffers = <%= scope["zulip::profile::postgresql::shared_buffers"] %>MB
wal_buffers = 4MB
checkpoint_completion_target = 0.7
<% if @random_page_cost != '' -%>
<% unless @random_page_cost.nil? -%>
random_page_cost = <%= @random_page_cost %>
<% end -%>
<% if @effective_io_concurrency != '' -%>
<% unless @effective_io_concurrency.nil? -%>
effective_io_concurrency = <%= @effective_io_concurrency %>
<% end -%>

<% if @listen_addresses != '' -%>
<% unless @listen_addresses.nil? -%>
listen_addresses = <%= @listen_addresses %>
<% end -%>

Expand All @@ -845,20 +845,20 @@ max_wal_senders = 5
archive_mode = on
archive_command = '/usr/bin/timeout 10m /usr/local/bin/env-wal-g wal-push %p'
restore_command = '/usr/local/bin/env-wal-g wal-fetch "%f" "%p"'
<% if @replication_primary != '' && @replication_user != '' -%>
<% unless @replication_primary.nil? || @replication_user.nil? -%>
primary_conninfo = 'host=<%= @replication_primary %> user=<%= @replication_user -%>
<% if @replication_password != '' %> password=<%= @replication_password %><% end -%>
<% if @ssl_mode != '' %> sslmode=<%= @ssl_mode %><% end -%>
<% unless @ssl_mode.nil? %> sslmode=<%= @ssl_mode %><% end -%>
'
<% end -%>
<% end -%>

<% if @ssl_cert_file != '' -%>
<% unless @ssl_cert_file.nil? -%>
ssl_cert_file = '<%= @ssl_cert_file %>' # (change requires restart)
<% end -%>
<% if @ssl_key_file != '' -%>
<% unless @ssl_key_file.nil? -%>
ssl_key_file = '<%= @ssl_key_file %>' # (change requires restart)
<% end -%>
<% if @ssl_ca_file != '' -%>
<% unless @ssl_ca_file.nil? -%>
ssl_ca_file = '<%= @ssl_ca_file %>' # (change requires restart)
<% end -%>
16 changes: 8 additions & 8 deletions puppet/zulip/templates/postgresql/zulip.conf.template.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% if @listen_addresses != '' -%>
<% unless @listen_addresses.nil? -%>
# Bind to specific address
listen_addresses = <%= @listen_addresses %>
<% end -%>
Expand Down Expand Up @@ -32,10 +32,10 @@ work_mem = <%= scope["zulip::profile::postgresql::work_mem"] %>MB
shared_buffers = <%= scope["zulip::profile::postgresql::shared_buffers"] %>MB
wal_buffers = 4MB
checkpoint_completion_target = 0.7
<% if @random_page_cost != '' -%>
<% unless @random_page_cost.nil? -%>
random_page_cost = <%= @random_page_cost %>
<% end -%>
<% if @effective_io_concurrency != '' -%>
<% unless @effective_io_concurrency.nil? -%>
effective_io_concurrency = <%= @effective_io_concurrency %>
<% end -%>

Expand All @@ -44,20 +44,20 @@ effective_io_concurrency = <%= @effective_io_concurrency %>
archive_mode = on
archive_command = '/usr/bin/timeout 10m /usr/local/bin/env-wal-g wal-push %p'
restore_command = '/usr/local/bin/env-wal-g wal-fetch "%f" "%p"'
<% if @replication_primary != '' && @replication_user != '' -%>
<% unless @replication_primary.nil? || @replication_user.nil? -%>
primary_conninfo = 'host=<%= @replication_primary %> user=<%= @replication_user -%>
<% if @replication_password != '' %> password=<%= @replication_password %><% end -%>
<% if @ssl_mode != '' %> sslmode=<%= @ssl_mode %><% end -%>
<% unless @ssl_mode.nil? %> sslmode=<%= @ssl_mode %><% end -%>
'
<% end -%>
<% end -%>

<% if @ssl_cert_file != '' -%>
<% unless @ssl_cert_file.nil? -%>
ssl_cert_file = '<%= @ssl_cert_file %>' # (change requires restart)
<% end -%>
<% if @ssl_key_file != '' -%>
<% unless @ssl_key_file.nil? -%>
ssl_key_file = '<%= @ssl_key_file %>' # (change requires restart)
<% end -%>
<% if @ssl_ca_file != '' -%>
<% unless @ssl_ca_file.nil? -%>
ssl_ca_file = '<%= @ssl_ca_file %>' # (change requires restart)
<% end -%>

0 comments on commit 16aa7c0

Please sign in to comment.