How do I run bundle remotely when RVM is involved - ruby-on-rails

I am trying to create a tiny shell script that will deploy a Rails app by first rsync'ing, then running the bundle command remotely via ssh. My shell script looks like this:
#!/bin/bash
REMOTE_SERVER="myserver.com"
REMOTE_USER="me"
REMOTE_PATH="/home/me/"
BUNDLE_PATH="/usr/local/rvm/gems/ruby-2.0.0-p353/bin/bundle"
# Step 1: Rsync
rsync -ave ssh --exclude-from '.ignore' ./ $REMOTE_USER#$REMOTE_SERVER:$REMOTE_PATH
# Step 2: Bundle
ssh $REMOTE_USER#$REMOTE_SERVER "cd $REMOTE_PATH && $BUNDLE_PATH install"
Rsync'ing works fine but when RVM is involved, the bundle line throws the following error:
/usr/bin/env: ruby_executable_hooks: No such file or directory
So, I'm wondering ... Is it possible to run the bundle (and other commands like rake) as part of a single ssh command?
If it matters, the remote server is running Ubuntu 14.

This problem has already been solved by the community. It's called Capistrano.
http://capistranorb.com/

Related

bundle command not found on bash script

I'm wrote a script to automatically run when reboot on crontab
this is my configuration in crontab -e
#reboot /home/deploy/startup_script >> /home/deploy/startup_script.log 2>$1
This start the script and create logs in /home/deploy
Then this is the startup_script
#!/bin/bash
echo "Changing directory"
cd /home/deploy/source/myapp
echo $PWD
echo "Pulling Dev Branch..."
git pull origin dev_branch
echo "Running Bundle Install"
sudo gem install bundler
bundle install
echo "Deploying to Staging..."
bundle exec cap staging deploy
when I run this script manually using ./startup_script it runs properly but when I run it automatically in crontab it shoes bundle command not found even I install the bundler already.
Here's the logs from startup_script.log
Changing directory
/home/deploy/source/myapp
Pulling Dev Branch...
From ssh://1.xx.xx.xx.io:20194/xx/myapp
* branch dev_branch -> FETCH_HEAD
Already up-to-date.
Running Bundle Install
Successfully installed bundler-2.0.2
Parsing documentation for bundler-2.0.2
Done installing documentation for bundler after 5 seconds
1 gem installed
/home/deploy/startup_script: line 12: bundle: command not found
Deploying to Staging...
/home/deploy/startup_script: line 15: bundle: command not found
The cron often clears the whole environment, including this $PATH variable. Therefore, the script may behave differently in your cron compared to the behavior in the shell. To avoid having to type the absolute path to a command, shells introduced the $PATH environment variable, each directory is separated by a : and searches are done from left to right.
Option I: You can use absolute path:
Run which bundle as sudoer to get the full path for the bundle command. If the output is /usr/bin/bundle, your bundle command in the script would look like:
/usr/bin/bundle install
Option II: Set the PATH variable:
Run echo "$PATH" as user who runs this script to get the $PATH variable and make sure this variable is available in your cron script too. For example, if the output was /usr/local/bin:/usr/bin:/bin, you would put the below line in the top of your shell script:
export PATH="/usr/local/bin:/usr/bin:/bin"
The environment that your crontab uses is going to be different than your regular login shell.
Now, I might be wrong about this, but I think when the crontab executes, it's not a login shell, so it doesn't have anything you've added to your path in your .bashrc or .bash_profile.
The best practice here would be to use the full path of the executable for bundle.
Redirecting stderr to stdout, there should be 2>&1
Is the path where the gem packages are installed is added to the $PATH variable? Try to provide the full path to this script
I suggest you make an entry to see what environment variables you have for crontab:
* * * * * printenv > ~/printenv.log

Sheduling start rails server after rebot with .sh script "script.sh: line 2: rails: command not found" error

I tried to automatically run my Rails server after a server reboot. I use cronetab that refers to file with scripts.
I have a problem with file.sh execution.
In this file I
#!/bin/bash
cd MyApp/ && rails s
redirect to app folder and try to run command to run the server.
I try to do a test run by launch script from bash
/directory/to/script/file.sh
but all i got is
/directory/to/script/file.sh: line 2: rails: command not found
When I run cd MyApp/ && rails s in bash directly everything works just fine.
Can you please help me. I need to automate starting rails server after rebooting.
System => Ubuntu Server 18.04.2 LTS
Thank you, guys.
In the end, the answer was already on Stack "Start rails server automatically when ever I start my ubuntu machine" but not the best-score answer was right :)
I dump idea with external script and pack everything to crontab
#reboot /bin/bash -l -c 'cd /full/path/MyApp && rails s'

How to deploy my Webpackered Rails app with Capistrano and Yarn

