Capistrano deploy removes table - ruby-on-rails

I use capistrano to deploy my Rails app to my VPS, but after every deploy when I visit my page I get an error. The log says:
I, [2014-11-04T08:20:16.659289 #12482] INFO -- : Started GET "/" for 82.73.170.71 at 2014-11-04 08:20:16 -0500
I, [2014-11-04T08:20:16.662717 #12482] INFO -- : Processing by HomeController#index as HTML
I, [2014-11-04T08:20:16.665979 #12482] INFO -- : Completed 500 Internal Server Error in 3ms
F, [2014-11-04T08:20:16.670152 #12482] FATAL -- :
ActiveRecord::StatementInvalid (Could not find table 'users'):
app/controllers/application_controller.rb:18:in `current_user'
app/helpers/sessions_helper.rb:26:in `logged_in?'
app/controllers/home_controller.rb:4:in `index'
I have to ssh into my VPS and go to my Rails root and run RAILS_ENV=production bundle exec rake db:migrate . In my db folder I do still have the production.sqlite3 file, but it's empty.
My deploy.rb
# config valid only for Capistrano 3.1
lock '3.1.0'
set :application, 'movieseat'
set :repo_url, 'git#github.com:alucardu/movieseat.git'
set :deploy_to, '/home/deploy/movieseat'
set :linked_files, %w{config/database.yml config/secrets.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
require 'capistrano-rbenv'
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
So why is Capistrano removing my database when I deploy?

Capistrano does not touch database migrations unless it is specified by deploy:migrate task within your Capfile or by calling bundle exec cap deploy:migrate.
Your database 'disappears' because SQLite is simply a file in your db directory. Since you do not specify it should be shared among releases (to be within shared directory) then it just disappears and stays in previous release. Add db/production.sqlite3 to your linked_files declaration.

Related

(SSHKit::Runner::ExecuteError) rake stdout: rake aborted!\r ActiveSupport::MessageEncryptor::InvalidMessage:

deploy.rb
set :linked_files, ['config/database.yml', 'config/credentials.yml.enc', 'config/master.key', 'config/storage.yml']
set :linked_dirs, %w[log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system]
I am trying to deploy the rails application with Capistrano, getting the following error while bundle exec rake assets: precompile
(SSHKit::Command::Failed)
rake stdout: rake aborted!\r
ActiveSupport::MessageEncryptor::InvalidMessage: ActiveSupport::MessageEncryptor::InvalidMessage\r
deployment is failed and the current folder is not being created on the server. I have installed node and yarn to the server

Capistrano 3 deployment broke for some reason

Okay,
I'm running into some weird issue with our deployment no longer working anymore. I cannot track everything that changed, but I'm hoping someone can help me out with debugging this issue and find out what is going wrong.
deploy.rb
# config valid only for current version of Capistrano
lock '3.4.0'
set :application, 'app_name'
set :repo_url, 'github_url'
set :deploy_to, '/home/user/path'
# Source Control Configuration
set :scm, :git
# Produces: Please enter the name of the branch. (develop):
set(:branch, ask('the name of the branch.', 'develop'))
# Ask whether we want to perform a backup.
# Produces: Please enter whether to perform a backup of the database? (Y):
set(:backup, ask('whether to perform a backup of the database?', 'Y'))
puts("setting branch to: #{fetch(:branch)}")
set :user, 'my_user'
set :assets_roles, [:web, :app, :vm]
set :keep_assets, 2
set :linked_files, fetch(:linked_files, []).push('.chamber.pem', 'Procfile', '.env')
set :linked_dirs, fetch(:linked_dirs, []).push('data', 'tmp', 'log', 'uploads')
set :keep_releases, 10
set :bundle_path, nil
set :bundle_binstubs, nil
set :bundle_flags, '--system'
set :rvm1_map_bins, fetch(:rvm1_map_bins, []).push('honeybadger')
# Slack integration options
set :slack_via_slackbot, Settings.slack.via_slackbot
set :slack_team, Settings.slack.team
set :slack_token, Settings.slack.token
set :slack_icon_emoji, -> { Settings.slack.icon_emoji }
set :slack_channel, -> { Settings.slack.channel }
set :slack_username, -> { Settings.slack.username }
set :slack_run_starting, -> { Settings.slack.run_starting }
set :slack_run_finished, -> { Settings.slack.run_finished }
set :slack_run_failed, -> { Settings.slack.run_failed }
set :slack_msg_finished, -> { "#channel #{ENV['USER'] || ENV['USERNAME']} has deployed branch *#{fetch :branch}* of #{fetch :application} to *#{fetch :rails_env}*." }
set :slack_msg_failed, -> { "#{ENV['USER'] || ENV['USERNAME']} failed to deploy branch #{fetch :branch} of #{fetch :application} to #{fetch :rails_env, 'production'}." }
# Rake config
SSHKit.config.command_map[:rake] = 'bundle exec rake'
before 'deploy:started', 'deploy:rm:stop'
after 'deploy:rm:stop', 'deploy:rm:backup'
after 'deploy:finished', 'deploy:rm:start'
after 'deploy:migrate', 'deploy:rm:seed
Capfile
# Load DSL and set up stages
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'rvm1/capistrano3' # Do not use capistrano/bundler
require 'whenever/capistrano'
require 'capistrano/honeybadger'
require 'slackistrano/capistrano'
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
require 'net/ssh/proxy/command'
def vm(tunnel:, tunnel_user: fetch(:user), vm_user: fetch(:user))
server "#{fetch(:stage)}-vm01", {
roles: [:vm],
# The user on the VM
user: vm_user,
ssh_options: {
# The user on the machine we're tunneling through
proxy: Net::SSH::Proxy::Command.new("ssh #{tunnel_user}##{tunnel} -W %h:%p"),
}
}
end
So I can run the cap command locally, and it does part of the work. Had some issues first with the new sprockets, but now it's complaining about the following when it needs to precompile the assets:
INFO [21eca199] Running bundle exec rake assets:precompile as riskmethods#playground.riskmethods.net DEBUG [21eca199] Command: cd /home/riskmethods/riskmethods/releases/20160212152030 && ( export RAILS_ENV="playground" ; bundle exec rake assets:precompile )
DEBUG [2eac7a5b] Finished in 0.078 seconds with exit status 0 (successful).
INFO [4b29f0c4] Running bundle exec rake assets:precompile as riskmethods#playground-vm01
DEBUG [4b29f0c4] Command: cd /home/riskmethods/riskmethods/releases/20160212152030 && ( export RAILS_ENV="playground" ; bundle exec rake assets:precompile )
DEBUG [21eca199] bash: bundle: command not found
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as riskmethods#playground.riskmethods.net: rake exit status: 127
rake stdout: Nothing written
rake stderr: bash: bundle: command not found
SSHKit::Command::Failed:
rake exit status: 127
rake stdout: Nothing written
rake stderr: bash: bundle: command not found
Tasks: TOP => deploy:assets:precompile
(See full trace by running task with --trace)
The deploy has failed with an error:
Exception while executing as riskmethods#playground.riskmethods.net:
rake exit status: 127
rake stdout: Nothing written
rake stderr: bash: bundle: command not found
DEBUG [4b29f0c4] bash: bundle: command not found
As your error message saying bash: bundle: command not found
It seems you dont have the gem bundler installed on the server
install bundler gem on server by running the following command that should do the trick
gem install bundler

cap v3 rails 4 passenger deploy Web application could not be started because Gemfile not found

When we deploy to our server, everything deploys fine, however we get the error "Gemfile not found" right after. At first I thought this was nginx not starting, but if I restart the box, the error goes away and the application works perfectly. We are trying to determine why this is occurring and how to fix it. As of right now, I am not sure where to begin and nothing I seem to research on the "google" has turned up answers.
The breakdown of the server setup and deployment:
rails 4
rbenv
ruby 2.1.0
capistrano v3
passenger
server build with chef
The Capfile:
require 'pry'
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
# Includes tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
# https://github.com/capistrano/rvm
# https://github.com/capistrano/rbenv
# https://github.com/capistrano/chruby
# https://github.com/capistrano/bundler
# https://github.com/capistrano/rails/tree/master/assets
# https://github.com/capistrano/rails/tree/master/migrations
#
# require 'capistrano/rvm'
require 'capistrano/rbenv'
# require 'capistrano/chruby'
require 'capistrano/bundler'
# require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
The deploy.rb:
set :application, 'api'
set :scm, :git
set :repo_url, 'git#github.com:PlacewiseMedia/API.git'
set :branch, 'develop'
set :deploy_to, '/home/apps/api'
set :deploy_via, :remote_cache
set :keep_releases, 10
set :user, 'deploy'
set :use_sudo, false
set :rbenv_type, :system
set :rbenv_ruby, '2.1.0'
set :rbenv_path, '/opt/rbenv'
namespace :deploy do
desc 'Restart application'
task :restart_application do
on roles(:app), in: :sequence, wait: 5 do
spacer("Setting up restart file")
execute "mkdir -p #{release_path}/tmp ; touch #{release_path}/tmp/restart.txt"
spacer("Restarting the nginx service")
execute "sudo service nginx restart"
spacer()
end
end
desc 'Run Migrations'
task :update_database do
on roles(:app), in: :sequence, wait: 5 do
within(release_path) do
with rails_env: fetch(:rails_env) do
spacer("Updating the database")
execute :rake, "db:migrate", "--trace"
spacer()
end
end
end
end
desc 'Create application symlinks'
task :shared_links do
on roles(:app), in: :sequence, wait: 5 do
spacer("Creating application symlinks")
execute "rm #{release_path}/config/database.yml"
execute "ln -s #{shared_path}/config/database.yml #{release_path}/config/database.yml"
execute "ln -s #{shared_path}/config/secrets.yml #{release_path}/config/secrets.yml"
execute "ln -s #{shared_path}/bin/passenger #{release_path}/bin/passenger"
spacer()
end
end
after 'deploy:updated', 'deploy:shared_links'
after :finishing, 'deploy:update_database'
after :finishing, 'deploy:restart_application'
after :finishing, 'deploy:cleanup'
end
namespace :setup do
desc 'Copy the secrets.yml and database.yml files'
task config: ['config/secrets.yml', 'config/database.yml'] do |t|
on roles(:all) do
execute "mkdir -p #{shared_path}/config"
t.prerequisites.each do |file|
upload! file, "#{shared_path}/config"
end
end
end
end
def spacer(desc = nil)
puts "-----------------------------------------------------------------------------"
if desc
puts desc
puts "-----------------------------------------------------------------------------"
end
end
The error:
UPDATE 09/25 : 02:10pm PST
After working with https://hackhands.com/ we discovered that multiple instances of nginx are running as shown:
I can work to kill the services and restart it but it seems like something may not be configured correctly on the server via chef or our cap deployment. If I restart the box things work as stated, but we also tried killing the nginx services. We discovered that works as well. Our dev ops team is working on this, but we are still perplexed how this has occurred or how to repair it.
UPDATE 09/26 : 11:06am PST
I found where the config comes from on the passenger spinup, if you
look in /etc/service/ you will see the folders for the apps that are
on the server. Look at the run file in the folder you're interested
in and you'll see the passenger config. That fires off a ruby .bin/passenger start process, which then fires off the /tmp nginx
process which is hanging on app restart. I've tried restarting all in
different combos, the one that seems to work is killing the nginx
process, then running sudo killall ruby to respawn the new app...
not ideal
So the update our DevOps team did to address this issue revolves around the way passenger spawns nginx. The config that it was using was the older spawn-lv2 from previous versions, and I changed it to spawn for the current version 4. What this seems to do is to stop creating new directories in /tmp that were referenced by the runit script but would fail because the old version was still running. Now it looks like the updates are done in the current /tmp directory instead, so it doesn't matter if the process is still running.
This update was done to the rackbox default attribute in chef: default["rackbox"]["default_config"]["passenger_runit"]["spawn_method"] = "smart"

NoMethodError: undefined method `fetch' for "QLite version 3.x":String

I am seeing this error when i am trying to deploy my rails app to production
SSHKit::Runner::ExecuteError: Exception while executing on host xxx.xxx.xxx.xx: rake exit status: 1
rake stdout: Nothing written
rake stderr: rake aborted!
NoMethodError: undefined method `fetch' for "QLite version 3.x":String
This error is invoked by th command
Command: cd /home/deploy/myapp/releases/20140616034148 && ( RAILS_ENV=production ~/.rvm/bin/rvm default do bundle exec rake assets:precompile )
capfile
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
require 'capistrano/bundler'
require 'capistrano/rails'
require 'capistrano/rvm'
set :rvm_ruby_version, '2.1.2'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
deploy.rb
lock '3.1.0'
set :application, 'myapp'
set :repo_url, 'git#bitbucket.org:username/myapp.git'
set :deploy_to, '/home/deploy/myapp'
set :branch, "master"
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
# Your restart mechanism here, for example:
execute :touch, release_path.join('tmp/restart.txt')
end
end
production.rb
set :stage, :production
role :app, %w{deploy#xxx.xxx.xxx.xx}
role :web, %w{deploy#xxx.xxx.xxx.xx}
role :db, %w{deploy#xxx.xxx.xxx.xx}
set :password, ask('Server password', nil)
server '107.170.187.98', user: 'deploy', password: fetch(:password), roles: %w{web app}
set :bundle_env_variables, { 'NOKOGIRI_USE_SYSTEM_LIBRARIES' => 1 }
I dont understand what is causing this issue. Can someone plz point me out in right direction.
Check the config/database.yml file you've set up on the production server. Its first line is probably "QLite version 3.x" - this is because it used to be "# SQLite version 3.x", but the initial "# S" has gone missing.
To fix this, just add the "# S" back to the front of it, or delete that line altogether. Once you've done that, your Capistrano deploy should start working again.
I'm guessing that you pasted your config/database.yml file from elsewhere into a Vim instance; I hit exactly the same problem as you did for exactly the same reason. If you don't put Vim into insert mode before you paste, it will paste the entire thing, but will drop the "# S" from the front.
The reason why these three in particular are deleted is because these letters are actually Vim commands. If you aren't in Insert mode, when you paste something, those commands will run. Those three characters, when pasted, do the following in sequence:
# - Moves the cursor to the previous occurrence of the last search. Since we didn't search for anything, nothing happens.
space - Moves the cursor to the right once. Since there's no content in the editor, nothing happens.
S - Deletes the number of lines there are before the S. Since there are no numbers before the S, nothing gets deleted. Once this is done, we enter Insert mode, and the rest of the document is pasted.
Source: http://vimdoc.sourceforge.net/htmldoc/

capistrano not using rails environment with bundler properly

I'm trying to set up Capistrano deployment for the first time and I wanted to test it on my development directory before attempting it on production. Normally I wouldn't even bother with Capistrano in the dev environment, but I'm having issues when deploying. It seems that Capistrano wants to:
A) run the bundle command: bundle --without development test
and
B) run rake assets:precompile in the development environment. I don't want that. Why would I? Maybe in the 'staging' environment if I ever wanted to do that, but certainly not in development mode.
The biggest hurdle at the moment is the fact that it bundles thinking it's in production mode and thus skips over gems which are required when it DOES correctly use the development environment when precompiling assets.
EDIT: Here's a sample of two scripts which are run - the first scripts run bundler as if we're in the production environment, and the last runs it in development environment (RAILS_ENV=development). We of course get the error because BetterErrors is a gem that's loaded only in the development environment and so it can't find BetterErrors because the call to bundle was in the production environment.
INFO [9797fc64] Running ~/.rvm/bin/rvm default do bundle install --binstubs /home/vps_user/rails_deployments/dev.www/shared/bin --path /home/vps_user/rails_deployments/dev.www/shared/bundle --without development test --deployment --quiet on localhost
DEBUG [9797fc64] Command: cd /home/vps_user/rails_deployments/dev.www/releases/20140217224858 && ~/.rvm/bin/rvm default do bundle install --binstubs /home/vps_user/rails_deployments/dev.www/shared/bin --path /home/vps_user/rails_deployments/dev.www/shared/bundle --without development test --deployment --quiet
INFO [9797fc64] Finished in 1.883 seconds with exit status 0 (successful).
DEBUG [da905ff7] Running /usr/bin/env if test ! -d /home/vps_user/rails_deployments/dev.www/releases/20140217224858; then echo "Directory does not exist '/home/vps_user/rails_deployments/dev.www/releases/20140217224858'" 1>&2; false; fi on localhost
DEBUG [da905ff7] Command: if test ! -d /home/sprvps_userucewo/rails_deployments/dev.www/releases/20140217224858; then echo "Directory does not exist '/home/vps_user/rails_deployments/dev.www/releases/20140217224858'" 1>&2; false; fi
DEBUG [da905ff7] Finished in 0.044 seconds with exit status 0 (successful).
INFO [0562438c] Running ~/.rvm/bin/rvm default do bundle exec rake assets:precompile on localhost
DEBUG [0562438c] Command: cd /home/vps_user/rails_deployments/dev.www/releases/20140217224858 && ( RAILS_ENV=development ~/.rvm/bin/rvm default do bundle exec rake assets:precompile )
DEBUG [0562438c] rake aborted!
DEBUG [0562438c] uninitialized constant BetterErrors
Is there something wrong with my setup? I'm using Capistrano v3+, with Ruby v2.1.0 inside rvm.
Gemfile:
if RUBY_PLATFORM !~ /mingw/
gem 'capistrano-rails'
gem 'capistrano-rvm'
gem 'capistrano-bundler'
end
Capfile:
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
require 'capistrano/rails'
require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
require 'capistrano/bundler'
# require 'capistrano/rails/assets'
# require 'capistrano/rails/migrations'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
deploy.rb:
# config valid only for Capistrano 3.1
lock '3.1.0'
set :application, 'website'
set :repo_url, 'git#bitbucket.org:MyUserName/website.git'
set :user, 'vps_user'
set :tmp_dir, '/home/vps_user/tmp'
# Default value for keep_releases is 5
set :keep_releases, 3
SSHKit.config.command_map[:rake] = "bundle exec rake"
SSHKit.config.command_map[:rails] = "bundle exec rails"
# Common directories (usually assets)
set :linked_dirs, %w{ public/assets/emails public/assets/events public/assets/photographs public/assets/updates public/assets/video public/assets/wines }
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
# Your restart mechanism here, for example:
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
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
end
deploy/development.rb
set :branch, :develop
set :stage, :development
set :rails_env, 'development'
set :deploy_to, '/home/vps_user/rails_deployments/dev.www'
server 'localhost', user: 'vps_user', roles: %w{web app}
deploy/production.rb
set :branch, :master
set :stage, :production
set :rails_env, 'production'
set :deploy_to, '/home/vps_user/rails_deployments/www'
server 'localhost', user: 'vps_user', roles: %w{web app}
And I run the deploy command as such: bundle exec cap development deploy
OK so looking at Capistrano::Bundler under 'Usage' I saw an option in there called :bundle_without and it looked promising. So I put set :bundle_without, 'production' inside of my development.rb deploy script and it worked!
This doesn't solve the issue that Capistrano keeps trying to generate precompiled assets but I'm sure there's a solution out there like overriding the rake task or something.

Resources