【】 HomesteadマシンのVagrantfileを参考に学んでいこう

参考ファイル

http://yamacent.hatenablog.com/entry/2015/05/31/235900

[JSON to YAML]
http://shtr28.hatenablog.com/entry/json-yaml-transform

[Vagrantfileのプロビジョナ] https://qiita.com/ringo0321/items/38743442a9abfc3be5b2





[ファイル名] Vagrantfile

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

# 使用する2つのライブラリを読み込み (jsonライブラリ, yamlライブラリ)
require 'json'
require 'yaml'


VAGRANTFILE_API_VERSION ||= "2"

# 
confDir = $confDir ||= File.expand_path(File.dirname(__FILE__))

# 5つのパスを変数に格納
homesteadYamlPath = confDir + "/Homestead.yaml"
homesteadJsonPath = confDir + "/Homestead.json"
afterScriptPath = confDir + "/after.sh"
customizationScriptPath = confDir + "/user-customizations.sh"
aliasesPath = confDir + "/aliases"



require File.expand_path(File.dirname(__FILE__) + '/scripts/homestead.rb')

# Vagrantのバージョンは、2.2.4以上
Vagrant.require_version '>= 2.2.4'


Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

    # エイリアスFileが存在していれば
    if File.exist? aliasesPath then
        # プロビジョニング (仮想マシン内で使用できるailasを読み込む)
        # sourceオプションに変数aliasesPathを指定。[aliasをホストOS側に置いた場所を指す]
        # destinationオプションに変数"/tmp/bash_aliases"を指定。[ゲストOS(Ubuntu)において、"/tmp/bash_aliases"にaliasを設置]
        config.vm.provision "file", source: aliasesPath, destination: "/tmp/bash_aliases"

        # プロビジョニング (仮想マシン内で使用できるailasを読み込む)
        # sourceオプションに変数aliasesPathを指定。
        config.vm.provision "shell" do |s|

            # /tmp/bash_aliasesの内容を/home/vagrant/.bash_aliases に出力
            # 仮想マシン内の/home/vagrant/.bash_aliasesにvagrantユーザーにユーザー所有権:グループ所有権を与える。
            s.inline = "awk '{ sub(\"\r$\", \"\"); print }' /tmp/bash_aliases > /home/vagrant/.bash_aliases 
                              && chown vagrant:vagrant /home/vagrant/.bash_aliases"
        end
    end

    # Homestead.yaml Fileが存在していれば
    if File.exist? homesteadYamlPath then
        settings = YAML::load(File.read(homesteadYamlPath))

    # Homestead.json Fileが存在していれば
    elsif File.exist? homesteadJsonPath then
        settings = JSON::parse(File.read(homesteadJsonPath))

    # Homestead.yamlとHomestead.json Fileの両方とも無ければ、
    else
        abort "Homestead settings file not found in #{confDir}"
    end

    Homestead.configure(config, settings)

    # afterScriptファイルがあれば
    if File.exist? afterScriptPath then
        config.vm.provision "shell", path: afterScriptPath, privileged: false, keep_color: true
    end

    # user-customizations.shファイルがあれば
    if File.exist? customizationScriptPath then
        config.vm.provision "shell", path: customizationScriptPath, privileged: false, keep_color: true
    end

    # vagrant-hostsupdaterプラグインがあれば、
    if Vagrant.has_plugin?('vagrant-hostsupdater')
        config.hostsupdater.aliases = settings['sites'].map { |site| site['map'] }

    # vagrant-hostmanagerプラグインがあれば、
    elsif Vagrant.has_plugin?('vagrant-hostmanager')
        config.hostmanager.enabled = true
        config.hostmanager.manage_host = true
        config.hostmanager.aliases = settings['sites'].map { |site| site['map'] }
    end

    # vagrant-notify-fowarderプラグインがあれば、
    if Vagrant.has_plugin?('vagrant-notify-forwarder')
        config.notify_forwarder.enable = true
    end
end