経理からエンジニア転向した人のメモ

元経理マンがエンジニアに転向して現在

vagrant 2.1.5にアップデートしたら動かなくなったのを修正した

Vagrantをアップデートしたら動かなくなった。

VirtualBoxkusanagiRuby on Railsの勉強でもしようと思って 不用意にもvagrantを2.1.1から2.1.5にアップデートして、いざvagrant upを実行したら以下のエラーが出た。

$ vagrant up
Vagrant failed to initialize at a very early stage:

The plugins failed to initialize correctly. This may be due to manual
modifications made within the Vagrant home directory. Vagrant can
attempt to automatically correct this issue by running:

  vagrant plugin repair

If Vagrant was recently updated, this error may be due to incompatible
versions of dependencies. To fix this problem please remove and re-install
all plugins. Vagrant can attempt to do this automatically by running:

  vagrant plugin expunge --reinstall

Or you may want to try updating the installed plugins to their latest
versions:

  vagrant plugin update

Error message given during initialization: Unable to resolve dependency: user requested 'vagrant (= 2.1.1)'

あまり何も考えずにvagrant plugin expunge --reinstall

何故真ん中のコマンドを実行したのかわからないが、

vagrant plugin expunge --reinstall

This command permanently deletes all currently installed user plugins. It
should only be used when a repair command is unable to properly fix the
system.

Continue? [N]: y

All user installed plugins have been removed from this Vagrant environment!

Vagrant will now attempt to reinstall user plugins that were removed.
Installing the 'sahara' plugin. This can take a few minutes...
Fetching: open4-1.3.4.gem (100%)
Fetching: Platform-0.4.1.gem (100%)
Fetching: popen4-0.1.2.gem (100%)
Fetching: sahara-0.0.17.gem (100%)
Fetching: micromachine-2.0.0.gem (100%)
Fetching: vagrant-vbguest-0.16.0.gem (100%)
Bundler, the underlying system Vagrant uses to install plugins,
reported an error. The error is shown below. These errors are usually
caused by misconfigured plugin installations or transient network
issues. The error from Bundler is:

Unable to resolve dependency: user requested 'vagrant (= 2.1.1)'

なんだこれは。となって色々ググっても みんなこのコマンドで直っていて、ここでエラー出ている人が見つからなかった。

その後も3つあるコマンドを色々繰り返しても同じようなエラーが出る。

しまいにはさっきと違うエラーが出たので結構やばいと思った。

$  vagrant plugin expunge --reinstall

This command permanently deletes all currently installed user plugins. It
should only be used when a repair command is unable to properly fix the
system.

Continue? [N]: 

Vagrant expunge has been declined. Skipping removal of plugins.

Vagrant will now attempt to reinstall user plugins that were removed.
Installing the 'vagrant' plugin. This can take a few minutes...
/opt/vagrant/embedded/gems/2.1.5/gems/vagrant-2.1.5/lib/vagrant/plugin/manager.rb:156:in `install_plugin': undefined method `name' for nil:NilClass (NoMethodError)
    from /opt/vagrant/embedded/gems/2.1.5/gems/vagrant-2.1.5/plugins/commands/plugin/action/install_gem.rb:30:in `call'
    from /opt/vagrant/embedded/gems/2.1.5/gems/vagrant-2.1.5/lib/vagrant/action/warden.rb:34:in `call'
    from /opt/vagrant/embedded/gems/2.1.5/gems/vagrant-2.1.5/lib/vagrant/action/builder.rb:116:in `call'
    from /opt/vagrant/embedded/gems/2.1.5/gems/vagrant-2.1.5/lib/vagrant/action/runner.rb:66:in `block in run'
    from /opt/vagrant/embedded/gems/2.1.5/gems/vagrant-2.1.5/lib/vagrant/util/busy.rb:19:in `busy'
    from /opt/vagrant/embedded/gems/2.1.5/gems/vagrant-2.1.5/lib/vagrant/action/runner.rb:66:in `run'
    from /opt/vagrant/embedded/gems/2.1.5/gems/vagrant-2.1.5/plugins/commands/plugin/command/base.rb:14:in `action'
    from /opt/vagrant/embedded/gems/2.1.5/gems/vagrant-2.1.5/plugins/commands/plugin/command/expunge.rb:77:in `block in execute'
    from /opt/vagrant/embedded/gems/2.1.5/gems/vagrant-2.1.5/plugins/commands/plugin/command/expunge.rb:69:in `each'
    from /opt/vagrant/embedded/gems/2.1.5/gems/vagrant-2.1.5/plugins/commands/plugin/command/expunge.rb:69:in `execute'
    from /opt/vagrant/embedded/gems/2.1.5/gems/vagrant-2.1.5/plugins/commands/plugin/command/root.rb:66:in `execute'
    from /opt/vagrant/embedded/gems/2.1.5/gems/vagrant-2.1.5/lib/vagrant/cli.rb:54:in `execute'
    from /opt/vagrant/embedded/gems/2.1.5/gems/vagrant-2.1.5/lib/vagrant/environment.rb:291:in `cli'
    from /opt/vagrant/embedded/gems/2.1.5/gems/vagrant-2.1.5/bin/vagrant:164:in `<main>'

