Class: ActivePostgres::Components::Repmgr

Inherits:
Base
  • Object
show all
Defined in:
lib/active_postgres/components/repmgr.rb

Instance Attribute Summary

Attributes inherited from Base

#config, #secrets, #ssh_executor

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from ActivePostgres::Components::Base

Instance Method Details

#installObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/active_postgres/components/repmgr.rb', line 4

def install
  puts 'Installing repmgr for High Availability...'

  install_repmgr_package
  setup_primary

  config.standby_hosts.each do |host|
    setup_standby(host)
  end

  setup_inter_node_ssh if config.standby_hosts.any?
  setup_dns_failover if dns_failover_enabled?

  # Verify the entire cluster is healthy after setup
  puts "\nšŸ„ Performing final health check..."
  verify_cluster_health
end

#restartObject



54
55
56
57
58
59
60
61
62
# File 'lib/active_postgres/components/repmgr.rb', line 54

def restart
  puts 'Restarting repmgr...'

  config.all_hosts.each do |host|
    ssh_executor.execute_on_host(host) do
      execute :sudo, 'systemctl', 'restart', 'repmgrd' if test('systemctl is-active repmgrd')
    end
  end
end

#setup_standby_only(standby_host) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/active_postgres/components/repmgr.rb', line 64

def setup_standby_only(standby_host)
  puts "Setting up standby #{standby_host} (primary will not be touched)..."

  version = config.version
  install_apt_packages(standby_host, "postgresql-#{version}-repmgr")

  setup_standby(standby_host)

  setup_dns_failover if dns_failover_enabled?
end

#uninstallObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/active_postgres/components/repmgr.rb', line 22

def uninstall
  puts 'Uninstalling repmgr...'

  config.all_hosts.each do |host|
    is_primary = host == config.primary_host
    node_id = if is_primary
                1
              else
                begin
                  config.standby_hosts.index(host) + 2
                rescue StandardError
                  999
                end
              end

    ssh_executor.execute_on_host(host) do
      cluster_output = begin
        capture(:sudo, '-u', 'postgres', 'repmgr', 'cluster', 'show')
      rescue StandardError
        ''
      end

      if cluster_output.include?("| #{node_id}") && cluster_output.include?('running')
        info "āœ“ Skipping repmgr removal from #{host} - node #{node_id} is running"
      else
        info "Removing repmgr from #{host}"
        execute :sudo, 'apt-get', 'remove', '-y', '-q', 'postgresql-*-repmgr', '||', 'true'
      end
    end
  end
end