LoginSignup
4
1

More than 3 years have passed since last update.

mastodonの開発環境をVagrantで立てる

Posted at

普段からイカちゃんのmastodonにお世話になっているのですが、勉強がてら開発環境を立てようと思ったところ、英語のdocumentがよく分からなかったり、日本語だと古かったり、WindowsだったりMacだったりで大変苦労しました…。
備忘録的に残しておきます。
あと誰か仕事下さい。

環境

  • MacOSX Catalina 10.15.6
  • VirtualBox 6.1.8 r137981 (Qt5.6.3)
  • Vagrant 2.2.7

方針

大まかな手順は以下の通りになります。
順次やっていきましょう。

  1. mastodonのrepositoryをgit cloneしてくる。
  2. vagrant upを叩く。
  3. 仮想環境が上がることを確認。
  4. ホストからvagrant ssh -c ~を叩いてサービスを起動。
  5. ホストからlocalhost:3000アクセスして応答があることを確認

手順

やり方を探す

まずここでハマりました。開発環境の立ち上げ方やベストプラクティス、お手軽な方法が全く分からん。そもそもVagrantで良いのか?
まず、mastodonのsourceはこちらです。

それで、別repositoryにマニュアルが上がっています。

readmeのリンクによると、ここで参照してねとなっているので、Webのdocumentにたどり着けました。

開発環境の種類

開発環境に関するページを掘り進めたところ、Technical overviewに突き当たります。
以下要約です。

  • ベストなのは仮想環境ではなく、すべての依存関係を自分のシステムにインストールすること
  • 配布されているDockerは本場環境用にチューニングしてるよ
  • 逆にVagrantは開発用で本番用ではないよ

つまりUbuntu 18.04を用意して諸々入れ込むのが一番良い…推奨している…と思って、お世話になっているadminに聞いたところ、「(Macの)Vagrantで開発環境立ててる」という力強いお返事を頂いたので、開発環境はVagrantでやっていくことにします。

Vagrantfile

cloneしてきたrepositoryのrootに、公式側でチューニングされた開発用のVagrantfileがあります。

The default value of RAILS_ENV is development, so you don’t need to set anything extra to run Mastodon in development mode.

公式のdocumentによると、開発用がデフォルトだからどこも触る必要が無いらしいです。
尚、私はRubyに関する知見は完全にゼロです。
結論から言うと、最終的に何も触る必要はありませんでしたが、このあとハマります。

取り敢えず実行してみる

Vagrantfileはそのままで良いとのことで、vagrant upを実行します。
このpluginは入れとけみたいに漏れなく書いてあったので、ついでに入れておきます。

console.log
$ vagrant plugin install vagrant-hostsupdater
$ vagrant up

そうすると、なんやかんやwarningが出つつ、以下のように戻ってきます。

console.log
==> default: Running provisioner: shell...
    default: Running: inline script
    default: To start server
    default:   $ vagrant ssh -c "cd /vagrant && foreman start"

suzukiyuuki at suzuki-mbp2015e in ~/work/suzuki84g/mastodon/mastodon (feature/ikadon●●)
$ 

vagrant ssh -c "cd /vagrant && foreman start"を実行するとサーバーがスタートするとのことです。
vagrant sshはvagrantの仮想マシンに対するssh接続、-cオプションはコンソールそのままでコマンドだけ実行、"cd /vagrant && foreman start"/vagrantディレクトリに移動してforeman startを実行する、です。
叩いてみましょう。

console.log
$ vagrant ssh -c "cd /vagrant && foreman start"
10:10:24 web.1     | started with pid 1756
10:10:24 sidekiq.1 | started with pid 1757
10:10:24 stream.1  | started with pid 1758
10:10:24 webpack.1 | started with pid 1759
10:10:24 web.1     | env: ‘bundle’: No such file or directory
10:10:24 sidekiq.1 | env: ‘bundle’: No such file or directory
10:10:24 web.1     | exited with code 127
10:10:24 system    | sending SIGTERM to all processes
10:10:24 sidekiq.1 | exited with code 127
10:10:25 stream.1  | terminated by SIGTERM
10:10:25 webpack.1 | terminated by SIGTERM
Connection to 127.0.0.1 closed.

動きません。話がちげーーーぞ!!!

原因の特定

ログが残ってないのですが、実はforeman start自体も弾かれました。これは構築したvagrantにforemanがインストールされていないということです。(foremanはRubyのフレームワーク的なやつらしいです)(上記ログは手動でforemanを入れた後のものです)
そして、上記ログは曰く、「web に bundle が install されてない」とのことです。
元々のVagrantfileに以下の行があるので、vagrant upが失敗しているようです。

〜中略〜
# Install gems and node modules
gem install bundler foreman
bundle install
yarn install

destroyして上げ直したり色々諸々試したところ、最終的に以下のエラーが出るようになりました。vagrantのバージョンや色々が上手く噛み合ってないっぽい…?

console.log
    default: GPG failed for the second time, please ensure network connectivity.
    default: Exiting...
The SSH command responded with a non-zero exit status. Vagrant
assumes that this means the command failed. The output for this command
should be in the log above. Please read the output to determine what
went wrong.

pluginの追加インストールで解決

色々調べたらvagrant-vbguestを入れると解決するようだったので、vagrant plugin install vagrant-vbguestでインストール、destroyしてからupしたところ、見覚えの無いログが大量に流れ、特にruby周りが色々インストールされていました。これは出来たっぽい!!
再度vagrant ssh -c "cd /vagrant && foreman start"を叩いたところ、無限にログが流れ、サーバが稼働している様子が伺えます。
ホストOSのChromeでlocalhost:3000に接続したところ…。

スクリーンショット 2020-09-18 16.50.17.png
でーきまーした!
(これはイカ鯖のrepositoryをcloneしてるのでちょっとデザインが違います)

追加のハマりポイント

  • Q. サーバを上げたけどユーザ作れないしadminのIDとpassは弾かれる
  • A. 公式documentが間違えてる

以下のコマンドでユーザ情報を確認できます。適宜確認して下さい。

console.log
*** System restart required ***
Last login: Thu Sep 17 13:24:45 2020 from 10.0.2.2
vagrant@mastodon:~$ /vagrant
-bash: /vagrant: Is a directory
vagrant@mastodon:~$
vagrant@mastodon:~$ cd /vagrant
vagrant@mastodon:/vagrant$
vagrant@mastodon:/vagrant$ rails c
Chewy console strategy is `urgent`
Loading development environment (Rails 5.2.4.2)
[1] pry(main)> User.all
  User Load (1.2ms)  SELECT "users".* FROM "users"
=> [#<User id: 1, email: "admin@mastodon.local", created_at: "2020-09-17 13:24:28", updated_at: "2020-09-17 13:24:28", admin: true, locale: nil, encrypted_otp_secret: nil, encrypted_otp_secret_iv: nil, encrypted_otp_secret_salt: nil, consumed_timestep: nil, otp_required_for_login: false, last_emailed_at: nil, otp_backup_codes: nil, filtered_languages: [], account_id: 1, disabled: false, moderator: false, invite_id: nil, chosen_languages: nil, created_by_application_id: nil, approved: true, otp_secret: nil>,

これでmastodonの検証環境が無事用意出来ました。
本題はscssの修正なので、引き続きやっていきます。
この記事がどなたかの参考になれば幸いです。

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1