カタカタブログ

SIerで働くITエンジニアがカタカタした記録を残す技術ブログ。Java, Oracle Database, Linuxが中心です。たまに数学やデータ分析なども。

Windows7にChef(11.6.0)とVagrant(1.7.2)を入れてプロビジョニングしてみた

前回MacにChefとVagrantを入れてプロビジョニングしてみた。
Mac(Yosemite)にChef(12.0.3)とVagrant(1.7.2)を入れてプロビジョニングしてみた - カタカタブログ

せっかくVagrantfileとChefクックブックをgitで管理しておけばどの環境でも仮想マシンがすぐに上げられるということで、Windows環境でもその恩恵に預かるべく、プロビジョニング環境をセットアップしてみた(ただし今回は手順再確認のため、git cloneはせず一から構築している)。結論として、Windows版でのChef環境セットアップはMacよりやや面倒だった。。

構成

基本的な構成は前回Macで作ったものと同じく、knife-soloでWindows側から仮想ノードをセットアップする。また、ツール類のバージョンも現時点の最新版で揃えた。

  • ホスト環境: Windows 7 64bit
  • 仮想ノードOS: Cent OS 7
  • Vagrant 1.7.2
    • vagrant-vbguest (0.10.0): Virtual Box Guest Addition更新のためのプラグイン
    • vagrant-omnibus (1.4.1): ホスト側から仮想ノードにChefをインストールするためのプラグイン
  • Virtual Box 4.3.20
  • Ruby 2.1.5
  • Chef 11.6.0
    • knife-solo 0.4.2
  • berkshelf 3.2.3 

Virtual BoxとVagrantのインストール

それぞれ公式サイトから最新版をダウンロードし、インストールする。すでにインストール済みでも普通にインストーラを実行すればアップデートされるようなので、最新化しておく。

Vagrantプラグインインストール

vagrant-vbguestインストール

> vagrant plugin install vagrant-vbguest
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Installed the plugin 'vagrant-vbguest (0.10.0)'!

vagrant-omnibusインストール

>vagrant plugin install vagrant-omnibus
Installing the 'vagrant-omnibus' plugin. This can take a few minutes...     
Installed the plugin 'vagrant-omnibus (1.4.1)'!

Cent OS 7のBoxをvagrantbox.esからcentos7という名前で取得

> vagrant box add centos7 https://f0fff3908f081cb6461b407be80daf97f07ac418.googledrive.com/host/0BwtuV7VyVTSkUG1PM3pCeDJ4dVE/centos7.box

Vagrantでcentos7仮想マシン作成

仮想マシン初期化

>vagrant init centos7
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.

Vagrantfile作成

作ったVagrantfileを以下のように編集する。

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
  config.vm.box = "centos7"
  config.vm.network "private_network", ip: "192.168.33.11"
  config.vm.hostname = "vmcentos"
  config.vbguest.auto_update = false # Guest Additions自動更新無効
  config.vm.provider "virtualbox" do |vb|vb.name = "centos7_vagrant"
    vb.memory = "2048"
end

仮想マシン起動

>vagrant up
(・・・略)
Failed to mount folders in Linux guest. This is usually because
the "vboxsf" file system is not available. Please verify that
the guest additions are properly installed in the guest and
can work properly. The command attempted was:

mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3`
vagrant /vagrant
mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant

The error output from the last command was:

/sbin/mount.vboxsf: mounting failed with the error: No such device

Macのときと同様、初回はGuest AdditionsとVirtual Boxのバージョン不一致エラーが出るので、vagrant-vaguestを使ってGuest Additionsを更新する。

Guest Additions更新

>vagrant vbguest

仮想マシンにsshログイン

Mac等にインストールしたVagrantならばここでvagrant sshとすればログインできるのだが、Windowsにはsshコマンドがないため、この時点ではvagrant sshコマンドでログインできない。対応のためには、Windowsでsshコマンドが実行できるようにする必要があるが、ここでは単純な動作確認のため、いったんTeraTermでログインすることにする。

以下の接続情報でログインできることを確認する。

  • ホスト:ポート => 127.0.0.1:2222
  • ユーザ/パスワード => vagrant/vagrant

f:id:osn_th:20150227075233p:plain


また、hostsに以下の設定を追記しておく。

192.168.33.10     vmcentos

これでsshでvmcentos:22にvagrant/vagrantでログインできることも合わせて確認しておく。

Windowsホスト側にChefセットアップ

Rubyセットアップ

WindowsにRubyとbundlerをインストールする必要があるが、ここでは構築手順を省略する。注意点として、WindowsでGemを使ってChefをインストールするために、DevKitというものがないとエラーになってしまうので、以下を参考にDevKitをインストールする。

参考: http://www.rubylife.jp/railsinstall/rails/index4.html
1. 以下から DevKit-mingw64-32-4.7.2-20130224-1151-sfx.exe をダウンロード
http://rubyinstaller.org/downloads
f:id:osn_th:20150227075235p:plain

2. インストーラを実行し以下のディレクトリに展開(フォルダは新規に作成する)
C:\Ruby21\DevKit
f:id:osn_th:20150227075238p:plain

3. 上記ディレクトリで以下のコマンドを実行

C:\Ruby21\DevKit>ruby dk.rb init
[INFO] found RubyInstaller v2.1.5 at C:/Ruby21

Initialization complete! Please review and modify the auto-generated
'config.yml' file to ensure it contains the root directories to all
of the installed Rubies you want enhanced by the DevKit.

4. インストールコマンドを実行

C:\Ruby21\DevKit>ruby dk.rb install
[INFO] Updating convenience notice gem override for 'C:/Ruby21'
[INFO] Installing 'C:/Ruby21/lib/ruby/site_ruby/devkit.rb'

Gemfileを以下の内容で配置

source 'https://rubygems.org'

gem "win32-process"
gem "windows-pr"
gem “chef"
gem "knife-solo"

gem "win32-api", "~> 1.5.1", platforms: [:mingw, :x64_mingw]
gem "berkshelf"

bundler install

C:\Ruby21\Gemfile>bundler install

・確認

>knife -v
Chef: 11.6.0

knifeコマンドが正しく実行できることを確認

Windowsホスト側にsshとrsyncを入れておく

sshとrsyncはVagrantおよびChefから仮想マシンへのプロビジョニング時にログインとクックブックファイル転送に必要だが、Windowsコマンドプロンプトではデフォルトで使えないため、MinGW、MSYSを使ってインストールする。

http://sourceforge.net/projects/mingw/files/
公式サイトから入手したインストールを進めるとパッケージが選択できるので、MSYS以下のmsys-openssh, msys-openssl, msys-rsync を選択し、Installation > Apply Changes > Apply でインストール開始
f:id:osn_th:20150227075240p:plain
インストール後、C:\MinGW\msys\1.0\bin をPATHに追加

これにより、vagrant sshがWindowsでも使えるようになった。

>vagrant ssh
Last login: Thu Feb 12 21:20:21 2015 from 192.168.33.1
[vagrant@vmcentos ~]$

ssh-config設定

このままだとssh時にパスワード入力が求められるので、ssh vmcentos7でパスワード無しvagrantユーザでログインできるようにしておく。

>vagrant ssh-config --host vmcentos >> C:\MinGW\msys\1.0\etc\ssh
\ssh_config

仮想マシンにChefインストール

>knife solo bootstrap vagrant@vmcentos

仮想マシンにログインし、Chefがインストールされたことを確認

C:\vms\centos7>ssh vmcentos
Last login: Fri Feb 13 00:37:52 2015 from 192.168.33.1
[vagrant@vmcentos ~]$ knife -v

Chef: 11.6.0

Chefクックブック作成

クックブックのひな形作成

>knife solo init .
WARNING: No knife configuration file found
Creating kitchen...
Creating knife.rb in kitchen...
Creating cupboards...
DL is deprecated, please use Fiddle
Setting up Berkshelf...

apacheのクックブック作成

>knife cookbook create apache -o site-cookbooks
** Creating cookbook apache
** Creating README for cookbook: apache
** Creating CHANGELOG for cookbook: apache
** Creating metadata for cookbook: apache

apacheのレシピ作成

site-cookbooks\apache\recipes\default.rb を以下の内容で編集する。

package "httpd" do
  action :install
end
service "httpd" do
  action [ :enable, :start ]
end

・ノード情報記載
nodes\vmcentos.jsonにapacheレシピを追加

{
  "run_list": [
"recipe[apache]"
  ],
  "automatic": {
    "ipaddress": "vmcentos"
  }
}

Chef-soloを実行しapacheをインストール

レシピ実行

>knife solo cook vmcentos
DL is deprecated, please use Fiddle
Running Chef on vmcentos...
Checking Chef version...
Installing Berkshelf cookbooks to 'cookbooks'...
DEPRECATED: Your Berksfile contains a site location pointing to the Opscode Comm
unity Site (site :opscode). Site locations have been replaced by the source loca
tion. Change this to: 'source "https://supermarket.chef.io"' to remove this warn
ing. For more information visit https://github.com/berkshelf/berkshelf/wiki/depr
ecated-locations
Resolving cookbook dependencies...
Uploading the kitchen...
rsync: change_dir "/cygdrive/C/Ruby21/lib/ruby/gems/2.1.0/gems/knife-solo-0.4.2/
lib/knife-solo/resources/patch_cookbooks" failed: Bad file number (9)
rsync error: some files/attrs were not transferred (see previous errors) (code 2
3) at /usr/src/rsync/rsync-3.0.8/main.c(1052) [sender=3.0.8]
ERROR: RuntimeError: Failed to launch command ["rsync", "-rL", "--chmod=ugo=rwX"
, "--rsh=ssh vagrant@vmcentos", "--delete-after", "--exclude=revision-deploys",
"--exclude=tmp", "--exclude=.git", "--exclude=.hg", "--exclude=.svn", "--exclude
=.bzr", "/cygdrive/C/Ruby21/lib/ruby/gems/2.1.0/gems/knife-solo-0.4.2/lib/knife-
solo/resources/patch_cookbooks/", ":~/chef-solo/cookbooks-1"]

rsyncでエラーとなっているよう。/cygdrive/Cというパスでクックブックを見ているよう。対策として、/cygdriveをマウントしてごまかすことにする。

/cygdriveマウント

C:\MinGW\msys\1.0\etc\fstabを編集し、以下の内容を記載する

#Win32_Path             Mount_Point
c:              /cygdrive/c

これで/cygdrive/cというパスにアクセスするとC:ドライブ直下にアクセスしたことになるので、/cygdrive/C/Ruby21・・・というパスでちゃんとC:ドライブ以下の正しいパスにアクセスできるはず。

レシピ再実行

>knife solo cook vmcentos
DL is deprecated, please use Fiddle
Running Chef on vmcentos...
Checking Chef version...
Installing Berkshelf cookbooks to 'cookbooks'...
DEPRECATED: Your Berksfile contains a site location pointing to the Opscode Comm
unity Site (site :opscode). Site locations have been replaced by the source loca
tion. Change this to: 'source "https://supermarket.chef.io"' to remove this warn
ing. For more information visit https://github.com/berkshelf/berkshelf/wiki/depr
ecated-locations
Resolving cookbook dependencies...
Uploading the kitchen...
WARNING: Local cookbook_path 'C:/vms/centos7/cookbooks' does not exist
Generating solo config...
Running Chef...
Starting Chef Client, version 11.6.0
Compiling Cookbooks...
Converging 2 resources
Recipe: apache::default
  * package[httpd] action install
    - install version 2.4.6-19.el7.centos of package httpd

  * service[httpd] action enable
    - enable service service[httpd]

  * service[httpd] action start
    - start service service[httpd]

Chef Client finished, 3 resources updated

今度は成功した!

仮想マシンにログインしapache動作確認

仮想マシンにログインする

[vagrant@vmcentos /]$ ps -ef | grep httpd | grep -v grep
root      5081     1  0 03:16 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    5082  5081  0 03:16 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    5083  5081  0 03:16 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    5084  5081  0 03:16 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    5085  5081  0 03:16 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    5086  5081  0 03:16 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND

apacheがインストールされ、起動している!

まとめ

前回のMac OSに引き続き 、今回はWindows7上で同一のVagrant、Chefをセットアップしてみた。やはりWindowsだと各種ツール、特にコマンドライン周りがLinux系OSと違って貧弱なため、追加でセットアップしないといけない内容が増えてしまう。ただ、セットアップが完了してしまえば、あとはVagrantfileとクックブックだけでMacと変わらずに仮想ノードのプロビジョニングができるので、これでWindowsとMacで同一のOS構成をgit等で管理することもできるようになった!

以上

関連記事