Capistrano symlink database.yml task error - ruby-on-rails-3

I'm running a Rails 3.2.x app with Capistrano 2.12.0. I've pulled my database.yml out of github/version control and manually placed a copy under /home/deploy/myapp/shared/config/database.yml
I'm trying to get my deploy.rb to symlink this file using before:assets:precompile so that the file is symlinked in the release directory and the deployment can continue. Here is the error I get:
/Users/james/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/capistrano-2.12.0/lib/capistrano/configuration/callbacks.rb:103:in `on': please specify either a task name or a block to invoke (ArgumentError)
Here is what my deploy.rb looks like:
deploy.rb
require "bundler/capistrano"
set :whenever_command, "bundle exec whenever"
require "whenever/capistrano"
set :shared_children, shared_children + %w{public/uploads}
server "72.14.181.22", :web, :app, :db, primary: true
set :application, "myapp"
set :user, "deploy"
set :deploy_to, "/home/#{user}/#{application}"
#set :deploy_via, :remote_cache
set :use_sudo, false
set :rails_env, "production"
set :scm, "git"
set :repository, "git#github.com:james/#{application}.git"
set :branch, "master"
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
after "deploy", "deploy:cleanup" # keep only the last 5 releases
after "deploy:update"
before "deploy:assets:precompile", "config_symlink"
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
desc "reload the database with seed data"
task :seed do
run "cd #{current_path}; bundle exec rake db:seed RAILS_ENV=#{rails_env}"
end
end
task :config_symlink do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
Basically what I need to do is symlink the /home/deploy/myapp/shared/config/database.yml to /home/deploy/myapp/current/config/database.yml before the assets precompile so the database can be accessed.
I'm sure I have some sort of syntax error here but could really use some help. I want to keep my database.yml out of version control, leave it in the myapp/shared/config directly and symlink it on deploy for security purposes.
For the longest time I thought I had been doing this, but apparently I hadn't so I need to figure this out.
Any help is greatly appreciated as I don't want to have to resort to keeping database.yml in github/version control.

I think I got this working. It looks like in my original deploy.rb I was calling after "deploy:update" but wasn't passing anything to the callback so it was bombing on me. I've cleaned up my deploy.rb file and have tested deployment to a staging server and it seems to work.
deploy.rb updated
require "bundler/capistrano"
set :whenever_command, "bundle exec whenever"
require "whenever/capistrano"
set :shared_children, shared_children + %w{public/uploads}
server "72.14.181.11", :web, :app, :db, primary: true
set :application, "myapp"
set :user, "deploy"
set :deploy_to, "/home/#{user}/#{application}"
#set :deploy_via, :remote_cache
set :use_sudo, false
set :rails_env, "production"
set :scm, "git"
set :repository, "git#github.com:james/#{application}.git"
set :branch, "master"
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
before "deploy:assets:precompile", "config_symlink"
after "deploy", "deploy:cleanup" # keep only the last 5 releases
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
desc "reload the database with seed data"
task :seed do
run "cd #{current_path}; bundle exec rake db:seed RAILS_ENV=#{rails_env}"
end
end
task :config_symlink do
run "ln -nfs #{deploy_to}/shared/config/database.yml #{release_path}/config/database.yml"
end

Related

Hide the list of files when running rspec?

When running rake spec on the command line for a large Rails project, I get a giant list of every rspec file that will be run.
Is there a way to hide that by default?
ruby-1.9.3-p448/bin/ruby -S rspec ./spec/acceptance/replicators/activity_replicator_spec.rb ./spec/acceptance/replicators/template_replicator_spec.rb ./spec/authorization_rules/admin_authorization_rules_spec.rb ...
When I run just rspec (no rake call) I don't get this console output.
EDIT 1
Working from phoet's answer, I tried
RSpec::Core::RakeTask.new(:spec) do |t|
t.verbose = false
t.warning = false
t.rcov = false
end
task :default => :spec
This did not solve the issue.
The last time I did this, I had to clear the rake task first.
if defined? RSpec
task(:spec).clear
RSpec::Core::RakeTask.new(:spec) do |t|
t.verbose = false
end
end
http://singlebrook.com/blog/disable-rspec-verbosity-to-hide-spec-list
You don't get such an output when calling rspec because the output comes from the RSpec::Core::RakeTask.
It's possible to configure this class and set the verbose flag:
RSpec::Core::RakeTask.new do |t|
t.verbose = false
end

ruby on rails action mailer - confirmation email not being sent

I'm working on a ruby on rails project made mostly by someone else, and my rails knowledge is so-so.
The registration process works like this (or used to, but doesn't seem to now, for some reason):
A person registers with name, email, password. (All done with Devise)
The person is sent an email, asking them to click on a link, and when they do, they are then a registered user and can log in to the app with their name and password.
When they do step 1 and click 'Sign Up' a message comes up on the screen, 'Congratulations! Check your email and click the link.'
But no email is being sent. In my App/config/environments/development.rb I have:
QuestionnaireSite::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
# config.action_mailer.delivery_method = :letter_opener
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com',
:user_name => 'questionnaire.dev#gmail.com',
:password => 'qwerty_123',
:authentication => 'plain',
:enable_starttls_auto => true
}
# Print deprecation notices to the Rails logger
config.active_support.deprecation = :log
# Only use best-standards-support built into browsers
config.action_dispatch.best_standards_support = :builtin
# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
config.active_record.auto_explain_threshold_in_seconds = 0.5
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
config.cache_store = :mem_cache_store
end
When I comment out the line:
config.action_mailer.delivery_method = :smtp
and uncomment:
# config.action_mailer.delivery_method = :letter_opener
Everything works as planned (letter_opener is a gem that automates the registration process) so I know there's something going on in this file. As I said, it was working before, and I'm pretty sure those are the only lines I've changed. Is there something else I should be doing?
You need the following in your development.rb file as well as all the other stuff:
config.action_mailer.perform_deliveries = true
Without the above, your app won't actually send the email, and instead will simply display it's contents in your server log... Check the command line output (where rails server is running)
Note: this setting is enabled by default in production, so there's no need to specify it there
First of all, thank you all above for your help and guidance.
#Peter Klipfel - in fact, you were right. I was simply refreshing my app, therefore no error messages with:
config.action_mailer.raise_delivery_errors = false
I had to restart webrick for it to take effect. When that was done I got the error:
Net::SMTPAuthenticationError in Devise::RegistrationsController#create
username and password don't match
As I said, I inherited this project from someone else - questionnaire.dev#gmail probably doesn't exist any more. I changed that part of the code to:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.perform_deliveries = true
config.action_mailer.default :charset => "utf-8"
config.action_mailer.delivery_method = :smtp
# config.action_mailer.delivery_method = :letter_opener
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
# in previous Stackoverflow I read :domain part wasn't needed, so leave it out
# :domain => 'gmail.com',
:user_name => 'my_gmail_address#gmail.com',
:password => 'my_gmail_password',
:authentication => 'plain',
:enable_starttls_auto => true
}
And now everything works as it should (for the moment anyway!)

Error on assets:precompile due to Model in engine gem

My situation is a follows:
I'm running application X
X uses the gem "core"
core extends the models of X with models A, B, C, D
In development this works perfectly. However when I run
(bundle exec) rake RAILS_ENV=staging RAILS_GROUPS=assets assets:precompile
it fails on
** Execute environment
rake aborted!
uninitialized constant A
I tried to fix this issue by placing Rails.application.eager_load! before the Application.initialize! in environments.rb, but I'm afraid that only led to other errors.
Is there a way to include models from an engine in a gem BEFORE the assets:precompile?
I read something about requiring them one by one instead of eager_load all, but the path to the gem differs for every system.
engine.rb in "core":
require 'paperclip'
module Core
class Engine < ::Rails::Engine
config.time_zone = 'Amsterdam'
config.encoding = "utf-8"
config.autoload_paths += %W(#{config.root}/lib/**)
config.generators do |g|
g.test_framework :rspec, :views => false, :fixture => true
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
end
initializer "core.load_app_instance_data" do |app|
Core.setup do |config|
config.app_root = app.root
end
app.class.configure do
#Pull in all the migrations from Commons to the application
config.paths['db/migrate'] += Core::Engine.paths['db/migrate'].existent
end
end
initializer "core.load_static_assets" do |app|
app.middleware.use ::ActionDispatch::Static, "#{root}/public"
end
end
end
I prefer to put any fix in the core gem instead of application X. However if it's not possible X is fine :)
Are you sure the "core" gem is loaded in the staging environment?

Carrierwave setup

please, could you please have a look to my config? I'm trying to setup carrierwave in my app but it doesn't work in the production server. It's working locally even as production environment. The thing is that it's saving the images into the tmp directory but the result is 'Empty file upload result'. I tried save to file and save to S3, same result. I'm using Passenger - Nginx.
I'm getting mad with this for two days now. Any idea or tip even about how I can debug this is welcome. I setup all permissions so I don't think it's a permissions issue. It could be a cache thing, the files are stored into tmp directory but it seems that the app don't see them as owned by the user???
Thanks!!!
#application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
Bundler.require(*Rails.groups(:assets => %w(development test)))
end
module MyApp
class Application < Rails::Application
config.encoding = "utf-8"
config.filter_parameters += [:password]
config.active_support.escape_html_entities_in_json = true
config.active_record.whitelist_attributes = true
config.assets.initialize_on_precompile = false
config.assets.enabled = true
config.assets.version = '1.0'
end
end
# avatar_uploader.rb
class AvatarUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
include ActiveModel::Conversion
extend ActiveModel::Naming
def extension_white_list
%w(jpg jpeg gif png mp3)
end
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process :resize_to_fill => [200,200]
end
end
# carrierwave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => 'ACCESS', # required
:aws_secret_access_key => 'SECRET', # required
:region => 'eu-west-1' # optional, defaults to 'us-east-1'
}
config.fog_directory = 'my_app_bucket'
end
# production.rb
MyApp::Application.configure do
config.cache_classes = true
config.consider_all_requests_local = true # default false, debug true
config.action_controller.perform_caching = true #default true
config.serve_static_assets = true # Carrierwave true - Default false
config.assets.compress = true
config.assets.compile = false # false for real production
config.assets.digest = true
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
config.log_level = :debug
config.cache_store = :dalli_store
config.assets.precompile += %w( search.js )
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.action_mailer.default_url_options = { :host => 'localhost' }
end
Solved!
My ImageMagick install was missing delegates. Now:
convert -list configure
DELEGATES bzlib fontconfig freetype jpeg jng lqr pango png x11 xml zlib

1st deploy - need to install all gems on the server

I'm trying to deploy for the 1 time to the server ( cap deploy:cold ) and it keep asking me to install gems. Just one example:
*** [err :: ip-address] Could not find net-ssh-2.3.0 in any of the sources
Is there something I could to to install all the gems needed in one shot?
Here is my deploy.rb file, and gemfile:
deploy.rb
set :application, "myapp"
set :repository, "repo goes here"
set :scm, :git
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
role :web, "ipaddress" # Your HTTP server, Apache/etc
role :app, "ipaddress" # This may be the same as your `Web` server
role :db, "ipaddress", :primary => true # This is where Rails migrations will run
# role :db, "your slave db-server here"
# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts
set :deploy_to, "/home/deploy/#{application}"
set :rails_env, 'production'
set :branch, "master"
set :scm, :git
set :user, "user"
set :runner, "user"
# ssh_options[:port] = 2232
set :use_sudo, false
set :normalize_asset_timestamps, false
# If you are using Passenger mod_rails uncomment this:
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
end
namespace :bundler do
task :create_symlink, :roles => :app do
shared_dir = File.join(shared_path, 'bundle')
release_dir = File.join(current_release, '.bundle')
run("mkdir -p #{shared_dir} && ln -s #{shared_dir} #{release_dir}")
end
task :bundle_new_release, :roles => :app do
bundler.create_symlink
run "cd #{release_path} && bundle install --without test"
end
task :lock, :roles => :app do
run "cd #{current_release} && bundle lock;"
end
task :unlock, :roles => :app do
run "cd #{current_release} && bundle unlock;"
end
end
after "deploy", "deploy:cleanup"
namespace :deploy do
desc "Restarting mod_rails with restart.txt"
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch #{current_path}/tmp/restart.txt"
end
[:start, :stop].each do |t|
desc "#{t} task is a no-op with mod_rails"
task t, :roles => :domain do ; end
end
end
task :after_update_code do
run "ln -nfs #{deploy_to}/shared/config/database.yml #{release_path}/config/database.yml"
end
gemfile:
source 'http://rubygems.org'
gem 'rails', '3.1.3'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'json'
gem 'rake', '0.9.2.2'
gem 'mysql2'
gem 'capistrano'
gem 'therubyracer'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.1.5'
gem 'coffee-rails', '~> 3.1.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# Use unicorn as the web server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'ruby-debug'
maybe if you change this line
run "cd #{release_path} && bundle install --without test"
to
run "cd #{release_path} && bundle install --path=you_gem_path --without #{bundle_without.join(' ')}"
you can find your gem path by running
$ echo $GEM_PATH
you also have to set bundle_without
you can set it in the beginning of file
set :bundle_without, [:development, :test]
try adding this to your deploy.rb
set :bundle_without, [:development, :test]
require 'bundler/capistrano'