Module: ForemanMaintain::Concerns::BaseDatabase
- Defined in:
- lib/foreman_maintain/concerns/base_database.rb
Instance Method Summary collapse
-
#backup_dir ⇒ Object
TODO: remove the backup file path tools from here.
- #backup_local(backup_file, extra_tar_options = {}) ⇒ Object
- #configuration ⇒ Object
- #data_dir ⇒ Object
- #db_version ⇒ Object
- #deb_postgresql_config_dirs ⇒ Object
- #deb_postgresql_data_dir ⇒ Object
- #deb_postgresql_versions ⇒ Object
- #dropdb ⇒ Object
- #dump_db(file) ⇒ Object
- #local? ⇒ Boolean
- #ping ⇒ Object
- #postgresql_conf ⇒ Object
- #psql(query) ⇒ Object
- #psql_cmd_available? ⇒ Boolean
- #query(sql) ⇒ Object
- #query_csv(sql) ⇒ Object
- #raise_psql_missing_error ⇒ Object
- #restore_dump(file, localdb) ⇒ Object
Instance Method Details
#backup_dir ⇒ Object
TODO: remove the backup file path tools from here. Lib Utils::Backup?
107 108 109 |
# File 'lib/foreman_maintain/concerns/base_database.rb', line 107 def backup_dir @backup_dir ||= File.(ForemanMaintain.config.db_backup_dir) end |
#backup_local(backup_file, extra_tar_options = {}) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/foreman_maintain/concerns/base_database.rb', line 90 def backup_local(backup_file, = {}) dir = .fetch(:data_dir, data_dir) restore_dir = .fetch(:restore_dir, data_dir) command = .fetch(:command, 'create') FileUtils.cd(dir) do = { :archive => backup_file, :command => command, :transform => "s,^,#{restore_dir[1..]},S", :files => '*', }.merge() feature(:tar).run() end end |
#configuration ⇒ Object
41 42 43 |
# File 'lib/foreman_maintain/concerns/base_database.rb', line 41 def configuration raise NotImplementedError end |
#data_dir ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/foreman_maintain/concerns/base_database.rb', line 4 def data_dir if debian_or_ubuntu? deb_postgresql_data_dir else '/var/lib/pgsql/data/' end end |
#db_version ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/foreman_maintain/concerns/base_database.rb', line 124 def db_version ping! query = 'SHOW server_version' server_version_cmd = 'psql --tuples-only --no-align' version_string = execute!(server_version_cmd, :stdin => query, :env => base_env) version(version_string) end |
#deb_postgresql_config_dirs ⇒ Object
35 36 37 38 39 |
# File 'lib/foreman_maintain/concerns/base_database.rb', line 35 def deb_postgresql_config_dirs deb_postgresql_versions.map do |ver| "/etc/postgresql/#{ver}/main/" end end |
#deb_postgresql_data_dir ⇒ Object
12 13 14 15 16 |
# File 'lib/foreman_maintain/concerns/base_database.rb', line 12 def deb_postgresql_data_dir deb_postgresql_versions.map do |ver| "/var/lib/postgresql/#{ver}/main/" end end |
#deb_postgresql_versions ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/foreman_maintain/concerns/base_database.rb', line 18 def deb_postgresql_versions @deb_postgresql_versions ||= begin installed_pkgs = package_manager.list_installed_packages('${binary:Package}\n') installed_pkgs.grep(/^postgresql-\d+$/).map do |name| name.split('-').last end end end |
#dropdb ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/foreman_maintain/concerns/base_database.rb', line 111 def dropdb if local? execute!("runuser - postgres -c 'dropdb #{configuration['database']}'") else delete_statement = psql(<<~SQL) select string_agg('drop table if exists \"' || tablename || '\" cascade;', '') from pg_tables where schemaname = 'public'; SQL psql(delete_statement) end end |
#dump_db(file) ⇒ Object
71 72 73 74 |
# File 'lib/foreman_maintain/concerns/base_database.rb', line 71 def dump_db(file) dump_command = "pg_dump -Fc -f #{file}" execute!(dump_command, :env => base_env) end |
#local? ⇒ Boolean
45 46 47 |
# File 'lib/foreman_maintain/concerns/base_database.rb', line 45 def local? ['localhost', '127.0.0.1', `hostname`.strip].include?(configuration['host']) end |
#ping ⇒ Object
65 66 67 68 69 |
# File 'lib/foreman_maintain/concerns/base_database.rb', line 65 def ping execute?('psql', :stdin => 'SELECT 1 as ping', :env => base_env) end |
#postgresql_conf ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/foreman_maintain/concerns/base_database.rb', line 27 def postgresql_conf return "#{data_dir}/postgresql.conf" if el? deb_postgresql_config_dirs.map do |conf_dir| "#{conf_dir}postgresql.conf" end end |
#psql(query) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/foreman_maintain/concerns/base_database.rb', line 57 def psql(query) ping! execute('psql', :stdin => query, :env => base_env) end |
#psql_cmd_available? ⇒ Boolean
133 134 135 136 |
# File 'lib/foreman_maintain/concerns/base_database.rb', line 133 def psql_cmd_available? exit_status, _output = execute_with_status('which psql') exit_status == 0 end |
#query(sql) ⇒ Object
49 50 51 |
# File 'lib/foreman_maintain/concerns/base_database.rb', line 49 def query(sql) parse_csv(query_csv(sql)) end |
#query_csv(sql) ⇒ Object
53 54 55 |
# File 'lib/foreman_maintain/concerns/base_database.rb', line 53 def query_csv(sql) psql(%{COPY (#{sql}) TO STDOUT WITH CSV HEADER}) end |
#raise_psql_missing_error ⇒ Object
138 139 140 141 |
# File 'lib/foreman_maintain/concerns/base_database.rb', line 138 def raise_psql_missing_error raise Error::Fail, 'The psql command not found.'\ ' Make sure system has psql utility installed.' end |
#restore_dump(file, localdb) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/foreman_maintain/concerns/base_database.rb', line 76 def restore_dump(file, localdb) if localdb dump_cmd = "runuser - postgres -c 'pg_restore -C -d postgres #{file}'" execute!(dump_cmd) else # TODO: figure out how to completely ignore errors. Currently this # sometimes exits with 1 even though errors are ignored by pg_restore dump_cmd = 'pg_restore --no-privileges --clean --disable-triggers -n public ' \ "-d #{configuration['database']} #{file}" execute!(dump_cmd, :env => base_env, :valid_exit_statuses => [0, 1]) end end |