意を決して.vagrant.dディレクトリを探る。

*ここからは、たまたまうまくいっただけだと思うので、万が一があっても自己責任でお願いします。。。 自分もそこまで理解してやっているわけではございませんので、、

プラグイン周りのエラーぽかったので、plugins.jsonを見てみる。

Unable to resolve dependency: user requested 'vagrant (= 2.1.1)'
最初からこのエラーがずっと出ていたので、気になっていた。
vagrant2.1.1の設定のままになっているファイルがあるはずと。

プラグインのreinstallやらを最初からコマンドで紹介されていたので、 plugins.jsonを見つけてピンときた。

{"version":"1","installed":{"vagrant":{"ruby_version":"2.4.4","vagrant_version":"2.1.1","gem_version":"","require":"","sources":["https://rubygems.org/","https://gems.hashicorp.com/"],"installed_gem_version":"2.1.1"}}}

ここに2.1.1の記述があるので、2.1.5に変更してやってみた、

{"version":"1","installed":{"vagrant":{"ruby_version":"2.4.4","vagrant_version":"2.1.5","gem_version":"","require":"","sources":["https://rubygems.org/","https://gems.hashicorp.com/"],"installed_gem_version":"2.1.5"}}}

そしてvagrant upしたら

 $ vagrant up

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'yuya_tajima/kusanagi'...
==> default: Matching MAC address for NAT networking...

うごいた!!

ディレクトリ内を見ても他に2.1.1の設定がなさそうだったので、解決したぽい。

エラー時に意味も理解しないで不用意にコマンドうちまくるとなかなか苦労します。。

CentOS7にrbenvでRubyをインストールする

Gitのインストール

// 入っているか確認する
$ git --version
-bash: git: command not found


// gitのインストール
$ sudo yum -y install git

rbenvのインストール

(rbenv)https://github.com/rbenv/rbenvとは、rubyバージョンを簡単に切り替えてくれる。 複数のバージョンを管理できるツール。

// git cloneする
$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv

// pathを通す
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile

// 初期化する
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

// シェルの再起動をして内容を反映
$ exec $SHELL -l

$ rbenv --version
rbenv 1.1.1-39-g59785f6

rbenvはインストールできた。

ruby-buildをclone

rbenvのプラグインだそうで。 こちらの記事が詳しく書いてある。

rbenv + ruby-build はどうやって動いているのか - takatoshiono's blog

$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

rubyが使えるようになるまで

rbenvでインストールできるリストを見る。 指定すれば複数のrubyバージョンをインストールできる。

$ rbenv install --list
Available versions:
...
(略)

// Ruby 2.5.0の安定版をインストール
$ rbenv install 2.5.0-rc1
Downloading ruby-2.5.0-rc1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.0-rc1.tar.bz2
Installing ruby-2.5.0-rc1...

BUILD FAILED (CentOS Linux 7 using ruby-build 20180822-6-g44f0f53)

Inspect or clean up the working tree at /tmp/ruby-build.20180904003149.6259
Results logged to /tmp/ruby-build.20180904003149.6259.log

Last 10 log lines:
installing rdoc:                    /home/vagrant/.rbenv/versions/2.5.0-rc1/share/ri/2.5.0/system
installing capi-docs:               /home/vagrant/.rbenv/versions/2.5.0-rc1/share/doc/ruby
The Ruby readline extension was not compiled.
ERROR: Ruby install aborted due to missing extensions
Try running `yum install -y readline-devel` to fetch missing dependencies.

Configure options used:
  --prefix=/home/vagrant/.rbenv/versions/2.5.0-rc1
  LDFLAGS=-L/home/vagrant/.rbenv/versions/2.5.0-rc1/lib 
  CPPFLAGS=-I/home/vagrant/.rbenv/versions/2.5.0-rc1/include 

失敗した。

以下に記載してあるように、エラーを見てみると、、、yum install -y readline-develしろって書いてある。 他のパッケージとの依存関係でエラーになったぽい。

ERROR: Ruby install aborted due to missing extensions
Try running `yum install -y readline-devel` to fetch missing dependencies.

試してみる。

$ sudo yum install -y readline-devel

もう一度トライ

