How to deploy my Webpackered Rails app with Capistrano and Yarn - ruby-on-rails

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

Related

Cant perform query in rails : may have been in progress in another thread when fork() was called

I am running under macOS Catalina version 10.15.1
When I run my rails console on my project and try to perform a query like User.first I get :
objc[57093]: +[__NSCFConstantString initialize] may have been in progress in another thread when fork() was called.
objc[57093]: +[__NSCFConstantString initialize] may have been in progress in another thread when fork() was called. We cannot safely call it or ignore it in the fork() child process. Crashing instead. Set a breakpoint on objc_initializeAfterForkError to debug.
I followed this answer and added OBJC_DISABLE_INITIALIZE_FORK_SAFETY to my .zshrcfile which looks like that :
ZSH=$HOME/.oh-my-zsh
# You can change the theme with another one:
# https://github.com/robbyrussell/oh-my-zsh/wiki/themes
ZSH_THEME="robbyrussell"
# Useful oh-my-zsh plugins for Le Wagon bootcamps
plugins=(git gitfast zsh-autosuggestions last-working-dir zsh-syntax-highlighting common-aliases history-substring-search)
# Prevent Homebrew from reporting - https://github.com/Homebrew/brew/blob/master/share/doc/homebrew/Analytics.md
export HOMEBREW_NO_ANALYTICS=1
# Actually load Oh-My-Zsh
source "${ZSH}/oh-my-zsh.sh"
unalias rm # No interactive rm by default (brought by plugins/common-aliases)
export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
# Load rbenv if installed (To manage your Ruby versions)
export PATH="${HOME}/.rbenv/bin:${PATH}"
type -a rbenv > /dev/null && eval "$(rbenv init -)"
# Load nvm if installed (To manage your Node versions)
export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"
# Anaconda binaries (python, pip, conda, jupyter, pytest, pylint etc.)
export PATH="/anaconda3/bin:${HOME}/anaconda3/bin:${PATH}"
# Rails and Ruby uses the local `bin` folder to store binstubs.
# So instead of running `bin/rails` like the doc says, just run `rails`
# Same for `./node_modules/.bin` and nodejs
export PATH="./bin:./node_modules/.bin:${PATH}:/usr/local/sbin"
# Store your own aliases in the ~/.aliases file and load the here.
[[ -f "$HOME/.aliases" ]] && source "$HOME/.aliases"
# Encoding stuff for the terminal
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
export EDITOR=atom
When I run in my terminal echo $OBJC_DISABLE_INITIALIZE_FORK_SAFETY I get YES, so I think the environment variable is correcly set... but that doesnt fix the issue.
How can I fix this problem ?
What worked for me, was disbaling Spring: export DISABLE_SPRING=true before starting your Rails console or server
I ran into this problem after I updated my Postgres database. I ended up updating to the latest ruby version and reinstalling all gems. After that everything worked again.
I tried the OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES suggestion as well, to no avail.

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

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).

How do I run bundle remotely when RVM is involved

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/

Run rake outside of rails_root/Incron doesn't run rake or bundle commands for rails

I have a rails application in /home/myuser/watchDir/myapp and an incron job set to watch the ../watchDir for modification. Once triggered, incron will run a script, /usr/local/bin/myscript.sh. This is the only place I could get incron to run a script from. In that script I have calls to run rake and bundle commands in my root app. I The script IS being run (I have a test for that) but the bundle and rake commands both fail silently. I am fairly new to linux and internet research has given some solutions. I have all absolute paths in my scripts. I tried adding my bash_profile to the scripts/incron commands. I tried having the incron script run another script located in my home directory. All the scripts are executable. I tried using the --gemfile option for bundle but that doesn't wokr. Does anyone know what I have to do here? Basically, I want to run the bundle and rake commands outside of RAILS_ROOT. I also would like to know if incron complicates the use of the rails commands. Thanks.
EDIT:
Here are the relevant files:
Incrontab:
/home/myuser/watchDir/ IN_MODIFY,IN_CLOSE_WRITE,IN_CLOSE_NOWRITE /bin/bash /usr/local/bin/runT.sh $#/$#
I also tried this:
/home/myuser/watchDir/ IN_MODIFY,IN_CLOSE,IN_CLOSE_WRITE,IN_CLOSE_NOWRITE source '/home/myuser/.bash_profile && /bin/sh /usr/local/bin/runT.sh' $#/$#
And here's the script it's calling:
#!/bin/bash
mkdir /home/myuser/worked #This is to ensure that that incron is running and executing this script
cd /home/myuser/watchDir/myapp
/home/myuser/.rvm/gems/ruby-1.9.3-p545/bin/bundle install --gemfile /home/myuser/watchDir/myApp/Gemfile
/home/myuser/.rvm/gems/ruby-1.9.3-p545/bin/rake -f /home/myUser/watchDir/myApp
My .bash_profile file:
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
source ~/.profile
To sum up my last comments ... change your icrontab entry to be:
/home/myuser/watchDir/ IN_MODIFY,IN_CLOSE_WRITE,IN_CLOSE_NOWRITE /bin/bash /usr/local/bin/runT.sh $#/$#
And the script to be:
#!/bin/bash
source /home/myuser/.bash_profile
mkdir /home/myuser/worked #This is to ensure that that incron is running and executing this script
cd /home/myuser/watchDir/myapp
/home/myuser/.rvm/gems/ruby-1.9.3-p545/bin/bundle install --gemfile /home/myuser/watchDir/myApp/Gemfile
#/home/myuser/.rvm/gems/ruby-1.9.3-p545/bin/rake -f /home/myUser/watchDir/myApp

Resources