vagrant 環境構築

環境リセットしたいと思います。とりあえずでびあんパケジを削除。

$ sudo dpkg -r vagrant

で、v1.1.5 な vagrant_x86_64.deb を以下から取得

導入。前使っていたのはちょっと古かったのかどうか。あまり関係ないだろうけど。

$ sudo dpkg -i vagrant_x86_64.deb

ええと、bundler は導入済みらしい。

$ gem list --local|grep bundle
bundler (1.2.4)

sahara は gem で導入済みです。どうしたものか。面倒なので Ubuntu 13.04にVagrantを入れてみる. にある方法をそのままなぞってみます。

$ git clone https://github.com/ryuzee/sahara.git sahara
$ cd sahara
$ bundle install
$ bundle exec rake build
sahara 0.0.14 built to pkg/sahara-0.0.14.gem
$ vagrant plugin install pkg/sahara-0.0.14.gem
Installing the 'pkg/sahara-0.0.14.gem' plugin. This can take a few minutes...
Installed the plugin 'sahara (0.0.14)'!

gem で導入してるソレと違うけどいいよね。veewee はとりあえずスルーで。
したらもっかい vagrant box add してみます。

$ vagrant box add base http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.3-x86_64-v20130101.box

しばらく時間がかかるはず。導入後のソレはこのまま追記の方向で。

追記

Vagrantfile を以下にして vagrant up。

Vagrant.configure("2") do |config|
  config.vm.box = "base"
  config.vm.network :private_network, ip: "192.168.33.10"

てこれも時間かかるな。。
どうも固まっている模様。いったん C-c で強制終了。

^C[default] Waiting for cleanup before exiting...

で、リトライしようとしたら以下とのこと。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...

え、と言いつつ vagrant ssh しようとしてるんですがだんまり。ping の返事も戻ってこないですね。一旦 halt してリトライしてみるか。

$ vagrant halt
[default] Attempting graceful shutdown of VM...

そして以下な自分エントリを発見し

同じようなことをしている模様。むむむ。
結局再び vagrant box add してたりしてます。何やってんだorz

再度 download

関連プロセスを kill して

$ kill -9 `ps aux|grep virtualbox|awk '{print $2;}'`

vagrant up してるのですがどうなのか。同じところでだんまりだぞ

[default] Waiting for VM to boot. This can take a few minutes.

うーん。つうか Bios で VT を有効に云々って記述を Qiita で発見。Windows 情報だけどちょっとアレ。
とりあえず /proc/cpuinfo な flags に vmx があるのは確認。BIOS の設定を確認してみます。これから再起動。

とほほ

BIOS で VT 有効になってなかった。有効にして起動後、vagrant up してみるに

[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] The guest additions on this VM do not match the installed version of
VirtualBox! In most cases this is fine, but in rare cases it can
cause things such as shared folders to not work properly. If you see
shared folder errors, please update the guest additions within the
virtual machine and reload your VM.

Guest Additions Version: 4.2.6
VirtualBox Version: 4.1
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant

一応動いた。vagrant ssh してみます。

$ vagrant ssh
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$ 

入れました。一旦 vagrant halt して設定変更を。て halt の出力微妙だな。

$ vagrant halt
[default] Attempting graceful shutdown of VM...
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["showvminfo", "43d7d4a0-8915-4571-bd98-25d05d2466cd", "--machinereadable"]

Stderr: VBoxManage: error: The object is not ready
VBoxManage: error: Details: code E_ACCESSDENIED (0x80070005), component , interface , callee nsISupports
Context: "COMGETTER(RemoteUSBDevices)(ComSafeArrayAsOutParam(coll))" at line 1698 of file VBoxManageInfo.cpp

とりあえずこれはスルーで Vagrantfile を以下にしてリトライ。

Vagrant.configure("2") do |config|
  config.vm.box = "base"
  config.vm.network :private_network, ip: "192.168.33.10"

どうなるか。起動後に確認してみると ping の返事も戻ってきます。ssh な接続も確認。
とりあえず、~/.ssh/config に以下を追加。

host 192.168.33.10
     Identityfile ~/.vagrant.d/insecure_private_key
     User vagrant

で、確認。

$ ssh 192.168.33.10
Last login: Wed May  1 10:39:30 2013 from 192.168.33.1
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$ 

やれやれ。

とりあえず

入門 ChefSolo 確認しつつ serverspec の動作確認も、ということで。
このまま続けるので何かあれば追記します。

その前に

sahara ですね。とは言え、vagrant sandbox が使えない。

$ vagrant plugin list
No plugins installed.

あら。入れてなかったのかな。

$ vagrant plugin install sahara/pkg/sahara-0.0.14.gem
Installing the 'sahara/pkg/sahara-0.0.14.gem' plugin. This can take a few minutes...
Installed the plugin 'sahara (0.0.14)'!

で、仮想ホストを起動した状態で以下。

$ vagrant sandbox on
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

