DRYな備忘録

Don't Repeat Yourself.

MacでVagrantつかってみるまで

ゴール

  • MacOSXvagrantをインストールして、VM環境をひとつつくってみる

もくじ

  1. Vagrantとは
  2. Macにインストールする
  3. OSイメージをダウンロードしてみる
  4. VMを立ち上げてみる
  5. VMを捨てて、さらにもっかい立ち上げてみる

Vagrantとは

ホストOSのうえにOSを作る(VM)タイプの環境構築(必要なパッケージを満たしつつアプリケーションが動く環境を作る)ツール、っていうことですかね。

Macにインストールする

VirtualBoxのインストールはこのエントリですでに済ませているので、割愛。Vagrantのダウンロードから。

MacOSXをクリックしてdmgファイルをダウンロード、実行、インストール。

f:id:otiai10:20141027110122p:plain

% which vagrant
/usr/bin/vagrant
% vagrant -v
Vagrant 1.6.5
%

入った。

OSイメージをダウンロードしてみる

じゃ今回はdebianでも入れてみようかな、ということで

% vagrant box list
There are no installed boxes! Use `vagrant box add` to add some.
% vagrant box add chef/debian-7.6
==> box: Loading metadata for box 'chef/debian-7.6'
    box: URL: https://vagrantcloud.com/chef/debian-7.6
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) virtualbox
2) vmware_desktop

Enter your choice: 1
==> box: Adding box 'chef/debian-7.6' (v1.0.0) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/chef/boxes/debian-7.6/versions/1/providers/virtualbox.box
==> box: Successfully added box 'chef/debian-7.6' (v1.0.0) for 'virtualbox'!
% vagrant box list
chef/debian-7.6 (virtualbox, 1.0.0)
%

VMを立ち上げてみる

% cd
% mkdir foo
% cd foo
% vagrant box list
chef/debian-7.6 (virtualbox, 1.0.0)
% vagrant init chef/debian-7.6
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.
% ls
Vagrantfile
% vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'chef/debian-7.6'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'chef/debian-7.6' is up to date...
==> default: Setting the name of the VM: foo_default_1414377320115_32350
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (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 timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/otiai10/foo
% vagrant ssh
Linux packer-debian-7 3.2.0-4-amd64 #1 SMP Debian 3.2.60-1+deb7u1 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Tue Jul 22 02:37:58 2014 from 10.0.2.2
vagrant@packer-debian-7:~$ pwd
/home/vagrant
vagrant@packer-debian-7:~$ uname -a
Linux packer-debian-7 3.2.0-4-amd64 #1 SMP Debian 3.2.60-1+deb7u1 x86_64 GNU/Linux
vagrant@packer-debian-7:~$

今回は~/fooというディレクトリでvagrant initしたので、どのシェルからも~/fooに移動してvagrant sshすればssh接続できるようになった。

VMを捨てて、さらにもっかい立ち上げてみる

% 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 -h
Usage: vagrant halt [options] [name]

Options:

    -f, --force                      Force shut down (equivalent of pulling power)
    -h, --help                       Print this help
% vagrant halt default
==> default: Attempting graceful shutdown of VM...
% vagrant ssh
VM must be running to open SSH connection. Run `vagrant up`
to start the virtual machine.
%

ほうほう

% vagrant halt -h
Usage: vagrant halt [options] [name]

Options:

    -f, --force                      Force shut down (equivalent of pulling power)
    -h, --help                       Print this help
% vagrant halt default
==> default: Attempting graceful shutdown of VM...
% vagrant ssh
VM must be running to open SSH connection. Run `vagrant up`
to start the virtual machine.
% vagrant status
Current machine states:

default                   poweroff (virtualbox)

The VM is powered off. To restart the VM, simply run `vagrant up`
%

完全に捨てるには?

% vagrant destroy default
    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Destroying VM and associated drives...
% vagrant status
Current machine states:

default                   not created (virtualbox)

The environment has not yet been created. Run `vagrant up` to
create the environment. If a machine is not created, only the
default provider will be shown. So if a provider is not listed,
then the machine is not created for that environment.
%

もっかい立ち上げてみる

% cd ~/foo
% vagrant init
# Vagrantfileすでにあるから!ってぉこされる
% vagrant up
# 立った
% vagrant ssh
# 入れた

雑感

  • docker, vagrant, chef, ansibleなどについて一度ひととおり調べてみるべきだと思った

f:id:otiai10:20141027115209j:plain

DRY