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/standalone.rb
Views: 11766
require 'msfdb_helpers/db_interface'1module MsfdbHelpers2class Standalone < DbInterface34def initialize(options:, db_conf:, connection_string:)5@options = options6@db_conf = db_conf7begin8@conn = PG.connect(connection_string)9rescue PG::ConnectionBad10print_error('Could not connect to standalone PostgreSQL instance. Ensure that the connection string is valid, and that the database is accessible')11raise12end1314conninfo = @conn.conninfo_hash15@options[:db_port] = conninfo[:port]16@options[:db_host] = conninfo[:host]17super(options)18end1920def init(msf_pass, msftest_pass)21create_db_users(msf_pass, msftest_pass)22end2324def delete25@conn.exec("DROP DATABASE IF EXISTS #{@options[:msf_db_name]};")26@conn.exec("DROP DATABASE IF EXISTS #{@options[:msftest_db_name]};")27@conn.exec("DROP USER IF EXISTS #{@options[:msf_db_user]};")28@conn.exec("DROP USER IF EXISTS #{@options[:msftest_db_user]};")29FileUtils.rm_r(@db_conf, force: true)30end3132def start33true34end3536def stop37puts 'A standalone database cannot be stopped by msfdb'38false39end4041def restart42raise NotImplementedError43end4445def exists?46!@conn.nil?47end4849def status50# Search for the database name51is_initialized = @conn.exec_params('select * from pg_catalog.pg_database where datname = $1', [@options[:msf_db_name]]).any?52if !is_initialized53DatabaseStatus::NEEDS_INIT54else55DatabaseStatus::RUNNING56end57end5859def write_db_client_auth_config60raise NotImplementedError61end6263def self.requirements64[]65end6667private6869def create_db_users(msf_pass, msftest_pass)70@conn.exec("create user #{@options[:msf_db_user]} with password '#{msf_pass}'")71@conn.exec("create user #{@options[:msftest_db_user]} with password '#{msftest_pass}'")72@conn.exec("alter role #{@options[:msf_db_user]} createdb")73@conn.exec("alter role #{@options[:msftest_db_user]} createdb")74@conn.exec("alter role #{@options[:msf_db_user]} with password '#{msf_pass}'")75@conn.exec("alter role #{@options[:msftest_db_user]} with password '#{msftest_pass}'")76@conn.exec("CREATE DATABASE #{@options[:msf_db_name]}")77@conn.exec("CREATE DATABASE #{@options[:msftest_db_name]}")78@conn.finish79end80end81end828384