rails seedを冪等性にする。sanitizeがデフォルトで許可しているタグ

rails 6.1 ruby 3.0

seedを冪等性にする

docker compose upなどでbin/rails db:seedを実行する環境などで何度実行されても同じ結果にしたいと思いました。

ActiveRecord::Base.connection.disable_referential_integrity do
  ActiveRecord::Base.connection.execute("TRUNCATE users")
  User.create(name: "user1")
end

TRUNCATEでidも元に戻るので、create時に指定しなくても大丈夫です。

プロジェクトのみんなからはそんなに好意的な意見は聞かれなかったので、とりあえずではあります。

docker起動時のコマンドを改善した方がいい気はする。

find_or_create_byは?

db:seedの後で、どこかのカラムを変更し、もう一度db:seedするときに、idを指定するとduplicateになり、指定しないと似たようなレコードがもう1レコードできてしまいます。

sanitizeがデフォルトで許可しているタグ

多分この辺です。ガイドにも書いてない。

github.com

htmlをそのまま表示しなければいけない時にscriptタグとかは許可したくない時に使えます。

rawよりは安全です。

BUFFALOのルータWSR-5400AX6でVPN(L2TP/IPsec)に繋がるようになった

昔に買ったWZR-450HP(発売時期: 2012年6月)は繋がったんですが、逆に新しい方がつながりませんでした。

結果としては、二つの設定を見比べると古い方はPPPoEでインターネットに接続していて、 新しい方はそうじゃないようでした。 じゃあ何で繋がってたかはよくわかってないです。IPoEですかね?

色々試した結果それがわかったので、新しい方のルータでも無理やりPPPoEで繋いで見たところ つながりました。

これがその設定です。 f:id:ta_watanabe:20220119074643p:plain

最初は「インターネット@スタートを行う」にチェックが入ってるんですが、それを上記のように変えました。

古いルータでは「PPPoEクライアント機能を使用する」の項目はありますが、「インターネット@スタートを行う」にチェックが入っていても VPNは繋がりました。

f:id:ta_watanabe:20220119075326p:plain
古いルータ

後、新しい方のルータでやったことは、ルータモードみたいなのをautoから常にONみたいなのにしました。

Android Firestore: grpc-javaのバグ

FlutterのプロジェクトでGoogle Playが入ってないエミュレータかつAndroid11で例外が発生するということをメンバーが見つけてくれました。

こんな感じのスタックトレースになってます。

