From 290af8177a377cc6c132850bd4d9272e0dd773ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Wed, 8 Apr 2026 13:21:42 +0400 Subject: [PATCH] Refactor postgres server recipes/resource --- .../kosmos_postgresql/attributes/default.rb | 5 ++ .../kosmos_postgresql/libraries/helpers.rb | 10 +++- .../kosmos_postgresql/recipes/primary.rb | 24 ---------- .../kosmos_postgresql/recipes/replica.rb | 48 +++++-------------- .../kosmos_postgresql/resources/server.rb | 18 +++++++ 5 files changed, 44 insertions(+), 61 deletions(-) diff --git a/site-cookbooks/kosmos_postgresql/attributes/default.rb b/site-cookbooks/kosmos_postgresql/attributes/default.rb index f3daf9b..29aae1f 100644 --- a/site-cookbooks/kosmos_postgresql/attributes/default.rb +++ b/site-cookbooks/kosmos_postgresql/attributes/default.rb @@ -1,3 +1,8 @@ +node.default['kosmos_postgresql']['postgresql_version'] = "14" + # This is set to false by default, and set to true in the server resource # for replicas. node.default['kosmos_postgresql']['ready_to_set_up_replica'] = false + +# Address space from which clients are allowed to connect +node.default['kosmos_postgresql']['access_addr'] = "10.1.1.0/24" diff --git a/site-cookbooks/kosmos_postgresql/libraries/helpers.rb b/site-cookbooks/kosmos_postgresql/libraries/helpers.rb index 18e245d..5de6e76 100644 --- a/site-cookbooks/kosmos_postgresql/libraries/helpers.rb +++ b/site-cookbooks/kosmos_postgresql/libraries/helpers.rb @@ -36,10 +36,16 @@ class Chef end end - def postgresql_service_name - postgresql_version = "12" + def postgresql_version + node['kosmos_postgresql']['postgresql_version'] + end + def postgresql_service "postgresql@#{postgresql_version}-main" end + + def postgresql_data_dir + "/var/lib/postgresql/#{postgresql_version}/main" + end end end diff --git a/site-cookbooks/kosmos_postgresql/recipes/primary.rb b/site-cookbooks/kosmos_postgresql/recipes/primary.rb index de7466f..406e2ae 100644 --- a/site-cookbooks/kosmos_postgresql/recipes/primary.rb +++ b/site-cookbooks/kosmos_postgresql/recipes/primary.rb @@ -3,31 +3,7 @@ # Recipe:: primary # -postgresql_version = "12" -postgresql_service = "postgresql@#{postgresql_version}-main" - -service postgresql_service do - supports restart: true, status: true, reload: true -end - postgresql_custom_server postgresql_version do role "primary" end -postgresql_access "zerotier members" do - access_type "host" - access_db "all" - access_user "all" - access_addr "10.1.1.0/24" - access_method "md5" - notifies :reload, "service[#{postgresql_service}]", :immediately -end - -postgresql_access "zerotier members replication" do - access_type "host" - access_db "replication" - access_user "replication" - access_addr "10.1.1.0/24" - access_method "md5" - notifies :reload, "service[#{postgresql_service}]", :immediately -end diff --git a/site-cookbooks/kosmos_postgresql/recipes/replica.rb b/site-cookbooks/kosmos_postgresql/recipes/replica.rb index b1dd345..69d5cd0 100644 --- a/site-cookbooks/kosmos_postgresql/recipes/replica.rb +++ b/site-cookbooks/kosmos_postgresql/recipes/replica.rb @@ -3,54 +3,32 @@ # Recipe:: replica # -postgresql_version = "12" -postgresql_service = "postgresql@#{postgresql_version}-main" +service postgresql_service do + supports restart: true, status: true, reload: true +end postgresql_custom_server postgresql_version do role "replica" end -service postgresql_service do - supports restart: true, status: true, reload: true -end - postgresql_data_bag_item = data_bag_item('credentials', 'postgresql') primary = postgresql_primary -unless primary.nil? - # TODO - postgresql_data_dir = "/var/lib/postgresql/#{postgresql_version}/main" +if primary.nil? + Chef::Log.warn("No PostgreSQL primary node found. Skipping replication setup.") + return +end - # FIXME get zerotier IP - execute "set up replication" do - command <<-EOF +execute "set up replication" do + command <<-EOF systemctl stop #{postgresql_service} mv #{postgresql_data_dir} #{postgresql_data_dir}.old pg_basebackup -h pg.kosmos.local -U replication -D #{postgresql_data_dir} -R chown -R postgres:postgres #{postgresql_data_dir} systemctl start #{postgresql_service} - EOF - environment 'PGPASSWORD' => postgresql_data_bag_item['replication_password'] - sensitive true - not_if { ::File.exist? "#{postgresql_data_dir}/standby.signal" } - end - - postgresql_access "zerotier members" do - access_type "host" - access_db "all" - access_user "all" - access_addr "10.1.1.0/24" - access_method "md5" - notifies :reload, "service[#{postgresql_service}]", :immediately - end - - postgresql_access "zerotier members replication" do - access_type "host" - access_db "replication" - access_user "replication" - access_addr "10.1.1.0/24" - access_method "md5" - notifies :reload, "service[#{postgresql_service}]", :immediately - end + EOF + environment 'PGPASSWORD' => postgresql_data_bag_item['replication_password'] + sensitive true + not_if { ::File.exist? "#{postgresql_data_dir}/standby.signal" } end diff --git a/site-cookbooks/kosmos_postgresql/resources/server.rb b/site-cookbooks/kosmos_postgresql/resources/server.rb index d5b38da..e024f78 100644 --- a/site-cookbooks/kosmos_postgresql/resources/server.rb +++ b/site-cookbooks/kosmos_postgresql/resources/server.rb @@ -70,6 +70,24 @@ action :create do replication true password postgresql_credentials['replication_password'] end + + postgresql_access "all members" do + access_type "host" + access_db "all" + access_user "all" + access_addr node['kosmos_postgresql']['access_addr'] + access_method "md5" + notifies :reload, "service[#{postgresql_service}]", :immediately + end + + postgresql_access "replication members" do + access_type "host" + access_db "replication" + access_user "replication" + access_addr node['kosmos_postgresql']['access_addr'] + access_method "md5" + notifies :reload, "service[#{postgresql_service}]", :immediately + end end action_class do