chefことはじめ2

続いて、nginxのインストール&コンフィギュレーションまでの自動化に挑戦。
引き続き本の通りに進めていきます。

入門Chef Solo - Infrastructure as Code

入門Chef Solo - Infrastructure as Code

まずは、nginxがyumからインストールできるように
パッケージを導入。

[root@chef ~]# rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm を取得中
警告: /var/tmp/rpm-tmp.MonN0Z: ヘッダ V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
準備中...                ########################################### [100%]
   1:nginx-release-centos   ########################################### [100%]

[root@chef yum.repos.d]# yum info nginx
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
nginx                                                    | 2.9 kB     00:00
nginx/primary_db                                         |  34 kB     00:00
利用可能なパッケージ
名前                : nginx
アーキテクチャ      : x86_64
バージョン          : 1.6.2
リリース            : 1.el6.ngx
容量                : 336 k
リポジトリー        : nginx
要約                : High performance web server
URL                 : http://nginx.org/
ライセンス          : 2-clause BSD-like license
説明                : nginx [engine x] is an HTTP and reverse proxy server, as
                    : well as a mail proxy server.

続いて、nginx用クックブックを作ろうとしたら
なぜか想定のディレクトリ(/home/ope/20150308_chef/chef-repo/cookbooks/配下)
ではなく、ユーザー直下に作成されてしまう。
仕方なく、cp -pirで引越し。(mvの方がよかったかな。。)

[ope@chef ~]$ sudo knife cookbook create nginx -o cookbooks
** Creating cookbook nginx in /home/ope/cookbooks
** Creating README for cookbook: nginx
** Creating CHANGELOG for cookbook: nginx
** Creating metadata for cookbook: nginx

気を取り直して、default.rb作成。

[ope@chef recipes]$ vi /home/ope/20150308_chef/chef-repo/cookbooks/nginx/recipes/default.rb

内容はこんな感じ。

#
# Cookbook Name:: nginx
# Recipe:: default
#
# Copyright 2015, 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

次にnginx.conf.erbを作成します。

[ope@chef default]$ vi /home/ope/20150308_chef/chef-repo/cookbooks/nginx/templates/default/nginx.conf.erb

内容はこんな感じ。

user nginx;
worker_processes 1;
error_log /var/log/nignx/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;
   }
}
}

最後に、localhost.jsonの書き換え。
前回のhello用のレコードは残っているとエラーが出るようなので
全て削除。



こんな感じ。

// localhost.json

{
  "run_list":[
     "recipe[hello]"
  ]
}

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

この時点でのディレクトリ構成

[root@chef 20150308_chef]# tree --charset=C
.
`-- chef-repo
    |-- LICENSE
    |-- README.md
    |-- chefignore
    |-- config
    |   `-- rake.rb
    |-- cookbooks
    |   |-- README.md
    |   |-- hello
    |   |   |-- CHANGELOG.md
    |   |   |-- README.md
    |   |   |-- attributes
    |   |   |-- definitions
    |   |   |-- files
    |   |   |   `-- default
    |   |   |-- libraries
    |   |   |-- metadata.rb
    |   |   |-- providers
    |   |   |-- recipes
    |   |   |   `-- default.rb
    |   |   |-- resources
    |   |   `-- templates
    |   |       `-- default
    |   `-- nginx
    |       |-- CHANGELOG.md
    |       |-- README.md
    |       |-- attributes
    |       |-- definitions
    |       |-- files
    |       |   `-- default
    |       |-- libraries
    |       |-- metadata.rb
    |       |-- providers
    |       |-- recipes
    |       |   `-- default.rb
    |       |-- resources
    |       `-- templates
    |           `-- default
    |               `-- nginx.conf.erb
    |-- data_bags
    |   `-- README.md
    |-- environments
    |   `-- README.md
    |-- locallhost.json
    |-- roles
    |   `-- README.md
    `-- solo.rb

28 directories, 19 files

最後にコマンドにて起動

[ope@chef chef-repo]$ sudo chef-solo -c solo.rb -j ./locallhost.json
Starting Chef Client, version 12.1.0
Compiling Cookbooks...
Converging 3 resources
Recipe: nginx::default
  * yum_package[nginx] action install
    - install version 1.6.2-1.el6.ngx of package nginx
  * service[nginx] action enable (up to date)
  * service[nginx] action start
    - start service service[nginx]
  * template[nginx.conf] action create
    - update content in file /etc/nginx/nginx.conf from 772e91 to 8fc397
    --- /etc/nginx/nginx.conf   2014-09-16 22:54:27.000000000 +0900
    +++ /tmp/chef-rendered-template20150308-9139-1nig49d        2015-03-08 16:43:35.334267815 +0900
    @@ -1,33 +1,23 @@
    +user nginx;
    +worker_processes 1;
    +error_log /var/log/nignx/error.log;
    +pid /var/run/nginx.pid;

    -user  nginx;
    -worker_processes  1;
    -
    -error_log  /var/log/nginx/error.log warn;
    -pid        /var/run/nginx.pid;
    -
    -
    -events {
    -    worker_connections  1024;
    +events{
    + worker_connections 1024;
     }

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

    -http {
    -    include       /etc/nginx/mime.types;
    -    default_type  application/octet-stream;
    -
    -    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    -                      '$status $body_bytes_sent "$http_referer" '
    -                      '"$http_user_agent" "$http_x_forwarded_for"';
    -
    -    access_log  /var/log/nginx/access.log  main;
    -
    -    sendfile        on;
    -    #tcp_nopush     on;
    -
    -    keepalive_timeout  65;
    -
    -    #gzip  on;
    -
    -    include /etc/nginx/conf.d/*.conf;
    + server{
    +  listen 80;
    +  server_name localhost;
    +  location/{
    +     root /usr/share/nginx/html;
    +     index index.html index.htm;
    +   }
    +}
     }
  * service[nginx] action reload
    - reload service service[nginx]

Running handlers:
Running handlers complete
Chef Client finished, 4/5 resources updated in 6.949092897 seconds
[ope@chef chef-repo]$

ちゃんと動いてますねー。よしよし。

[ope@chef chef-repo]$ rpm -qa | grep nginx
nginx-1.6.2-1.el6.ngx.x86_64
nginx-release-centos-6-0.el6.ngx.noarch

[ope@chef chef-repo]$ service nginx status
nginx (pid  9366) を実行中...
[ope@chef chef-repo]$