カクカクしかじか

技術的なアレコレ

mrubyから入るシステムプログラミング入門ハンズオン用の開発環境セットアップ

はじめに

mrubyから入るシステムプログラミング入門 に参加するための事前環境構築を行いました!
ちなみに、その手順リポジトリはこちらです。

github.com

振り返りがてら行なった内容を記します!!

※ ハンズオン当日の実施内容を記した記事はこちら

以下の記事の実施内容は本記事の VagrantとVirtualboxのインストール から下の章を全て実施した前提になりますので、なぞって動かしてみたい方はセットアップを最後まで行ってみてくださいませ!

fuqda.hatenablog.com

VagrantVirtualboxのインストール

Vagrant

macOS 64-bitを選択 www.vagrantup.com

VirtualBox 6.0.10 platform packages

OS X hostsを選択 www.virtualbox.org

ローカル(Mac)での作業

手順1 : 作業用リポジトリのチェックアウトと更新

$ git clone https://github.com/udzura/handsons.git; cd handsons
$ git pull --rebase origin master

手順2 : Vagrant経由で開発マシンを立ち上げる

プラグインのインストール

~/handsons (master|) $ vagrant plugin install vagrant-disksize
Installing the 'vagrant-disksize' plugin. This can take a few minutes...
Fetching: vagrant-disksize-0.1.3.gem (100%)
Installed the plugin 'vagrant-disksize (0.1.3)'! 

vagrant up(vagrant 起動)

(vagrant updocker-compose up と同じイメージかな?)

~/handsons (master|) $ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'ubuntu/bionic64' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'ubuntu/bionic64'
    default: URL: https://vagrantcloud.com/ubuntu/bionic64
==> default: Adding box 'ubuntu/bionic64' (v20190718.0.0) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/ubuntu/boxes/bionic64/versions/20190718.0.0/providers/virtualbox.box
    default: Download redirected to host: cloud-images.ubuntu.com
==> default: Successfully added box 'ubuntu/bionic64' (v20190718.0.0) for 'virtualbox'!
==> default: Importing base box 'ubuntu/bionic64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/bionic64' version '20190718.0.0' is up to date...
==> default: Setting the name of the VM: handsons_default_1563600151302_27213
Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening
on these ports. The forwarded port to 8080 is already in use
on the host machine.

To fix this, modify your current project's Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:

  config.vm.network :forwarded_port, guest: 80, host: 1234

Sometimes, Vagrant will attempt to auto-correct this for you. In this
case, Vagrant was unable to. This is usually because the guest machine
is in a state which doesn't allow modifying port forwarding. You could
try 'vagrant reload' (equivalent of running a halt followed by an up)
so vagrant can attempt to auto-correct this upon booting. Be warned
that any unsaved work might be lost.

エラーが出た...

The forwarded port to 8080 is already in use on the host machine.

何やらVagrantで指定したポートがすでに使われてる様子...

何にポートを使われてるのか調べる場合

lsofコマンドを使う

(lsof -i:ポート番号 で検索)

~/handsons (maste|) $ lsof -i:8080
~/handsons (maste|✔) $

あれ... プロセスが見つからない...

かくなる上は... Macネットワークユーティリティ から使用されているポートを調べることにしてみます!

オープンポートをスキャンするインターネットアドレスを入力してください。 の部分に自身のIPを表す 127.0.0.1 を入力します! (127.0.0.1 はローカル・ループ・バックアドレスとも呼ばれます)

image.png

何やら Open TCP Port: 8080 http-alt という記述を発見しました。 http-alt ってプロセスが使用してるっぽい...
なんか殺しちゃダメそう?
わからんので、後述の方法で対応することにしました...
(時間があるときに調べよう!)

エラー解決

Vagrantfile で指定しているportを 8090 に変更!
(Vagrantfile 自体を変更する苦肉の策)

-config.vm.network "forwarded_port", guest: 80, host: 8080
+config.vm.network "forwarded_port", guest: 80, host: 8090

これで改めて vagrant upしたらイケました!

Vagrantsshする

ローカル(Mac)側

$ vagrant ssh

Vagrant

共有ディレクトリができてることを確認

vagrant@ubuntu-bionic:~$ ls -l /vagrant
total 60
-rw-r--r-- 1 vagrant vagrant  1068 Jul 20 05:03 LICENSE
-rw-r--r-- 1 vagrant vagrant   428 Jul 20 05:03 README.md
-rw-r--r-- 1 vagrant vagrant  1098 Jul 20 06:44 Vagrantfile
drwxr-xr-x 1 vagrant vagrant   192 Jul 20 05:03 handsons
drwxr-xr-x 1 vagrant vagrant   352 Jul 20 05:03 mruby-loadavg
drwxr-xr-x 1 vagrant vagrant   384 Jul 20 05:03 mruby-myuname
drwxr-xr-x 1 vagrant vagrant   256 Jul 20 05:03 ngx_mruby
-rw------- 1 vagrant vagrant 45589 Jul 20 06:50 ubuntu-bionic-18.04-cloudimg-console.log
drwxr-xr-x 1 vagrant vagrant   128 Jul 20 07:03 workspace

mrbgem-template コマンドをインストール

~/handsons (master|) $ brew tap mrbgems/mrbgem-template
~/handsons (master|) $ brew install mrbgem-template
~/handsons (master|✔) $ mrbgem-template --help
Usage: mrbgem-template [options] mrbgem_name
    -h, --help                       Show usage
    -v, --version                    Show version
    -l, --license [LICENSE]          Set license
    -u, --github-user [USER]         Set user name on github
    -p, --mrbgem-prexif [PREFIX]     Set prefix dir to mgem project
    -c, --class-name [CLASS]         Set class name
    -a, --author [AUTHOR]            Set the author of this mgem
    -b, --[no-]local-builder         Enable or disable local builder
    -C, --[no-]ci                    Enable or disable CI by travis

mruby 自体のビルドができるかVagrant上で確認

vagrant@ubuntu-bionic:~$ cd /vagrant/workspace
vagrant@ubuntu-bionic:/vagrant/workspace$ git clone https://github.com/mruby/mruby.git
vagrant@ubuntu-bionic:/vagrant/workspace$ cd mruby
vagrant@ubuntu-bionic:/vagrant/workspace/mruby$ make

mruby のバージョン確認とコード実行

vagrant@ubuntu-bionic:/vagrant/workspace/mruby$ ./bin/mruby --version
mruby 2.0.1 (2019-04-04)
vagrant@ubuntu-bionic:/vagrant/workspace/mruby$ ./bin/mruby -e "puts 'Hello, systems in depth'"
Hello, systems in depth

mrubyの立ち上げ

vagrant@ubuntu-bionic:/vagrant/workspace/mruby$ ./bin/mirb
mirb - Embeddable Interactive Ruby Shell

> puts 'Hello, systems in depth!'
Hello, systems in depth!
 => nil
> exit

イケたっぽいです!
これにてセットアップ完了!!

最後に

ポート番号被ってる!ってなったときに ポート番号からプロセスを調べるコマンド ってなんだっけ?ってなってしまったので、普段からこういうLinuxコマンドの類は常識的な範囲で抑えておかねば...と反省した次第です。
明日のハンズオンでも疑問点など細かにメモしておいて、この機会に血肉にしていければと思います!(楽しみだ)

参考URL

qiita.com

qiita.com

teratail.com

qiita.com

qiita.com

qiita.com

joppot.info