Class: ManageIQ::ApplianceConsole::DatabaseReplication

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/manageiq/appliance_console/database_replication.rb

Constant Summary collapse

PGPASS_FILE =
'/var/lib/pgsql/.pgpass'.freeze
NETWORK_INTERFACE =
'eth0'.freeze
REPGMR_FILE_LOCATIONS =
{
  "repmgr10" => {
    "config" => "/etc/repmgr/10/repmgr.conf",
    "log"    => "/var/log/repmgr/repmgrd.log"
  },
  "repmgr13" => {
    "config" => "/etc/repmgr/13/repmgr.conf",
    "log"    => "/var/log/repmgr/repmgrd-13.log"
  }
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#error_and_logging_from_command_result_error, #error_and_logging_from_standard_error, #interactive, #interactive=, interactive?, #interactive?, #log_and_feedback, #log_and_feedback_exception, #log_and_feedback_info, #log_error, #log_prefix, #logger, #logger=, #say_error, #say_info

Instance Attribute Details

#database_nameObject

Returns the value of attribute database_name.



24
25
26
# File 'lib/manageiq/appliance_console/database_replication.rb', line 24

def database_name
  @database_name
end

#database_passwordObject

Returns the value of attribute database_password.



24
25
26
# File 'lib/manageiq/appliance_console/database_replication.rb', line 24

def database_password
  @database_password
end

#database_userObject

Returns the value of attribute database_user.



24
25
26
# File 'lib/manageiq/appliance_console/database_replication.rb', line 24

def database_user
  @database_user
end

#node_numberObject

Returns the value of attribute node_number.



24
25
26
# File 'lib/manageiq/appliance_console/database_replication.rb', line 24

def node_number
  @node_number
end

#primary_hostObject

Returns the value of attribute primary_host.



24
25
26
# File 'lib/manageiq/appliance_console/database_replication.rb', line 24

def primary_host
  @primary_host
end

Class Method Details

.repmgr_configObject



49
50
51
# File 'lib/manageiq/appliance_console/database_replication.rb', line 49

def self.repmgr_config
  repmgr_file_locations["config"]
end

.repmgr_configured?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/manageiq/appliance_console/database_replication.rb', line 53

def self.repmgr_configured?
  File.exist?(repmgr_config)
end

.repmgr_file_locationsObject



57
58
59
# File 'lib/manageiq/appliance_console/database_replication.rb', line 57

def self.repmgr_file_locations
  REPGMR_FILE_LOCATIONS[repmgr_service_name]
end

.repmgr_logObject



61
62
63
# File 'lib/manageiq/appliance_console/database_replication.rb', line 61

def self.repmgr_log
  repmgr_file_locations["log"]
end

.repmgr_service_nameObject



65
66
67
# File 'lib/manageiq/appliance_console/database_replication.rb', line 65

def self.repmgr_service_name
  @repmgr_service_name ||= File.exist?(REPGMR_FILE_LOCATIONS["repmgr13"]["config"]) ? "repmgr13" : "repmgr10"
end

Instance Method Details

#ask_for_database_credentialsObject



31
32
33
34
# File 'lib/manageiq/appliance_console/database_replication.rb', line 31

def ask_for_database_credentials
  ask_for_cluster_database_credentials
  self.primary_host = ask_for_ip_or_hostname("primary database hostname or IP address", primary_host)
end

#ask_for_unique_cluster_node_numberObject



27
28
29
# File 'lib/manageiq/appliance_console/database_replication.rb', line 27

def ask_for_unique_cluster_node_number
  self.node_number = ask_for_integer("number uniquely identifying this node in the replication cluster")
end

#config_file_contents(host) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/manageiq/appliance_console/database_replication.rb', line 82

def config_file_contents(host)
  service_name = PostgresAdmin.service_name
  # FYI, 5.0 made quoting strings strict.  Always use single quoted strings.
  # https://repmgr.org/docs/current/release-5.0.html
  "node_id='\#{node_number}'\nnode_name='\#{host}'\nconninfo='host=\#{host} user=\#{database_user} dbname=\#{database_name}'\nuse_replication_slots='1'\npg_basebackup_options='--wal-method=stream'\nfailover='automatic'\npromote_command='repmgr standby promote -f \#{repmgr_config} --log-to-file'\nfollow_command='repmgr standby follow -f \#{repmgr_config} --log-to-file --upstream-node-id=%n'\nlog_file='\#{repmgr_log}'\nservice_start_command='sudo systemctl start \#{service_name}'\nservice_stop_command='sudo systemctl stop \#{service_name}'\nservice_restart_command='sudo systemctl restart \#{service_name}'\nservice_reload_command='sudo systemctl reload \#{service_name}'\ndata_directory='\#{PostgresAdmin.data_directory}'\n".strip_heredoc
end

#confirmObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/manageiq/appliance_console/database_replication.rb', line 36

def confirm
  clear_screen
  say("Replication Server Configuration\n\nCluster Node Number:        \#{node_number}\nCluster Database Name:      \#{database_name}\nCluster Database User:      \#{database_user}\nCluster Database Password:  \"********\"\nCluster Primary Host:       \#{primary_host}\n")
end

#confirm_reconfigurationObject



71
72
73
74
75
# File 'lib/manageiq/appliance_console/database_replication.rb', line 71

def confirm_reconfiguration
  say("Warning: File #{repmgr_config} exists. Replication is already configured")
  logger.warn("Warning: File #{repmgr_config} exists. Replication is already configured")
  agree("Continue with configuration? (Y/N): ")
end

#create_config_file(host) ⇒ Object



77
78
79
80
# File 'lib/manageiq/appliance_console/database_replication.rb', line 77

def create_config_file(host)
  File.write(repmgr_config, config_file_contents(host))
  true
end

#write_pgpass_fileObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/manageiq/appliance_console/database_replication.rb', line 104

def write_pgpass_file
  File.open(PGPASS_FILE, "w") do |f|
    f.write("*:*:#{database_name}:#{database_user}:#{database_password}\n")
    f.write("*:*:replication:#{database_user}:#{database_password}\n")
  end

  FileUtils.chmod(0600, PGPASS_FILE)
  FileUtils.chown("postgres", "postgres", PGPASS_FILE)
  true
end