$ rbenv install 2.5.0-rc1
Downloading ruby-2.5.0-rc1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.0-rc1.tar.bz2
Installing ruby-2.5.0-rc1...
Installed ruby-2.5.0-rc1 to /home/vagrant/.rbenv/versions/2.5.0-rc1

できた!

$  rbenv versions
  2.5.0-rc1

$ rbenv global 2.5.0-rc1

$ ruby -v
ruby 2.5.0rc1 (2017-12-14 trunk 61243) [x86_64-linux]

rbenv globalで複数のバージョンを指定している時に使いたいバージョンを指定できる。

Docker体験してみる

最近の開発環境事情でDockerという言葉をよく聞く。 しかし、Vagrant(VirtualBox)と何が違うかよくわからないので、さくらのナレッジを参考に実際に使ってみる。

knowledge.sakura.ad.jp

OSごと仮想化(ゲストOSとして稼働)しているのがVagrant(VirtualBox)、 ホストOSの上に複数のコンテナという概念の仮想環境を使えるのがDockerということだそうだ。

今回はCentOS7のVM環境を使って、Dockerを導入してみる。

VM目線でいうとMacがホストOSでCentOSがゲストOS Docker目線でいうとCentOSがホストOS(Macは関係なし)

まずはVagrantで起動

いつもどおりcentos/7でinitし、起動する。 詳しいことは割愛

導入

まずは、Dockerインストールのための準備

# yum yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

yumリポジトリの追加

# yum-config-manager \
> --add-repo \
> https://download.docker.com/linux/centos/docker-ce.repo

Loaded plugins: fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

入っているか確認

# ll /etc/yum.repos.d/
total 36
(略)

-rw-r--r--. 1 root root 2424 Sep  2 09:22 docker-ce.repo

Docker CEをインストール

# yum install docker-ce

ちなみに

  • Docker CEとは

    • Docker Community Edition (無償版)
  • Docker EEとは

    • Docker Enterprize Edition (有償版)

と、なっている。

確認

# yum list installed | grep docker-ce

docker-ce.x86_64                18.06.1.ce-3.el7               @docker-ce-stable

入った。

Dockerサービスの起動

# systemctl status docker

● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: https://docs.docker.com


# systemctl start docker

# systemctl enable docker

Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2018-09-03 07:23:24 UTC; 7s ago
     Docs: https://docs.docker.com
 Main PID: 2629 (dockerd)
   CGroup: /system.slice/docker.service
           ├─2629 /usr/bin/dockerd
           └─2635 docker-containerd --config /var/run/docker/containerd/containerd.toml

手動で起動して、次回から自動で起動するようにも設定した。

Dockerの情報をみる

当然、何も動いていない。

# docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 18.06.1-ce
Storage Driver: overlay2
 Backing Filesystem: xfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 468a545b9edcd5932818eb9de8e72413e616e86e
runc version: 69663f0bd4b60df09991c08812a60108003fa340
init version: fec3683
Security Options:
 seccomp
  Profile: default
Kernel Version: 3.10.0-862.2.3.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 487.7MiB
Name: localhost.localdomain
ID: 6FFH:V7Y2:DT4N:6OWT:Y65K:VRPV:MQ2R:FT7R:HHKS:GY5E:VEWK:7I2L
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled

基本操作

イメージの確認

# docker images

imageは(Docker Hub)https://hub.docker.com/explore/に上がっているので、 そこからイメージをとってくる。 この辺はvagrantと似ている。

nginxをDockerで動かしてみる。

nginxを取得/起動

# docker pull nginx

Using default tag: latest
latest: Pulling from library/nginx
be8881be8156: Pull complete 
65206e5c5e2d: Pull complete 
8e029c3e2376: Pull complete 
Digest: sha256:1b109555ad28bb5ec429422ee136c5f5ab5ee6faaeb518836a5c9a3b6436a1bd
Status: Downloaded newer image for nginx:latest

# docker run -d --name nginx-container -p 8181:80 nginx

63e357140ef5078fe165736f06617b5da313f0e738d2344434faf3a0c5c2a7f7
  • -dはバックグラウンドで起動
  • --nameでコンテナの指定
  • -p でポートの指定。
    • ホストとコンテナ間のポートフォワード設定

確認

ブラウザにインスタンスIPアドレスとポート番号(8181)を打ち込めば 「Welcome〜」とブラウザ上で表示される。

nginxも動いているのが確認できる。

# docker ps -a

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
63e357140ef5        nginx               "nginx -g 'daemon of…"   6 minutes ago       Up 6 minutes        0.0.0.0:8181->80/tcp   nginx-container

止める

# docker stop nginx-container

nginx-container

# docker ps -a

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
63e357140ef5        nginx               "nginx -g 'daemon of…"   7 minutes ago       Exited (0) 3 seconds ago                       nginx-container