/
①の続き(リンク→ https://zenn.dev/atushi/scraps/cc4685f3e2215b)
エラー内容は下記の通り
00:00 rbenv:validate
WARN rbenv: ruby-2.7.3 is not installed or not found in $HOME/.rbenv/versions/ruby-2.7.3 on
****(Elastic IP番号)
EC2にログイン→作業
ssh my_app
__| __|_ )
_| ( / Amazon Linux 2 AMI
___|\___|___|
https://aws.amazon.com/amazon-linux-2/
下記を参考にRubyのインストール作業開始
・・・
# Rybyインストール必要なパッケージのインストール
sudo yum -y install make gcc-c++ patch openssl-devel libyaml-devel libffi-devel libicu-devel libxml2 libxslt libxml2-devel libxslt-devel zlib-devel readline-devel
・・・
# 導入後、再びrubyインストール
rbenv install 2.7.3
Downloading ruby-2.7.3.tar.bz2...
Rubyインストール開始・・・
※後日追記
コンテナデプロイなんでこの作業いらん。
rbenv global 2.7.3
rbenv rehash
# Rubyインストールの確認
ruby -v
ruby 2.7.3p183 (2021-04-05 revision 6847ee089d) [x86_64-linux]
Ruby導入後はdockerコンテナに戻り $ bundle exec cap production deploy
を再び実行。
※後日追記
この作業いらん。
同様のエラー
00:00 rbenv:validate
WARN rbenv: ruby-2.7.3 is not installed or not found in $HOME/.rbenv/versions/ruby-2.7.3 on ****(Elastic IP番号)
翌日の午前、作業再開。
コンテナ内でデプロイコマンド入力後、接続拒否のエラー出現
Caused by:
Errno::ECONNREFUSED: Connection refused - connect(2) for 13.114.99.183:22
VPCセキュリティグループ、ssh接続のインバウンドルールを更新(マイIPを更新)後、接続できた。
再び、上記$ ruby-2.7.3 is not installed or not found
のエラー改善作業開始。
app/config/deploy.rb内のファイルを編集し、コンテナ再ビルド後、デプロイが開始された。
# config valid for current version and patch releases of Capistrano
lock "~> 3.16.0"
set :application, "fd_tube_app"
set :repo_url, "git@github.com:kenkennfree/fd_tube_app.git"
## 下記を修正 ##
# set :rbenv_ruby, "File.read('.ruby-version').strip" #<=変更前
set :rbenv_ruby, "2.7.3" #<=変更後
##
set :branch, ENV['BRANCH'] || "master"
set :nginx_config_name, "#{fetch(:application)}.conf"
set :nginx_sites_enabled_path, "/etc/nginx/conf.d"
append :linked_files, "config/master.key"
append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "node_modules"
デプロイ中、master.keyがありませんエラー出現
00:02 deploy:check:linked_files
ERROR linked file /var/www/fd_tube_app/shared/config/master.key does not exist on 13.114.99.183
下記の記事通り、マスターキーを配置する。
root@〇〇〇〇:/app_name# scp config/master.key app_name:/var/www/app_name/shared/config
master.key
再デプロイ時エラー発生、ログをチェック。
cat log/capistrano.log
結果↓
To see why this extension failed to compile, please check the mkmf.log which can
be found here:
/var/www/fd_tube_app/shared/bundle/ruby/2.7.0/extensions/x86_64-linux/2.7.0/pg-1.2.3/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in
/var/www/fd_tube_app/shared/bundle/ruby/2.7.0/gems/pg-1.2.3 for inspection.
Results logged to
/var/www/fd_tube_app/shared/bundle/ruby/2.7.0/extensions/x86_64-linux/2.7.0/pg-1.2.3/gem_make.out
An error occurred while installing pg (1.2.3), and Bundler cannot continue.
Make sure that `gem install pg -v '1.2.3' --source 'https://rubygems.org/'`
succeeds before bundling.
In Gemfile:
pg
EC2からデータベースにログインしていなかったので下記を実行
node,node.jsが見つかりませんエラー
DEBUG [ed08a4c7] sh: node: command not found
DEBUG [ed08a4c7] sh: nodejs: command not found
DEBUG [ed08a4c7] Node.js not installed. Please download and install Node.js https://nodejs.org/en/download/
アプリ内のDockerファイル内でnode/yarnのインストールを行なっているが、EC2上でインストールする必要はあるのか?
・Docker Capistranoデプロイ流れをチェック
・もしくは下記通り、EC2上でDockerコンテナを起動させれば良いかも?
Capistranoデプロイ時、pullしてくるファイル内に.envが無いので追加する
/var/www/my_app/shared内は下記
. .. .env bundle config log node_modules public puma.rb tmp
一度、手動デプロイが必要みたいなので以下の作業を開始。
- EC2サーバー上 /var/www内のアプリを消去
- EC2サーバー上 /var/www内にアプリをgit cloneする
- EC2サーバー上 /var/www/my_appにローカルからmaster.keyと.envファイルをコピーしてくる
- EC2サーバー上 /var/www/my_appに移動してdocker-compose build
上記作業の最後の行を実行後、nio4r (2.5.8)のインストール中にエラーが発生。
[myuser@xxx.xxx.xxx.xxx ~ my_app]
・・・
An error occurred while installing nio4r (2.5.8), and Bundler cannot continue.
Make sure that `gem install nio4r -v '2.5.8' --source 'https://rubygems.org/'`
succeeds before bundling.
In Gemfile:
rails was resolved to 6.1.4.1, which depends on
actioncable was resolved to 6.1.4.1, which depends on
nio4r
docker-compose build後にCapistranoでデプロイ実行後、AWSのCPUが高騰しログインできなくなった。
インスタンス再起動後、しばらくしたらログインできるようになった。
直近デプロイ時のログを dockerコンテナ内で$ cat log/capistrano.log
を実行しエラーを確認。
$ cat log/capistrano.log
・・・
DEBUG [・・・] sh: node: command not found
DEBUG [・・・] sh: nodejs: command not found
DEBUG [・・・] Node.js not installed. Please download and install Node.js https://nodejs.org/en/download/・・・・
Exiting!
EC2サーバー上でnodeとyarnのインストールを実施する。$ yarn -v
でインストールの確認ができなかったので、yarnのインストール後$ source ~/.bashrc
を実施。
$ yarn -v
1.22.15
ローカルのコンテナ内で再度$ bundle exec cap production deploy
を実行
新たなエラー出現。データベース関連のエラーっぽい。。
・・・
DEBUG [ae90df62] rake aborted!
DEBUG [ae90df62] ActiveRecord::ConnectionNotEstablished: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
・・・
DEBUG [9cd3994a]
Caused by:
DEBUG [9cd3994a] PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
DEBUG [9cd3994a] /var/www/fd_tube_app/shared/bundle/ruby/2.7.0/gems/pg-1.2.3/lib/pg.rb:58:in `initialize'
(dockerコンテナの設定をここで修正した)
上記と同様のエラー(dockerの容量警告出現)
EC2鯖内でビルドチャレンジするもエラー。
$ docker-compose build
・・・
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
・・・
An error occurred while installing nio4r (2.5.8), and Bundler cannot continue.
Make sure that `gem install nio4r -v '2.5.8' --source 'https://rubygems.org/'`
succeeds before bundling.
In Gemfile:
rails was resolved to 6.1.4.1, which depends on
actioncable was resolved to 6.1.4.1, which depends on
nio4r
●考えられる原因
・容量不足によるエラー
EC2のスワップファイルの領域を広げる
この記事の容量分スワップファイルの容量を増やすことでビルドに成功!
続いて、この記事通りに作業を実行。
$ rails db:migrate
時に下記のマイグレーションエラー出現。
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::DuplicateColumn: ERROR: column "s3file_id" of relation "videos" already exists
/fd_tube_app/db/migrate/20210921140512_add_s3file_id_to_videos.rb:3:in `change'
/fd_tube_app/bin/rails:5:in `<top (required)>'
/fd_tube_app/bin/spring:10:in `block in <top (required)>'
/fd_tube_app/bin/spring:7:in `tap'
/fd_tube_app/bin/spring:7:in `<top (required)>'
Caused by:
ActiveRecord::StatementInvalid: PG::DuplicateColumn: ERROR: column "s3file_id" of relation "videos" already exists
/fd_tube_app/db/migrate/20210921140512_add_s3file_id_to_videos.rb:3:in `change'
/fd_tube_app/bin/rails:5:in `<top (required)>'
/fd_tube_app/bin/spring:10:in `block in <top (required)>'
/fd_tube_app/bin/spring:7:in `tap'
/fd_tube_app/bin/spring:7:in `<top (required)>'
Caused by:
PG::DuplicateColumn: ERROR: column "s3file_id" of relation "videos" already exists
/fd_tube_app/db/migrate/20210921140512_add_s3file_id_to_videos.rb:3:in `change'
/fd_tube_app/bin/rails:5:in `<top (required)>'
/fd_tube_app/bin/spring:10:in `block in <top (required)>'
/fd_tube_app/bin/spring:7:in `tap'
/fd_tube_app/bin/spring:7:in `<top (required)>'
Tasks: TOP => db:migrate
カラムが既にあります問題。考えられる対応
・DBにアクセスしてカラムをいじる(危険度高そう)
・ローカルとEC2上のDBコンテナの相違点を確認する。
・ローカルのdockerコンテナをそのままデプロイする方法を調べる
docker-compose.ymlファイルで、ローカルと本番環境のネットワークを共有する設定を行うことでデプロイ完了した。
●ローカル環境のファイル
●本番環境のファイル
今後やること
・docker-compose network の詳細について
・seed、その他のテーブルが共有できていない説の検証
・本番環境のコンテナ内でrails db:migrateが下記のエラーになる件についての改善
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
・・・
PG::DuplicateColumn: ERROR: column "s3file_id" of relation "videos" already exists
ActiveRecord::StatementInvalid: PG::DuplicateColumn: ERROR: column "s3file_id" of relation "videos" already
・・・
PG::DuplicateColumn: ERROR: column "s3file_id" of relation "videos" already exists
・・・
その後、
・ALB設定
・HTTPS化
・capistranoでデプロイ自動化
これを実行することで手動デプロイ完了した。