class ManageIQ::PostgresHaAdmin::RailsConfigHandler

Attributes

environment[R]
file_path[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/manageiq/postgres_ha_admin/config_handler/rails_config_handler.rb, line 10
def initialize(options = {})
  @file_path   = options[:file_path]
  @environment = options[:environment]
end

Public Instance Methods

name() click to toggle source
# File lib/manageiq/postgres_ha_admin/config_handler/rails_config_handler.rb, line 15
def name
  "Rails #{environment} Config Handler"
end
read() click to toggle source
# File lib/manageiq/postgres_ha_admin/config_handler/rails_config_handler.rb, line 19
def read
  rails_params_to_pg(YAML.load_file(file_path)[environment])
end
write(params) click to toggle source
# File lib/manageiq/postgres_ha_admin/config_handler/rails_config_handler.rb, line 23
def write(params)
  db_yml = YAML.load_file(file_path)
  db_yml[environment].merge!(pg_parameters_to_rails(params))
  remove_empty(db_yml[environment])

  new_name = "#{file_path}_#{Time.current.strftime("%d-%B-%Y_%H.%M.%S")}"
  FileUtils.copy(file_path, new_name)
  begin
    File.write(file_path, db_yml.to_yaml)
  rescue
    FileUtils.mv(new_name, file_path)
    raise
  end
  new_name
end

Private Instance Methods

pg_parameters_to_rails(pg_params) click to toggle source
# File lib/manageiq/postgres_ha_admin/config_handler/rails_config_handler.rb, line 51
def pg_parameters_to_rails(pg_params)
  params = {}
  params['username'] = pg_params[:user]
  params['database'] = pg_params[:dbname]
  params['port'] = pg_params[:port]
  params['host'] = pg_params[:host]
  remove_empty(params)
end
rails_params_to_pg(params) click to toggle source
# File lib/manageiq/postgres_ha_admin/config_handler/rails_config_handler.rb, line 41
def rails_params_to_pg(params)
  pg_params = {}
  pg_params[:dbname] = params['database']
  pg_params[:user] = params['username']
  pg_params[:port] = params['port']
  pg_params[:host] = params['host']
  pg_params[:password] = ManageIQ::Password.try_decrypt(params['password'])
  remove_empty(pg_params)
end
remove_empty(hash) click to toggle source
# File lib/manageiq/postgres_ha_admin/config_handler/rails_config_handler.rb, line 60
def remove_empty(hash)
  hash.delete_if { |_k, v| v.nil? || v.to_s.strip.empty? }
end