From afda425fab2d64462caec8897c86b2900fbcd42d Mon Sep 17 00:00:00 2001 From: Ben Vincent Date: Tue, 3 Sep 2024 22:11:21 +1000 Subject: [PATCH] feat: psql changes on master only - add fact to detect if a psql host is a slave - only import users/db/grants on master --- modules/libs/lib/facter/psql_is_slave.rb | 11 +++++++++++ site/profiles/manifests/sql/patroni.pp | 13 ++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 modules/libs/lib/facter/psql_is_slave.rb diff --git a/modules/libs/lib/facter/psql_is_slave.rb b/modules/libs/lib/facter/psql_is_slave.rb new file mode 100644 index 0000000..3cb8938 --- /dev/null +++ b/modules/libs/lib/facter/psql_is_slave.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +Facter.add(:psql_is_slave) do + setcode do + # Command to check if PostgreSQL is in recovery mode + command = 'sudo -iu postgres psql -tAc "select pg_is_in_recovery()"' + + # Execute the command and map the output to a boolean value + { 't' => true, 'f' => false }[Facter::Core::Execution.execute(command, on_fail: nil)] + end +end diff --git a/site/profiles/manifests/sql/patroni.pp b/site/profiles/manifests/sql/patroni.pp index e53dcc4..45b91c4 100644 --- a/site/profiles/manifests/sql/patroni.pp +++ b/site/profiles/manifests/sql/patroni.pp @@ -88,11 +88,14 @@ class profiles::sql::patroni ( } - # collect exported resources - $tag = "${facts['country']}-${facts['region']}-${facts['environment']}" - Profiles::Sql::Postgres::Db <<| tag == $tag |>> {} - Profiles::Sql::Postgres::User <<| tag == $tag |>> {} - Profiles::Sql::Postgres::Grant <<| tag == $tag |>> {} + # only apply changes to DBs/Users/Grants on master + if ! $facts['psql_is_slave'] { + # collect exported resources + $tag = "${facts['country']}-${facts['region']}-${facts['environment']}" + Profiles::Sql::Postgres::Db <<| tag == $tag |>> {} + Profiles::Sql::Postgres::User <<| tag == $tag |>> {} + Profiles::Sql::Postgres::Grant <<| tag == $tag |>> {} + } if $postgres_exporter_enabled { class { 'prometheus::postgres_exporter': -- 2.47.3