Sha256: 79125ac93ba32f221e1d29b61253e7bdc0fa73b21e1e7c1e96261b24374c7614

Contents?: true

Size: 1.89 KB

Versions: 7

Compression:

Stored size: 1.89 KB

Contents

require 'active_support/all'
require 'pg'
require 'pg/dsn_parser'

module ManageIQ
module PostgresHaAdmin
  class ServerStore
    include Logging

    TABLE_NAME = "repmgr.nodes".freeze

    attr_reader :servers

    def initialize
      @servers = []
    end

    def connection_info_list
      valid_keys = PG::Connection.conndefaults_hash.keys + [:requiressl]
      servers.map! do |db_info|
        db_info.keep_if { |k, _v| valid_keys.include?(k) }
      end
    end

    def update_servers(connection)
      new_servers = query_repmgr(connection)
      if servers_changed?(new_servers)
        logger.info("Updating servers cache to #{new_servers}")
        @servers = new_servers
      end
    rescue IOError => err
      logger.error("#{err.class}: #{err}")
      logger.error(err.backtrace.join("\n"))
    end

    def host_is_primary?(host, connection)
      query_repmgr(connection).each do |record|
        if record[:host] == host && record[:type] == 'primary'
          return true
        end
      end
      false
    end

    private

    def servers_changed?(new_servers)
      ((servers - new_servers) + (new_servers - servers)).any?
    end

    def query_repmgr(connection)
      return [] unless table_exists?(connection, TABLE_NAME)
      result = []
      db_result = connection.exec("SELECT type, conninfo, active FROM #{TABLE_NAME} WHERE active")
      db_result.map_types!(PG::BasicTypeMapForResults.new(connection)).each do |record|
        dsn = PG::DSNParser.parse(record.delete("conninfo"))
        result << record.symbolize_keys.merge(dsn)
      end
      db_result.clear
      result
    rescue PG::Error => err
      logger.error("#{err.class}: #{err}")
      logger.error(err.backtrace.join("\n"))
      result
    end

    def table_exists?(connection, table_name)
      result = connection.exec("SELECT to_regclass('#{table_name}')").first
      !result['to_regclass'].nil?
    end
  end
end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
manageiq-postgres_ha_admin-3.1.4 lib/manageiq/postgres_ha_admin/server_store.rb
manageiq-postgres_ha_admin-3.1.3 lib/manageiq/postgres_ha_admin/server_store.rb
manageiq-postgres_ha_admin-3.1.2 lib/manageiq/postgres_ha_admin/server_store.rb
manageiq-postgres_ha_admin-3.1.1 lib/manageiq/postgres_ha_admin/server_store.rb
manageiq-postgres_ha_admin-3.1.0 lib/manageiq/postgres_ha_admin/server_store.rb
manageiq-postgres_ha_admin-3.0.1 lib/manageiq/postgres_ha_admin/server_store.rb
manageiq-postgres_ha_admin-3.0.0 lib/manageiq/postgres_ha_admin/server_store.rb