nginx_source.rb |
|
---|---|
Variables |
|
The directory that the tarballs will be downloaded to. |
cache_dir = "/var/cache/downloads" |
The full URL to download the nginx tarball. |
tar_url = "http://nginx.org/download/nginx-0.9.7.tar.gz" |
The SHA256 checksum for the downloaded file. Chef can check to see if the downloaded tarball copy validates and if so it will skip going to the network to re-download and verify. |
tar_checksum = "2feb0acee473cc360a620ee862907b9570a4121956c40cbd27da35f5b0a96045" |
The name of the tarball without the URL at the beggining. There is more than one way to do this, but splits are fun. |
tar_file = tar_url.split('/').last |
The directory that will get created when extracting the tarball.
Same string mangling tricks here too but this time using |
tar_dir = tar_file.sub(/\.tar\.gz$/, '') |
The configure flags used when configuring nginx. We’re targetting
a custom directory in |
configure_flags = [
"--prefix=/opt/#{tar_dir}",
"--conf-path=/etc/nginx/nginx.conf",
"--with-http_ssl_module"
] |
A list of Ubuntu packages that must be installed prior to
compiling nginx. We don’t need wget anymore as Chef will
download files for us just fine. The funny |
pkgs = %w{ build-essential binutils-doc autoconf flex bison
libpcre3 libpcre3-dev libssl-dev } |
Install Pre-requisite Packages |
|
Loop through the The |
pkgs.each do |pkg|
package pkg do
action :install
end
end |
Fetching and Extraction |
|
Ensure that the |
directory cache_dir do
owner "root"
group "root"
mode "0755"
recursive true
action :create
end |
Download the the nginx tarball into the The |
remote_file "#{cache_dir}/#{tar_file}" do
source tar_url
checksum tar_checksum
owner "root"
group "root"
mode "0644"
end |
Next, extract the downloaded tarball into The We’re using a |
execute "extract nginx tarball" do
user "root"
group "root"
cwd cache_dir
command %{tar zxf #{tar_file}}
creates "#{cache_dir}/#{tar_dir}"
end |
Compilation and Installation |
|
The most likely place for something to go wrong :) nginx
follows a very standard (and sane) build process of running
We’re using |
execute "compile nginx" do
user "root"
group "root"
cwd "#{cache_dir}/#{tar_dir}"
command %{./configure #{configure_flags.join(' ')} && make && make install}
creates "/opt/#{tar_dir}/sbin/nginx"
notifies :restart, "service[nginx]"
end |
Now we’ll loop through a few directories that need to exist for nginx to start. Here is a more traditional Ruby array of strings, just to be different. |
[ "/var/log/nginx", "/etc/nginx",
"/etc/nginx/conf.d", "/etc/nginx/sites-enabled"
].each do |dir|
directory dir do
owner "root"
group "root"
mode "0755"
recursive true
action :create
end
end |
A There are some tricks to storing multiple source files for different operating systems and platform versions, but that’s out of scope here. |
cookbook_file "/etc/nginx/nginx.conf" do
source "nginx.conf"
owner "root"
group "root"
mode "0755"
notifies :restart, "service[nginx]"
end |
Same thing with our default nginx site configuration. |
cookbook_file "/etc/nginx/sites-enabled/_default.conf" do
source "default-site.conf"
owner "root"
group "root"
mode "0755"
notifies :restart, "service[nginx]"
end |
Same here with our init.d rc script. |
cookbook_file "/etc/init.d/nginx" do
source "nginx.init"
owner "root"
group "root"
mode "0755"
notifies :restart, "service[nginx]"
end |
Finally we can register a service called Since we are managing our own rc script, we can tell Chef
what functionality our script supports such as being able to
restart, reload, and get a status. Setting these values to
false will cause Chef to use other tricks (possibly grepping
through Once a serive is registered it can get notified to restart, stop,
etc. from other Chef resources. This is what the
Finally, we have 2 actions here which is different. We want to enable (register with the operating system) and start nginx. Fancypants! |
service "nginx" do
supports :restart => true, :reload => true, :status => true
action [ :enable, :start ]
end |
And that’s it. Are you still here? I thought you’d be out buying a coffee with all this spare time you’ve freed up. |
|