雑なメモ

学びを記す

初期状態のVagrantfileのコメントアウト部分を読む

boxはchef/centos-6.5を使った。
vagrant init chef/centos-6.5したときのVagrantfileのコメントアウト部分を読もうと思う。
コメントアウト部分は「きっとなにか役立つことが書いてあるんだろうなあ、一応消さないでおこう」という想いでいつも残してしまう。
なんとなくカッコ悪い気がするので内容を理解して、これからは消してしまおうと思っている。
なお英文は、意訳したり補足したりした。

初期状態のVagrantfile

これを上から順に読んでいく。

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "chef/centos-6.5"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  # config.push.define "atlas" do |push|
  #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
  # end

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   sudo apt-get update
  #   sudo apt-get install -y apache2
  # SHELL
end

エディタの設定

# -*- mode: ruby -*-
# vi: set ft=ruby :

これはエディタ固有の設定(シンタックスハイライトやインデント等)である。Vagrantとは関係ない。
# -*- mode: ruby -*-emacsの設定で、vi: set ft=ruby :がviの設定である。

Vagrantの構成におけるバージョン

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|

Vagrantの構成は以下の記述から成る。Vagrant.configure(2)というのはVagrantの構成におけるバージョンである(上位互換性のために古い記法もサポートしている)。挙動が理解できないうちは変更しないでほしい。

リファレンスの紹介

# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.

一般的な設定は以下にコメントとして残してる。リファレンスは https://docs.vagrantup.com を参照してほしい。

boxの話

# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "chef/centos-6.5"

Vagrantにおける開発環境ではboxが必要である。boxは https://atlas.hashicorp.com/search で探せる

boxの更新チェックについて

# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false

falseにするとvagrant box outdatedを実行しない限り更新チェックはされない。このオプションは非推奨である

AUTOMATIC UPDATE CHECKING

デフォルトだとtrueで、毎回vagrant up時にboxが最新かどうかのチェックをしている。最新のBoxが見つかると、更新を促され(y/n)、ユーザは更新を無視することもできる。vagrant box updateを実行すると手動でアップデートできる。

vagrant box outdatedの詳細

boxが最新かどうかチェックする。--globalを付加すると全てのBoxをチェックする。

$ vagrant box list
chef/centos-6.5 (virtualbox, 1.0.0)
coreos-stable   (virtualbox, 607.0.0)
$ vagrant box outdated
Checking if box 'chef/centos-6.5' is up to date...
$ vagrant box outdated --global
* 'coreos-stable' is outdated! Current: 607.0.0. Latest: 633.1.0
* 'chef/centos-6.5' (v1.0.0) is up to date

# この場合、'coreos-stable'というBoxが古いことがわかった。

ポートフォワーディング

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# config.vm.network "forwarded_port", guest: 80, host: 8080

ポートフォワーディングの設定。ポートフォワーディングによって、ゲストマシンのポートは、ホストマシンのポートを経由するように設定される。 config.vm.network "forwarded_port", guest: 80, host: 8080と設定すると、localhost:8080にアクセスした際にホストマシンの8080番ポート経由でゲストマシンの80番ポートにアクセスする。

プライベートネットワーク

# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"

プライベートネットワークを構築し、指定したIPによるホストオンリーアクセスを可能にする。

パブリックネットワーク

# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"

パブリックネットワークを構築し、そのブリッジも設定する。

フォルダの同期

# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"

ゲストOSとフォルダを共有する。第一引数はホストOSのディレクトリのパス。第二引数はマウント先であるゲストOSのディレクトリのパス。第三引数は任意のオプション。

プロバイダ

# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
#   # Display the VirtualBox GUI when booting the machine
#   vb.gui = true
#
#   # Customize the amount of memory on the VM:
#   vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.

プロバイダ(virtualbox,vmware等)固有の設定であり、Vagrantに合わせて調整できる。以下は、VirtualBoxの例である。(GUIモードで起動し、メモリが1GB割り当てられる)

プロバイダはプラグイン構造である

Vagrantで作成されたVMを操作するプロバイダはプラグイン構造になっており、その種類は豊富だ。ドキュメントには、VMwareとかDockerなどが載ってるがプラグインを探してみると、awsやdigitalocean、GCEなどもある。

Vagrant Push

# Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
# such as FTP and Heroku are also available. See the documentation at
# https://docs.vagrantup.com/v2/push/atlas.html for more information.
# config.push.define "atlas" do |push|
#   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
# end

Atlasにpushする際のVagrant Pushの方法を定義する。FTPやHerokuにもpushできる。

プロビジョニング

# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
#   sudo apt-get update
#   sudo apt-get install -y apache2
# SHELL

シェルスクリプトでのプロビジョニングを可能にする。Puppet, Chef, Ansible, Salt, Dockerもプロビジョニングツールとして使える。(インラインで記述されたシェルコマンドを実行する例が書かれている)