class ManageIQ::ApplianceConsole::DatabaseReplicationStandby

Constants

REGISTER_CMD
REPMGRD_SERVICE

Attributes

disk[RW]
force_register[RW]
resync_data[RW]
run_repmgrd_configuration[RW]
standby_host[RW]

Public Class Methods

new() click to toggle source
# File lib/manageiq/appliance_console/database_replication_standby.rb, line 15
def initialize
  self.node_number       = nil
  self.database_name     = "vmdb_production"
  self.database_user     = "root"
  self.database_password = nil
  self.primary_host      = nil
  self.standby_host      = LinuxAdmin::NetworkInterface.new(NETWORK_INTERFACE).address
  self.resync_data       = false
end

Public Instance Methods

activate() click to toggle source
# File lib/manageiq/appliance_console/database_replication_standby.rb, line 59
def activate
  say("Configuring Replication Standby Server...")
  stop_postgres
  stop_repmgrd
  initialize_postgresql_disk if disk
  PostgresAdmin.prep_data_directory if disk || resync_data
  relabel_postgresql_dir
  save_database_yml
  create_config_file(standby_host) &&
    write_pgpass_file &&
    clone_standby_server &&
    start_postgres &&
    register_standby_server &&
    (run_repmgrd_configuration ? start_repmgrd : true)
end
ask_for_repmgrd_configuration() click to toggle source
# File lib/manageiq/appliance_console/database_replication_standby.rb, line 55
def ask_for_repmgrd_configuration
  self.run_repmgrd_configuration = ask_yn?("Configure Replication Manager (repmgrd) for automatic failover")
end
ask_for_standby_host() click to toggle source
# File lib/manageiq/appliance_console/database_replication_standby.rb, line 51
def ask_for_standby_host
  self.standby_host = ask_for_ip_or_hostname("Standby Server hostname or IP address", standby_host)
end
ask_questions() click to toggle source
# File lib/manageiq/appliance_console/database_replication_standby.rb, line 25
def ask_questions
  clear_screen
  say("Establish Replication Standby Server\n")
  return false if !data_dir_empty? && !confirm_data_resync
  self.disk = ask_for_disk("Standby database disk")
  ask_for_unique_cluster_node_number
  ask_for_database_credentials
  ask_for_standby_host
  ask_for_repmgrd_configuration
  return false unless node_number_valid?
  return false if repmgr_configured? && !confirm_reconfiguration
  confirm
end
clone_standby_server() click to toggle source
# File lib/manageiq/appliance_console/database_replication_standby.rb, line 89
def clone_standby_server
  params = { :h  => primary_host,
             :U  => database_user,
             :d  => database_name,
             :D  => PostgresAdmin.data_directory,
             nil => %w(standby clone)
           }
  run_repmgr_command("repmgr", params)
end
confirm() click to toggle source
# File lib/manageiq/appliance_console/database_replication_standby.rb, line 39
    def confirm
      super
      say(<<-EOS) if disk
        Database Disk:              #{disk.path}
      EOS
      say(<<-EOS)
        Standby Host:               #{standby_host}
        Automatic Failover:         #{run_repmgrd_configuration ? "enabled" : "disabled"}
      EOS
      agree("Apply this Replication Server Configuration? (Y/N): ")
    end
confirm_data_resync() click to toggle source
# File lib/manageiq/appliance_console/database_replication_standby.rb, line 79
def confirm_data_resync
  logger.info("Appliance database found under: #{PostgresAdmin.data_directory}")
  say("")
  say("Appliance database found under: #{PostgresAdmin.data_directory}")
  say("Replication standby server can not be configured if the database already exists")
  say("Would you like to remove the existing database before configuring as a standby server?")
  say("  WARNING: This is destructive. This will remove all previous data from this server")
  self.resync_data = ask_yn?("Continue")
