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

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

我正在尝试使用 capistrano(存储库在 gitlab 上)部署我的 Ruby 应用程序,但出现此错误:

>> 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>

我的~/.ssh/config:

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

我的deploy.rb:

set :branch, ENV['BRANCH'] || 'master'
set :application, 'example'
set :repo_url, 'ssh://git@gitlab.com/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

我的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}

尝试将允许的 ssh 密钥添加到您的生产机器/服务器。即:

  1. 运行 cat ~/.ssh/id_rsa.pub 在您的本地计算机上
  2. 然后 运行 ~/.ssh/authorized_keys 在你的 server/production 机器上
  3. 将上述第 1 步的结果复制粘贴到服务器上的 authorized_keys 文件中
  4. 然后运行cap production deploy再次命令

以防在 运行 ~/.ssh/authorized_keys 时看不到 authorized_keys 文件 您可以通过 运行ning 组合上述所有命令 1 到 3 cat ~/.ssh/id_rsa.pub | ssh USER@HOST "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys".

感谢您的帮助!

不幸的是,昨天我累了,注意力不集中。问题确实是错误的用户。我用 deployer 替换了 git 并且一切都启动了。