Why does "bundle exec" eat the parameters I pass in? - ruby

When I invoke commands using bundle exec it takes the parameters I pass in. An example for this would be:
bundle exec my_command run --verbose
In this case --verbose is used as a bundler argument where as it should be used for my_command. I know the following way would work:
bundle exec 'my_command run --verbose'
Is it possible to avoid the quotes? The command I use has already a lot of quotes. I expected something like this would work but it didn't:
bundle exec -- my_command run --verbose
I don't see much documentation about this for bundler. Any ideas would be greatly appreciated.

This looks like what is a common problem when passing one command to another in the shell, and it looks like you're close to what I'd use. Instead of using:
bundle exec my_command run --verbose
Or:
bundle exec -- my_command run --verbose
Try:
bundle exec my_command -- run --verbose
Using bundle exec -- breaks the command-chain for bundle exec. exec is a sub-command for bundle and my_command is a parameter for exec. The parameters for my_command, well, neither bundle or exec needs to know about them so the -- goes where you want to break that chain of parameters to bundle.

Inspecting from source of bundler, it is default behavior to pass all the parameters after bundle exec to Kernel.exec, so the --verbose parameters will be passed to your command, not bundle.
bundle exec my_command run --verbose
will run the following under the context of bundle
Kernel.exec('my_command', 'run', '--verbose')
and
bundle exec -- my_command run --verbose
results in an error because no command/script is named --.
Check the test case here:
#!/usr/bin/env ruby
# coding: utf-8
# file: test.rb
p ARGV
test:
$ bundle exec ruby test.rb --verbose --arg1
["--verbose", "--arg1"]

Related

Bash script not navigating to folder, only accepting input when "exit" is typed manually

I have the following bash script I am trying to use so I can speed up updates to my code. For some reason it gets stuck at cd /var/www/myapp/code and only prompts input when I type exit to return to the root user.
#!/bin/bash
# Pulls from remote repo and implements changes
su - rails
cd /var/www/myapp/code
git pull
expect "sername for 'https://bitbucket.org':"
send "myusername"
interact
expect "assword for 'https://username#bitbucket.org':"
send "mypassword"
interact
exit
cd /var/www/myapp/code
RAILS_ENV=production bundle exec rake assets:clobber
RAILS_ENV=production bundle exec rake assets:precompile
nginx -t && sudo nginx -s reload
I tried manually entering cd /var/www/myapp/code but it's still not executing until I type exit.
Well, it's a bit of re-factoring, but a solution is to wrap everything in a single su - (user) -c "bla" call, which will automatically exit the subshell as (user) once the commands are all done.
Syntax:
su - (user) -c "command"
Here's how I'd do it:
Note that we have to break commands up with ';', and escape double-quotes with a '\'.
#!/bin/bash
# Pulls from remote repo and implements changes
su - rails -c "cd /var/www/myapp/code;
git pull;
expect \"sername for 'https://bitbucket.org':\";
send \"myusername\";
interact;
expect \"assword for 'https://bitbucket.org':\";
send \"mypassword\";
interact" # Now that the final command's ran,
# we'll auto - exit the subshell
cd /var/www/myapp/code
RAILS_ENV=production bundle exec rake assets:clobber
RAILS_ENV=production bundle exec rake assets:precompile
nginx -t && sudo nginx -s reload

Ubuntu - Terminal closes when I type command?

export PS1="\e[48;5;88m\e[38;5;15m\e[K\e[1m\t \e[38;5;82m\u\e[38;5;15m#\e[38;5;82m\h\e[38;5;15m on: \W [\e[38;5;82m\$\e[38;5;15m]\e[0m\n"
With this variable set in .bashrc, exerytime I type exec bundle rake db:migrate.
I don't know what is wrong with the colors commands.
I use GNOME Terminal 3.18.3.
Your command exec bundle... should be bundle exec.... Running the exec command replaces your shell process with the proceeding command.

Ruby RVM under another user from Bash script

What is the best way to launch ruby script under another user via su command and from another script?
I have a bash script with the following command for launching unicorn:
sudo -u unicornuser sh -l -c "bundle exec unicorn_rails -E production -D"
This script is an init.d script and it being execute when system starts and manually also.
The problem is the default ruby on system is 1.8, other rubyes (1.9) is working under RVM. I need to modify above script that it can execute ruby with RVM (non system wide)
Now I've migrated for this notation:
su -l -c "rvm use ruby-1.9.3-p125 && bundle exec unicorn_rails -E production -D" unicornuser
this worked for me, but I guess there must be a better way to do this.
You should use wrappers:
rvm wrapper ruby-1.9.3-p125 ext_1.9.3 bundle
This will create $rvm_path/bin/ext_1.9.3_bundle, so now you can use:
/full/path/to/rvm/bin/ext_1.9.3_bundle exec unicorn_rails -E production -D
replace /full/path/to/rvm with output from echo $rvm_path

Why does ruby script stop when trying to start unicorn_rails as daemon?

I'm trying to start unicorn_rails in a ruby script, and after executing many commands in the script, when the script gets to the following line
%x[bash -ic "bash <(. ~/.bashrc); cd /home/www-data/rails_app; bundle exec unicorn_rails -p 8000 -E production -c /home/www-data/rails_app/config/unicorn.rb -D"]
the script stops, generating the following output
[1]+ Stopped ./setup_rails.rb
and returns to the Linux prompt. If I type "fg", the script finishes running, the line where the script had stopped gets executed and unicorn gets started as a daemon.
If I run the line in a separate script, the script completes without stopping.
UPDATE_1 -
I source .bashrc because earlier in the script I install rvm and to get it to run with the correct environment I have the following:
%x[echo "[[ -s \"$HOME/.rvm/scripts/rvm\" ]] && source \"$HOME/.rvm/scripts/rvm\"" >> .bashrc]
%x[bash -ic "bash <(. ~/.bashrc); rvm install ruby-1.9.2-p290; rvm 1.9.2-p290 --default;"]
So if I want to run correct version of rvm, ruby and bundle I need to source .bashrc
end UPDATE_1
Does anyone have any idea what could cause a ruby script to halt as if control-Z was pressed?
Not sure why it's stopping, but my general rule of thumb is to never source my .bashrc in a script -- that might be the source of your problem right there, but I can't be sure without seeing what's in it. You should be able to change your script to something like:
$ vi setup_rails.sh
#!/usr/bin/bash
# EDIT from comments below
# expanding from a one liner to a better script...
$RVM_PATH=$HOME/.rvm/scripts
# install 1.9.2-p290 unless it's installed
$RVM_PATH/rvm info 1.9.2-p290 2&>1 >/dev/null || $RVM_SH install 1.9.2-p290
# run startup command inside rvm shell
$RVM_PATH/rvm-shell 1.9.2-p290 -c "cd /home/www-data/rails_app && bundle exec unicorn_rails -p 8000 -E production -c /home/www-data/rails_app/config/unicorn.rb -D"
This should give you the same result.

Run a gem from rvm with runit

I need to create a runit service that runs a gem's binary that was installed with rvm, the problem is that a non-login bash shell, which is how runit runs its services does not have the correct path's for rvm. Is there any automatic way of doing this?
I use following script:
#!/bin/sh
exec 2>&1
DIR=/var/www/apps/mega_app/current
export rvm_path=/usr/local/rvm
export rvm_ignore_rvmrc=1
cd $DIR
exec chpst -u user:group /usr/local/rvm/bin/rvm ree exec bundle exec ${DIR}/daemons/mega_daemon.rb
Does su - USERNAME -c '/path/to/script' work? It should preserve the $PATH variables.

Resources