Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/msfdb_helpers/pg_ctlcluster.rb
Views: 11766
require 'msfdb_helpers/db_interface'12module MsfdbHelpers3class PgCtlcluster < DbInterface45def initialize(db_path:, options:, localconf:, db_conf:)6@db = db_path7@options = options8@pg_version = get_postgres_version9@localconf = localconf10@db_conf = db_conf11@pg_cluster_conf_root = "#{@localconf}/.local/etc/postgresql"12ENV['PG_CLUSTER_CONF_ROOT'] = @pg_cluster_conf_root13super(options)14end1516def init(msf_pass, msftest_pass)17puts "Creating database at #{@db}"18Dir.mkdir(@db)19FileUtils.mkdir_p(@pg_cluster_conf_root)20run_cmd("pg_createcluster --user=$(whoami) -l #{@db.shellescape}/log -d #{@db.shellescape} -s /tmp --encoding=UTF8 #{@pg_version} #{@options[:msf_db_name].shellescape} -- --username=$(whoami) --auth-host=trust --auth-local=trust")21File.open("#{@pg_cluster_conf_root}/#{@pg_version}/#{@options[:msf_db_name]}/postgresql.conf", 'a') do |f|22f.puts "port = #{@options[:db_port]}"23end2425start2627create_db_users(msf_pass, msftest_pass)2829write_db_client_auth_config30restart31end3233def delete34if exists?35stop3637if @options[:delete_existing_data]38puts "Deleting all data at #{@db}"39run_cmd("pg_dropcluster #{@pg_version} #{@options[:msf_db_name].shellescape}")40FileUtils.rm_rf(@db)41FileUtils.rm_rf("#{@localconf}/.local/etc/postgresql")42FileUtils.rm_r(@db_conf, force: true)43end44else45puts "No data at #{@db}, doing nothing"46end47end4849def start50print "Starting database at #{@db}..."51status = run_cmd("pg_ctlcluster #{@pg_version} #{@options[:msf_db_name].shellescape} start -- -o \"-p #{@options[:db_port]}\" -D #{@db.shellescape} -l #{@db.shellescape}/log")52case status53when 054puts 'success'.green.bold.to_s55return true56when 257puts "Database already started at #{@db}"58return true59else60puts 'failed'.red.bold.to_s61return false62end63end6465def stop66run_cmd("pg_ctlcluster #{get_postgres_version} #{@options[:msf_db_name].shellescape} stop -- -o \"-p #{@options[:db_port]}\" -D #{@db.shellescape}")67end6869def restart70run_cmd("pg_ctlcluster #{@pg_version} #{@options[:msf_db_name].shellescape} reload -- -o \"-p #{@options[:db_port]}\" -D #{@db.shellescape} -l #{@db.shellescape}/log")71end7273def exists?74Dir.exist?(@db)75end7677def status78if exists?79if run_cmd("pg_ctlcluster #{@pg_version} #{@options[:msf_db_name].shellescape} status -- -o \"-p #{@options[:db_port]}\" -D #{@db.shellescape}") == 080DatabaseStatus::RUNNING81else82DatabaseStatus::INACTIVE83end84else85DatabaseStatus::NOT_FOUND86end87end8889def write_db_client_auth_config90client_auth_config = "#{@pg_cluster_conf_root}/#{@pg_version}/#{@options[:msf_db_name]}/pg_hba.conf"91super(client_auth_config)92end9394def self.requirements95%w[psql pg_ctlcluster pg_dropcluster pg_createcluster pg_config]96end9798private99100def get_postgres_version101output, _status = Open3.capture2('pg_config --version') # Example outputs102# PostgreSQL 12.6 (Ubuntu 12.6-0ubuntu0.20.04.1)103# PostgreSQL 13.2 (Debian 13.2-1)104# PostgreSQL 11.11105/PostgreSQL\s(?<version>\d+)\.\d+/ =~ output106version107end108109def create_db_users(msf_pass, msftest_pass)110puts 'Creating database users'111run_psql("create user #{@options[:msf_db_user].shellescape} with password '#{msf_pass}'")112run_psql("create user #{@options[:msftest_db_user].shellescape} with password '#{msftest_pass}'")113run_psql("alter role #{@options[:msf_db_user].shellescape} createdb")114run_psql("alter role #{@options[:msftest_db_user].shellescape} createdb")115run_psql("alter role #{@options[:msf_db_user].shellescape} with password '#{msf_pass}'")116run_psql("alter role #{@options[:msftest_db_user].shellescape} with password '#{msftest_pass}'")117118conn = PG.connect(host: @options[:db_host], dbname: 'postgres', port: @options[:db_port], user: @options[:msf_db_user], password: msf_pass)119conn.exec("CREATE DATABASE #{@options[:msf_db_name]}")120conn.exec("CREATE DATABASE #{@options[:msftest_db_name]}")121conn.finish122end123end124end125126127