Ruby on Rails app 500 Internal Server error - ruby-on-rails

I can get the index.html and a test page to come up in the browser from the public folder, but the exact/path in the controller is not coming up. The browser continues to search for the page until I stop it. It works in my test environment and it works in Heroku, but doesn't not work on my amazon ec2 server. I am using Ubuntu 11.10. Why would this be? Thanks in advance.
Update: It looks like I am getting a 500 Internal Server error and the controller is not seen.

It turns out I needed to prcompile the assets by doing one of the folling
Adding this to my deploy.rb file
after "deploy:restart", "deploy:precompile"
namespace :deploy do
desc "Compile assets"
task :precompile, :roles => :app do
run "cd #{release_path} && rake RAILS_ENV=#{rails_env} assets:precompile"
end
end
Running this in the current directory
RAILS_ENV=production rake assets:precompile
Or adding this to the capfile
load 'deploy/assets'

Related

Capistrano v3 task fails to start unicorn server with error "eval: bundle not found"

I'm using Capistrano v3 to deploy a rails 4 app to a VPS using unicorn with nginx.
Following the capistrano most recent official documentation, I managed to set up everything related to the deployment itself:
I use the gems 'capistrano', 'capistrano-bundler', 'capistrano-rails' and 'capistrano-rvm' and when I do cap production deploy everything seems to work without any error message (the repository is pulled from github and copied on the server, assets are precompiled and so on).
At this point if I connect to the sever via ssh and type /etc/init.d/unicorn start the server starts as expected, serving my rails app.
However, I created a task to automate this with capistrano v3 that looks like:
namespace :unicorn do
desc 'Start Unicorn'
task :start do
on roles(:app) do
within current_path do
execute "/etc/init.d/unicorn start"
end
end
end
desc 'Stop Unicorn'
task :stop do
on roles(:app) do
within current_path do
execute "/etc/init.d/unicorn stop"
end
end
end
end
But whenever I try capistrano deploy unicorn:start I get the following error:
/etc/init.d/unicorn: 1: eval: bundle: not found
cap aborted!
/etc/init.d/unicorn start stdout: Nothing written
/etc/init.d/unicorn start stderr: Nothing written
What's even stranger is that when I start unicorn manually and then do cap production unicorn:stop it works seamlessly.
I suspected some differences in available environment variables when logging in via ssh so I configured 'rvm_bin_path', 'path' and 'gem_path' to be the same as on server but I still get the same error.
I'm running out of ideas, anyone knows what could cause this?
Cheers.
When rvm is used on the deploy server rvm1-capistrano3 saves you. You can use this template as how-to

Capistrano: trying to run rake db:seed on remote server

I'm trying to run my seed file on a remote server using capistrano. My deploy is OK, so there is no issue there. Here is the code for running the seed file in config/deploy.rb
namespace :seed do
desc "Run a task on a remote server."
# run like: cap staging rake:invoke task=a_certain_task
task :default do
run("cd #{deploy_to}/current; /usr/bin/env bundle exec rake #{ENV['db:seed']} RAILS_ENV=#{rails_env}")
end
end
I am evoking this task by running 'cap seed'.
Whats weird is it looks like tests are running when I run this..HERE is a snippet.
Maybe the problem is with #{ENV['db:seed']} part. Isn't it should be just db:seed. The eniviroment variable db:seed doesn't exist so You are calling a pure rake command.
Try this:
run("cd #{deploy_to}/current; /usr/bin/env bundle exec rake db:seed RAILS_ENV=#{rails_env}")

Rails deployment - how do you do rake db:reset with capistrano?

I am using Linode with Ubuntu 10.04 and Capistrano, Unicorn, & Nginx to deploy.
How do I do the equivalent of heroku run rake db:reset with this setup? Is it as simple as cap deploy:cold again to run the migrations?
I've already deployed and want to drop all databases and rerun all the migrations but am not sure which commands to run with this setup to do so.
I wrote a tiny little file you can copy to run arbitrary rake tasks via capistrano: http://jessewolgamott.com/blog/2012/09/10/the-one-where-you-run-rake-commands-with-capistrano/
once setup, you can:
cap sake:invoke task="db:reset"
For Capistrano 3 without actual dropping the database. Use bundle exec cap db:reset
namespace :db do
desc 'Resets DB without create/drop'
task :reset do
on primary :db do
within release_path do
with rails_env: fetch(:stage) do
execute :rake, 'db:schema:load'
execute :rake, 'db:seed'
end
end
end
end
end
You could add the following to your deploy.rb file
namespace :custom do
task :task do
run "cd #{current_path} && bundle exec rake db:reset RAILS_ENV=#{rails_env}"
end
end
Then run cap custom:task to clear the database.
If you are using Capistrano 3, consider using the capistrano-rails-collection.
You can also use copy the code directly from db.rake file from the repository.
Or, if you want a full-fledged solution to run all your rake tasks on a remote server, check out the Cape gem.

