[CentOS6][Chef] ChefでLAMP構成をつくる6 - 設定ファイルを修正したらサービス再起動


Create: 2013/07/30
LastUpdate: 2013/07/31
[メニューに戻る]

ここでは、Chef11を使用して、段階的にLAMP環境を構築していきます。

今回は、前回の「 [CentOS6][Chef] ChefでLAMP構成をつくる5 - サービス開始と自動起動設定」に引き続き、Apacheの設定ファイルを修正したらサービスを再起動するレシピを追加します。

Chef11のテスト環境の詳細については、「メニュー」を参照してください。

  [テスト環境]
  • 管理サーバ
    192.168.1.67(chetos6g) ・・・ Chefサーバ + ワークステーション
  • 管理対象
    192.168.1.68(centos6h) ・・・ ノード

前回同様、テスト駆動開発のように、まずテストケースを作成してからレシピを作成し、実際にノードで実行する前に Cookbook が期待する動作を行うかテストすることにします。

Chefのレシピの書き方や、Chefspecのテストケースの書き方は、以下を参考にしました。

今回使用する管理対象のノードは、前回のレシピを実行した状態になっているので、Apacheのサービスは起動中です。
そこで、今回作成する新しいレシピを実行することで、管理対象ノードのApacheの設定ファイルを変更し、サービスを再起動するようにします。

1.Apache 用のCookbook 修正


以下、管理サーバに chefユーザでログインして、Chefのワークステーションで作業します。

1.1.Chefspec のテストケース追加


以下のファイルにChefspecのテストケースを追加します。
  • ~/chef-repo/cookbooks/apache/spec/default_spec.rb 
Apacheの設定ファイル(httpd.conf)が修正されたら、サービスを再起動しているかチェックします。
内容は以下のとおり。赤字部分を追加。
require 'chefspec'

describe 'apache::default' do
  let (:chef_run) { ChefSpec::ChefRunner.new.converge 'apache::default' }

  # package
  it 'should install apache' do
    chef_run.should install_package 'httpd'
  end

  # template
  it 'should create httpd.conf' do
    expect(chef_run).to create_file '/etc/httpd/conf/httpd.conf'
    file = chef_run.template('/etc/httpd/conf/httpd.conf')
    expect(file.mode).to eq "0644"
    expect(file).to be_owned_by('root', 'root')
  end

  # service
  it 'should start searvice' do
    expect(chef_run).to set_service_to_start_on_boot 'httpd'
    expect(chef_run).to start_service 'httpd'
    chef_run.template("/etc/httpd/conf/httpd.conf").should notify( 'service[httpd]', :restart )
  end

end

1.2.レシピ修正


以下のファイルにレシピを追加します。
  • ~/chef-repo/cookbooks/apache/recipes/default.rb
Apacheの設定ファイル(httpd.conf)が修正されたら、サービスを再起動するようにします。
内容は以下のとおり。赤字部分を追加
#
# Cookbook Name:: apache
# Recipe:: default
#
# Copyright 2013, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#

# package
package "httpd" do
  action :install
end

# httpd.conf
template "/etc/httpd/conf/httpd.conf" do
 source "httpd.conf.erb"
 owner "root"
 group "root"
 mode "0644"
 variables(
  :servername      => node["apache"]["servername"],
  :port            => node["apache"]["port"],
  :serveradmin     => node["apache"]["serveradmin"]
 )
end

# service
service "httpd" do
  supports :status => true, :restart => true
  action [ :enable, :start ]
  #subscribes :restart, resources(:template => "/etc/httpd/conf/httpd.conf")
  subscribes :restart, resources("template[/etc/httpd/conf/httpd.conf]")
end 
上記レシピの赤字で示した subscribes は、青字でコメントアウトした文法でも動きますが、foodcritic の文法チェックでは  FC043 のエラーになります。
Chefspecのテストケースを実行してレシピをチェックします。
結果が、以下のように "0 failures" であればOKです。
$ cd ~/chef-repo/cookbooks
$ rspec -fd --color apache

apache::default
  should install apache
  should create httpd.conf
  should start searvice

Finished in 0.02134 seconds
3 examples, 0 failures

1.3.テンプレートの変更


以下のファイルを修正して httpd.conf を修正します。
  • ~/chef-repo/cookbooks/apache/templates/default/httpd.conf.erb