Problem with capistrano and Yarn
I tried to deploy my webpackered rails app to AWS with this capistrano script,
namespace :webpacker do
task :install do
on roles(:web) do
within release_path do
execute "bin/yarn"
end
end
end
end
after 'bundler:install', 'webpacker:install'
however I got the following log and deploy was failed.
INFO [74b2160e] Running /usr/bin/env bin/yarn as webmaster#example.com
DEBUG [74b2160e] Command: cd /data/example/releases/20170324031517 && ( export PATH="/usr/local/bin:$PATH" RBENV_ROOT="/usr/local/rbenv" RBENV_VERSION="2.4.0" NODENV_ROOT="/usr/local/nodenv" NODENV_VERSION="7.0.0" ; /usr/bin/env bin/yarn )
DEBUG [74b2160e] Node version 0.10.48 is not supported, please use Node.js 4.0 or higher.
So, I did ssh as webmaster to confirm that env was correct by following shell command.
which node
/usr/local/nodenv/shims/node
node --version
v7.0.0
which yarn
/usr/bin/yarn
yarn --version
0.21.3
I assume env variables is incorrect but don't know why env variable is incorrect.
Would you teach me how to fix this?
Node version 0.10.48 is not supported, please use Node.js 4.0 or higher
added
When added append :nodenv_map_bins, 'bin/yarn', another issue appear.
INFO [aab774c8] Running NODENV_ROOT=/usr/local/nodenv NODENV_VERSION=7.0.0 /usr/local/nodenv/bin/nodenv exec bin/yarn as webmaster#example.com
DEBUG [aab774c8] Command: cd /data/example/releases/20170324175015 && ( export PATH="/usr/local/bin:$PATH" RBENV_ROOT="/usr/local/rbenv" RBENV_VERSION="2.4.0" NODENV_ROOT="/usr/local/nodenv" NODENV_VERSION="7.0.0" RAILS_ENV="staging" ; NODENV_ROOT=/usr/local/nodenv NODENV_VERSION=7.0.0 /usr/local/nodenv/bin/nodenv exec bin/yarn )
DEBUG [aab774c8] nodenv: bin/yarn: command not found
The issue appears to be that you are using Nodenv. Capistrano runs in a Non-login, Non-interactive shell: http://capistranorb.com/documentation/advanced-features/ptys/ As a result, Capistrano is using system Node, not the overridden version in your .bash_profile.
This means that you probably need to evaluate the Nodenv script as part of running commands. Thankfully, it looks like there is a Gem for that: https://github.com/platanus/capistrano-nodenv
You will probably need to add bin/yarn to the :nodenv_map_bins. Something like:
append :nodenv_map_bins, 'bin/yarn'
You need to add yarn to your path or create an alias.
There's a gem for this as well: https://github.com/ManifoldScholar/capistrano-yarn

bundler: command not found: rails with docker-compose on a sample project

I'd like to develop a rails app using docker so I don't have to install a database locally. Therefore I created the following basic example (new rails app):
https://gist.github.com/solars/62eeae2f86ab6ec3fa35
As you can see there is a problem with the bundler environment, can anyone tell me how to fix this?
Usually the Gemfile is copied over in the Dockerfile - but I think this is not necessary if mounting the app in a volume, is this right? (Most of the examples are using ADD or COPY for either the Gemfile or the app, but as I'm developing, I'd like to avoid that so I can always change things.
I also thought that something like docker-compose run app echo $PATH should return the $PATH in my container, but it seems to be the same as my local path? Same with echo $RAILS_ENV which returns nothing, although it's set in the compose file..
Solution: The container of course does not persist the bundled gems, that was the problem. So the solution is to either bundle in the Dockerfile, or have the gems in the app directory which is shared as a volume.
docker-compose run app echo $PATH will always return your local path, as the environment variable is interpreted by the local shell.
This would work better:
docker-compose run app sh -c 'echo $PATH'
Regarding rails, check "Rails 4 bundler: command not found: rails"
A bundle exec rake rails:update:bin might help (for instance, as a RUN directive in your Dockerfile).

Unable to run ruby script over ssh into ec2 instance

I'm working with ec2 instances and was trying to execute a ruby script on another instance after ssh to that instance.
I have a ruby script which updates configuration files, so i need to run that script as super user. when i run the script manually on that instance, sudo ruby recreate-532d01c.rb, the error that comes is
sudo: ruby: command not found
Running simple scripts with no root permissions works, eg.ruby file_1.rb.
Using rvmsudo in place of sudo executes the script with warning,
ubuntu#ip-10-0-0-111:~$ rvmsudo ruby recreate-82bb000012.rb
Warning: can not check `/etc/sudoers` for `secure_path`, falling back to call via `/usr/bin/env`, this breaks rules from `/etc/sudoers`. Run:
export rvmsudo_secure_path=1
to avoid the warning, put it in shell initialization file to make it persistent.
In case there is no `secure_path` in `/etc/sudoers`. Run:
export rvmsudo_secure_path=0
to avoid the warning, put it in shell initialization file to make it persistent.
I tried to execute the below command from rails console of one of the instance to test and it fails to recognize ruby as command
1.9.3-p545 :002 > system("ssh -i /home/ubuntu/.ssh/own_key.pem ubuntu#**.***.***.** ruby execute-52d.rb")
bash: ruby: command not found
I tried with possible solutions over web, but could not resolve the issue. I have the same configuration running for one of my old aws acount, this is a newly created account. Not sure if this could be issue in any way as currently ec2 instances fall under vpc by default and have some changes after dec 2013
Nothing to do with your VPC. So when you run your ruby script with sudo your environment that your user is using doesn't get set for Ruby.
Sounds like you may be using rvm and you probably set it up with a 'single user' config.
Try running as your user:
which ruby
and see where your ruby executable is located at. That's what you have to make sure that when your run your script as sudo it's available in the PATH.
Worst case you would have to reinstall rvm with multiuser config which should work when you run with sudo:
user$ \curl -sSL https://get.rvm.io | sudo bash -s stable

Resources