LoginSignup
2
2

More than 5 years have passed since last update.

ChefでCentOS 6.7 + nginxを設置する。- カスタムクックブック

Last updated at Posted at 2016-03-16

関連記事

クックブック

  • 雛形生成
% bin/knife cookbook create nginx -o site-cookbooks
  • 外部クックブックとカスタムクックブックが同じ名前で一緒に存在する場合、カスタムクックブックが優先される。
% cat .chef/knife.rb
cookbook_path    ["cookbooks", "site-cookbooks"]
node_path        "nodes"
role_path        "roles"
environment_path "environments"
data_bag_path    "data_bags"
# verbosity        :debug

knife[:berkshelf_path] = "cookbooks"
Chef::Config[:ssl_verify_mode] = :verify_peer if defined? ::Chef

属性(attribute)

  • ハードコーディングすべきでない内容をattributeに記述する。
  • バージョン番号、ディレクトリ名、ポート番号、ユーザ名、メモリやディスクの容量など
  • site-cookbooks/nginx/attributes/default.rb

レシピ

  • 外部クックブックは各moduleをレシピで提供している。
  • 提供されていない場合は新しいレシピを作成しなければならない。
  • 最初はクックブックをオーバーライドすることを考えていたが、cookbooksの配下にあるレシピを直接修正しないとできないみたい。
  • 外部クックブックを参考してtarballでインストールするカスタムクックブックを生成した。
nginx_url = node['nginx']['url']
nginx_filename = "nginx-#{node['nginx']['ver']}.tar.gz"
src_filepath  = "#{Chef::Config['file_cache_path'] || '/tmp'}/#{nginx_filename}"

%w(pcre pcre-devel).each do |pkg|
  package pkg do
    action :install
  end
end

remote_file nginx_url do
  source nginx_url
  path src_filepath
  backup false
end

user node['nginx']['user'] do
  comment "create user of nginx"
  system true
  shell '/bin/false'
end

directory "/var/log/nginx" do
  owner 'root'
  group 'root'
  mode '0644'
  action :create
end

directory "#{node['nginx']['dir']}" do
  owner 'root'
  group 'root'
  mode '0755'
  action :create
end

cookbook_file "#{node['nginx']['dir']}/mime.types" do
  source 'mime.types'
  owner 'root'
  group 'root'
  mode '0644'
  notifies :reload, 'service[nginx]'
end

cookbook_file "#{node['nginx']['dir']}/authorized_ip" do
  source 'authorized_ip'
  owner 'root'
  group 'root'
  mode '0644'
  notifies :reload, 'service[nginx]'
end

template '/etc/sysconfig/nginx' do
  source 'nginx.sysconfig.erb'
  owner 'root'
  group 'root'
  mode '0644'
end

%w(/etc/init.d/nginx /etc/rc.d/init.d/nginx).each do |src|
  template src do
    source 'nginx.init.erb'
    owner 'root'
    group 'root'
    mode '0755'
  end
end

%w(sites-available sites-enabled).each do |dir|
  directory "#{node['nginx']['dir']}/#{dir}" do
    owner 'root'
    group 'root'
    mode '0755'
    action :create
  end
end

bash "install nginx" do
  cwd File.dirname(src_filepath)
  code <<-EOH
    tar zxf #{nginx_filename} -C #{File.dirname(src_filepath)}
    cd #{File.dirname(src_filepath)}/#{File.basename(nginx_filename, ".tar.gz")}
    ./configure #{configure_flags.join(' ')}
    make
    make install
  EOH

  notifies :restart, 'service[nginx]'
end

execute 'add to the list using the chkconfig command' do
  command 'chkconfig --add nginx && chkconfig nginx on'
  not_if 'chkconfig --list nginx'
end

template "#{node['nginx']['dir']}/nginx.conf" do
  source 'nginx.conf.erb'
  owner 'root'
  group 'root'
  mode '0644'
  notifies :reload, 'service[nginx]'
end

%w(default nginx_status).each do |site|
  template "#{node['nginx']['dir']}/sites-available/#{site}" do
    source "#{site}.erb"
    owner 'root'
    group 'root'
    mode '0644'
  end
end

%w(default nginx_status).each do |site|
  link "#{node['nginx']['dir']}/sites-enabled/#{site}" do
    to "#{node['nginx']['dir']}/sites-available/#{site}"
    notifies :reload, 'service[nginx]'
  end
end

service 'nginx' do
  action [:enable, :start]
end

テンプレート

終了ステータスとは

  • コマンド終了のとき、終了ステータスと呼ばれるコマンドの成否を表す数値が$?に自動で保持される。
  • コマンド成功のときは0、失敗のときは1(0以外)が設定される。

終了ステータスの確認

  • nginxがserviceとして登録されているのか確認したい。
  • chkconfig --listで確認するとserviceに登録されているdaemonのリストが表示される。
  • ここで表示されている場合はserviceで登録されていると見なす。
  • serviceに登録されている場合
$ chkconfig --list nginx
nginx           0:off   1:off   2:on    3:on    4:on    5:on    6:off
$ echo $?
0
  • serviceで登録されていない場合
$ chkconfig --list nginx
error reading information on service nginx: No such file or directory
$ echo $?
1
  • not_ifではブロックの場合、真偽で判断する。文字列の場合はコマンドの成功(0)や失敗(0以外)で判断する。

参考

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2