SSHKit error during Capistrano deployment of Rails app - ruby-on-rails

After installing a new gem, Slackistrano to already succesfully deployed app with Capistrano, our deployments started failing with the following error during rake assets:precompile or rake db:migrate tasks
cap aborted!
SSHKit::Command::Failed: rake stdout: Nothing written
rake stderr: Nothing written
/Users/pete/.rvm/gems/ruby-2.2.2/gems/sshkit-1.3.0/lib/sshkit/command.rb:94:in `exit_status='
This error is obviously not very helpful.
If I ran the capistrano task cap staging assets:precompile, it would succeed.
So what's going on here?
There are a few other solutions to SSHKit failures during cap deployments, but most of them were for first time deployments and didn't work for me.

Here's how I figured out the solution(after much hair pulling).
I knew that it was probably the new gem that was causing the problem. So I went into the source code for sshkit/command.rb and outputted the command it was trying to execute on the remote server.
I then logged into the remote server and ran it manually and got the following output:
$ cd /home/web/sites/site.com/releases/20160106224143 && ( RAILS_ENV=production ~/.rvm/bin/rvm default do bundle exec rake db:migrate )
rake aborted!
NoMethodError: undefined method `each_pair' for "slack:deploy:updating":String
/home/web/sites/site.com/shared/bundle/ruby/2.2.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1602:in `compile!'
/home/web/sites/site.com/shared/bundle/ruby/2.2.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1344:in `add_filter'
/home/web/sites/site.com/shared/bundle/ruby/2.2.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1331:in `before'
/home/web/sites/site.com/shared/bundle/ruby/2.2.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1989:in `block (2 levels) in delegate'
/home/web/sites/site.com/shared/bundle/ruby/2.2.0/gems/slackistrano-1.0.0/lib/slackistrano/tasks/slack.rake:121:in `<top (required)>'
So, the problem started with Slackistrano, but then, what? why is Sinatra there? I don't use Sinatra in my app.
Well, I discovered that there was a gem in my Gemfile that was for development only(mailcatcher) but was in the production section of the Gemfile. I removed this and redeployed successfully.
However, there is obviously a deeper issue because there seems to be some sort of name collision on the "before" method of sinatra and capistrano that caused this in the first place.
I'm posting here to help others debug possible SSHKit errors during Cap deployments and also perhaps to guide others on weird name collisions between Sinatra and Capistrano.

For me this issue can arise if you're accidentally on an incompatible version of Ruby whilst running Capistrano. I had to add a .ruby-version to clamp it for me to 2.7.5 whilst using gem 'capistrano', '3.11.0'.
So I'm not sure if stating Ruby version 2 or higher is strictly true.

Related

Heroku Rails build failing due to Bootsnap and Rake issues

I'm trying to follow along with the Rails Tutorial and can't seem to get builds working on Heroku.
My build log is here:
https://pastebin.com/pBnGkBXe
Here are the errors I've tried debugging so far:
! Could not detect rake tasks
! ensure you can run `$ bundle exec rake -P` against your app
! and using the production group of your Gemfile.
Running bundle exec rake -P locally does work on my repository.
! /tmp/build_5f6ec788/config/boot.rb:4:in `require': cannot load such file -- bootsnap/setup (LoadError)
! from /tmp/build_5f6ec788/config/boot.rb:4:in `<top (required)>'
! from /tmp/build_5f6ec788/bin/rake:3:in `require_relative'
! from /tmp/build_5f6ec788/bin/rake:3:in `<main>'
I wasn't able to figure out what was happening here.
I tried running this with both the Rails Default Gemfile and the Rails Tutorial Gemfile and it resulted in the same Bootsnap error.
Can anyone make suggestions on how to get this Heroku deployment working?
Thank you!
Figured it out.
I was running Ruby v2.6.3. Upgrade to 2.7.2 and fixing that in the Gemfile resulted in a successful deploy to Heroku.

ActiveRecord::PendingMigrationError - migrating from one server to another

I'm new to Rails so please bear with me.
Currently there is an existing project (Rails 4.1.6, Ruby 2.1.3) running on digital ocean VM (Ubuntu 14.04) and its connecting to Postgresdb 9.6 without any issues. I have to migrate this to new VM.
I have setup a new vm with Ubuntu 16.04, installed Rails and Ruby with exact same versions. I tried creating a test rails app and its working fine. However, when I tried to deployed the project from old server to this new one its giving me errors.
Details:
There was an error while trying to load the gem 'uglifier'.
Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
Backtrace for gem load error is:
/usr/local/lib/ruby/gems/2.1.0/gems/execjs-2.7.0/lib/execjs /runtimes.rb:58:in `autodetect'
/usr/local/lib/ruby/gems/2.1.0/gems/execjs-2.7.0/lib/execjs.rb:5:in `<module:ExecJS>'
/usr/local/lib/ruby/gems/2.1.0/gems/execjs-2.7.0/lib/execjs.rb:4:in `<top (required)>'
/usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.1.6/lib /active_support/dependencies.rb:247:in `require'
/usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `block in require'
/usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:232:in `load_dependency'
After searching on internet, most of them suggested to uncomment this line in the Gemfile.
# gem 'therubyracer', platforms: :ruby
This was commented in the old server, however, I tried uncommenting it and it came back with different error.
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
Website was connecting to the same database and old webportal was running fine but it gave me this error. So I setup a new test db and ran the following commands -
RAILS_ENV=production bundle exec rake db:create
RAILS_ENV=production bundle exec rake db:setup
RAILS_ENV=production bundle exec rake db:migrate
All those commands were executed successfully but for some reason I'm still receiving "Migrations are pending....." error.
Please advise.
The migration-error seems to think you should be on the development environment... but the migrations you ran are for production... so either your environment-setting is not correct, or you need to run the migrations for development instead

Rails: Could not find rake-10.1.1 in any of the sources

I am experiencing an error with running the Rake command. When I try to Rake in my Rails project, I get an error that says Could not find rake-10.1.1 in any of the sources.
I put Rake 10.1.1 in my Gemfile, but it appears to do nothing when I bundle install.
I've tried uninstalling then installing the Rake gem in my computer's Ruby, RVM Ruby-2.1.0, the RVM Ruby-2.1.0#global, and the RVM gemset that I created for the specific project. I've also tried removing the Gemfile.lock and then bundle installing.
I've tried to manually run rake out of the terminal in multiple different Ruby/gems files including RVM. It gives this error:
/Users/me/.rvm/gems/ruby-2.1.0/gems/rake-10.1.1/bin/rake ; exit;
~ me$ /Users/me/.rvm/gems/ruby-2.1.0/gems/rake-10.1.1/bin/rake ; exit;
rake aborted!
No Rakefile found (looking for: rakefile, Rakefile, rakefile.rb, Rakefile.rb)
I have a Rakefile in my project and have tried renaming it as well (Rakefile.rb, rakefile).
When I run Rake commands such as rake db:migrate, I get this error:
rake aborted!
undefined local variable or method config' for main:Object
/Users/me/RailsProject/config/environment.rb:4:in'
/Users/me/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in require'
/Users/me/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:inblock in require'
/Users/me/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:214:in load_dependency'
/Users/me/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:inrequire'
/Users/me/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/application.rb:189:in require_environment!'
/Users/me/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/application.rb:250:inblock in run_tasks_blocks'
/Users/me/.rvm/gems/ruby-2.1.0/bin/ruby_executable_hooks:15:in eval'
/Users/me/.rvm/gems/ruby-2.1.0/bin/ruby_executable_hooks:15:in'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)
I've looked everywhere for the solution before coming here, but I could not find it. I would be appreciative if someone could give me some advice on this issue. Thank you in advance.
Looks like it it having trouble finding dependencies. Have you installed rake on your system with gem install rake? If not you will need to use bundle exec rake.
I don't know what the problem was, but I solved it by creating a new gemset, installing Rake to it, and using it. I also switched from Rubymine to Sublime. It may have been Rubymine trying to use a different gemset. Beware of those IDE settings.

Heroku upload-Precompiling assets failed

I need help. When trying to upload my app to heroku, I get this error, anyone know why? A few was wrong. thanks
Using rake (10.1.0)
...
Using tlsmail (0.0.1)
Using uglifier (2.1.2)
Your bundle is complete! It was installed into ./vendor/bundle
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
/tmp/build_e8889be5-168c-49ed-81e7-b71061fc82ee/vendor/bundle/ruby/1.9.1/gems/tlsmail-0.0.1/lib/net/smtp.rb:806: warning: already initialized constant SMTPSession
...
/tmp/build_e8889be5-168c-49ed-81e7-b71061fc82ee/vendor/bundle/ruby/1.9.1/gems/tlsmail-0.0.1/lib/net/pop.rb:702: warning: already initialized constant APOPSession
DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /tmp/build_e8889be5-168c-49ed-81e7-b71061fc82ee/Rakefile:7)
...
rake aborted!
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
/tmp/build_e8889be5-168c-49ed-81e7-b71061fc82ee/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `initialize'
/tmp/build_e8889be5-168c-49ed-81e7-b71061fc82ee/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `new'
...
/tmp/build_e8889be5-168c-49ed-81e7-b71061fc82ee/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/sprockets/assets.rake:29:in `block (2 levels) in <top (required)>'
Tasks: TOP => environment
(See full trace by running task with --trace)
!
! Precompiling assets failed.
From the Heroku docs:
This means that your app is attempting to connect to the database as part of rake assets:precompile. Because the config vars are not present in the environment, we use a placeholder DATABASE_URL to satisfy Rails.
To resolve this issue, ensure that the following line appears in your config/application.rb:
# config/application.rb
config.assets.initialize_on_precompile = false
Once added, commit your change and redeploy to Heroku – your assets should compile without your app attempting to connect to the database, which should resolve the error you're witnessing.
UPDATE:
Line 46 of your stacktrace includes the following message: Devise.secret_key was not set.
According to the author of Devise, José Valim, this issue can be resolved in the following manner:
Please add the following to your Devise initializer:
config.secret_key = '-- secret key --'
Alternatively, the following solution seems to have worked for a number of users:
I went to my routes.rb file and commented out the line devise_for :installs
Then I went back and reran rails generate devise:install. If that doesn't work, use the previous version of devise by editing your Gemfile's reference to Devise like this: gem 'devise', '3.0.3' and then follow the steps i mentioned above.
There a few things that solved this issue for me:
# config/application.rb
config.assets.initialize_on_precompile = false
Then, before I deployed, I compiled my assets locally and committed them:
RAILS_ENV=production bundle exec rake assets:precompile
Also, I installed this heroku add on, as was specified by the app I was launching (in my case, Spree commerce)
heroku labs:enable user-env-compile -a myapp
And of course, make sure your database.yml file is set to use adapter: postgresql.
Commit all of this, push to heroku, and hopefully it will launch. If you still cannot open your app, try looking at the Heroku logs: heroku logs -n 500
I still needed to migrate my database with heroku run rake db:migrate
when you are using github and you are pushing to heroku while you are in develop branch, dont do it, go to master branch and get the updates in the develop by git merge develop
after that,
rails precompile:assets
git add -A
git commit -m "Precompile assets"
git push heroku master
if you want to open the website that you deployed
heroku open
if nothing shows, migrate your database first by:
heroku run rails db:migrate
heroku open
I have failed Heroku proceompiling with same error message.
Carrierwave causes that because I have missed set up SECRET_KEY_BASE to Heroku setting.