これで vagrant sandbox rollback で戻せるのか。とりあえず毎回ヤッた方が良いのかどうか。あるいは destroy で良いのかな。

6 章おさらい

とりあえず vagrant up の部分はスルーで。とは言え、knife なアレがナニ。gem で knife-solo を入れておきゃ良いのかどうか。

$ gem install knife-solo -r --no-ri --no-rdoc
Fetching: mixlib-config-1.1.2.gem (100%)
Fetching: mixlib-cli-1.3.0.gem (100%)
Fetching: mixlib-log-1.6.0.gem (100%)
Fetching: mixlib-authentication-1.3.0.gem (100%)
Fetching: mixlib-shellout-1.1.0.gem (100%)
Fetching: systemu-2.5.2.gem (100%)
Fetching: yajl-ruby-1.1.0.gem (100%)
Building native extensions.  This could take a while...
Fetching: ipaddress-0.8.0.gem (100%)
Fetching: ohai-6.16.0.gem (100%)
Fetching: rest-client-1.6.7.gem (100%)
Fetching: net-ssh-2.6.7.gem (100%)
Fetching: net-ssh-gateway-1.2.0.gem (100%)
Fetching: net-ssh-multi-1.1.gem (100%)
Fetching: highline-1.6.18.gem (100%)
Fetching: chef-11.4.4.gem (100%)
Fetching: librarian-0.0.26.gem (100%)
Fetching: knife-solo-0.2.0.gem (100%)
Thanks for installing knife-solo!

If you run into any issues please let us know at:
  https://github.com/matschaffer/knife-solo/issues

If you are upgrading knife-solo please uninstall any old versions by
running `gem clean knife-solo` to avoid any errors.

See http://bit.ly/CHEF-3255 for more information on the knife bug
that causes this.
Successfully installed mixlib-config-1.1.2
Successfully installed mixlib-cli-1.3.0
Successfully installed mixlib-log-1.6.0
Successfully installed mixlib-authentication-1.3.0
Successfully installed mixlib-shellout-1.1.0
Successfully installed systemu-2.5.2
Successfully installed yajl-ruby-1.1.0
Successfully installed ipaddress-0.8.0
Successfully installed ohai-6.16.0
Successfully installed rest-client-1.6.7
Successfully installed net-ssh-2.6.7
Successfully installed net-ssh-gateway-1.2.0
Successfully installed net-ssh-multi-1.1
Successfully installed highline-1.6.18
Successfully installed chef-11.4.4
Successfully installed librarian-0.0.26
Successfully installed knife-solo-0.2.0
17 gems installed

で、rbenv rehash して以下。

$ knife solo init chef-repo
$ cd chef-repo
$ git init
$ git add .
$ git commit -m 'initial commit'

次に knife solo prepare とあります。ここでは IP でナニ。

$ knife solo prepare 192.168.33.10
Bootstrapping Chef...

curl: (6) Couldn't resolve host 'www.opscode.com'
bash: install.sh: No such file or directory
Generating node config 'nodes/192.168.33.10.json'...

大丈夫かな。とりあえず中身は以下らしい。

$ cat nodes/192.168.33.10.json 
{"run_list":[]}

git 方面にアレ。

$ git add nodes/192.168.33.10.json
$ git commit -m 'add node json file'
[master 003db99] add node json file
 1 file changed, 1 insertion(+)
 create mode 100644 nodes/192.168.33.10.json

次にクックブック作成 & レシピ編集、とのこと。

$ knife cookbook create nginx -o site-cookbooks
** Creating cookbook nginx
** Creating README for cookbook: nginx
** Creating CHANGELOG for cookbook: nginx
** Creating metadata for cookbook: nginx

ちょっと 3 章に戻って中身確認。以下を編集とありますがどうなんだろ。

  • site-cookbooks/nginx/recipes/default.rb
  • nodes/192.168.33.10.json

レシピは以下、とありますね。

#
# Cookbook Name:: nginx
# Recipe:: default
#
# Copyright 2013, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#

package "nginx" do
  action :install
end

service "nginx" do
  supports :status => true, :restart => true, :reload => true
  action [ :enable, :start ]
end

template "nginx.conf" do
  path "/etc/nginx/nginx.conf"
  source "nginx.conf.erb"
  owner "root"
  group "root"
  mode 0644
  notifies :reload, 'service[nginx]'
end

で、templates/default/nginx.conf.erb が以下。

user nginx;
worker_processes 1;
error_log /var/log/nginx/error_log;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    server {
        listen <%= node['nginx']['port'] %>
        server_name localhost;
        location / {
            root /usr/share/nginx/html;
            index index.html index.htm;
        }
    }
}

次に nodes/192.168.33.10.json を以下に。

{
    "nginx": {
        "port" : 80
    },
    "run_list":[
        "nginx"
    ]
}

これで実行できるのかな。

