Closed30

/

あつしあつし

EC2にログイン→作業

ローカルのターミナル
ssh my_app

       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/

下記を参考にRubyのインストール作業開始
https://qiita.com/take18k_tech/items/5710ad9d00ea4c13ce36#55-rubyのインストール

EC2サーバー内
・・・

# 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インストール開始・・・

※後日追記
コンテナデプロイなんでこの作業いらん。
あつしあつし
EC2サーバー内

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を再び実行。

※後日追記
この作業いらん。

あつしあつし

同様のエラー

dockerコンテナ内
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番号)
あつしあつし

翌日の午前、作業再開。
コンテナ内でデプロイコマンド入力後、接続拒否のエラー出現

dockerコンテナ内
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/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
あつしあつし

再デプロイ時エラー発生、ログをチェック。

コンテナ内
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
あつしあつし

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上でインストールする必要はあるのか?

あつしあつし

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)のインストール中にエラーが発生。

EC2サーバー内
[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 を実行しエラーを確認。

dockerコンテナ内
$ 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!
あつしあつし

ローカルのコンテナ内で再度$ 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'

https://zenn.dev/ryota_koba04/articles/357b5d43ab597d
https://qiita.com/nikawa2161/items/ea0fc5b611fc9fd28a07

あつしあつし

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

●考えられる原因
・容量不足によるエラー

https://teratail.com/questions/265993
https://qiita.com/matata0623/items/0cf3e32c4eca71487b2f

あつしあつし

$ 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 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でデプロイ自動化

このスクラップは2021/10/18にクローズされました