vagrantの起動元となるboxは、packerで作成する。
(いろんなサイトがあるので、今回は省略。)
vagrant のインストール
brewになかったので、サイトからダウンロードしてインストール
Vagrant
VirtualBoxのインストール
VirtualBoxのサイトから、 mac版をダウンロードしてインストール。
最新版は、4.3.22(2015/02現在)だが、4.3.20 をインストールする
(MacがSleepするとVagrant上のインスタンスがabortedになる不具合が発生した。
4.3.8 で修正されたようだが、4.3.22で再発している模様。
4.3.20では、この不具合が発生しなかったので、今回は4.3.20をインストール。)
4.3.24で解決しているそうなので、4.3.24をインストール (2015/03現在)
chefとBarksfile
Vagrantでvm起動時に必要なpackageのインストール、自動設定を行うために、chefを利用。
Gemfileに以下を書いて、bundle installする。
1 2 3 4 5 |
source 'https://rubygems.org' gem "chef" gem "knife-solo" gem "berkshelf" |
vagrant plugin
ubuntu 14.04 の場合、最新のchefをインストールすると、
以下のエラーがでて先に進めない。
The following SSH command responded with a non-zero exit status. Vagrant assumes that this means the command failed!
apt-get update -y -qq
そこで、chef をVersion指定でインストールするために、
vagrantのomnibus pluginをインストール
1 2 3 |
$ vagrant plugin install vagrant-omnibus $ vagrant plugin list omnibus がlistにあればOK |
apt
berksでモジュールをインストールする際に、そもそもapt-get update されていないとインストールに失敗する。
Berksでaptを入れる手もあるが、それ以外のpackge installも兼ねてsite-cookbooksにprebuildを作って対応。
1 |
knife cookbook create prebuild -o site-cookbooks |
site-cookboosの下にprebuildができるので、recipes/default.rbを編集
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
execute 'apt-get-update' do command 'apt-get update' ignore_failure true end pkgs = [ "libpcre3-dev", "libssl-dev", "openssl", "zlib1g-dev", "emacs24-nox" ] pkgs.each do |pkg| package pkg do action :install end end |
berks
モジュールをBerksfileに書いて以下を実行。
chefのcookbooksにダウンロードされる。
1 2 3 |
cookbooks ディレクトリがある場合は、消しておく $ \rm -r cookbooks $ berks vendor cookbooks |
Berksfileは、こんな感じ
1 2 3 4 5 6 7 8 9 10 11 |
source "https://api.berkshelf.com" cookbook 'git' cookbook 'build-essential' cookbook 'xml' cookbook 'ruby_build' cookbook 'rbenv', :git => 'git://github.com/fnichol/chef-rbenv.git', :ref => 'v0.7.2' cookbook 'nginx' cookbook 'imagemagick' cookbook 'sqlite', '~> 1.1.0' cookbook 'mysql', '~> 5.3.6' |
Vagrantfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# -*- mode: ruby -*- # vi: set ft=ruby : VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "ubuntu" config.vm.box_url = "../packer/ubuntu/box/ubuntu14.04-1.box" config.vm.synced_folder ".", "/vagrant", disabled: true config.vm.define "ubuntu" do |x| x.vm.hostname = "ubuntu" x.vm.network :forwarded_port, guest: 80, host: 8080 x.vm.provider "virtualbox" do |v| v.cpus = 4 v.memory = 4096 end end config.omnibus.chef_version = "11.14.6" config.vm.provision :chef_solo do |chef| chef.cookbooks_path = ["./cookbooks", "./site-cookbooks"] chef.json = { "rbenv" => { "rubies" => [ "2.1.3" ], "gems" => { "2.1.3" => [ { 'name' => 'bundler' } ] } } } chef.run_list = [ 'prebuild', 'git', 'xml', 'ruby_build', 'rbenv::system', 'nginx', 'imagemagick', 'mysql::server', 'mysql::client', 'sqlite' ] end end |
- packer で作成したubuntuのboxを指定するようにVagrantfileを生成。
- omnibusでchef_versionを指定する。
- cookbooks_pathでsite-cookbooksも指定する。prebuild用
- あとは、run_listにbarksでダウンロードしたcookbookを指定する。
ディレクトリ構成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
├── Gemfile ├── packer │ └── ubuntu │ └── box │ └── ubuntu14.04-1.box └── vagrant ├── Berksfile ├── Vagrantfile ├── cookbooks // berks コマンドで作られる ├── nodes // なくても良い ├── roles // なくても良い └── site-cookbooks └── prebuild └── recipes └── default.rb |
vagrant 実行
1 |
vagrant up |
でvagrantの実行。エラーなく終了したら、vagrant sshでログインできる。
コメント