end
data_dir_empty?() click to toggle source
# File lib/manageiq/appliance_console/database_replication_standby.rb, line 75
def data_dir_empty?
  Dir[PostgresAdmin.data_directory.join("*")].empty?
end
node_number_valid?() click to toggle source
# File lib/manageiq/appliance_console/database_replication_standby.rb, line 128
def node_number_valid?
  rec = record_for_node_number

  return true if rec.nil?
  node_state = rec["active"] ? "active" : "inactive"

  say("An #{node_state} #{rec["type"]} node (#{rec["node_name"]}) with the node number #{node_number} already exists")
  ask_yn?("Would you like to continue configuration by overwriting the existing node", "N")

rescue PG::Error => e
  error_msg = "Failed to validate node number #{node_number}. #{e.message}"
  say(error_msg)
  logger.error(error_msg)
  return false
end
register_standby_server() click to toggle source
# File lib/manageiq/appliance_console/database_replication_standby.rb, line 109
def register_standby_server
  run_repmgr_command(REGISTER_CMD, :force => nil, :wait_sync= => 60)
end
start_postgres() click to toggle source
# File lib/manageiq/appliance_console/database_replication_standby.rb, line 99
def start_postgres
  LinuxAdmin::Service.new(PostgresAdmin.service_name).enable.start
  true
end
start_repmgrd() click to toggle source
# File lib/manageiq/appliance_console/database_replication_standby.rb, line 113
def start_repmgrd
  LinuxAdmin::Service.new(REPMGRD_SERVICE).enable.start
  true
rescue AwesomeSpawn::CommandResultError => e
  message = "Failed to start repmgrd: #{e.message}"
  logger.error(message)
  say(message)
  false
end
stop_postgres() click to toggle source
# File lib/manageiq/appliance_console/database_replication_standby.rb, line 104
def stop_postgres
  LinuxAdmin::Service.new(PostgresAdmin.service_name).stop
  true
end
stop_repmgrd() click to toggle source
# File lib/manageiq/appliance_console/database_replication_standby.rb, line 123
def stop_repmgrd
  LinuxAdmin::Service.new(REPMGRD_SERVICE).stop
  true
end

Private Instance Methods

initialize_postgresql_disk() click to toggle source
# File lib/manageiq/appliance_console/database_replication_standby.rb, line 158
def initialize_postgresql_disk
  log_and_feedback(__method__) do
    LogicalVolumeManagement.new(:disk                => disk,
                                :mount_point         => PostgresAdmin.mount_point,
                                :name                => "pg",
                                :volume_group_name   => PostgresAdmin.volume_group_name,
                                :filesystem_type     => PostgresAdmin.database_disk_filesystem,
                                :logical_volume_path => PostgresAdmin.logical_volume_path).setup

    # if we mounted the disk onto the postgres user's home directory, fix the permissions
    if PostgresAdmin.mount_point.to_s == "/var/lib/pgsql"
      FileUtils.chown(PostgresAdmin.user, PostgresAdmin.group, "/var/lib/pgsql")
      FileUtils.chmod(0o700, "/var/lib/pgsql")
    end
  end
end
record_for_node_number() click to toggle source
# File lib/manageiq/appliance_console/database_replication_standby.rb, line 150
    def record_for_node_number
      c = PG::Connection.new(primary_connection_hash)
      c.exec_params(<<-SQL, [node_number]).map_types!(PG::BasicTypeMapForResults.new(c)).first
        SELECT type, node_name, active
        FROM repmgr.nodes where node_id = $1
      SQL
    end
relabel_postgresql_dir() click to toggle source
# File lib/manageiq/appliance_console/database_replication_standby.rb, line 175
def relabel_postgresql_dir
  AwesomeSpawn.run!("/sbin/restorecon -R -v #{PostgresAdmin.mount_point}")
end
save_database_yml() click to toggle source
# File lib/manageiq/appliance_console/database_replication_standby.rb, line 146
def save_database_yml
  InternalDatabaseConfiguration.new(:password => database_password).save
end