LoginSignup
12
14

More than 5 years have passed since last update.

VagrantにSaharaを導入

Last updated at Posted at 2017-09-22

(追記)
現在はVagrantにsnapshotが追加されています。
コメントで教えていただきました。
詳しい使い方がこちらの記事で紹介されています。
https://qiita.com/j-imai/items/cc1fc6d96160735da53a


前回はVagrantで仮想マシンを作りました。

ただ単に仮想マシンを作っただけでは面白くないので、Saharaというプラグインを使って仮想環境をサンドボックス化してみます。

サンドボックス化?

ここでいう"サンドボックス化"というのは、

ある時点での仮想マシンの状態を保存しておいて、仮想マシンに変更を加えたのちに、保存された状態に戻せる。

というものです。

VirtualBoxのスナップショットというかそのものです。
VirtualBoxマネージャでも同じことができますが、Saharaを使うとコマンドラインからとても簡単に仮想マシンを復元できます。

どこが便利なのか

仮想環境で開発するだけの場合は、あまり出番はないかもしれません。

しかし、環境構築のテスト環境として、仮想マシンを使う場合はけっこう便利です。

たとえば、手作業なり Ansible なり Chef なり手段はなんでも良いですが、環境構築スクリプトを作成して、それを仮想マシン上で実行したとします。

スクリプトに不具合があって、スクリプト修正後にまっさらな状態のマシンで再度スクリプトを走らせたい場合、どうしますか?
仮想マシンを削除して、もういちど作りなおしますか?

さすがにそれは面倒です。

そんな時に仮想マシンをサンドボックス化していると便利です。
ロールバックすればすぐに仮想マシンをスクリプト実行前の状態に復元できます。

Saharaをインストール

plugin install でプラグインをインストールします。

$ vagrant plugin install sahara
Installing the 'sahara' plugin. This can take a few minutes...
Installed the plugin 'sahara (0.0.17)'!

とりあえず状態を確認します。

$ vagrant sandbox status
[default] Sandbox mode is off

off になってます。
あと、コマンドが sahara ではなく sandbox になっているので注意(なぜsaharaにしなかったのだろうか...)。

Saharaを有効化

off のままでは当然Saharaが動いてくれないので on で有効化します。

$ vagrant sandbox on
[default] Starting sandbox mode...
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

状態を確認します。

$ vagrant sandbox status
[default] Sandbox mode is on

on になっていますね。

ちなみに off で無効化できます。

サンドボックスをコミット

とりあえず、サンプルとして素のCentOS7環境を作ります。

$ vagrant init centos/7
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
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' is up to date...
==> default: A newer version of the box 'centos/7' is available! You currently
==> default: have version '1704.01'. The latest is version '1708.01'. Run
==> default: `vagrant box update` to update.
==> default: Setting the name of the VM: centos7_default_1506059895526_81170
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> 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: Warning: Remote connection disconnect. Retrying...
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default: 
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Rsyncing folder: /Users/sudachi808/centos7/ => /vagrant

では、なにもしてないこの状態にいつでも戻せるように、ここでサンドボックスをコミットします。

$ vagrant sandbox commit
[default] Committing the virtual machine...
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

コミットできました。
今度、何があってもこの状態に戻せるので安心です(素の状態に戻せても嬉しくありませんがサンプルなので...)。

ロールバック

仮想マシンに変更を加えたのちに、ロールバックして無かったことにしてみます。

仮想マシンにログインして...

vagrant ssh
[vagrant@localhost ~]$

取り返しのつかないことをしてみます!

[vagrant@localhost ~]$ sudo rm -f /etc/passwd
[vagrant@localhost ~]$ exit
logout
Connection to 127.0.0.1 closed.

当然、ログインできなくなります。

$ vagrant ssh
ssh_exchange_identification: Connection closed by remote host

rollback でロールバックしてみます。

$ vagrant sandbox rollback
[default] Rolling back the virtual machine...
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

すると...

$ vagrant ssh
[vagrant@localhost ~]$ 

復活しました!
これで何度でもやり直しがききますね。

12
14
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
14