Program LOG

勉強した内容をまとめ中。。。間違ってたら教えて。。。

windows+CygwinでChef - 5.公開レシピを試してみる

さて、前回までに、LAMPの構築を行ってきました。
何とか無事に、ChefでLAMP環境を作れましたが、Chefの事を色々調べていくと、
公開レシピなるものが大量にヒットしてきました・・・。
ちょっとそのサードパーティのChefクックブックというものを使っていきたいと思います。

Windos+Chef入門目次を作成しました!
windows+CygwinでChef - 目次 - Program LOG

テスト実行環境を用意する。

まずは、前回までの環境とは別に再度Vagrant仮想マシンを用意します。
それと、新しくcookbookを作成しておきたいと思います。
今回は以下のように実行しました。

#chefテスト用の192.168.33.13を指定したので、chef_13って名前の仮想マシンにしちゃいましたw
$ mkdir chef_13
$ cd ./chef_13
$ vagrant init centos6.5
$ vagrant up
#鍵認証
$ vagrant ssh-config --host chef_13 >> ~/.ssh/config
#接続テスト
$ ssh chef_13
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$ exit
logout
Connection to 127.0.0.1 closed.

#リポジトリの作成と仮想マシンにchefを用意
$ knife solo init opstest
$ cd opstest
$ knife solo prepare chef_13
$ vagrant sandbox on

これで、環境は一応整いました!
これは、今後実行をテストするのに使えそうなので、要チェックです!w
そして、今回は、すぐに戻せるように、vagrant sandbox onにしておきます。

一応Vagrantのsandbox(saharaプラグイン)のコマンドをおさらいしておきます。

# sandboxモード開始
$ vagrant sandbox on
# 変更を決定する
$ vagrant sandbox commit
# 変更を破棄し、ロールバックする
$ vagrant sandbox rollback
# sandboxモード終了(commitしていないものは破棄される)
$ vagrant sandbox off
# 現在sandboxモードかどうかの確認
$ vagrant sandbox status

さて、いよいよ、実行テストの開始です!

Chefサードパーティを使うには

まず、メジャーなソフトのクックブックにはサードパーティライブラリが公開されている事が多く、
Chefを公開している、Opscode Communityには、そういったサードパーティクックブックがまとめっており、
knifeコマンドを使うと、簡単にインポートする事ができるようです。
ただし、opscodeにアカウントを作って、秘密鍵をダウンロードしないとダメなようです!

早速ユーザー登録など、やっていきたいと思います。
まず、Opscode Communityへアクセスし、SignUpを行います。

f:id:aimtaku:20140614102614p:plain

ユーザー名や、メールアドレスを入力して、登録を完了させます。
すると、秘密鍵が表示されますので、「./chef/ユーザー名.pem」という名前のファイルを作成し、
そこに貼り付けて保存します。

次に、「./chef/knife.rb」ファイルに以下を追記します。

client_key       '/Users/ユーザー名/.chef/ユーザー名.pem'

正直、これって何のパスか分からないんだが・・・w
こうするものって事で・・・。

そして、knifeコマンドを実行します。
ちなみに今回は、yumのepelリポジトリを使えるようにしたいと思います!

$ knife cookbook site vendor yum -o cookbooks/
Installing yum to /cygdrive/c/Users/takumi-main/Desktop/dev/chef_13/opstest/cookbooks
ERROR: The cookbook repo /cygdrive/c/Users/takumi-main/Desktop/dev/chef_13/opstest/cookbooks is not a git repository.
Use `git init` to initialize a git repo

はい!エラーいただきました!w
何かgit initしろって言われましたねw
どうやらgitリポジトリが必要みたいです。。。

というわけで、Cygwinを入れた時に、Gitも一緒に入れていたので、動くはずって事でコマンド実行しました。

$ git init
Initialized empty Git repository in /cygdrive/c/Users/takumi-main/Desktop/dev/chef_13/opstest/.git/
$ git add -A
$ git commit

それでは、先ほどのコマンドを再度実行します。

$ knife cookbook site vendor yum -o cookbooks/
Installing yum to /chef_13/opstest/cookbooks
Checking out the master branch.
Creating pristine copy branch chef-vendor-yum
Downloading yum from the cookbooks site at version 3.2.0 to /chef_13/opstest/cookbooks/yum.tar.gz
Cookbook saved: /cygdrive/c/Users/takumi-main/Desktop/dev/chef_13/opstest/cookbooks/yum.tar.gz
Removing pre-existing version.
Uncompressing yum version 3.2.0.
removing downloaded tarball
No changes made to yum
Checking out the master branch.

・・・・うまくいけたのかな?w
本で見る限り、このコマンドは、cookbooksディレクトリ以下にyumクックブックをダウンロードし、
git add~gitcommitまで自動的に実行してくれるらしい。。。。
出来た事にして、nodeのrun_listを編集してみます。

{
    "run_list":[
        "yum::epel"
    ]
}

そして、いつものようにcookですね。

$ knife solo cook chef_13
・・・・中略・・・・
================================================================================
Recipe Compile Error
================================================================================


Chef::Exceptions::RecipeNotFound

could not find recipe epel for cookbook yum



Running handlers:
[2014-06-01T06:17:08+00:00] ERROR: Running exception handlers
Running handlers complete

