Skip to content

Instantly share code, notes, and snippets.

@skippy
Created September 16, 2011 05:18
Show Gist options
  • Save skippy/1221259 to your computer and use it in GitHub Desktop.
Save skippy/1221259 to your computer and use it in GitHub Desktop.
chef nginx source.rb
# NOTES ON CHANGE:
# * runit_service will launch the service, so put it at the end
# * because of the immediate restart needs, do some ugly stuff at the end to deal with that
# * concern: TOO complex; the immediate restarts should not be needed...
#
#
# Cookbook Name:: nginx
# Recipe:: source
#
# Author:: Adam Jacob (<[email protected]>)
# Author:: Joshua Timberman (<[email protected]>)
#
# Copyright 2009-2011, Opscode, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
include_recipe "build-essential"
unless platform?("centos","redhat","fedora")
include_recipe "runit"
end
packages = value_for_platform(
["centos","redhat","fedora"] => {'default' => ['pcre-devel', 'openssl-devel']},
"default" => ['libpcre3', 'libpcre3-dev', 'libssl-dev']
)
packages.each do |devpkg|
package devpkg
end
nginx_version = node[:nginx][:version]
node.set[:nginx][:install_path] = "/opt/nginx-#{nginx_version}"
node.set[:nginx][:src_binary] = "#{node[:nginx][:install_path]}/sbin/nginx"
node.set[:nginx][:daemon_disable] = true
node.set[:nginx][:configure_flags] = [
"--prefix=#{node[:nginx][:install_path]}",
"--conf-path=#{node[:nginx][:dir]}/nginx.conf",
"--http-log-path=#{node[:nginx][:log_dir]}/access.log",
"--error-log-path=#{node[:nginx][:log_dir]}/error.log",
"--http-client-body-temp-path=/var/tmp/nginx/client",
"--http-proxy-temp-path=/var/tmp/nginx/proxy",
"--with-http_ssl_module",
"--with-http_gzip_static_module"
]
configure_flags = node[:nginx][:configure_flags].join(" ")
remote_file "#{Chef::Config[:file_cache_path]}/nginx-#{nginx_version}.tar.gz" do
source "http://nginx.org/download/nginx-#{nginx_version}.tar.gz"
action :create_if_missing
end
bash "compile_nginx_source" do
cwd Chef::Config[:file_cache_path]
code <<-EOH
tar zxf nginx-#{nginx_version}.tar.gz
cd nginx-#{nginx_version} && ./configure #{configure_flags}
make && make install
EOH
creates node[:nginx][:src_binary]
notifies :create, "ruby_block[restart-nginx]"
end
directory node[:nginx][:log_dir] do
mode 0755
owner node[:nginx][:user]
action :create
end
directory node[:nginx][:dir] do
owner "root"
group "root"
mode "0755"
end
directory node['nginx']['client_temp'] do
owner "root"
group "root"
mode 0755
recursive true
end
%w{ sites-available sites-enabled conf.d }.each do |dir|
directory "#{node[:nginx][:dir]}/#{dir}" do
owner "root"
group "root"
mode "0755"
end
end
%w{nxensite nxdissite}.each do |nxscript|
template "/usr/sbin/#{nxscript}" do
source "#{nxscript}.erb"
mode "0755"
owner "root"
group "root"
end
end
template "nginx.conf" do
path "#{node[:nginx][:dir]}/nginx.conf"
source "nginx.conf.erb"
owner "root"
group "root"
mode "0644"
notifies :create, "ruby_block[restart-nginx]", :immediately
end
cookbook_file "#{node[:nginx][:dir]}/mime.types" do
source "mime.types"
owner "root"
group "root"
mode "0644"
notifies :create, "ruby_block[restart-nginx]", :immediately
end
unless platform?("centos","redhat","fedora")
runit_service "nginx" do
run_restart false
end
else
#install init db script
template "/etc/init.d/nginx" do
source "nginx.init.erb"
owner "root"
group "root"
mode "0755"
end
#install sysconfig file (not really needed but standard)
template "/etc/sysconfig/nginx" do
source "nginx.sysconfig.erb"
owner "root"
group "root"
mode "0644"
end
#register service
service "nginx" do
supports :status => true, :restart => true, :reload => true
action :enable
end
end
Thread.current[:restart_nginx] = false
ruby_block "restart-nginx" do
block do
Thread.current[:restart_nginx] = true
end
action :nothing
end
ruby_block "restart-nginx-final" do
block do
end
notifies :restart, resources(:service => "nginx"), :immediately
only_if{ Thread.current[:restart_nginx] }
end
# NOTES ON CHANGE:
# * runit_service will launch the service, so put it at the end
# * simplifying by making no immediate restart requests. Assuming
# that changes to app config files will be picked up by nginx_site
# recipe, and nginx.conf can wait until the end
#
#
# Cookbook Name:: nginx
# Recipe:: source
#
# Author:: Adam Jacob (<[email protected]>)
# Author:: Joshua Timberman (<[email protected]>)
#
# Copyright 2009-2011, Opscode, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
include_recipe "build-essential"
unless platform?("centos","redhat","fedora")
include_recipe "runit"
end
packages = value_for_platform(
["centos","redhat","fedora"] => {'default' => ['pcre-devel', 'openssl-devel']},
"default" => ['libpcre3', 'libpcre3-dev', 'libssl-dev']
)
packages.each do |devpkg|
package devpkg
end
service 'nginx' do
action :nothing
end
nginx_version = node[:nginx][:version]
node.set[:nginx][:install_path] = "/opt/nginx-#{nginx_version}"
node.set[:nginx][:src_binary] = "#{node[:nginx][:install_path]}/sbin/nginx"
node.set[:nginx][:daemon_disable] = true
node.set[:nginx][:configure_flags] = [
"--prefix=#{node[:nginx][:install_path]}",
"--conf-path=#{node[:nginx][:dir]}/nginx.conf",
"--http-log-path=#{node[:nginx][:log_dir]}/access.log",
"--error-log-path=#{node[:nginx][:log_dir]}/error.log",
"--http-client-body-temp-path=/var/tmp/nginx/client",
"--http-proxy-temp-path=/var/tmp/nginx/proxy",
"--with-http_ssl_module",
"--with-http_gzip_static_module"
]
configure_flags = node[:nginx][:configure_flags].join(" ")
remote_file "#{Chef::Config[:file_cache_path]}/nginx-#{nginx_version}.tar.gz" do
source "http://nginx.org/download/nginx-#{nginx_version}.tar.gz"
action :create_if_missing
end
bash "compile_nginx_source" do
cwd Chef::Config[:file_cache_path]
code <<-EOH
tar zxf nginx-#{nginx_version}.tar.gz
cd nginx-#{nginx_version} && ./configure #{configure_flags}
make && make install
EOH
creates node[:nginx][:src_binary]
notifies :restart, resources(:service => "nginx"), :delayed
end
directory node[:nginx][:log_dir] do
mode 0755
owner node[:nginx][:user]
action :create
end
directory node[:nginx][:dir] do
owner "root"
group "root"
mode "0755"
end
directory node['nginx']['client_temp'] do
owner "root"
group "root"
mode 0755
recursive true
end
%w{ sites-available sites-enabled conf.d }.each do |dir|
directory "#{node[:nginx][:dir]}/#{dir}" do
owner "root"
group "root"
mode "0755"
end
end
%w{nxensite nxdissite}.each do |nxscript|
template "/usr/sbin/#{nxscript}" do
source "#{nxscript}.erb"
mode "0755"
owner "root"
group "root"
end
end
template "nginx.conf" do
path "#{node[:nginx][:dir]}/nginx.conf"
source "nginx.conf.erb"
owner "root"
group "root"
mode "0644"
notifies :restart, resources(:service => "nginx"), :delayed
end
cookbook_file "#{node[:nginx][:dir]}/mime.types" do
source "mime.types"
owner "root"
group "root"
mode "0644"
notifies :restart, resources(:service => "nginx"), :delayed
end
unless platform?("centos","redhat","fedora")
runit_service "nginx" do
run_restart false
end
else
#install init db script
template "/etc/init.d/nginx" do
source "nginx.init.erb"
owner "root"
group "root"
mode "0755"
end
#install sysconfig file (not really needed but standard)
template "/etc/sysconfig/nginx" do
source "nginx.sysconfig.erb"
owner "root"
group "root"
mode "0644"
end
#register service
service "nginx" do
supports :status => true, :restart => true, :reload => true
action :enable
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment