lib/pgtk/pgsql_task.rb in pgtk-0.9.6 vs lib/pgtk/pgsql_task.rb in pgtk-0.10.0

- old
+ new

@@ -20,10 +20,11 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. require 'cgi' require 'English' +require 'qbash' require 'rake' require 'rake/tasklib' require 'random-port' require 'shellwords' require 'tempfile' @@ -64,49 +65,57 @@ FileUtils.rm_rf(home) if @fresh_start raise "Directory/file #{home} is present, use fresh_start=true" if File.exist?(home) out = "2>&1 #{@quiet ? '>/dev/null' : ''}" Tempfile.open do |pwfile| File.write(pwfile.path, @password) - system( + qbash( [ 'initdb --auth=trust', - "-D #{Shellwords.escape(home)}", + '-D', + Shellwords.escape(home), '--username', Shellwords.escape(@user), '--pwfile', Shellwords.escape(pwfile.path), out - ].join(' ') + ] ) end - raise unless $CHILD_STATUS.exitstatus.zero? port = @port if port.nil? port = RandomPort::Pool::SINGLETON.acquire - puts "Random TCP port #{port} is used" + puts "Random TCP port #{port} is used for PostgreSQL server" unless @quiet else - puts "Required TCP port #{port} is used" + puts "Required TCP port #{port} is used for PostgreSQL server" unless @quiet end - pid = Process.spawn('postgres', '-k', home, '-D', home, "--port=#{port}") + pid = Process.spawn( + [ + 'postgres', + '-k', Shellwords.escape(home), + '-D', Shellwords.escape(home), + "--port=#{port}" + ].join(' '), + $stdout => File.join(home, 'stdout.txt'), + $stderr => File.join(home, 'stderr.txt') + ) File.write(File.join(@dir, 'pid'), pid) at_exit do `kill -TERM #{pid}` - puts "PostgreSQL killed in PID #{pid}" + puts "PostgreSQL killed in PID #{pid}" unless @quiet end sleep 1 attempt = 0 begin - system( + qbash( [ "createdb -h localhost -p #{port}", '--username', Shellwords.escape(@user), Shellwords.escape(@dbname), out - ].join(' ') + ] ) - raise unless $CHILD_STATUS.exitstatus.zero? rescue StandardError => e puts e.message sleep(5) attempt += 1 raise if attempt > 10 @@ -126,8 +135,8 @@ "#{CGI.escape(@dbname)}?user=#{CGI.escape(@user)}" ].join } }.to_yaml ) - puts "PostgreSQL has been started in process ##{pid}, port #{port}" + puts "PostgreSQL has been started in process ##{pid}, port #{port}" unless @quiet end end