わすれっぽいきみえ

みらいのじぶんにやさしくしてやる

14日目: vagrant global-statusを使う

vagrantにいくつか新しいコマンドが追加されていたことを確認していなかった。
その中で vagrant global-status を使って、他に立ち上がってるインスタンスがないか確かめた。

vagrant global-status
id       name       provider   state    directory
------------------------------------------------------------------------------------------
b2d5eae  default    virtualbox running  /Users/kimikimi714/Documents/vagrant/docker
41fd07f  web_server virtualbox poweroff /Users/kimikimi714/Documents/vagrant/centos65
4331d53  web_server virtualbox poweroff /Users/kimikimi714/Documents/vagrant/playground
0044cb1  db_server  virtualbox poweroff /Users/kimikimi714/Documents/vagrant/playground
9a905ff  host       virtualbox poweroff /Users/kimikimi714/Documents/vagrant/playground
c3fffe8  default    virtualbox running  /Users/kimikimi714/Documents/open-kamattechan-php

The above shows information about all known Vagrant environments
on this machine. This data is cached and may not be completely
up-to-date. To interact with any of the machines, you can go to
that directory and run Vagrant, or you can use the ID directly
with Vagrant commands from any directory. For example:
"vagrant destroy 1a2b3c4d"

あ、vagrantディレクトリごと潰したことを忘れてた…。

というわけで一個ずつ vagrant destroy b2d5eae などを実行したんだが、 vagrant global-status --prune でもう使われてないインスタンスをまとめて消すことができたらしい。
なのでわざとやってみる。

$ mkdir vagrant-test
$ cd vagrant-test
// とりあえずインスタンスを作る
$ vagrant init laravel/homestead
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
$ vagrant up
$ vagrant global-status
id       name    provider   state   directory
--------------------------------------------------------------------------------------
c3fffe8  default virtualbox running /Users/kimikimi714/Documents/open-kamattechan-php
f0d5fc7  default virtualbox running /Users/kimikimi714/Documents/vagrant-test

The above shows information about all known Vagrant environments
on this machine. This data is cached and may not be completely
up-to-date. To interact with any of the machines, you can go to
that directory and run Vagrant, or you can use the ID directly
with Vagrant commands from any directory. For example:
"vagrant destroy 1a2b3c4d"

// インスタンスを落とす
$ vagrant halt
// ディレクトリごと潰す
$ cd ..
$ rm -rf vagrant-test
// vagrant global-statusで残ってるか確認
$ vagrant global-status
id       name    provider   state    directory
---------------------------------------------------------------------------------------
c3fffe8  default virtualbox running  /Users/kimikimi714/Documents/open-kamattechan-php
f0d5fc7  default virtualbox poweroff /Users/kimikimi714/Documents/vagrant-test

The above shows information about all known Vagrant environments
on this machine. This data is cached and may not be completely
up-to-date. To interact with any of the machines, you can go to
that directory and run Vagrant, or you can use the ID directly
with Vagrant commands from any directory. For example:
"vagrant destroy 1a2b3c4d"
// pruneオプションを付けてみる
$ vagrant global-status --prune
id       name    provider   state   directory
--------------------------------------------------------------------------------------
c3fffe8  default virtualbox running /Users/kimikimi714/Documents/open-kamattechan-php

The above shows information about all known Vagrant environments
on this machine. This data is cached and may not be completely
up-to-date. To interact with any of the machines, you can go to
that directory and run Vagrant, or you can use the ID directly
with Vagrant commands from any directory. For example:
"vagrant destroy 1a2b3c4d"

ちゃんと消えた。 でもvagrant仮想マシンを見なくなったというだけで、仮想マシンインスタンスそのものがなくなったわけではないかもしれない。 なので仮想マシンインスタンスがどうなったかを一応確認してみた。

// prune前
ls ~/VirtualBox\ VMs
open-kamattechan-php/  vagrant-test_default_1480581091999_9417/
// prune後
ls ~/VirtualBox\ VMs
open-kamattechan-php/  vagrant-test_default_1480581091999_9417/

やはり残っていた。
あくまでもvagrant global-statusvagrant管理下にあるもののステータスを見せてくれるためのものであって、インスタンスが正常に破棄されたかどうかまでは関知しないということがわかった。なので今あるboxを確認するにはいいかもしれないが、インスタンスそのものを破棄したいなら、ちゃんと vagrant destroy を実行するのが良い。

今日はここまで。