正直ruby on windowsでいろいろするの疲れるので、VM入れてLinux入れてって思ったんだけど、設定失敗したらやるたびに最初からやり直しとか、それしたくないならゲストOSのバックアップ取っておくとか、やるまえからかなりつらみが伴うので、Vagrantってのを使えばらくできるらしいというのをネットで見たのでやっていきたい。
インストール
ここみながらやった
あとここほんと参考になる
- https://gist.github.com/voluntas/5525719
- ベースのboxはここからいろいろ選べてすごい便利
2まではまんま同じ手順
- vagrantが入ってるか確認
vagrant -v
Vagrant version 1.2.7
3は現時点の最新のバージョンを入れた
- addでboxをキャッシュ?するらしい
vagrant box add centos
http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130427.box
- 回線にもよるけどすげえ長い
- いったいどこにイメージを保存しているのか…
- あった
- いったいどこにイメージを保存しているのか…
C:\Users\username\.vagrant.d\boxes\centos\virtualbox
- add終わり
Downloading or copying the box...
Extracting box...ate: 147k/s, Estimated time remaining: --:--:--)
Successfully added box 'centos' with provider 'virtualbox'!
- initする
- Vagrantfile(設定ファイル?)を作るっぽい
vagrant init centos
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.
-
- Vagrantfile作ったから読めとか書いてるけど、どこに作られるか予め調べとかないと見ることすらできない
- Linuxなら ~/.vagrant にできるっぽい
- Vagrantfile作ったから読めとか書いてるけど、どこに作られるか予め調べとかないと見ることすらできない
- upする
- 入れたcentosを起動する
vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Mounting shared folders...
[default] -- /vagrant
- statusする
- 動いてるか確認する
vagrant status
Current machine states:
default running (virtualbox)
The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
-
- 動いてるっぽい
- vagrant haltで殺せる
- 動いてるっぽい
- sshする
- ログインできるか確認する
vagrant ssh
`ssh` executable not found in any directories in the %PATH% variable. Is an
SSH client installed? Try installing Cygwin, MinGW or Git, all of which
contain an SSH client. Or use the PuTTY SSH client with the following
authentication information shown below:
Host: 127.0.0.1
Port: 2222
Username: vagrant
Private key: C:/Users/username/.vagrant.d/insecure_private_key
login as: vagrant
vagrant@127.0.0.1's password:
Last login: Wed Aug 14 08:07:51 2013 from 10.0.2.2
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$ ruby -v
ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
-
- 普通に動いてすごい
sahara入れる
- サンドボックスでrollbackとかできるっぽい
- https://github.com/jedi4ever/sahara
vagrant plugin install sahara
snapshot入れる
- いくらrollbackできるとはいえある時点での環境は永続的に保存しておきたいんで
- https://gist.github.com/voluntas/5525719
vagrant plugin install vagrant-vbox-snapshot
# スナップショットを名前を指定して取得します
$ vagrant snapshot take <name>
# 一番最新のスナップショットに戻します
$ vagrant snapshot back
# スナップショット一覧を表示します
$ vagrant snapshot list
# スナップショットを削除します
$ vagrant snapshot delete <name>
# 指定したスナップショット(名前)に戻します
$ vagrant snapshot go <name>
- takeしてもlistしてもなんの反応もなし
- どういうことなのか…
終了する
vagrant halt [default] Attempting graceful shutdown of VM...
また起動する
vagrant up
ぶっ壊す
- destroyしてもbox自体は残ってるっぽい
vagrant destroy
Are you sure you want to destroy the 'default' VM? [y/N] y
[default] Destroying VM and associated drives...
- boxを消す
vagrant box remove box名
- boxの一覧
vagrant box list
その後
box名にcentosってつけちゃってcentos6にしたいなって思ってrename方法探したけどよくわからなかったので、destroyしてremoveしてまたaddしてinitしたら
`Vagrantfile` already exists in this directory. Remove it before running `vagrant init`
って出てたので、centos6のフォルダに入ってるVagrantfileを消してまたinitしても同じのが出てダメだった。
ぐぐってもわからなかったんだけど、upとかしてみるとどうも前のcentosを起動しに行ってるっぽかったので、なんかの設定ファイルにちゃんとdefaultのbox名が変わっていないっぽい現象な気がする。
boxesの中にcentosって前のboxのフォルダ(中身は当然空)があったので、centosもcentos6も消して再度addしてみたけどだめだった。
解決
意味がやっとわかった。
Vagrantの仮想マシンを動かす環境ファイルとかは自分で好きなところに作れるってことっぽい。
addからずっとcmd.exe叩いたときのホームディレクトリのままでやってたので、必然的に
C:/Users/username/
でやってて、ここで
vagrant init
ってやると C:/Users/username/ 直下に Vagrantfile ができて、そこにはもう消してあるcentosってbox名が
config.vm.box = "centos"
このままずっと残ってるため、C:/Users/username/ で延々initやってもそりゃ確かにcentosを起動しに行きますわって感じだった。
なので、C:\Users\username\.vagrant.d\boxes の中の Vagrantfile を消してもなんの解決もしない上に、この Vagrantfile はVMを動かすときに使うからむしろ消してダメってことっぽい。
意味がわかれば簡単なことで、そのままやりたければホームフォルダ上の Vagrantfile を消してinitし直せばいいし、OSごとに環境分けたければ適当にフォルダ作ってそこでinitしてしまえばいいだけだった。
サーバにいろいろ入れてみる
Chefっていうので設定書けば自動でインストールできるらしいけど、いろいろめんどくさそうな感じがするのでAnsibleってがシンプルらしいということなので、次回はこれを使っていろいろやっていきたい。