Chef,如何在创建服务之前运行创建init.d脚本的模板

时间:2012-03-30 06:56:49

标签: chef

下面是我的nginx模板。

我正面临一个问题22.我需要安装一个init.d模板。所以我在/etc/init.d/nginx中有一个nginx erb模板。

我甚至尝试将代码置于食谱之上。配方依赖于init.d文件但它没有被激活,直到它为时已晚,结果我得到了这个错误:

STDERR: update-rc.d: /etc/init.d/nginx: file does not exist
---- End output of "bash"  "/tmp/chef-script20120330-26326-3ologp-0" ----
Ran "bash"  "/tmp/chef-script20120330-26326-3ologp-0" returned 1
[Fri, 30 Mar 2012 06:22:15 +0000] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
[Fri, 30 Mar 2012 06:22:15 +0000] ERROR: Sleeping for 60 seconds before trying again

在下一次厨师客户端运行时,事情有效,因为这样就形成了模板。

如何在注册nginx的服务资源之前立即执行模板?一个快速解决方法是我创建一个配方,在nginx之前运行模板来安装模板,但这似乎相当荒谬。

需要在注册服务之前安装此模板。

template "nginx" do
      path "/etc/init.d/nginx"
      source "nginx.erb"
      owner "root"
      group "root"
      mode "0755"
    end

然后注册服务器。

service "nginx" do
      supports :restart => true, :start => true, :stop => true, :reload => true
      action [ :enable, :start]
    end







####################################
#Install Nginx
####################################
#http://wiki.nginx.org/HttpLuaModule#Installation
version = node[:nginx][:version]
package "libpcre3" do
  action :install
end
package "libpcre3-dev" do
  action :install
end
package "libpcre++-dev" do
  action :install
end
package "openssl" do
  action :install
end

template "nginx" do
  path "/etc/init.d/nginx"
  source "nginx.erb"
  owner "root"
  group "root"
  mode "0755"
end

#mkdir /var/log/nginx
directory "/var/log/nginx" do
  owner "root"
  group "root"
  mode "0755"
  action :create
  #not_if "test -f /etc/nginx/lock"
end

remote_file "/tmp/nginx-#{version}.tar.gz" do
  source "http://nginx.org/download/nginx-#{version}.tar.gz"
  checksum node[:nginx][:checksum]
  action :create_if_missing
  notifies :run, "bash[install_nginx]", :immediately
end


bash "install_nginx" do
  user "root"
  cwd "/tmp"
  code <<-EOH
    tar -xvf nginx-#{version}.tar.gz
    cd nginx-#{version}
    ./configure --with-http_stub_status_module  
    make -j2
    make install
    ln -s /usr/local/nginx/conf/ /etc/nginx
    /usr/sbin/update-rc.d -f nginx defaults
  EOH
  action :nothing
  creates "/usr/sbin/nginx"
  notifies :restart, "service[nginx]"
  #not_if "test -f /etc/redis/lock"
end


service "nginx" do
  supports :restart => true, :start => true, :stop => true, :reload => true
  action [ :enable, :start]
end

#ln -s /usr/local/nginx/conf/ /etc/nginx
#link "/usr/local/nginx/conf/" do
#  to "/etc/nginx"
#  link_type :hard
#end

link "/usr/local/nginx/logs/access.log" do
  to "/var/log/nginx/access.log"
end

link "/usr/local/nginx/logs/error.log" do
  to "/var/log/nginx/error.log"
end

file "/etc/nginx/lock" do
  owner "root"
  group "root"
  mode "0755"
  action :create_if_missing
end

template "nginx.conf" do
  path "/etc/nginx/nginx.conf"
  source "nginx.conf.erb"
  owner "root"
  group "root"
  mode "0644"
  #notifies :reload, "service[nginx]"
  notifies :reload, resources(:service => "nginx"), :immediately
end

由于

3 个答案:

答案 0 :(得分:45)

您可以将服务上的操作设置为:nothing。然后让模板通知服务启用。这样,模板将被处理,然后服务将被启用:

service "nginx" do
  supports :restart => true, :start => true, :stop => true, :reload => true
  action :nothing
end 

template "nginx" do
  path "/etc/init.d/nginx"
  source "nginx.erb"
  owner "root"
  group "root"
  mode "0755"
  notifies :enable, "service[nginx]"
  notifies :start, "service[nginx]"
end

然后服务将等待模板处理完毕。

答案 1 :(得分:5)

template "nginx" do
    path "/etc/init.d/nginx"
    source "nginx.erb"
    owner "root"
    group "root"
    mode "0755"
end

service "nginx" do
    supports :restart => true, :start => true, :stop => true, :reload => true
    action [ :enable, :start ]
    subscribes :restart, "template[nginx]", :immediately
end 

答案 2 :(得分:-2)

虽然在这种情况下使用notifies更好,但在其他情况下,您没有那么奢侈。以下是指示Chef立即创建模板的方法:

tpl = template "nginx" do
  action :nothing
  path "/etc/init.d/nginx"
  source "nginx.erb"
  owner "root"
  group "root"
  mode "0755"
end
tpl.run_action(:create)