undefined method `[]' for nil:NilClass during assets:precompile rake while deploying with capistrano

I've got capistrano configured so that assets are compiled on production when i run deploy via capistrano. Im using Ruby 1.9.3, Rails 3.2.13 with MongoDb database. Problem exists only during deployment, on both developement and production i can compile assets manually without any problem
desc "Update the deployed code."
task :update_code, :except => { :no_release => true } do
run "cd #{current_path}; git fetch origin; git reset --hard #{branch}"
finalize_update
assets.precompile
end
namespace :assets do
desc "Assets precompileing"
task :precompile, :roles => :web, :except => { :no_release => true } do
run "cd #{current_path}; rm -rf public/assets/*"
run "cd #{current_path}; bundle exec rake RAILS_ENV=#{rails_env} assets:precompile"
end
end
but i'm getting this error
executing command
/usr/local/rvm/rubies/ruby-1.9.2-p290/bin/ruby /my-app/path/shared/bundle/ruby/1.9.1/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
rake aborted!
undefined method `[]' for nil:NilClass
/my-app/path/shared/bundle/ruby/1.9.1/gems/moped-1.4.5/lib/moped/protocol/reply.rb:71:in `command_failure?'
/my-app/path/shared/bundle/ruby/1.9.1/gems/moped-1.4.5/lib/moped/node.rb:82:in `block in command'
/my-app/path/shared/bundle/ruby/1.9.1/gems/moped-1.4.5/lib/moped/node.rb:582:in `[]'
/my-app/path/shared/bundle/ruby/1.9.1/gems/moped-1.4.5/lib/moped/node.rb:582:in `block (3 levels) in flush'
/my-app/path/shared/bundle/ruby/1.9.1/gems/moped-1.4.5/lib/moped/node.rb:581:in `map'
...
Tasks: TOP => assets:precompile
(See full trace by running task with --trace)
command finished in 14427ms
failed: "env RAILS_ENV=production sh -c 'cd /my-app/path/current; bundle exec rake RAILS_ENV=production assets:precompile'"
zlib(finalizer): the stream was freed prematurely.
So it's not related to any special file in assets, i do use only pure .js and .css files.
Thought there might be some problem with multiple manifest files which i am using, but i added
config.assets.precompile += ['another-manifest.js', 'another-manifest.css']
to application.rb and it works like charm on development and on production, if i turn off asset compilation during deploy and do it manually after deployment is finished.
I also tried leaving only one manifest file that required only self and the error was still present.
Mongoid documentation got similiar looking problem discussed in tips section
http://mongoid.org/en/mongoid/docs/tips.html
But im currently using 1.9.3 Ruby build p392 so it should be fine.
Any help on this would be great!
If you have any asset-url helpers like image-url and etc., your style files must be .scss.
Using scss helpers in pure css rises sprockets error during precompiling and ruins your deploy.

Capistrano deployment changing directory for migrations

I have a rails app that is not in the root directory of the repository. When it is deployed, some other static files are deployed with it in a parent directory. The structure is something like this:
root
-- otherstuff
-- railsapp
When I do a deployment with cap deploy:migrations, the Capistrano command that gets executed looks like this, which of course doesn't work:
cd /u/apps/minicart/releases/20100717215044; rake RAILS_ENV=staging db:migrate
How do I change this so that it will be:
cd /u/apps/minicart/releases/20100717215044/railsapp; rake RAILS_ENV=staging db:migrate
I made it work by adding a task that executes this command after deploy:finalize_update, but I would prefer to use the built in method, plus my hacked version is executed with every deployment.
Any advice would be appreciated.
Tim
This turned out to be very simple.
I added a deploy namepace to my deploy.rb file and then redefined the migrate method. Now my method runs on cap deploy:migrations.
namespace :deploy do
desc "Migrating the database"
task :migrate, :roles => :app do
run <<-CMD
cd #{release_path}/minicart; RAILS_ENV=#{stage} rake db:migrate
CMD
end
end

Resources