Error (upload image) after deploy using capistrano rails 4 - ruby-on-rails-4

Yesterday i tried to deploy my rails app to my own server. For deploy, i have followed gorails tutorial -> https://gorails.com/deploy/ubuntu/14.04. After deployed, finally my rails app has running live. But i have a problem when i try to create room module with image, i get error like this
We're sorry, but something went wrong.
If i create room without image, it success. I'm sure that i can't upload image.
gem 'paperclip'
room.rb
has_attached_file :image, :styles => { :for_index => "336x227>", :thumb => "150x150>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
deploy.rb
lock '3.1.0'
set :application, 'hotelxxxxx'
set :repo_url, 'git#bitbucket.org:xxxxxx/hotelxxxxx.git'
set :deploy_to, '/home/xxxxxx/hotelxxxxx'
set :linked_files, %w{config/database.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
execute :touch, release_path.join('tmp/restart.txt')
end
end
after :publishing, 'deploy:restart'
after :finishing, 'deploy:cleanup'
end
How to fix that?

The first step to troubleshoot this issue would be to look at the Rails log on your server. It should be in the log/production.log file. Having the actual error message in the log would make it a lot easier to help you figure this out.

Finally image uploaded !! I see error from nginx error.log => 'Paperclip::Errors::NotIdentifiedByImageMagick'. After that i try to re-install imagemagick from https://www.enovate.co.uk/web-design-blog/2015/02/25/how-to-install-imagemagick-from-source-on-ubuntu-14.04/
Imagemagick re-install succeed and open config/application.rb in my rails app
config/application.rb
# add line
Paperclip.options[:command_path] = "/usr/local/bin/identify"
Deploy again

Related

Capybara::Poltergeist Status error, failed to reach server

I am trying to make some tests with Rspec/Capybara/Poltergeist on my Rails 4 application, Phantomjs is installed (version 2.2.1), but I always get this error :
Failure/Error: visit(perfect_landing_page_path)
Capybara::Poltergeist::StatusFailError:
Request to 'http://127.0.0.1:49623/path' failed to reach server, check DNS
and/or server status
The test i'm working on :
require 'rails_helper'
RSpec.feature 'Subscription', :type => :feature do
let!(:plan) { create(:plan) }
let!(:landing_page) { create(:landing_page) }
before(:each) { landing_page.default_plan = plan }
describe 'landing_page#perfect_show' do
scenario 'form display', js: true do
plan_2 = create(:plan)
plan_3 = create(:plan)
landing_page.plans << plan_2
landing_page.plans << plan_3
visit(perfect_landing_page_path)
expect(page).to have_css(".start-now", count: 3)
first(".start-now").click
expect(page).to have_css("#new_user")
end
end
end
My Gemfile looks like this :
gem 'rspec-rails', '~> 3.0'
gem 'factory_girl_rails'
gem 'guard-rspec'
group :test do
gem 'database_cleaner'
gem 'capybara'
gem 'capybara-screenshot'
gem 'poltergeist'
gem 'selenium-webdriver'
gem 'shoulda-matchers', require: false
gem 'show_me_the_cookies'
end
My spec/support/capybara.rb file :
require 'capybara/rails'
require 'capybara/rspec'
require 'capybara-screenshot/rspec'
require "capybara/poltergeist" # Add this line to require poltergeist
require 'selenium-webdriver'
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {:js_errors => false, :default_max_wait_time => 30, :timeout => 30, phantomjs_options: [
'--load-images=no',
'--ignore-ssl-errors=true',
'--ssl-protocol=any']})
end
Capybara.register_driver :poltergeist_debug do |app|
Capybara::Poltergeist::Driver.new(app, :inspector => true)
end
Capybara.configure do |config|
config.javascript_driver = :poltergeist
config.ignore_hidden_elements = true
config.default_max_wait_time = 30
end
My spec_helper.rb file :
require 'capybara/rspec'
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.filter_run :focus
config.run_all_when_everything_filtered = true
Did someone encounter this problem before ? Does anyone have a solution for this ? I have been banging my head for days looking on the web...
Thank you very much.
PS : it works fine on my friend's mac (Yosemite or El Capitan), just not on mine.
I was having this problem in CI (CodeShip).
Adding a call to bundle exec rake assets:precompile at the end of the setup commands seemed to fix it:
CodeShip CI Setup Commands:
rvm use 2.3.4 --install
bundle install
export RAILS_ENV=test
bundle exec rake db:schema:load
bundle exec rake assets:precompile
Also had a whitelist per #agbodike but in the rails_helper.rb
config.before(:each, js: true) do
page.driver.browser.url_whitelist = ["127.0.0.1"]
end
I had the same issue and it was due to a 3rd party script timing out. You can prevent loading of 3rd party scripts with a blacklist. For example:
config.before(:each, js: true) do
page.driver.browser.url_blacklist = ["http://use.typekit.net"]
end
would prevent any URL starting with http://use.typekit.net from being called for each test that uses the js driver. More information can be found at:
https://robots.thoughtbot.com/speed-up-javascript-capybara-specs-by-blacklisting-urls
Alternatively you can use a whitelist:
config.before(:each, js: true) do
page.driver.browser.url_whitelist = ["127.0.0.1"]
end
which will block all requests not to 127.0.0.1
I placed the configuration in spec/feature_helper.rb to ensure it was only set for feature specs.
You can avoid these by precompile the assets before tests.
And the code is:
RSpec.configure do |config|
config.before :all do
ENV['PRECOMPILE_ASSETS'] ||= begin
case self.class.metadata[:type]
when :feature, :view
STDOUT.write "Precompiling assets..."
require 'rake'
Rails.application.load_tasks
Rake::Task['assets:precompile'].invoke
STDOUT.puts " done."
Time.now.to_s
end
end
end
end
more info

Capistrano 3.4 Deployment Problems

I have inherited a Rails project and I am unable to deploy it using the supplied instructions using Capistrano. I am not familiar with capistrano at all.
Whatever I try I always get the following error message
(Backtrace restricted to imported tasks)
cap aborted!
ArgumentError: wrong number of arguments (1 for 2)
Tasks: TOP => production
(See full trace by running task with --trace)
This includes
$ bundle exec cap -T
$ bundle exec cap production deploy
I am following the guide here https://github.com/capistrano/capistrano
I have starred out important info but here is the a copy of the config/deploy/staging.rb
role :app, %w{deploy#**.**.**.**:****}
role :web, %w{deploy#**.**.**.**:****}
role :db, %w{deploy#**.**.**.**:****}
set :branch, 'develop'
set :deploy_to, '/var/www/soreen.staged.ws'
set :ssh_options, {
keepalive: true,
keepalive_interval: 60 #seconds
}
and here is Capfile
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
require 'capistrano/rbenv'
require 'capistrano/bundler'
require 'capistrano/rails/migrations'
require 'capistrano/rails/assets'
require 'capistrano/rails/collection'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
and here is config/deploy.rb
# config valid only for current version of Capistrano
lock '3.4.0'
set :application, 'Soreen'
set :repo_url => "****************"
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')
set :rbenv_type, :user # or :system, depends on your rbenv setup
set :rbenv_ruby, '2.1.2'
set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=# {fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"
set :rbenv_map_bins, %w{rake gem bundle ruby rails}
set :rbenv_roles, :all # default value
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
# Your restart mechanism here, for example:
execute :mkdir, '-p', "#{ release_path }/tmp"
execute :touch, release_path.join('tmp/restart.txt')
end
end
after :publishing, :restart
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
end
end
end
Please help !
You can get a backtrace for the error using the following:
bundle exec cap --backtrace -T
That will give you more information. Likely this is a syntax error in your configuration.

No such file or directory - connect(2) for "/tmp/puma-status-1439451994589-14316"

I want to deploy my rails through Capistrano with Puma and Nginx. I have configured deploy.rb for puma and added required gems in gem files.
I am able to run initial deploy command as 'cap production deploy:initial' and able to access my rails app as described below.
But when I want to deploy some new changes or restart puma it fails and gave this error.
Gemfile:
gem 'capistrano', '~> 3.4.0'
gem 'capistrano-rvm', require: false
gem 'capistrano-rails', require: false
gem 'capistrano-bundler', require: false
gem 'capistrano3-puma', require: false
# gem 'capistrano-passenger', require: false
gem 'capistrano-ext', require: false
gem 'capistrano-faster-assets', '~> 1.0.2'
Capfile:
# Load DSL and set up stages
require 'capistrano/setup'
# Include default deployment tasks
require 'capistrano/deploy'
require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/faster_assets'
require 'capistrano/rvm'
require 'capistrano/puma'
require 'capistrano/puma/workers'
require 'capistrano/puma/nginx'
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
deploy.rb
# Puma Server Configuration
set :puma_threads, [4, 16]
set :puma_workers, 1
# Don't change these unless you know what you're doing
set :pty, true
set :use_sudo, false
set :puma_bind, "unix://#{shared_path}/tmp/sockets/puma.sock"
# set :puma_conf, "#{shared_path}/puma.rb"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
end
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/capistrano`
puts "WARNING: HEAD is not the same as origin/capistrano"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'puma:restart'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
I have also used the below command to generate template for Puma and Nginx as below.
rails g capistrano:nginx_puma:config
I have run below commands to deploy my rails to EC2 instance (with Ubuntu)
cap production deploy:check
cap production puma:config
cap production puma:nginx_config
cap production deploy:initial
Now, I want to deploy some changes with below code.
cap production deploy
But I am getting the error as below.
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as ubuntu#54.175.134.149: bundle exit status: 1
bundle stdout: No such file or directory - connect(2) for "/tmp/puma-status-1439451994589-14316"
bundle stderr: Nothing written
SSHKit::Command::Failed: bundle exit status: 1
bundle stdout: No such file or directory - connect(2) for "/tmp/puma-status-1439451994589-14316"
bundle stderr: Nothing written
Tasks: TOP => deploy:restart
(See full trace by running task with --trace)
Please help!
Thanks
I found one workaround to fix this issue.
Just add the snippet below to your deploy.rb file.
It will override puma restart task.
Rake::Task["puma:restart"].clear_actions
namespace :puma do
task :restart do
on roles(:all) do
execute "RACK_ENV=#{fetch(:rails_env)} #{fetch(:rvm_binary)} #{fetch(:rvm_ruby_version)} do pumactl -S #{shared_path}/tmp/pids/puma.state restart"
end
end
end

Rails, ember-cli-rails gem

I tried to deploy a Rails & Ember-cli application to a VPS by Capistrano. I have integrated Ember-cli into Rails by using ember-cli-rails gem. The problem happens when Capistrano wants to do assets:precompile. Throwing an error like EmberCLI Rails requires your Ember app to have an addon. In this case, I am able to run assets:precompile on my local machine and on the server in the released directory without any problem. On server I have installed `Nodejs and NPM.
deploy.rb
lock '3.1.0'
set :application, 'APPLICATION NAME'
set :repo_url, 'GIT ADDRESS'
set :deploy_to, 'SERVER SIDE ADDRESS'
set :linked_files, %w{config/database.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
end
end
after :publishing, 'deploy:restart'
after :finishing, 'deploy:cleanup'
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
end
end
end
Capfile
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/bundler'
require 'capistrano/rails'
require 'capistrano/rvm'
set :rvm_type, :user
set :rvm_ruby_version, '2.2.0'
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
Related Gems
gem 'rails', '4.2.0'
gem 'capistrano', '~> 3.1.0'
gem 'capistrano-bundler', '~> 1.1.2'
gem 'capistrano-rails', '~> 1.1.1'
gem 'capistrano-rvm', github: "capistrano/rvm"
any help would be appreciated.
I just fixed this problem by adding some tasks to my deploy.rb file.
desc "Build Ember addon"
task :build_ember_addon do
on roles(:app) do
execute "cd #{release_path}/frontend && /home/$USER/.nvm/versions/node/v0.12.2/bin/npm install --save-dev ember-cli-rails-addon#0.0.11"
end
end
desc "Intall npm"
task :npm_install do
on roles(:app) do
execute "cd #{release_path}/frontend && /home/$USER/.nvm/versions/node/v0.12.2/bin/npm install"
end
end
desc "bower install"
task :bower_install do
on roles(:app) do
execute "cd #{release_path}/frontend && /home/$USER/.nvm/versions/node/v0.12.2/bin/bower install --config.interactive=false"
end
end
before 'deploy:updated', 'deploy:build_ember_addon'
before 'deploy:updated', 'deploy:npm_install'
before 'deploy:updated', 'deploy:bower_install'

Rails 4, capistrano 3, delayed_job - can't find bin/delayed_job on one of the servers

When i deploy to pre server everything's woking fine. But if i try to deploy to staging server i get this error:
bundler: command not found: bin/delayed_job
file - config/deploy/staging.rb
set :rails_env, 'staging'
set :eager_load, :true
set :unicorn_rack_env, 'staging'
role :app, %w{deploy#x.x.x.x}
role :web, %w{deploy#x.x.x.x}
role :db, %w{deploy#x.x.x.x}
set :rvm_type, :auto # Defaults to: :auto
set :rvm_ruby_version, '2.1.2'
set :rails_env, 'staging'
set :eager_load, :true
role :app, %w{deploy#x.x.x.}
role :web, %w{deploy#x.x.x.}
role :db, %w{deploy#x.x.x.}
set :rvm_type, :auto # Defaults to: :auto
set :rvm_ruby_version, '2.1.2'
set :deploy_to, '/var/www/app'
server 'x.x.x', user: 'deploy', roles: %w{web app} , port: 222
set :unicorn_pid, ->{ "#{deploy_to}/shared/tmp/pids/unicorn.pid" }
set :scm, :git
set :ssh_options, { user: 'superman' }
set :keep_releases, 5
restart of delayed jobs -
namespace :delayed_job do
def args
fetch(:delayed_job_args, "")
end
def delayed_job_roles
fetch(:delayed_job_server_role, :app)
end
desc 'Stop the delayed_job process'
task :stop do
on roles(delayed_job_roles) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :bundle, :exec, :'bin/delayed_job', :stop
end
end
end
end
desc 'Start the delayed_job process'
task :start do
on roles(delayed_job_roles) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :bundle, :exec, :'bin/delayed_job', args, :start
end
end
end
end
desc 'Restart the delayed_job process'
task :restart do
on roles(delayed_job_roles) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :bundle, :exec, :'bin/delayed_job', args, :restart
end
end
end
end
end
Could you please give me a hand in this issue ? Thanks in advance!
UPDATE 1
More info on this error :
lib/capistrano/tasks/delayed_job.rake:33 which is execute :bundle, :exec, :'bin/delayed_job', args, :restart
You should run once on destination server:
bundle exec rails generate delayed_job
This will generate delayed_job executable script in shared/bin
Check if you can see delayed_job on your staging server in #{deploy_to}/shared/bin/
If it's not there, copy it there from your project's bin folder.
In addition to operations in answer by Rider. Following will perform 'bundle exec rails generate delayed_job' on remote if bin/delayed_job is missing there. Put it in config/deploy.rb file.
namespace :delayed_job do
desc "Install Deployed Job executable if needed"
task :install do
on roles(delayed_job_roles) do |host|
within release_path do
# Only install if not already present
unless test("[ -f #{release_path}/#{delayed_job_bin} ]")
with rails_env: fetch(:rails_env) do
execute :bundle, :exec, :rails, :generate, :delayed_job
end
end
end
end
end
before :start, :install
before :restart, :install
end
This is happening because the delayed_job executable is missing on the server. It should exist in the bin directory. To fix this you should generate the executable on your development machine and check it in to source control.
This command will generate the executable for you:
bundle exec rails generate delayed_job
Its better to use Capistrano::DelayedJob Gem
Delayed Job support for Capistrano 3.x
Add this line to your application's Gemfile:
gem 'capistrano3-delayed-job', '~> 1.0'
Require in Capfile to use the default task:
require 'capistrano/delayed-job'
Add this to your deploy.rb file
after 'deploy:published', 'restart' do
invoke 'delayed_job:restart'
end
For more configurable options https://github.com/platanus/capistrano3-delayed-job
Please check if you setup your rails_env system variable correctly.