class ManageIQ::ApplianceConsole::InternalDatabaseConfiguration

Constants

DEDICATED_DB_SHARED_BUFFERS
SHARED_DB_SHARED_BUFFERS

Attributes

disk[RW]
run_as_evm_server[RW]

Public Class Methods

new(hash = {}) click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 21
def initialize(hash = {})
  set_defaults
  super
end
postgres_dir() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 13
def self.postgres_dir
  PostgresAdmin.data_directory.relative_path_from(Pathname.new("/"))
end
postgresql_template() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 17
def self.postgresql_template
  PostgresAdmin.template_directory.join(postgres_dir)
end

Public Instance Methods

activate() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 33
    def activate
      if PostgresAdmin.initialized?
        say(<<-EOF.gsub!(/^\s+/, ""))
          An internal database already exists.
          Choose "Reset Configured Database" to reset the existing installation
          EOF
        return false
      end
      initialize_postgresql_disk if disk
      initialize_postgresql
      run_as_evm_server ? (return super) : save
      true
    end
ask_questions() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 47
    def ask_questions
      choose_disk
      check_disk_is_mount_point
      self.run_as_evm_server = !ask_yn?(<<-EOS.gsub!(/^ +/m, ""), "N")

        Should this appliance run as a standalone database server?

        NOTE:
        * The #{I18n.t("product.name")} application will not be running.
        * This is required when using highly available database deployments.
        * CAUTION: This is not reversible.

      EOS
      # TODO: Assume we want to create a region for a new internal database disk
      # until we allow for the internal selection against an already initialized disk.
      create_new_region_questions(false) if run_as_evm_server
      ask_for_database_credentials
    end
check_disk_is_mount_point() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 70
def check_disk_is_mount_point
  error_message = "Internal databases require a volume mounted at #{mount_point}. Please add an unpartitioned disk and try again."
  raise error_message unless disk || pg_mount_point?
end
choose_disk() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 66
def choose_disk
  @disk = ask_for_disk("database disk", false, true)
end
configure_postgres() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 106
def configure_postgres
  copy_template "postgresql.conf"
  copy_template "pg_hba.conf"
  copy_template "pg_ident.conf"
end
initialize_postgresql() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 92
def initialize_postgresql
  log_and_feedback(__method__) do
    PostgresAdmin.prep_data_directory
    run_initdb
    configure_ssl
    relabel_postgresql_dir
    configure_postgres
    start_postgres
    create_postgres_root_user
    create_postgres_database
    apply_initial_configuration
  end
end
initialize_postgresql_disk() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 75
def initialize_postgresql_disk
  log_and_feedback(__method__) do
    LogicalVolumeManagement.new(:disk                => disk,
                                :mount_point         => 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
  end

  # if we mounted the disk onto the postgres user's home directory, fix the permissions
  if mount_point.to_s == "/var/lib/pgsql"
    FileUtils.chown(PostgresAdmin.user, PostgresAdmin.group, "/var/lib/pgsql")
    FileUtils.chmod(0o700, "/var/lib/pgsql")
  end
end
post_activation() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 112
def post_activation
  start_evm if run_as_evm_server
end
set_defaults() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 26
def set_defaults
  self.host              = 'localhost'
  self.username          = "root"
  self.database          = "vmdb_production"
  self.run_as_evm_server = true
end

Private Instance Methods

apply_initial_configuration() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 167
def apply_initial_configuration
  shared_buffers = run_as_evm_server ? SHARED_DB_SHARED_BUFFERS : DEDICATED_DB_SHARED_BUFFERS
  PostgresAdmin.with_pg_connection { |conn| conn.exec("ALTER SYSTEM SET shared_buffers TO #{shared_buffers}") }

  restart_postgres
end
block_until_postgres_accepts_connections() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 144
def block_until_postgres_accepts_connections
  loop do
    break if AwesomeSpawn.run("psql -U postgres -c 'select 1'").success?
  end
end
configure_ssl() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 174
def configure_ssl
  cert_file = PostgresAdmin.data_directory.join("server.crt").to_s
  key_file  = PostgresAdmin.data_directory.join("server.key").to_s
  AwesomeSpawn.run!("/usr/bin/generate_miq_server_cert.sh", :env => {"NEW_CERT_FILE" => cert_file, "NEW_KEY_FILE"  => key_file})

  FileUtils.chown("postgres", "postgres", cert_file)
  FileUtils.chown("postgres", "postgres", key_file)
  FileUtils.chmod(0644, cert_file)
  FileUtils.chmod(0600, key_file)
end
copy_template(src) click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 122
def copy_template(src)
  FileUtils.cp(self.class.postgresql_template.join(src), PostgresAdmin.data_directory)
end
create_postgres_database() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 157
def create_postgres_database
  PostgresAdmin.with_pg_connection do |conn|
    conn.exec("CREATE DATABASE #{database} OWNER #{username} ENCODING 'utf8'")
  end
end
create_postgres_root_user() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 150
def create_postgres_root_user
  PostgresAdmin.with_pg_connection do |conn|
    esc_pass = conn.escape_string(password)
    conn.exec("CREATE ROLE #{username} WITH LOGIN CREATEDB SUPERUSER PASSWORD '#{esc_pass}'")
  end
end
mount_point() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 118
def mount_point
  PostgresAdmin.mount_point
end
pg_mount_point?() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 126
def pg_mount_point?
  LinuxAdmin::LogicalVolume.mount_point_exists?(mount_point.to_s)
end
relabel_postgresql_dir() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 163
def relabel_postgresql_dir
  AwesomeSpawn.run!("/sbin/restorecon -R -v #{mount_point}")
end
restart_postgres() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 139
def restart_postgres
  LinuxAdmin::Service.new(PostgresAdmin.service_name).restart
  block_until_postgres_accepts_connections
end
run_initdb() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 130
def run_initdb
  AwesomeSpawn.run!("postgresql-setup", :params => {:initdb => nil, :unit => PostgresAdmin.service_name})
end
start_postgres() click to toggle source
# File lib/manageiq/appliance_console/internal_database_configuration.rb, line 134
def start_postgres
  LinuxAdmin::Service.new(PostgresAdmin.service_name).enable.start
  block_until_postgres_accepts_connections
end