升級 Ruby
## (目前專案)3.2.2->3.3.1
[官網的 3.3.1 release log](https://www.ruby-lang.org/en/news/2024/04/23/ruby-3-3-1-released/) 只有提到關於 security 的更新,且上次官方release 的版本為 3.3.0,所以應該去看的是 [3.3.0 release](https://www.ruby-lang.org/en/news/2023/12/25/ruby-3-3-0-released/)
(3.3.0 有新增許多不論是 gem 或是底層新增套件優化效能,所以大致上要看過)
接著在 local 端實作驗證專案
(開啟專案時,需打開 rails console,有時升級會在此 log 問題)
這次升級若使用 rvm 則會遇到 ssl 版本 3 的問題,
```
$rvm install 3.3.1 --with-openssl-dir=$(brew --prefix openssl)
#這是在網路上查到的解法
```
但是從這個例子來說,雖然錯誤顯示是關於 openssl 的版本,實際上真的是 openssl 3的問題嗎?可能的原因還有可能是哪些?例如:1. rvm 2. 作業系統(macOS 太新,或者是 windows 跟 mac的不同)
=>假設是 rvm 的問題,**那是否用rbenv 安裝就沒有問題?**
應該要延伸去探究問題點。
確定沒有問題後,我們使用的是 [ansible](https://hackmd.io/@blueskyson/learn-ansible) 作為方便管理 sevices 的環境設定自動化引擎
## ansible
參考 [ansible wiki](https://github.com/minkabu/kabutan_modish_ansible/wiki/Bump-Ruby-version-guide)
(需要先 clone 專案到本地端)
### 首先新增 branch
接著按照 wiki 指示,進到專案(ktus, main, oboe)
修改 `group_vars/all.yml`,升級順序為 ktus->main->oboe
```ruby:
version:
full: '3.3.1'
minor: '3.3'
location: /opt/ruby-3.3.1
gem_path: /opt/ruby-3.3.1/lib/ruby/gems/3.3
```
修改之後傳至遠端
```
$git push origin #branch(e.g. issue#ruby_upgrade_to_3.3.1)
```
(通常會先在 stg 確認升級沒問題後,才會是全專案的升級)
## 修改 capistrono 的 `config/deploy.rb`
(一樣需要先 clone 專案到本地端)
```
set :default_env, { path: "/opt/ruby-3.3.1/bin:$PATH" }
```
* (check the file in kabutan_capistrano under kabutan or oboe /config/deploy.rb with target Ruby env setting
```
# config valid for current version and patch releases of Capistrano
lock "~> 3.14.0"
set :application, "kabutan_us"
set :repo_url, "git@github.com:minkabu/kabutan_usa.git"
ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
set :deploy_to, "/srv/app"
set :format, :airbrussh
set :format_options, command_output: true, log_file: "log/capistrano.log", color: :auto, truncate: :auto
set :pty, true
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/sockets', 'vendor/bundle', 'public/system')
set :default_env, { path: "/opt/ruby-3.3.1/bin:$PATH" }
#注意這行有沒有更新
set :keep_releases, 10
set :ssh_options, user: 'app-user'
...)
```
commit 之後
```
$git push origin main
```
* execute ansible-playbook to deploy branch with target Ruby version
```
$ssh kt-us-bastion
```
```
$sudo su - ec2-user
```
```
$cd kabutan_modish_ansible
```
```
$cd ktus/
```
```
$git pull origin #branch
```
```
$./ansible.sh staging
#執行 ansible.sh 檔案,playbook 指向 ktus的 playbook.yml
#"Usage: ansible [environment]"
```
```
ansible.sh
if [ $# -ne 1 ]; then
echo "Usage: ansible [environment]" 1>&2
echo "ex) ansible.sh staging" 1>&2
exit 1
fi
APP=$(basename `pwd`)
MSG="Ansibleを実行します。 \n app: *$APP*\n env: *$1*"
JSON="{ \"text\": \"$MSG\" }"
RES=`curl -s -X POST -H 'Content-Type: application/json' -d "$JSON" $SLACK_WEBHOOK_URL`
ansible-playbook -i inventories/$1 playbook.yml
```
* after ansible-playbook has done, enter web-server and use systemctl status puma to check Puma instance alive. enter rails console and use RUBY_VERSION to check runtime Ruby version
```
$ssh kt-us-bastion
```
```
$ssh stg-ktus-par01
```
```
$sudo systemctl status puma
#取得 puma 狀態
```
### 確認 datadog 、EC2-RDS 狀況,效能需提升不能降低
進入 ec2>執行個體>xxxxxxx 監控
跟 ruby gem 升級驗證流程一樣,會需要放在 stg 幾天觀察,確認資料進出都沒有問題
### 打開 stg 環境中的 rails console,確認 ruby version
```
$ssh kt-us-bastion
```
```
$ssh stg-ktus-par01
```
```
$cd /srv/app/current/
```
```
$bundle exec rails c
```
```
$RUBY_VERSION
#應要印出 "3.3.1"
```
## 確認過後,開另一個 terminal 作業 capistrano 的 deploy
```
$ssh kt-us-bastion
```
```
$cd kabutan_capistrano/ktus/
```
```
$git checkout #brach
```
```
$bundle exec cap staging deploy
```
* deploy kabutan or kabutan_oboe upgrade ruby branch with Travis CI and .ruby-version settings