2021-04-30 14:24:56 +01:00
|
|
|
module MsfdbHelpers
|
|
|
|
|
class DbInterface
|
2021-04-16 13:52:55 +01:00
|
|
|
|
2021-04-30 14:24:56 +01:00
|
|
|
def initialize(options)
|
|
|
|
|
@options = options
|
|
|
|
|
end
|
2021-04-26 02:24:11 +01:00
|
|
|
|
2021-04-30 14:24:56 +01:00
|
|
|
def init
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
end
|
2021-04-16 13:52:55 +01:00
|
|
|
|
2021-04-30 14:24:56 +01:00
|
|
|
def delete
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
end
|
2021-04-16 13:52:55 +01:00
|
|
|
|
2021-04-30 14:24:56 +01:00
|
|
|
def start
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
end
|
2021-04-16 13:52:55 +01:00
|
|
|
|
2021-04-30 14:24:56 +01:00
|
|
|
def stop
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
end
|
2021-04-16 13:52:55 +01:00
|
|
|
|
2021-04-30 14:24:56 +01:00
|
|
|
def restart
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
end
|
2021-04-16 13:52:55 +01:00
|
|
|
|
2021-04-30 14:24:56 +01:00
|
|
|
def status
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
end
|
2021-04-16 13:52:55 +01:00
|
|
|
|
2021-04-30 14:24:56 +01:00
|
|
|
def write_db_client_auth_config(client_auth_config)
|
|
|
|
|
puts "Writing client authentication configuration file #{client_auth_config}"
|
|
|
|
|
File.open(client_auth_config, 'w') do |f|
|
|
|
|
|
f.puts "host \"#{@options[:msf_db_name]}\" \"#{@options[:msf_db_user]}\" 127.0.0.1/32 md5"
|
|
|
|
|
f.puts "host \"#{@options[:msftest_db_name]}\" \"#{@options[:msftest_db_user]}\" 127.0.0.1/32 md5"
|
|
|
|
|
f.puts "host \"postgres\" \"#{@options[:msftest_db_user]}\" 127.0.0.1/32 md5"
|
|
|
|
|
f.puts 'host "template1" all 127.0.0.1/32 trust'
|
|
|
|
|
if Gem.win_platform?
|
|
|
|
|
f.puts 'host all all 127.0.0.1/32 trust'
|
|
|
|
|
f.puts 'host all all ::1/128 trust'
|
|
|
|
|
else
|
|
|
|
|
f.puts 'local all all trust'
|
|
|
|
|
end
|
2021-04-26 02:24:11 +01:00
|
|
|
end
|
|
|
|
|
end
|
2021-04-16 13:52:55 +01:00
|
|
|
|
2021-04-30 14:24:56 +01:00
|
|
|
def self.requirements
|
2021-05-05 14:43:12 +01:00
|
|
|
[]
|
2021-04-30 14:24:56 +01:00
|
|
|
end
|
2021-04-20 13:47:47 +01:00
|
|
|
|
2021-04-30 14:24:56 +01:00
|
|
|
def run_cmd(cmd, input: nil, env: {})
|
|
|
|
|
puts "run_cmd: cmd=#{cmd}, input=#{input}, env=#{env}" if @options[:debug]
|
2021-04-26 02:24:11 +01:00
|
|
|
|
2021-04-30 14:24:56 +01:00
|
|
|
output, status = Open3.capture2e(env, cmd)
|
|
|
|
|
if @options[:debug]
|
|
|
|
|
puts "'#{cmd}' returned #{status.exitstatus}"
|
|
|
|
|
puts output
|
|
|
|
|
end
|
|
|
|
|
status.exitstatus
|
2021-04-26 02:24:11 +01:00
|
|
|
end
|
|
|
|
|
|
2022-06-23 16:50:23 +05:30
|
|
|
def run_psql(cmd, socket_directory= "#{Dir.tmpdir}", db_name: 'postgres')
|
2021-04-30 14:24:56 +01:00
|
|
|
if @options[:debug]
|
2022-06-23 16:50:23 +05:30
|
|
|
puts "psql -h #{socket_directory} -p #{@options[:db_port]} -c \"#{cmd};\" #{db_name}"
|
2022-01-24 00:25:25 +05:30
|
|
|
end
|
2022-01-25 20:24:14 +05:30
|
|
|
|
2022-06-23 16:50:23 +05:30
|
|
|
run_cmd("psql -h #{socket_directory} -p #{@options[:db_port]} -c \"#{cmd};\" #{db_name}")
|
2021-04-26 02:24:11 +01:00
|
|
|
end
|
2022-01-25 20:24:14 +05:30
|
|
|
|
2021-04-26 02:24:11 +01:00
|
|
|
end
|
2021-04-16 13:52:55 +01:00
|
|
|
end
|