How to fix the uninitialized constant Rake::DSL problem on Heroku?

I am getting errors similar to the ones in these questions, except mine are occuring on Heroku:
2011-05-30T09:03:29+00:00 heroku[worker.1]: Starting process with command: `rake jobs:work`
2011-05-30T09:03:30+00:00 app[worker.1]: (in /app)
2011-05-30T09:03:30+00:00 heroku[worker.1]: State changed from starting to up
2011-05-30T09:03:33+00:00 app[worker.1]: rake aborted!
2011-05-30T09:03:33+00:00 app[worker.1]: uninitialized constant Rake::DSL
2011-05-30T09:03:33+00:00 app[worker.1]: /app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.0/lib/rake/tasklib.rb:8:in `<class:TaskLib>'
The answer in those questions seems to be to specify gem 'rake', '0.8.7' because the 0.9 version causes the problem.
When I try to add gem 'rake', '0.8.7' to my gemfile and push to Heroku I get this error:
Unresolved dependencies detected; Installing...
You have modified your Gemfile in development but did not check
the resulting snapshot (Gemfile.lock) into version control
You have added to the Gemfile:
* rake (= 0.8.7)
FAILED: http://devcenter.heroku.com/articles/bundler
! Heroku push rejected, failed to install gems via Bundler
error: hooks/pre-receive exited with error code 1
To git#heroku.com:my_app.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git#heroku.com:my_app.git'
My gemfile normally works fine on Heroku. What should I do?
Put this in your Rakefile above require 'rake':
require 'rake/dsl_definition'
Any time you change your Gemfile, you need to bundle install to update your lockfile (Gemfile.lock). The error you're getting on push is not specific to changing the version of rake.
bundle install
git commit -a -m "update lockfile"
git push heroku master
Note the error message you received:
You have modified your Gemfile in development but did not check the resulting snapshot (Gemfile.lock) into version control
I solved this, finally, after a lot of mucking about. The short version of what I did, missing out the many experiments, was this:
1) change the Gemfile to specify Rake 0.8.7
#in Gemfile
gem "rake", "0.8.7"
2) Take out a hack that I had previously added to Rakefile based on Stack Overflow question Ruby on Rails and Rake problems: uninitialized constant Rake::DSL:
So, my Rakefile is now back to being the standard Rakefile for my app:
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
require 'rake'
MyApp::Application.load_tasks
3) Change Heroku to run my app in Ruby 1.9.2:
heroku stack:migrate bamboo-mri-1.9.2 --app myapp
git push heroku master
And it seems fine now - the scheduled cron task is running anyway.
EDIT: It did run fine, once, then blew up again next time I pushed something! Arrgh. I think I fixed it now, with the addition of the delayed_job gem, based on the conversation Don't know how to build task jobs:work.
Installing delayed_job doesn't seem like a great solution, but it HAS worked, and I might want to use it sometime I guess, especially with Heroku's once-per-hour cron job (which just isn't frequent enough - there are things I'll probably want to run every five minutes). After I installed the delayed_job gem I had to do the setup for it, otherwise Heroku complains about the missing delayed_jobs table:
#add to gemfile
gem 'delayed_job'
#at command line
bundle install
rails g delayed_job
rake db:migrate
git add -A
git commit -a -m "added delayed_job gem"
git push
heroku rake db:migrate --app myapp
heroku restart --app myapp
I had a Rails 3.0.11 app, which specified rake version 0.8.7 in the Gemfile to get around the version 0.9.2 Rake::DSL problem.
After I converted the app to Rails 3.2.0 (Heroku Cedar stack), I was having a problem with the worker (a rake task) crashing. I changed "gem 'rake', '0.8.7'" to "gem 'rake'", which bundled rake version 0.9.2.2. The worker stopped crashing with the new version.
Your problem is caused by not deleting the Gemfile.lock file and is not specific to Heroku. Deleting Gemfile.lock should fix this problem, but will lead you straight to another one:
To git#heroku.com:tailored-landing-pages.git
* [new branch] master -> master
manfred#painstation2:~/Desktop/projects/ror/ta/tlp307$ heroku rake db:migrate
rake aborted!
ninitialized constant Rake::DSL
/app/Rakefile:13:in `<class:Application>'
/app/Rakefile:12:in `<module:Tlp307>'
/app/Rakefile:11:in `<top (required)>'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2373:in `load'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2373:in `raw_load_rakefile'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2007:in `block in load_rakefile'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2006:in `load_rakefile'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:1991:in `run'
/usr/ruby1.9.2/bin/rake:31:in `<main>'
Unfortunately, I haven't found the solution for that problem yet, since downgrading Rake to 0.8.7 doesn't seem to work here. If somebody else has an answer, I would appreciate it very much.

Resources