[[Mastodonを読む]]
 
 #contents
 
 *はじめに [#edb34c21]
 
 コードを読む際に、「よくわからん」ということはよくあります。その場合は実際に動かしてみてその結果から動作を確認するのが一番です。
 コマンドラインのプログラムなら簡単に動かせますが、Webアプリとなると依存するソフトも多く動作環境を構築するのも手間がかかります。
 
 最近では[[DevOps>https://ja.wikipedia.org/wiki/DevOps]]という考え方が広まっており、開発、運用の環境を簡単に構築できるようになっています。Mastodonでも、運用環境にはDocker、開発にはVagrantが利用されています。
 今回は読解の準備として[[ドキュメント>https://github.com/tootsuite/documentation/blob/master/Running-Mastodon/Vagrant-guide.md]]に従い、Vagrantを用いた開発環境の構築を行います。なお、私の環境(32ビットWindows)に限定した話になっています。
 
 というわけで、以降は読解はありません。
 
 *VagrantとVirtualBoxのインストール [#veea66e7]
 
 まず、[[Vagrant>https://www.vagrantup.com/]]と[[VirtualBox>https://www.virtualbox.org/]]をインストールします。Windowsの場合、これらは本当にインストーラを動かすだけでした。
 
 *vagrant-hostsupdaterのインストール(SSLエラー×2) [#ea4dd25f]
 
 ドキュメントに従ってVagrantのプラグイン入れようとしたらエラーになりました。
 
  C:\Users\junjis>vagrant plugin install vagrant-hostsupdater
  Installing the 'vagrant-hostsupdater' plugin. This can take a few minutes...
  Vagrant failed to load a configured plugin source. This can be caused
  by a variety of issues including: transient connectivity issues, proxy
  filtering rejecting access to a configured plugin source, or a configured
  plugin source not responding correctly. Please review the error message
  below to help resolve the issue:
  
    SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (https://api.rubygems.org/specs.4.8.gz)
  
  Source: https://rubygems.org/
 
 「SSL_connect~」をそのまま検索してみたところ、証明書が更新されたのが問題の様子。[[「Windows環境でgemがSSLエラー 2016」>http://qiita.com/betrue12/items/059c2424f27fd31969f9]]を参考に更新を試みる。
 
  C:\Users\junjis>cd C:\HashiCorp\Vagrant\embedded\bin
  C:\HashiCorp\Vagrant\embedded\bin>gem install rubygems-update --source http://rubygems.org/
  Fetching: rubygems-update-2.6.11.gem (100%)
  ERROR:  While executing gem ... (Errno::EACCES)
      Permission denied @ rb_sysopen - C:/HashiCorp/Vagrant/embedded/lib/ruby/gems/2.2.0/cache/rubygems-update-2.6.11.gem
 ぐふっ、と思ったらコマンドプロンプトを管理者で開いてないからか。管理者で開いてやり直し、
 
  C:\HashiCorp\Vagrant\embedded\bin>gem install rubygems-update --source http://rubygems.org/
  Fetching: rubygems-update-2.6.11.gem (100%)
  Successfully installed rubygems-update-2.6.11
  Parsing documentation for rubygems-update-2.6.11
  Installing ri documentation for rubygems-update-2.6.11
  Done installing documentation for rubygems-update after 210 seconds
  WARNING:  Unable to pull data from 'https://rubygems.org/': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (https://api.rubygems.org/specs.4.8.gz)
  1 gem installed
  C:\HashiCorp\Vagrant\embedded\bin>update_rubygems
 
 プラグインインストールリトライ、
 
  C:\Users\junjis>vagrant plugin install vagrant-hostsupdater
  Installing the 'vagrant-hostsupdater' plugin. This can take a few minutes...
  ERROR:  SSL verification error at depth 3: unable to get local issuer certificate (20)
  ERROR:  You must add /C=US/O=Starfield Technologies, Inc./OU=Starfield Class 2 Certification Authority to your local trusted store
  Vagrant failed to load a configured plugin source. This can be caused
  by a variety of issues including: transient connectivity issues, proxy
  filtering rejecting access to a configured plugin source, or a configured
  plugin source not responding correctly. Please review the error message
  below to help resolve the issue:
  
    SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (https://gems.hashicorp.com/specs.4.8.gz)
  
  Source: https://gems.hashicorp.com/
 
 うーむ。
 
 [[「Windows上のRubyでSSL接続時にcertificate verify failedが出る場合の対処」>http://qiita.com/whiteleaf7@github/items/4504b208ad2eec1f9357]]を参考に、cacert.pemダウンロードして、
 
  C:\Users\junjis>set SSL_CERT_FILE=Downloads/cacert.pem
  C:\Users\junjis>vagrant plugin install vagrant-hostsupdater
  Installing the 'vagrant-hostsupdater' plugin. This can take a few minutes...
  Fetching: vagrant-hostsupdater-1.0.2.gem (100%)
  Installed the plugin 'vagrant-hostsupdater (1.0.2)'!
 
 入った?hostsupdater見当たらんぞ??と思ったら、ホーム以下に「.vagrant.d」ディレクトリができててそこにあった。
 
 ちなみに、後述の「vagrant upがうまくいかない」試行錯誤で別のデスクトップで試した時はSSLエラーにならずにすんなりインストールできました。なんでだろ。
 
 *Mastodonのチェックアウト [#m3d46e4d]
 
 gitは[[Git for Windows>https://git-for-windows.github.io/]]を使います。でまずは、
 
  D:\src>git clone https://github.com/tootsuite/mastodon.git
  D:\src>cd mastodon
 
 ドライブ移ってるのはマシンの都合で深い意味はありません。ちなみに、VMはデフォルトではCドライブのホーム以下にできるようなのでそれもDドライブに置くように変更。
 
 このままmaster読んでもいいですが、読解なのでバージョン付けされたものにしといたほうがいいでしょう。bashならtail使えますがコマンドプロンプトでやってるので、
 
  D:\src\mastodon>git tag
  省略
  v1.2.2
  v1.3
  v1.3.1
  v1.3.2
  D:\src\mastodon>git checkout v1.3.2
 
 ちなみに、4/27にプレ調査でcloneしたときは1.2.2が最新でしたが、5/1時点で1.3.2になってます。のんびり読んでるともうなくなってしまったコード読んでてしまう危険性(笑)
 
 *Vagrantの実行(不具合と対応) [#l0c5123e]
 
 それではVagrantを実行し、開発環境を立ち上げます。
 
  D:\src\mastodon>vagrant up
  Bringing machine 'default' up with 'virtualbox' provider...
  ==> default: Box 'ubuntu/trusty64' could not be found. Attempting to find and install...
      default: Box Provider: virtualbox
      default: Box Version: >= 0
  ==> default: Loading metadata for box 'ubuntu/trusty64'
      default: URL: https://atlas.hashicorp.com/ubuntu/trusty64
  ==> default: Adding box 'ubuntu/trusty64' (v20170422.0.0) for provider: virtualbox
      default: Downloading: https://atlas.hashicorp.com/ubuntu/boxes/trusty64/versions/20170422.0.0/providers/virtualbox.box
      default: Progress: 100% (Rate: 2485k/s, Estimated time remaining: --:--:--)
  ==> default: Successfully added box 'ubuntu/trusty64' (v20170422.0.0) for 'virtualbox'!
  ==> default: Importing base box 'ubuntu/trusty64'...
  ==> default: Matching MAC address for NAT networking...
  ==> default: Checking if box 'ubuntu/trusty64' is up to date...
  ==> default: Setting the name of the VM: mastodon
  ==> default: Clearing any previously set forwarded ports...
  ==> default: Clearing any previously set network interfaces...
  ==> default: Preparing network interfaces based on configuration...
      default: Adapter 1: nat
      default: Adapter 2: hostonly
  ==> default: Forwarding ports...
      default: 80 (guest) => 3000 (host) (adapter 1)
      default: 22 (guest) => 2222 (host) (adapter 1)
  ==> default: Running 'pre-boot' VM customizations...
  ==> default: Booting VM...
  ==> default: Waiting for machine to boot. This may take a few minutes...
      default: SSH address: 127.0.0.1:2222
      default: SSH username: vagrant
      default: SSH auth method: private key
      default: Warning: Connection reset. Retrying...
  ==> default: Forcing shutdown of VM...
  ==> default: Destroying VM and associated drives...
  C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/transport/server_version.rb:54:in `readpartial': An established connection was aborted by the software in your host machine. (Errno::ECONNABORTED)
          以下省略
 
 うーん、と、管理者モードで試してみても駄目。32ビットだから64ビットOS実行できないかとVagrantfileをtrusty32に変えてみても駄目でした。
 
 [[「vagrant up起動に失敗したら」>http://qiita.com/amateurgrammer/items/21e4456b8318db877f5b]]を参考にGUIを表示してみる。すると、起動している最中にForcing shutdownされている雰囲気(なぜかGUI表示するとConnection resetは出力されない)
 
 例外が起きているところを見ても普通にSSHのネゴシエーションをしているだけなので「vagrant up readpartial」で検索してみたところ、[[「Vagrant up forces shutdown of VM」>https://github.com/mitchellh/vagrant/issues/8520]]というIssueが引っ掛かりました。Issueによると同様の事態が発生している人は多くいて、「不具合修正はした、1.9.5には反映する」ということでIssueは閉じられてました。
 
 で、今すぐ直したい場合は自分でファイルを置き換えてねとのことなので、[[joelhandwellさんのコメント>https://github.com/mitchellh/vagrant/issues/8520#issuecomment-297792410]]に従い、en.yml, errors.rb, communicator.rbをダウンロードして
 
 -C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/templates/locales/en.yml
 -C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/lib/vagrant/errors.rb
 -C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/plugins/communicators/ssh/communicator.rb
 
 の3ファイルをそれぞれ置き換え。
 
 では改めて「vagrant up」。おぉ進んだ・・・と順調なところでまたエラー。
 
  D:\src\mastodon>vagrant up
          これより前スクロールアウト
          from C:/Users/junjis/.vagrant.d/gems/2.2.5/gems/vagrant-hostsupdater-1.0.2/lib/vagrant-hostsupdater/Action/UpdateHosts.rb:17:in `call'
          以下省略
 
 スタックトレースがコマンドラインバッファ越えるぐらい出ましたが、一番上を見るとhostsupdaterとあるので、管理者権限で動かさないといけないのかなーとやり直す。
 
  D:\src\mastodon>vagrant up
  省略
  ==> default: Machine booted and ready!
  ==> default: Checking for guest additions in VM...
      default: The guest additions on this VM do not match the installed version of
      default: VirtualBox! In most cases this is fine, but in rare cases it can
      default: prevent things such as shared folders from working properly. If you see
      default: shared folder errors, please make sure the guest additions within the
      default: virtual machine match the version of VirtualBox you have installed on
      default: your host and reload your VM.
      default:
      default: Guest Additions Version: 4.3.36
      default: VirtualBox Version: 5.1
  ==> default: [vagrant-hostsupdater] Checking for host entries
  ==> default: [vagrant-hostsupdater] Writing the following entries to (C:/WINDOWS/system32/drivers/etc/hosts)
  ==> default: [vagrant-hostsupdater]   192.168.42.42  mastodon.dev  # VAGRANT: 1399fc42910a21732845271202098047 (default) / 1c052e7a-159a-47a3-b5ad-b2a882f9d2a7
  ==> default: [vagrant-hostsupdater] This operation requires administrative access. You may skip it by manually adding equivalent entries to the hosts file.
  ==> default: Setting hostname...
  ==> default: Configuring and enabling network interfaces...
  ==> default: Mounting shared folders...
      default: /vagrant => D:/src/mastodon
  Vagrant was unable to mount VirtualBox shared folders. This is usually
  because the filesystem "vboxsf" is not available. This filesystem is
  made available via the VirtualBox Guest Additions and kernel module.
  Please verify that these guest additions are properly installed in the
  guest. This is not a bug in Vagrant and is usually caused by a faulty
  Vagrant box. For context, the command attempted was:
  
  mount -t vboxsf -o rw,vers=3,tcp,uid=1000,gid=1000 vagrant /vagrant
  
  The error output from the command was:
  
    fmask     =<arg> umask of regular files
 
 ぐふ。まあ初めの方で言われているようにGuest Additionsのバージョンが古いせいでしょう。
 幸い?GUI表示しているのでGuest Additionsを更新しようと思ったら光学ドライブがないマシン設定のため挿入できませんでした・・・
 
 とりあえず「vagrant halt」して止めておく。
 
 *Vagrant実行(N度目の正直) [#b696badd]
 
 Guest Additions更新する方法を考えてみたのですがとりあえず「mastodon vagrant」で検索したところ、[[「Windowsホストのvagrantでmastodonを動かす」>https://argrath.ub32.org/annex/2017/04/24-01.html]]を見つけました。Guest Additions更新しても結局、シンボリックリンク張るとこでエラーになるらしいのでgistに置いていただいているVagrantfileを拾って置き換え。
 
 「vagrant up」したところ、上でVM作られているのにまた作ろうとしてエラー(リネームエラー)。「vagrant destroy」するとリネーム前の今作られたVMが消され、もう一度「vagrant destroy」しても前からあるmastodon VMは削除されないので手動で削除。
 
 で改めて「vagrant up」→SSH接続待ち中にinvalid stateになったというエラー。「vagrant halt」→「vagrant destroy」する。VMのディレクトリが削除されない?なんでだろ。
 
 VagrantfileでGUIを表示するようにして、ついでにプロビジョニングもさっきホストで固定したバージョンを使うように「git checkout」を追加。「vagrant up」・・・、そうか、マウント設定消しとかないと同じエラーになるか。「vagrant halt」→「vagrant destroy」
 
 というわけで何度目かわからないけど「vagrant up」、おープロビジョニングに入ったー、Rubyのビルドがすっげー時間かかってるー。ホストマシンの問題でビルド系にかなり時間がかかるみたいですね。プロビジョニング終わるまでに1時間ぐらいかかりました。
 
 http://mastodon.devにアクセスして、admin@mastodon.devでログイン!・・・できないと思ったら、管理ユーザが「admin@localhost:3000」で作られてました。なんでだろ、GUI開いてるので、vagrant/vagrantでログインして、mastodonディレクトリに移動、
 
  $ rails c
  > u = User.first
  > u.email = 'admin@mastodon.dev'
  > u.save
 
 で修正。これでようやくログイン、トゥートすることができました。長かった。
 
 

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS