Search code examples
ruby-on-railsrubydeploymentgitlabcapistrano

cap aborted! SSHKit::Runner::ExecuteError: Exception while executing on host xxx.xxx.xx.xx: deployer


I'm trying to deploy my Ruby app using capistrano (repository is on gitlab) and I get this error:

>> cap production deploy 

cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing on host xxx.xxx.xx.xx: deployer
/home/deployer/.rvm/gems/ruby-2.1.2/gems/net-ssh-2.7.0/lib/net/ssh.rb:215:in `start'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/connection_pool.rb:50:in `call'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/connection_pool.rb:50:in `create_new_entry'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/connection_pool.rb:22:in `checkout'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:179:in `with_ssh'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:131:in `block in _execute'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:128:in `tap'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:128:in `_execute'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:66:in `execute'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/capistrano-3.2.1/lib/capistrano/tasks/git.rake:17:in `block (3 levels) in <top (required)>'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `instance_exec'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `run'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/runners/parallel.rb:13:in `block (2 levels) in execute'
Net::SSH::AuthenticationFailed: deployer
/home/deployer/.rvm/gems/ruby-2.1.2/gems/net-ssh-2.7.0/lib/net/ssh.rb:215:in `start'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/connection_pool.rb:50:in `call'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/connection_pool.rb:50:in `create_new_entry'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/connection_pool.rb:22:in `checkout'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:179:in `with_ssh'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:131:in `block in _execute'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:128:in `tap'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:128:in `_execute'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:66:in `execute'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/capistrano-3.2.1/lib/capistrano/tasks/git.rake:17:in `block (3 levels) in <top (required)>'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `instance_exec'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `run'
/home/deployer/.rvm/gems/ruby-2.1.2/gems/sshkit-1.5.1/lib/sshkit/runners/parallel.rb:13:in `block (2 levels) in execute'
Tasks: TOP => git:check => git:wrapper
(See full trace by running task with --trace)
The deploy has failed with an error: #<SSHKit::Runner::ExecuteError: Exception while executing on host xxx.xxx.xx.xx: deployer>

My ~/.ssh/config:

Host gitlab.com
  IdentityFile ~/.ssh/id_rsa
  User git
  PreferredAuthentications publickey
  ForwardAgent yes

My deploy.rb:

set :branch, ENV['BRANCH'] || 'master'
set :application, 'example'
set :repo_url, 'ssh://[email protected]/xxxx/xxxxxxxxx.git'
set :rbenv_type, :user # or :system, depends on your rbenv setup
set :rbenv_ruby, '2.1.2'
set :deploy_to, '/home/deployer/example'
set :scm, :git
set :log_level, :debug
set :linked_files, %w{config/database.yml config/initializers/s3.rb config/redis.yml config/skylight.yml config/sunspot.yml config/secrets.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads solr}
set :keep_releases, 10
set :whenever_identifier, -> { "#{fetch(:application)}_#{fetch(:stage)}" }
set :whenever_roles, :app
namespace :deploy do
  desc 'Compile assets'
  task :gulp_build do
    on roles(:app) do
      within "#{release_path}/app/assets/javascripts/app" do
        with bower_registry: 'http://registry.bower.io' do
          execute :bower, 'install -s'
        end
      end
      within release_path do
        execute :npm, 'install --silent'
        execute :bundle, 'exec gulp build --production'
      end
    end
  end
  %i(start stop restart force_stop reload).each do |task_name|
    namespace :unicorn do
      task task_name do
        invoke "unicorn:#{task_name}"
      end
    end
  end
  %i(start stop restart).each do |task_name|
    namespace :solr do
      task task_name do
        invoke "solr:#{task_name}"
      end
    end
  end
  after :publishing, 'deploy:gulp_build'
  after :publishing, 'deploy:unicorn:restart'
end
set :unicorn_conf_path, "#{release_path}/config/unicorn/#{fetch(:stage)}.rb"
set :unicorn_pid_path, "#{shared_path}/tmp/pids/unicorn.pid"
namespace :unicorn do
  def run_unicorn
    execute :bundle, 'exec unicorn', '-c', fetch(:unicorn_conf_path), '-E', fetch(:rails_env), '-D'
  end
  desc 'Start unicorn'
  task :start do
    on roles(:app) do
      within current_path do
        run_unicorn
      end
    end
  end
  desc 'Stop unicorn'
  task :stop do
    on roles(:app) do
      within current_path do
        if test "[ -f #{fetch(:unicorn_pid_path)} ]"
          execute :kill, "-QUIT `cat #{fetch(:unicorn_pid_path)}`"
        end
      end
    end
  end
  desc 'Force stop unicorn (kill -9)'
  task :force_stop do
    on roles(:app) do
      within current_path do
        if test "[ -f #{fetch(:unicorn_pid_path)} ]"
          execute :kill, "-9 `cat #{fetch(:unicorn_pid_path)}`"
          execute :rm, fetch(:unicorn_pid_path)
        end
      end
    end
  end
  desc 'Restart unicorn; use this when preload_app: true'
  task :restart do
    on roles(:app) do
      within current_path do
        if test "[ -f #{fetch(:unicorn_pid_path)} ]"
          execute :kill, "-USR2 `cat #{fetch(:unicorn_pid_path)}`"
        else
          run_unicorn
        end
      end
    end
  end
  desc 'Restart unicorn; use this when preload_app: false'
  task :reload do
    on roles(:app) do
      within current_path do
        if test "[ -f #{fetch(:unicorn_pid_path)} ]"
          execute :kill, "-HUP `cat #{fetch(:unicorn_pid_path)}`"
        else
          run_unicorn
        end
      end
    end
  end
end
namespace :solr do
  %i(start stop).each do |task_name|
    desc "Sunspot solr #{task_name}"
    task task_name do
      on roles(:app) do
        within current_path do
          with rails_env: fetch(:rails_env, 'production') do
            execute :bundle, 'exec', :rake, "sunspot:solr:#{task_name}"
          end
        end
      end
    end
  end
  desc "Sunspot solr restart"
  task :restart do
    on roles(:app) do
      within current_path do
        with rails_env: fetch(:rails_env, 'production') do
          execute :bundle, 'exec', :rake, "sunspot:solr:stop"
          sleep 5
          execute :bundle, 'exec', :rake, "sunspot:solr:start"
        end
      end
    end
  end
end

My production:

set :rails_env, :production
set :application, 'example-production'
set :rbenv_ruby, '2.2.10'
set :branch, 'api'
set :deploy_to, '/home/deployer/api'
set :unicorn_conf_path, "#{release_path}/config/unicorn/proapi.rb"
set :unicorn_pid_path, "#{shared_path}/tmp/pids/unicorn.pid"
server 'xxx.xxx.xx.xx', user: 'deployer', roles: %w{web app db}

Solution

  • Thanks for the help!

    Unfortunately, yesterday I was tired and inattentive. The problem was really the wrong user. I replaced git with a deployer and everything started up.