[2014-06-01T06:17:08+00:00] ERROR: Exception handlers complete
[2014-06-01T06:17:08+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
Chef Client failed. 0 resources updated in 6.881102691 seconds
[2014-06-01T06:17:08+00:00] ERROR: could not find recipe epel for cookbook yum
[2014-06-01T06:17:08+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
ERROR: RuntimeError: chef-solo failed. See output above.

あれ?wエラー?wなして?w
調査してみた所、この方法は使えないようになっていたもよう・・・w
マジかー!でも、対処方法が見つかりましたので、そっちで対応してみます。

まずは、自分でcookbookを作成しないといけないようですので、
以下のように、作成しました。

$ knife cookbook create phpstartup -o site-cookbooks/

次に、nodesのjsonファイルを以下のように修正します。

{
    "run_list":[
        "yum",
        "recipe[phpstartup]"
    ]
}

そして、作成したcookbookのrecipeに以下のように設定。

# add the EPEL repo
yum_repository 'epel' do
  description 'Extra Packages for Enterprise Linux'
  mirrorlist 'http://mirrors.fedoraproject.org/mirrorlist?repo=epel-6&arch=$basearch'
  fastestmirror_enabled true
  gpgkey 'http://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6'
  action :create
end

そして、実行結果は以下の通りです。

$ knife solo cook chef_13
Running Chef on chef_13...
・・・中略・・・
Starting Chef Client, version 11.12.4
Compiling Cookbooks...
Converging 1 resources
Recipe: yum::default
  * yum_globalconfig[/etc/yum.conf] action createRecipe: 
  * template[/etc/yum.conf] action create
    - update content in file /etc/yum.conf from a403d7 to 31c39a
        ・・・中略・・・
        -# PUT YOUR REPOS HERE OR IN separate files named file.repo
        -# in /etc/yum.repos.d
Recipe: phpstartup::default
  * yum_repository[epel] action createRecipe: 
  * template[/etc/yum.repos.d/epel.repo] action create
    - create new file /etc/yum.repos.d/epel.repo
    - update content in file /etc/yum.repos.d/epel.repo from none to 4dd1d3
       ・・・・中略・・・・
  * execute[yum-makecache-epel] action run
    - execute yum -q makecache --disablerepo=* --enablerepo=epel

  * ruby_block[yum-cache-reload-epel] action create
    - execute the ruby block yum-cache-reload-epel

  * execute[yum-makecache-epel] action nothing (skipped due to action :nothing)
  * ruby_block[yum-cache-reload-epel] action nothing (skipped due to action :nothing)

Running handlers:
Running handlers complete

お?いけたっぽい?w
早速vagrantで確認してみたいと思います。

$ vagrant ssh
[vagrant@localhost yum]$ yum repolist
Loaded plugins: fastestmirror
Determining fastest mirrors
・・・中略・・・
epel                   Extra Packages for Enterprise Linux   10,893

おおー!ありましたw
じゃあ、ついでにって事でremiも入れておきたいと思います。
recipeは以下のように追加しました。

yum_repository 'remi' do
  description 'Les RPM de Remi - Repository'
  baseurl 'http://rpms.famillecollet.com/enterprise/6/remi/x86_64/'
  gpgkey 'http://rpms.famillecollet.com/RPM-GPG-KEY-remi'
  fastestmirror_enabled true
  action :create
end

そして、再度実行します。

$ knife solo cook chef_13
Running Chef on chef_13...
・・・中略・・・
Recipe: phpstartup::default
  * yum_repository[remi] action createRecipe: 
  * template[/etc/yum.repos.d/remi.repo] action create
    - create new file /etc/yum.repos.d/remi.repo
    - update content in file /etc/yum.repos.d/remi.repo from none to f62c82
       ・・・中略・・・
    - change mode from '' to '0644'
    
  * execute[yum-makecache-remi] action run
    - execute yum -q makecache --disablerepo=* --enablerepo=remi

  * ruby_block[yum-cache-reload-remi] action create
    - execute the ruby block yum-cache-reload-remi

  * execute[yum-makecache-remi] action nothing (skipped due to action :nothing)
  * ruby_block[yum-cache-reload-remi] action nothing (skipped due to action :nothing)

おーこちらも入ったっぽいです!
せっかくなので、PHPをインストールしてみたいと思います。
packageのoptionsにリポジトリを設定する事で指定する事が出来ます。
recipeに以下を追記しました。

package "php" do
    options "--enablerepo=remi"
    action :install
end

以下が実行結果になります。

$ knife solo cook chef_13
Running Chef on chef_13...
Checking Chef version...
Uploading the kitchen...
・・・中略・・・
Recipe: phpstartup::default
  * package[php] action install
    - install version 5.4.29-1.el6.remi of package php


Running handlers:
Running handlers complete

Chef Client finished, 1/7 resources updated in 70.525623356 seconds

おー!バージョン上がって、入ったようですので、phpのバージョンを確認してみたいと思います。

$ vagrant ssh
[vagrant@localhost ~]$ php -v
PHP 5.4.29 (cli) (built: May 28 2014 15:02:43)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies

おー!いけましたw

という事で、今回はサードパーティを使用して、レシピを実行するという目的は果たせました!
使えて、実績のあるレシピは、今後も使っていきたいと思います。
今回紹介したレシピ以外にも、もっと色んなレシピがあるようですので、
試して、紹介していきたいと思います!

使用環境