NoMethodError: undefined method `fetch' for "QLite version 3.x":String - ruby-on-rails

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/

Related

Capistrano deploy configuration

Help please to configure capistrano for deployment.
I have ssh:
user: User
host: 8.8.8.8:6554
pass: 123
Then i have bitbucket repository git#bitbucket.org:somerepo/code.git
user: Repouser#gmail.com
pass: repopass
I am just need to deploy code from default branch to User#8.8.8.8:8888:/public_html/test/ . On local machine i have ssh key, that allows me to connect without password. But capistrano didn't connect.
There is my config:
lock '3.3.5'
set :application, 'App'
set :scm, :git
set :repo_url, 'git#bitbucket.org:somerepo/code.git'
set :scm_passphrase, ""
set :scm_user, "Repouser#gmail.com"
set :user, 'User'
set :deploy_to, '/public_html/test'
set :app_dir, "/public_html/test"
set :ssh_options, {:forward_agent => true}
role :web, '8.8.8.8:6554'
namespace :deploy do
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
end
end
end
Error:
connection closed by remote host
** Invoke deploy:failed (first_time)
** Execute deploy:failed
Step 1: in Gemfile
gem 'capistrano'
gem 'capistrano-bundler'
gem 'capistrano-rails'
Step 2: bundle
Step 3: cap install ## it will generate set of file
Step 4: go in Capfile and paste the following code ## this file will be parallel to your rails application
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
Step 5: Config/deploy.rb that will be common to both ENV
This file will be shared/common across the application environment
set :application, 'your_app' ## keep in mind that your app dir name will be your_app
set :repo_url, 'git#bitbucket.org:somerepo/code.git'
set :branch, 'master'
set :use_sudo, true
set :deploy_to, '/public_html/test'
set :linked_files, fetch(:linked_files, []).push('config/database.yml')
set :linked_dirs, fetch(:linked_dirs, []).push('bin', 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')
namespace :deploy do
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
Step6: Lets create ENV specific file for now For production Environment
config/deploy/production.rb ## this file will be generate by cap install command that you did earlier no need for this time
do comment all the code except this
role :app, %w{8.8.8.8:6554}
set :ssh_options, {
user: 'User'
}
Step 6: now do ssh to your server ssh User#8.8.8.8:6554
now it will ask for the password ... give password
Step 7: now by default your app will go /var/www/app and here you need to create the folder accordingly But in your case as you set :deploy_to, '/public_html/test' # make sure Dir name is followed by / 'Forward slash' this mistake i did many times
sudo mkdir -p /public_html
sudo mkdir -p /public_html/test
sudo chown User:User /public_html/test # `chown` will change the owner ship so that `User` user can `**Read/Write**`
umask 0002
mkdir /public_html/test/releases ## these are convention
mkdir /public_html/test/shared ## these are convention
sudo chown User:User public_html/test/releases
sudo chown User:User public_html/test/shared
mkdir .ssh
chmod .ssh 007
ssh-keygen -t rsa
and follow the step ## this will generate ssh key
cat .ssh/id_rsa.pub
Now add this key to your repo's go to => setting => deployment keys Button => click on that and add Key. Put the label name any thing you want and paste the ssh key here.
That it from server side
Step8: Now you need to add your ssh key to server
For that do cat ~/.ssh/id_rsa.pub if you have rsa key other wise generate rsa key it is very easy to crate
Step 9: Login to your server using ssh
`vi .ssh/authorized_keys` and paste your local machine rsa key
save and exit
Step 10 : cap -T ## list out all the task
step 11: cap production deploy:check
It will throw an error because database.yml file is not there
For that vi /public_html/test/shared/config/database.yml
development:
adapter: postgresql
database: testdb_cap
pool: 5
timeout: 5000
save and exit
Do again cap production deploy:check
This time would not throw any error
Step 12:
cap production deploy
And That's it
Check this also ruby rake task after deploymewnt

Capistrano deploy removes table

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.

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"

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.

gems not found on target server after Capistrano deployment

I deployed my rails 3 app onto the production server using Capistrano. The deployment is correct, the application server is correctly restarted but when I log onto the production server and go into "my_path/current", I cannot run "rails c":
The program 'rails' is currently not installed. You can install it by typing:
sudo apt-get install rails
I checked the gem list and everything seems to be correctly installed though.
My config/deploy.rb file is:
require "bundler/capistrano"
# Add RVM's lib directory to the load path.
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
# Load RVM's capistrano plugin.
require "rvm/capistrano"
set :rvm_ruby_string, '1.9.3'
set :rvm_type, :user # Don't use system-wide RVM
set :application, "MyApp"
set :repository, "git#github.com:user/myapp.git"
set :scm, :git
set :branch, "master"
set :scm_verbose, true
set :scm_username, "user" # GITHUB user name
set :user, "user" # VPS user name
set :scm_passphrase, "pass" # VPS password
set :deploy_to, "/home/luc/be"
set :use_sudo, false
server "my_server", :web, :app, :db, :primary => true
# start / stop application server
namespace :deploy do
task :start do
invoke_command "cd /home/luc/be/current; bundle exec thin start -C config/thin.config"
end
task :stop do
invoke_command "cd /home/luc/be/current; bundle exec thin stop -C config/thin.config"
end
task :restart do
invoke_command "cd /home/luc/be/current; bundle exec thin restart -C config/thin.config"
end
end
Try bundle exec rails c. Also check what error, if any, is thrown when you visit the page in your browser

Resources