English 中文(简体)
Using Ruby popen and PostgreSQL createuser
原标题:

I am attempting to write a very simple rake task (and merge it into a rather large rake task) that will call the following command and pass in a randomly generated password. For the moment, let s even fake the random generation and just give it a set password of test :

createuser -SDPRE test

The code I have for my task is as follows:

desc "Create a test user with test password"
task "test" do
  puts( Creating User )
  IO.popen "createuser -SDRPE test",  w+  do |io|
    io.write "test
test
"
    io.close_write
  end

  raise  createuser failed  unless $?.success?
end

The io.write appears not to work as I still have to enter the password. It s also not being clobbered. After running the task and entering the password manually, I can use that password to log in with psql.

I have tried quite a few variations such as io.close, opening the file as w and even r or r+ because I saw other examples that use it.

I am a bit stumped on how to get this to work. Any ideas/comments/answers would be greatly appreciated!

Edit 1: This is on a Debian (Lenny) Linux system in case it makes any difference.

最佳回答

createuser is opening /dev/tty to read the password

Programs that read passwords usually sacrifice the Unix tools paradigm with respect to passwords. This is a reasonable thing to do because it allows other functionality to respect standard input and output, and put it all on pause while it opens /dev/tty for an interactive password.

In any case, an strace(1) reveals that createuser is in fact opening /dev/tty.

All is not lost, createuser is just a link to /usr/share/postgresql-common/pg_wrapper which is a Perl program. I m sure it can be hacked for password scripting compatibility.

问题回答

I know this is not an answer to your question, but have you considered creating PG user by using SQL instead of invoking shell commands?

http://www.postgresql.org/docs/8.0/interactive/sql-createuser.html

It seems safer and more reliable.





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签