Casey Watson
Casey Watson

Reputation: 52682

Passing arguments to ruby interpreter when stdin is used as ruby source

I built a rails deploy dashboard that kicks off a ruby script on a remote machine to update a deployed application.

The command to run the script looks like this:

ssh test-host-02 
  "wget -q -O - http://server/deploy.rb | sudo ruby" 
  > /tmp/update-test-host-02.log 2>&1

Now I need to pass arguments into the deploy.rb script. Such as which build to deploy.

What is the best way to pass arguments to deploy.rb?

Upvotes: 4

Views: 552

Answers (2)

cam
cam

Reputation: 14222

Ruby follows the standard convention of allowing you to specify stdin input with -, which allows you to pass arguments after.

$ ruby -- - HELLO
puts ARGV.first # outputs HELLO

The double-dash isn't necessary in most cases.

Another option is to use turn the output of wget into a file descriptor using bash's <() operator:

$ ruby <(wget -O - http://example.com/deploy.rb) option1 option2 ...

Upvotes: 5

the Tin Man
the Tin Man

Reputation: 160551

I'd modify the wget line:

"wget -q -O http://server/deploy.rb && sudo ruby deploy.rb -p 1 --another parm" 

I haven't tested it, but the idea is to chain Ruby's invocation rather than pipe into it.

Upvotes: 2

Related Questions