テスト用に赤字部分を修正しました。
#
# Don't give away too much information about all the subcomponents
# we are running.  Comment out this line if you don't mind remote sites
# finding out what major optional modules you are running
ServerTokens Prod

 

1.4.バージョンの変更


以下のファイルを修正して Cookbookのバージョン番号を変更します。
  • ~/chef-repo/cookbooks/apache/metadata.rb
赤字部分の番号を "'0.1.3" にします。
name             'apache'
maintainer       'blue21'
maintainer_email 'canopus@blue21.ddo.jp'
license          'All rights reserved'
description      'Installs/Configures apache'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version          '0.1.3'
Foodcritic で Cookbookの全体的なチェック(文法、矛盾など)をします。
以下のように何も表示されなければOKです。
$ cd ~/chef-repo/cookbooks
$ foodcritic ./apache

1.5.CookbookをChefサーバに登録


修正した ApacheのCookbook をChefサーバにアップロードします。
$ cd ~/chef-repo/cookbooks
$ knife cookbook upload apache
Uploading apache         [0.1.3]
Uploaded 1 cookbook.

2.ノードでレシピの実行


管理対象のノードに、root ユーザでログインして作業します。
まず、設定変更前のApacheの状態を確認します。
以下のようにコマンドを実行すると、赤字部分に "OS" が表示されます。
# curl -I http://192.168.1.68:8888/
HTTP/1.1 403 Forbidden
Date: Tue, 31 Jul 2013 00:41:16 GMT
Server: Apache/2.2.15 (CentOS)
Accept-Ranges: bytes
Content-Length: 5039
Connection: close
Content-Type: text/html; charset=UTF-8 
次に、レシピを実行してApacheの設定ファイルを変更します。
ノードのChefクライアントをデーモン化していれば、自動的にレシピが実行されるのですが、今回はデーモン化していないので、管理対象のノードにログインして、Chefクライアントを実行します。
以下のようにコマンドを実行します。
# /opt/chef/bin/chef-client
Starting Chef Client, version 11.4.4
resolving cookbooks for run list: ["apache", "php", "mysql"]
Synchronizing Cookbooks:
  - mysql
  - php
  - apache
Compiling Cookbooks...
Converging 10 resources
Recipe: apache::default
  * package[httpd] action install (up to date)
  * template[/etc/httpd/conf/httpd.conf] action create
    - update template[/etc/httpd/conf/httpd.conf] from 9b2aa5 to 1b4371
        --- /etc/httpd/conf/httpd.conf  2013-07-31 08:57:19.460556425 +0900
        +++ /tmp/chef-rendered-template20130731-1952-zm4o7q     2013-07-31 08:57:35.967555049 +0900
        @@ -41,7 +41,7 @@
         # Don't give away too much information about all the subcomponents
         # we are running.  Comment out this line if you don't mind remote sites
         # finding out what major optional modules you are running
        -ServerTokens OS
        +ServerTokens Prod

         #
         # ServerRoot: The top of the directory tree under which the server's

  * service[httpd] action enable (up to date)
  * service[httpd] action start (up to date)
Recipe: php::default
  * package[php] action install (up to date)
  * package[php-mysql] action install (up to date)
  * template[/etc/php.ini] action create (up to date)
Recipe: mysql::default
  * package[mysql] action install (up to date)
  * package[mysql-server] action install (up to date)
  * template[/etc/my.cnf] action create (up to date)
  * service[mysqld] action enable (up to date)
  * service[mysqld] action start (up to date)
Recipe: apache::default
  * service[httpd] action restart
    - restart service service[httpd]

Chef Client finished, 2 resources updated
適用済みのレシピは、何も行われていません。(up to date)
Apacheの設定ファイルが変更されて、赤字部分で、Apacheのサービスを再起動しているのがわかります。
最後に、Apache の状態を確認してみます。
以下のようにコマンドを実行します。
# curl -I http://192.168.1.68:8888/
HTTP/1.1 403 Forbidden
Date: Tue, 31 Jul 2013 00:42:01 GMT
Server: Apache
Accept-Ranges: bytes
Content-Length: 5039
Connection: close
Content-Type: text/html; charset=UTF-8
赤字部分の表示が、製品名の "Apache" だけになりました。
これで、Apacheのサービス再起動が成功して、設定変更が反映されたことがわかります。