$ knife solo cook 192.168.33.10
Checking Chef version...
bash: rsync: command not found
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: remote command not found (code 127) at io.c(605) [sender=3.0.9]
ERROR: RuntimeError: Failed to launch command rsync -rl  --rsh="ssh vagrant@192.168.33.10" --delete --exclude revision-deploys --exclude tmp --exclude '.*'  ./ :/tmp/chef-solo

む、これどうなったのかな。なんとなく微妙感満点。ssh でログインして確認してみましたが 80 は開いてませんね。つうか rsync 入れないと、なのかな。

うーん、テキストでも 0.3.0 使え、ってあしましたが以下も。

入れてみます。とりあえず gem を削除。

$ gem uninstall knife-solo
Successfully uninstalled knife-solo-0.2.0

で、git clone ですか。

$ git clone git://github.com/matschaffer/knife-solo.git

つうかここで rake install したらどこに入るんだろ。

$ rake install
rake aborted!

あ、駄目か。なんか bundle install せよ、とか出てるな。rbenv rehash してリトライしてみましたが、挙動は変わらず。

$ gem i knife-solo --pre --no-ri --no-rdoc

でも OK なのか。どうなのか。

$ rbenv rehash
$ gem list|grep knife
knife-solo (0.3.0.pre3)

これでリトライ。

$ knife solo cook 192.168.33.10
WARNING: solo.rb found, but since knife-solo v0.3.0 it is not used any more
WARNING: Please read the upgrade instructions: https://github.com/matschaffer/knife-solo/wiki/Upgrading-to-0.3.0
Running Chef on 192.168.33.10...
Checking Chef version...
Uploading the kitchen...
WARNING: Local cookbook_path '/var/chef/cookbooks' does not exist
WARNING: Local cookbook_path '/var/chef/site-cookbooks' does not exist
bash: rsync: command not found
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: remote command not found (code 127) at io.c(605) [sender=3.0.9]
ERROR: RuntimeError: Failed to launch command rsync -rl  --rsh="ssh vagrant@192.168.33.10" --delete --exclude revision-deploys --exclude tmp --exclude .git --exclude .hg --exclude .svn --exclude .bzr  /home/rms/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/knife-solo-0.3.0.pre3/lib/knife-solo/resources/patch_cookbooks/ :~/chef-solo/cookbooks-3

まだ駄目なのかなぁ。netstat では 80 は開いていない。リモホ側で rsync が無いと駄目、ってのは無いよなぁ。

あら?

以下を実行せい、なの?

$ knife solo prepare 192.168.33.10
Bootstrapping Chef...

curl: (6) Couldn't resolve host 'www.opscode.com'
bash: install.sh: No such file or directory

なんか出力微妙なんですが、構わず実行。

$ knife solo cook 192.168.33.10

NG でした。うーん、何が悪いのか分からんな。とりあえず knife solo prepare がアレなのかな。あら、Couldn't resolv host って出てますね。
これ、単純に vagrant なナニが外と通信できないのが原因なのか。

思いだしたぞ。これって親マシンな 12.10 なうぶんつな /etc/resolv.conf が

nameserver 127.0.1.1

なんだorz

手で直す

/usr/bin/vagrant の中身が以下。

#!/usr/bin/env bash
#
# This script just forwards all arguments to the real vagrant binary.

/opt/vagrant/bin/vagrant "$@"

これも bash なスクリプトだな。本体は /opt/vagrant/embedded/gems/gems/vagrant-1.1.5/bin/vagrant な模様。
nameserver で grep したら以下な出力がありますね。

$ find /opt/vagrant/embedded/gems/gems/vagrant-1.1.5/|xargs grep nameserver 2>/dev/null
/opt/vagrant/embedded/gems/gems/vagrant-1.1.5/plugins/providers/virtualbox/action/sane_defaults.rb:            if contents =~ /^nameserver 127\.0\.(0|1)\.1$/

ぐぬぬぬ。つうか同じ問題でハマッてるのに全然覚えていない、ってのがアレ。

そして

ググッて出てきたのが自分エントリorz
しかも根本的な解決ができてないorz
で、以下のあたりを参考にしつつ

以下を盛り込んで vagrant up したんですが virtualbox が起動したぞ。でも ping の返事は戻ってきますね。
knife なソレをリトライも挙動変わらずorz
あら、もひとつ指定せよ、とありますね。つうかいちいち virtualbox 起動するの止めて欲しいんですがorz
現時点で指定してるソレは以下。

   config.vm.provider :virtualbox do |vb|
     # Don't boot with headless mode
     vb.gui = true
  
     # Use VBoxManage to customize the VM. For example to change memory:
     vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
     vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
     #vb.customize ["modifyvm", :id, "--memory", "1024"]
   end

あ、vb.gui に true 代入してるのがダウトなのか。つうかそろそろ限界だな。
出力されてるソレはあまり変わりばえしないカンジなのがアレ。

とゆーことで

今日はこれでギブアップします。