W/GooglePlayServicesUtil( 3931): com.example.debug requires the Google Play Store, but it is missing.
E/GooglePlayServicesUtil( 3931): GooglePlayServices not available due to error 9
W/Firestore( 3931): (23.0.3) [GrpcCallProvider]: Failed to update ssl context: com.google.android.gms.common.GooglePlayServicesNotAvailableException
I/.learning.debu( 3931): NativeAlloc concurrent copying GC freed 68025(3478KB) AllocSpace objects, 17(532KB) LOS objects, 49% free, 3278KB/6556KB, paused 2.569ms total 126.063ms
W/.learning.debu( 3931): Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->getAlpnSelectedProtocol()[B (greylist-max-q,core-platform-api, reflection, denied)
W/.learning.debu( 3931): Accessing hidden method Lcom/android/org/conscrypt/AbstractConscryptSocket;->getAlpnSelectedProtocol()[B (greylist-max-q, reflection, denied)
E/AndroidRuntime( 3931): FATAL EXCEPTION: grpc-okhttp-0
E/AndroidRuntime( 3931): Process: com.example.debug, PID: 3931
E/AndroidRuntime( 3931): java.lang.AssertionError: Method getAlpnSelectedProtocol not supported for object SSL socket over Socket[address=firestore.googleapis.com/172.217.31.138,port=443,localPort=53942]
E/AndroidRuntime( 3931):    at io.grpc.okhttp.internal.OptionalMethod.invoke(OptionalMethod.java:114)
E/AndroidRuntime( 3931):    at io.grpc.okhttp.internal.OptionalMethod.invokeWithoutCheckedException(OptionalMethod.java:135)
E/AndroidRuntime( 3931):    at io.grpc.okhttp.OkHttpProtocolNegotiator$AndroidNegotiator.getSelectedProtocol(OkHttpProtocolNegotiator.java:183)
E/AndroidRuntime( 3931):    at io.grpc.okhttp.OkHttpProtocolNegotiator$AndroidNegotiator.negotiate(OkHttpProtocolNegotiator.java:145)
E/AndroidRuntime( 3931):    at io.grpc.okhttp.OkHttpTlsUpgrader.upgrade(OkHttpTlsUpgrader.java:63)
E/AndroidRuntime( 3931):    at io.grpc.okhttp.OkHttpClientTransport$4.run(OkHttpClientTransport.java:571)
E/AndroidRuntime( 3931):    at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
E/AndroidRuntime( 3931):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
E/AndroidRuntime( 3931):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
E/AndroidRuntime( 3931):    at java.lang.Thread.run(Thread.java:923)
I/Process ( 3931): Sending signal. PID: 3931 SIG: 9
Lost connection to device.
Exited (sigterm)

playの入ってないエミュレータでもAndroid10なら例外は発生しませんでした。

リリースの必須要件にplayの定期購入があるので、気にしなくても大丈夫だと思いますが、 念の為調べました。

getAlpnSelectedProtocolはAndroid 11 からブロック対象

https://developer.android.com/about/versions/11/non-sdk-11?hl=ja

このメソッドがio.grpc.okhttpというパッケージで呼び出されているのが主な原因のようです。

該当箇所はここですね
https://github.com/grpc/grpc-java/blob/v1.28.x/okhttp/src/main/java/io/grpc/okhttp/OkHttpProtocolNegotiator.java#L183

ただ、playが入っているAndroid11でも例外が発生してもよさそう。

ログに出ているGrpcCallProviderとは

どうやらこの辺りの処理のようです。
firebase-android-sdk/GrpcCallProvider.java at ac3276ce7d5f5181b1c419731495eb88a98537af · firebase/firebase-android-sdk · GitHub

ここで呼び出されているinstallIfNeededというメソッドは下記で説明があります。
セキュリティ プロバイダを更新して SSL エクスプロイトから保護する  |  Android デベロッパー  |  Android Developers

ここだけ読んでも良くわかりませんが、どうやらSSLの強度を上げてくれるものっぽいですね。

grpc-java側でもOkHttpTlsUpgraderというクラスがありますが、 firebase sdk側の処理が失敗した時だけ動くんだと予想できます。

その辺りの詳細は調べ切れてませんが、下記のissueを辿っていけば grpc-javaの新しいバージョンで 新しいメソッドのgetApplicationProtocolが呼ばれるような修正が入っていることがわかります。 Crash due to use of getAlpnSelectedProtocol on Android 11 · Issue #7519 · grpc/grpc-java · GitHub

これに伴って、firebase sdk側もアップデートされてますので、 それを利用するようにすればOKなはずです。

reduxの学習と気づいたこと

https://github.com/na8esin/react-redux-practice

今日の時点では、下記が終わったところ

https://redux.js.org/tutorials/essentials/part-7-rtk-query-basics

最近は、 reduxの評判もあまり良くないので、学習に一旦区切りをつけます。

また、typescriptで書かれてないので、書き換えながらやってますが下記のサイトが便利
https://react-typescript-cheatsheet.netlify.app/

noImplicitAnyにしている人は上を見ながら型指定ができます。

それとチュートリアルをやってる時に気になったことがあります。

ソースを動かすときにapiはmswを使うんですが、それを呼び出すときのコードがこんな感じです。 https://github.com/reduxjs/redux-essentials-example-app/blob/master/src/api/client.js

このソースのコメントで下記のリンクが載っています。
Replace axios with a simple custom fetch wrapper

これを読むとaxiosもなるべく使わない方向で行こうかなという気持ちになりました。

Using GraphQL with Ruby on Rails: 作業ログ

https://www.apollographql.com/blog/community/backend/using-graphql-with-ruby-on-rails/

apollo blogが一番わかりやすかったので見ながら作成

まずはpostgresqlをインストール。

sudo port install postgresql14

  postgresql14 has the following notes:
    To use the postgresql server, install the postgresql14-server port

sudo port install postgresql14-server

--->  Some of the ports you installed have notes:
  postgresql14-server has the following notes:
    To create a database instance, after install do
     sudo mkdir -p /opt/local/var/db/postgresql14/defaultdb
     sudo chown postgres:postgres /opt/local/var/db/postgresql14/defaultdb
     sudo su postgres -c 'cd /opt/local/var/db/postgresql14 && /opt/local/lib/postgresql14/bin/initdb -D /opt/local/var/db/postgresql14/defaultdb'

    A startup item has been generated that will aid in starting postgresql14-server with launchd. It is disabled by default. Execute the following
    command to start it, and to cause it to launch at startup:
    
        sudo port load postgresql14-server

上記のコマンドを上から流す。そうすると最後のコマンドのログはこんな感じ

$ sudo su postgres -c 'cd /opt/local/var/db/postgresql14 && /opt/local/lib/postgresql14/bin/initdb -D /opt/local/var/db/postgresql14/defaultdb'
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "ja_JP.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

fixing permissions on existing directory /opt/local/var/db/postgresql14/defaultdb ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Tokyo
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /opt/local/lib/postgresql14/bin/pg_ctl -D /opt/local/var/db/postgresql14/defaultdb -l logfile start

最後のコマンドを実行してみる

$ /opt/local/lib/postgresql14/bin/pg_ctl -D /opt/local/var/db/postgresql14/defaultdb -l logfile start
pg_ctl: could not open PID file "/opt/local/var/db/postgresql14/defaultdb/postmaster.pid": Permission denied
$ sudo /opt/local/lib/postgresql14/bin/pg_ctl -D /opt/local/var/db/postgresql14/defaultdb -l logfile start
Password:
pg_ctl: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.

Permission deniedなのにsudoをつけるとエラーになる。

さっきやってなかったコマンド

~ takayuki$ sudo port load postgresql14-server
Password:
--->  Loading startupitem 'postgresql14-server' for postgresql14-server
$ sudo ls -l /opt/local/var/db/postgresql14/defaultdb/postmaster.pid
-rw-------  1 postgres  postgres  107 11 30 22:19 /opt/local/var/db/postgresql14/defaultdb/postmaster.pid

pidファイルはpostgresユーザなのは当たり前

~ takayuki$ /opt/local/lib/postgresql14/bin/psql -U postgres
psql (14.0)
Type "help" for help.

postgres=# 

接続はできるので、とりあえず、先に進む。

やっとここでrails new。とりあえず今回はかなり単純に実行 rails new rails_graphql_practice -d postgresql

こんなエラーが出て失敗

checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header

https://stackoverflow.com/questions/6040583/cant-find-the-libpq-fe-h-header-when-trying-to-install-pg-gem

古い記事ですが、これのMacPortsを参考にするとこんな感じ

$ sudo gem install pg -- --with-pg-config=/opt/local/lib/postgresql14/bin/pg_config
Password:
Building native extensions with: '--with-pg-config=/opt/local/lib/postgresql14/bin/pg_config'
This could take a while...
Successfully installed pg-1.2.3
Parsing documentation for pg-1.2.3
Installing ri documentation for pg-1.2.3
Done installing documentation for pg after 0 seconds
1 gem installed

成功

bundle installも成功

ここから先は先ほどの記事の内容を上からなぞっていきます。

models作成

rails g model Artist first_name last_name email
rails g model Item title description:text image_url artist:references

そして、seedsの追記やdb:migrateなど、なんやかんやありまして、 rails g graphql:object itemするところでエラー。

bundle add graphqlでGemfileにgraphiql-railsも追加されるが、別途bundle installしないとgemが追加されてなかったです。

また、rails g graphql:object -hすると最後の行に

Create a GraphQL::ObjectType with the given name and fields.If the given type name matches an existing ActiveRecord model, the generated type will automatically include fields for the models database columns.

と表示されるので、databaseの内容を直接読み出してくれてるみたいです。

item_type.rbをgenerateした後でartist fieldを追加する

item_type.rb: add field :artist · na8esin/rails_graphql_practice@843f9fa · GitHub

こんな感じです。ここは流石に手動で追加しないといけないみたいです。

rails webpacker:installはやっぱり必要

artist_type.rbも生成した後で、rails sを実行したところエラー。

rails new の時に--apiつければいらないのだろうか?

何はともあれ、この時点でrails sで起動して、 http://localhost:3000/graphiql にアクセスするとクエリを実行することができます。

f:id:ta_watanabe:20211223104439p:plain

ログはこんな感じで出力されます。

この時点だと、全てのItemのArtistがtaylorなので同じArtistがn回呼び出されてます。 キャッシュが使われているので要件によってはこれでいいのかもしれないですが、 できれば

https://graphql-ruby.org/dataloader/overview.html

のあたりが使いたいです。

自分の描いたソース

GitHub - na8esin/rails_graphql_practice: railsでgraphqlやってみる

m1 mac: rails6.1でbin/rails server が成功するまでの作業ログ

公式を見ながらインストールしていきます。

https://guides.rubyonrails.org/getting_started.html#installing-ruby

rubyのバージョン確認

~ takayuki$ ruby -v
ruby 2.6.8p205 (2021-07-07 revision 67951) [universal.x86_64-darwin21]

下記をみるとバージョンは大丈夫
https://guides.rubyonrails.org/getting_started.html#installing-ruby

念の為gemのバージョン

~ takayuki$ gem --version
Ignoring ffi-1.15.0 because its extensions are not built. Try: gem pristine ffi --version 1.15.0
3.0.3.1

flutterというか、podsでも登場したffi。指示通りのコマンドを打ってみる。

~ takayuki$ gem pristine ffi --version 1.15.0
Ignoring ffi-1.15.0 because its extensions are not built. Try: gem pristine ffi --version 1.15.0
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.

sudoつけ忘れ

~ takayuki$ sudo gem pristine ffi --version 1.15.0
Password:
Ignoring ffi-1.15.0 because its extensions are not built. Try: gem pristine ffi --version 1.15.0
Restoring gems to pristine condition...
Building native extensions. This could take a while...
Restored ffi-1.15.0
~ takayuki$ gem --version                    
3.0.3.1

今度は成功。ちなみにターミナルはrossetaで動かしてます。 ffiのissuesを見ても、まだrossetaがないと動かなそう

https://github.com/ffi/ffi/issues?q=is%3Aissue+is%3Aopen+m1

bundler

~ takayuki$ sudo gem install bundler
Password:
Fetching bundler-2.2.31.gem
Successfully installed bundler-2.2.31
Parsing documentation for bundler-2.2.31
Installing ri documentation for bundler-2.2.31
Done installing documentation for bundler after 2 seconds
1 gem installed

リファレンスはここ

https://bundler.io/v2.2/guides/rails.html

そして、rails newでエラー

/Library/Ruby/Gems/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/message_encryptor.rb:170:in `auth_data=': couldn't set additional authenticated data (OpenSSL::Cipher::CipherError)
    from /Library/Ruby/Gems/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/message_encryptor.rb:170:in `_encrypt'
    from /Library/Ruby/Gems/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/message_encryptor.rb:148:in `encrypt_and_sign'
    from /Library/Ruby/Gems/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/encrypted_file.rb:88:in `encrypt'
    from /Library/Ruby/Gems/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/encrypted_file.rb:61:in `write'
    from /Library/Ruby/Gems/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/encrypted_configuration.rb:29:in `write'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/lib/rails/generators/rails/credentials/credentials_generator.rb:30:in `add_credentials_file_silently'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/lib/rails/generators/rails/app/app_generator.rb:194:in `credentials'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/lib/rails/generators/app_base.rb:165:in `public_send'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/lib/rails/generators/app_base.rb:165:in `build'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/lib/rails/generators/rails/app/app_generator.rb:386:in `create_credentials'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/command.rb:27:in `run'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/invocation.rb:127:in `invoke_command'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/invocation.rb:134:in `block in invoke_all'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/invocation.rb:134:in `each'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/invocation.rb:134:in `map'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/invocation.rb:134:in `invoke_all'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/group.rb:232:in `dispatch'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/base.rb:485:in `start'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/lib/rails/commands/application/application_command.rb:26:in `perform'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/command.rb:27:in `run'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor/invocation.rb:127:in `invoke_command'
    from /Library/Ruby/Gems/2.6.0/gems/thor-1.1.0/lib/thor.rb:392:in `dispatch'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/lib/rails/command/base.rb:69:in `perform'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/lib/rails/command.rb:48:in `invoke'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/lib/rails/cli.rb:18:in `<top (required)>'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Library/Ruby/Gems/2.6.0/gems/railties-6.1.4.1/exe/rails:10:in `<top (required)>'
    from /usr/bin/rails:22:in `load'
    from /usr/bin/rails:22:in `<main>'

https://github.com/ruby/openssl/blob/5c85b4385f114400d901ed7dd89ce43489b9bceb/ext/openssl/ossl_cipher.c#L577

https://github.com/openssl/openssl/blob/0f70d6013435308ada5d0eb662b31f370b07ebd7/crypto/evp/evp_enc.c#L401

ここまで深く読んでもよくわからないので、railsのバージョンを下げることに。

sudo gem uninstall rails
sudo gem uninstall railties -v 6.1.4.1

sudo gem install rails -v 6.0.4.1

でも6.0.4.1でもだめ。

rubyのバージョンを上げる。ためrvmをインストール。

https://rvm.io/

takayuki$ sudo port install gnupg2
Password:
Error: Current platform "darwin 21" does not match expected platform "darwin 20"
Error: If you upgraded your OS, please follow the migration instructions: https://trac.macports.org/wiki/Migration
OS platform mismatch
    while executing
"mportinit ui_options global_options global_variations"
Error: /opt/local/bin/port: Failed to initialize MacPorts, OS platform mismatch

MacPorts Migration

https://trac.macports.org/wiki/Migration

  • xcodeのバージョンはApp Storeで確認したので最新のはず
  • MacPortsのMonterey v12用のpkgをダウンロード。

Reinstall your ports

~ takayuki$ port -qv installed > myports.txt
~ takayuki$ port echo requested | cut -d ' ' -f 1 | uniq > requested.txt
~ takayuki$ sudo port -f uninstall installed
Password:
--->  Deactivating openjdk8 @8u292_0
--->  Cleaning openjdk8
--->  Uninstalling openjdk8 @8u292_0
--->  Cleaning openjdk8
~ takayuki$ sudo port reclaim
--->  Checking for unnecessary unrequested ports
Found no unrequested ports without requested dependents.
--->  Checking for inactive ports
Found no inactive ports.
--->  Building list of distfiles still in use
--->  Searching for unused distfiles
Found 1 files (total 98.98 MiB) that are no longer needed and can be deleted.
[l]ist/[d]elete/[K]eep: d
Deleting...
--->  Build location: /opt/local/var/macports/build
This appears to be the first time you have run 'port reclaim'. Would you like to be reminded to run it every two weeks? [Y/n]: n
Reminders disabled. Run 'port reclaim --enable-reminders' to enable.

openjdk8しかなかった。

ファイル移動

~ takayuki$ mkdir MigratingMacPorts 
~ takayuki$ mv myports.txt MigratingMacPorts 
~ takayuki$ mv requested.txt MigratingMacPorts 

restore

~ takayuki$ cd MigratingMacPorts 
~/MigratingMacPorts takayuki$ curl --location --remote-name https://github.com/macports/macports-contrib/raw/master/restore_ports/restore_ports.tcl
chmod +x restore_ports.tcl
xattr -d com.apple.quarantine restore_ports.tcl
sudo ./restore_ports.tcl myports.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   164  100   164    0     0    518      0 --:--:-- --:--:-- --:--:--   532
100  9934  100  9934    0     0  11422      0 --:--:-- --:--:-- --:--:--     0
xattr: restore_ports.tcl: No such xattr: com.apple.quarantine
Password:
--->  Computing dependencies for openjdk8
--->  Dependencies to be installed: openjdk8-zulu
--->  Fetching archive for openjdk8-zulu
--->  Attempting to fetch openjdk8-zulu-8.58.0.13_0.darwin_21.arm64.tbz2 from https://packages.macports.org/openjdk8-zulu
--->  Attempting to fetch openjdk8-zulu-8.58.0.13_0.darwin_21.arm64.tbz2 from https://nue.de.packages.macports.org/openjdk8-zulu
--->  Attempting to fetch openjdk8-zulu-8.58.0.13_0.darwin_21.arm64.tbz2 from http://atl.us.packages.macports.org/openjdk8-zulu
--->  Fetching distfiles for openjdk8-zulu
--->  Attempting to fetch zulu8.58.0.13-ca-jdk8.0.312-macosx_aarch64.tar.gz from https://cdn.azul.com/zulu/bin/
--->  Verifying checksums for openjdk8-zulu
--->  Extracting openjdk8-zulu
--->  Configuring openjdk8-zulu
--->  Building openjdk8-zulu
--->  Staging openjdk8-zulu into destroot
Warning: openjdk8-zulu installs files outside the common directory structure.
--->  Installing openjdk8-zulu @8.58.0.13_0
--->  Activating openjdk8-zulu @8.58.0.13_0
--->  Cleaning openjdk8-zulu
--->  Fetching archive for openjdk8
--->  Attempting to fetch openjdk8-8u302_0.darwin_21.arm64.tbz2 from https://packages.macports.org/openjdk8
--->  Attempting to fetch openjdk8-8u302_0.darwin_21.arm64.tbz2 from https://nue.de.packages.macports.org/openjdk8
--->  Attempting to fetch openjdk8-8u302_0.darwin_21.arm64.tbz2 from http://atl.us.packages.macports.org/openjdk8
--->  Fetching distfiles for openjdk8
--->  Verifying checksums for openjdk8
--->  Extracting openjdk8
--->  Configuring openjdk8
--->  Building openjdk8
--->  Staging openjdk8 into destroot
--->  Installing openjdk8 @8u302_0
--->  Activating openjdk8 @8u302_0
--->  Cleaning openjdk8
sudo port unsetrequested installed
xargs sudo port setrequested < requested.txt

sudo port install gnupg2ログが長いので割愛

下記の手順に戻って、上から実行。bashのままでも実行できる
https://rvm.io/

インストール成功

~ takayuki$ rvm -v
rvm 1.29.12 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
~ takayuki$ rvm install 2.7.4
Searching for binary rubies, this might take some time.
No binary rubies available for: osx/12.0/x86_64/ruby-2.7.4.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for osx.
Installing requirements for osx.
Updating system - please wait
takayuki password required for 'port -dv selfupdate': 
Installing required packages: autoconf, automake, gdbm, libtool, libyaml, pkgconfig, openssl - please wait
There were package installation errors, make sure to read the log.
Error running 'requirements_osx_port_libs_install autoconf automake gdbm libtool libyaml pkgconfig openssl',
please read /Users/takayuki/.rvm/log/1637541408_ruby-2.7.4/package_install_autoconf_automake_gdbm_libtool_libyaml_pkgconfig_openssl.log
Requirements installation failed with status: 1.
Error: Failed to build openssl3: command execution failed
148 Error: See /opt/local/var/macports/logs/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_devel_openssl3/openssl3/main.log for details.
149 Error: rev-upgrade failed: Error rebuilding openssl3
150 Error: Follow https://guide.macports.org/#project.tickets if you believe there is a bug.
5188 :error:build See /opt/local/var/macports/logs/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_devel_openssl3/openssl3/main.log for details.

ログを見てもよくわからないので、今度はportでruby27をインストール

sudo port select --set ruby ruby27

ターミナル再起動

~ takayuki$ ruby -v
ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x86_64-darwin21]

改めてrailsインストール。古いバージョンがある場合は、ターミナルを再起動

~ takayuki$ rails -v
Rails 6.1.4.1

sqlite3関連でエラーが出たのでsudo port install sqlite3でインストールしたけど、 develも足りない。

portでrb-sqlite3というのがあるのでインストールしてみることに。 ruby18もインストールされたけど、それは無視して、再度bundle installするとsqlite3のgemのインストールに 成功した。

なので、bin/rails serverを実行すると...

/opt/local/lib/ruby2.7/gems/2.7.0/gems/webpacker-5.4.3/lib/webpacker/configuration.rb:103:in `rescue in load': Webpacker configuration file not found /Users/takayuki/github/rails6_1-practice/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_check_realpath_internal - /Users/takayuki/github/rails6_1-practice/config/webpacker.yml (RuntimeError)
~/github/rails6_1-practice takayuki$ rails webpacker:install
Yarn not installed. Please download and install Yarn from https://yarnpkg.com/lang/en/docs/install/
Exiting!
~/github/rails6_1-practice takayuki$ npm install --global yarn

さらにrails webpacker:install。これでようやくbin/rails serverが成功しました。