前提・実現したいこと
Ruby on railsでアプリケーションを作成しています。
最終的にはcapistranoを使用し、自動デプロイが実行できるようにしたいと考えています。
デプロイ先はAWSになります。エラーメッセージが表示されているため表示されているエラーメッセージの内容を解決したいと考えています。
ローカルPC内にvirtualboxとVagrantを使用してUbuntuの環境を構築し、その中にアプリケーションを置いています。
AWSの鍵はローカルPCの.sshディレクトリの中に格納しています。
発生している問題・エラーメッセージ
アプリケーションのディレクトリに移動し、下記コマンドを実行するとエラーメッセージが表示されます
$ bundle exec cap production deploy:check
以下エラーメッセージ
[Deprecation Notice] Future versions of Capistrano will not load the Git SCM plugin by default. To silence this deprecation warning, add the following to your Capfile after `require "capistrano/deploy"`: require "capistrano/scm/git" install_plugin Capistrano::SCM::Git #<Thread:0x000055ca107f5320@/var/lib/gems/2.5.0/gems/sshkit-1.18.0/lib/sshkit/runners/parallel.rb:10 run> terminated with exception (report_on_exception is true): Traceback (most recent call last): 1: from /var/lib/gems/2.5.0/gems/sshkit-1.18.0/lib/sshkit/runners/parallel.rb:11:in `block (2 levels) in execute' /var/lib/gems/2.5.0/gems/sshkit-1.18.0/lib/sshkit/runners/parallel.rb:15:in `rescue in block (2 levels) in execute': Exception while executing as ec2-user@**.**.**.** : Authentication failed for user ec2-user@**.**.**.** (SSHKit::Runner::ExecuteError) (Backtrace restricted to imported tasks) cap aborted! SSHKit::Runner::ExecuteError: Exception while executing as ec2-user@**.**.**.** : Authentication failed for user ec2-user@**.**.**.** Caused by: Net::SSH::AuthenticationFailed: Authentication failed for user ec2-user@**.**.**.**
該当のソースコード
unicorn.rb
ruby
1app_path = File.expand_path('../../../', __FILE__) 2 3worker_processes 1 4 5working_directory "#{app_path}/current" 6pid "#{app_path}/shared/tmp/pids/unicorn.pid" 7stderr_path "#{app_path}/shared/log/unicorn.stderr.log" 8stdout_path "#{app_path}/shared/log/unicorn.stdout.log" 9 10listen "#{app_path}/shared/tmp/sockets/unicorn.sock" 11timeout 60 12 13preload_app true 14GC.respond_to?(:copy_on_write_friendly=) && GC.copy_on_write_friendly = true 15 16check_client_connection false 17 18run_once = true 19 20before_fork do |server, worker| 21 defined?(ActiveRecord::Base) && 22 ActiveRecord::Base.connection.disconnect! 23 24 if run_once 25 run_once = false # prevent from firing again 26 end 27 28 old_pid = "#{server.config[:pid]}.oldbin" 29 if File.exist?(old_pid) && server.pid != old_pid 30 begin 31 sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU 32 Process.kill(sig, File.read(old_pid).to_i) 33 rescue Errno::ENOENT, Errno::ESRCH => e 34 logger.error e 35 end 36 end 37end 38 39after_fork do |_server, _worker| 40 defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection 41end 42
production.rb
ruby
1server '**.**.**.**',user: 'ec2-user', roles: %w{app db web}
deploy.rb
ruby
1# config valid only for current version of Capistrano 2 3set :application, 'yyyyy' 4set :repo_url, 'git@github.com:xxxxxx/yyyyy.git' 5set :deploy_to, '/var/www/yyyyy' 6 7set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/uploads') 8 9set :rbenv_type, :user 10set :rbenv_ruby, '2.5.1' 11set :ssh_options, auth_methods: ['publickey'], 12 keys: ['~/.ssh/key_pair.pem'] 13set :unicorn_pid, -> { "#{shared_path}/tmp/pids/unicorn.pid" } 14set :unicorn_config_path, -> { "#{current_path}/config/unicorn.rb" } 15set :keep_releases, 5 16 17after 'deploy:publishing', 'deploy:restart' 18namespace :deploy do 19 task :restart do 20 invoke 'unicorn:restart' 21 end 22end 23
capfile.rb
ruby
1require "capistrano/setup" 2require "capistrano/deploy" 3 4require 'capistrano/rbenv' 5require 'capistrano/bundler' 6require 'capistrano/rails/assets' 7require 'capistrano/rails/migrations' 8require 'capistrano3/unicorn' 9 10Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r } 11
試したこと
deploy.rb
に記述しているkey_pair.pem
を使用し、AWSのアクセスができるかを試しました。
$ ssh -i key_pair.pem ec2-user@**.**.**.**
__| __|_ ) _| ( / Amazon Linux AMI ___|\___|___| https://aws.amazon.com/amazon-linux-ami/2018.03-release-notes/ [ec2-user@ip-***-**-**-*** ~]$
結果接続はできました。そのため、使用する鍵はあっていると考えています。
補足情報(FW/ツールのバージョンなど)
Ubuntu環境
ubuntu: 18.04.1
ruby: 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]
Ruby on Rails: Rails 5.2.2
capistrano (3.11.0)
capistrano-bundler (1.5.0)
capistrano-rails (1.4.0)
capistrano-rbenv (2.1.4)
capistrano3-unicorn (0.2.1)
AWS環境
ruby: 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]
Ruby on Rails: Rails 5.2.2
どなたかご教授いただけたらと思います。
ご不明点があればお手数ですがご一報くださいますようお願いします。