Skip to content
Browse files

Allow non-bundled nginx modules to be compiled into nginx when using …

…the ::source cookbook. Completes COOK-1038.
  • Loading branch information...
1 parent 316fc24 commit d56bddeae72ea78aa5416874084ac5897057de06 @freerobby committed
Showing with 116 additions and 2 deletions.
  1. +58 −1 nginx/README.md
  2. +1 −0 nginx/attributes/default.rb
  3. +5 −1 nginx/metadata.rb
  4. +52 −0 nginx/recipes/source.rb
View
59 nginx/README.md
@@ -41,7 +41,8 @@ All node attributes are set under the `nginx` namespace.
* `worker_processes` - number of workers to spawn.
* `worker_connections` - number of connections per worker.
* `server_names_hash_bucket_size`
-* `additional_modules` - additional nginx modules to install (compatible with automatically-set configure_flags)
+* `additional_modules` - array additional nginx module names to install (compatible with automatically-set configure_flags)
+* `additional_src_modules` - hash of additional non-bundled nginx modules to install (compatible with automatically-set configure_flags). See Additional Source Modules section for details.
The following attributes are set at the 'normal' node level via the `nginx::source` recipe.
@@ -49,6 +50,62 @@ The following attributes are set at the 'normal' node level via the `nginx::sour
* `src_binary` - for nginx::source, sets the binary location.
* `configure_flags` - for nginx::source, an array of flags to use for compilation.
+Additional Source Modules
+-------------------------
+
+When using nginx::source, you may compile nginx with additional modules that do not come with the nginx sources package. The `additional_src_modules` hash takes keys named after nginx modules and values of hashes, each containing the following elements:
+
+* "local" - Path to custom nginx module tarfile on local machine. .tar, .tar.gz and .tgz type packages are supported.
+* "http" - URL for custom nginx module.
+* "save\_as" - Sets the filename to use for local storage when pulling from an HTTP source. Useful when pulling from sources that do not have correct names or extensions. If omitted, uses filename from HTTP request.
+* "module\_folder" - Overrides folder name that is expected to be in the tarfile. If omitted, uses name of the tarfile without the extension.
+
+You must supply either "http" or "local". "local" will take precedence if both are supplied.
+
+Here is a comprehensive example of what our role looks like if we want to install the set-misc module via Github:
+
+ override_attributes(
+ :nginx => {
+ :additional_src_modules => {
+ "ngx_devel_kit" => {
+ "http" => "https://github.com/simpl/ngx_devel_kit/tarball/v0.2.16",
+ "save_as" => "simpl-ngx_devel_kit-83bcfaf.tar.gz" #,
+ # "module_folder" => "simpl-ngx_devel_kit-83bcfaf" # Not needed because it's the same as the filename
+ },
+ "set-misc-nginx-module" => {
+ "http" => "https://github.com/agentzh/set-misc-nginx-module/tarball/v0.21",
+ "save_as" => "agentzh-set-misc-nginx-module-4b0512a.tgz" #,
+ # "module_folder" => "agentzh-set-misc-nginx-module-4b0512a" # Not needed because it's the same as the filename
+ }
+ }
+ }
+ )
+
+Here is what it looks like if we're installing it from a file that's already on our box:
+
+ override_attributes(
+ :nginx => {
+ :additional_src_modules => {
+ "ngx_devel_kit" => {
+ "local" => "/data/modules/nginx_devel_kit.tar.gz",
+ "module_folder" => "simpl-ngx_devel_kit-83bcfaf"
+ },
+ "set-misc-nginx-module" => {
+ "local" => "/data/modules/agentzh-set-misc-nginx-module-4b0512a.tgz" #,
+ # "module_folder" => "agentzh-set-misc-nginx-module-4b0512a" # Not needed because it's the same as the filename.
+ }
+ }
+ }
+ )
+
+If we run `nginx -V` we see it was compiled with these additional modules:
+
+ $ ./nginx -V
+ nginx version: nginx/1.0.12
+ built by gcc 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
+ TLS SNI support enabled
+ configure arguments: --add-module=/var/chef/cache/agentzh-set-misc-nginx-module-4b0512a --add-module=/var/chef/cache/simpl-ngx_devel_kit-83bcfaf
+
Usage
=====
View
1 nginx/attributes/default.rb
@@ -62,3 +62,4 @@
default[:nginx][:disable_access_log] = false
default[:nginx][:additional_modules] = ["http_ssl_module", "http_gzip_static_module"]
+default[:nginx][:additional_src_modules] = {}
View
6 nginx/metadata.rb
@@ -90,4 +90,8 @@
attribute "nginx/additional_modules",
:display_name => "Additional Modules",
- :default => "http_ssl_module, http_gzip_static_module"
+ :default => "http_ssl_module, http_gzip_static_module"
+
+attribute "nginx/additional_src_modules",
+ :display_name => "Additional Source Modules",
+ :default => "{}"
View
52 nginx/recipes/source.rb
@@ -31,6 +31,57 @@
package devpkg
end
+# Download custom nginx modules
+custom_nginx_module_sources = []
+
+node[:nginx][:additional_src_modules].each do |nginx_module_name, hash_contents|
+ local_filename = ""
+ local_filepath = ""
+ local_folder = ""
+ local_extension = ""
+
+ # If using a local package, set our variables.
+ if hash_contents.has_key?("local")
+ local_filepath = hash_contents["local"]
+ # "/var/x.y/some-nginx-module.tar.gz".rpartition("/") => ["/var/x.y", "/", "some-nginx-module.tar.gz"]
+ local_filename = local_filepath.rpartition("/")[2]
+ # "some-nginx-module.tar.gz".partition(".") => ["some-nginx-module", ".", "tar.gz"]
+ local_folder = hash_contents.has_key?("module_folder") ? hash_contents["module_folder"] : local_filename.partition(".")[0]
+ local_extension = local_filename.partition(".")[1..2].join
+
+ # If using HTTP, assign variables and grab the file
+ elsif hash_contents.has_key?("http")
+ # "http://google.com/a/b/c/d.tar".rpartition("/") => ["http://google.com/a/b/c", "/", "d.tar"]
+ local_filename = hash_contents.has_key?("save_as") ? hash_contents["save_as"] : hash_contents["http"].rpartition("/")[2]
+ local_filepath = "#{Chef::Config[:file_cache_path]}/#{local_filename}"
+ # "some-nginx-module.tar.gz".partition(".") => ["some-nginx-module", ".", "tar.gz"]
+ local_folder = hash_contents.has_key?("module_folder") ? hash_contents["module_folder"] : local_filename.partition(".")[0]
+ local_extension = local_filename.partition(".")[1..2].join
+
+ remote_file local_filepath do
+ source hash_contents["http"]
+ action :create_if_missing
+ end
+ end
+
+ # Extraction dependent on file type
+ if local_extension == ".tgz" || local_extension == ".tar.gz"
+ bash "extract_#{nginx_module_name}" do
+ cwd Chef::Config[:file_cache_path]
+ code "tar zxf #{local_filepath}"
+ end
+ elsif local_extension == ".tar"
+ bash "extract_#{nginx_module_name}" do
+ cwd Chef::Config[:file_cache_path]
+ code "tar xf #{local_filepath}"
+ end
+ else
+ raise "Extension: #{local_extension} not supported for custom-sourced nginx modules."
+ end
+
+ custom_nginx_module_sources << "#{Chef::Config[:file_cache_path]}/#{local_folder}"
+end
+
nginx_version = node[:nginx][:version]
node.set[:nginx][:install_path] = "/opt/nginx-#{nginx_version}"
@@ -41,6 +92,7 @@
"--conf-path=#{node[:nginx][:dir]}/nginx.conf"
]
node[:nginx][:additional_modules].uniq.each {|m| node[:nginx][:configure_flags] += ["--with-#{m}"]}
+custom_nginx_module_sources.each {|s| node[:nginx][:configure_flags] += ["--add-module=#{s}"]}
configure_flags = node[:nginx][:configure_flags].join(" ")

0 comments on commit d56bdde

Please sign in to comment.
Something went wrong with that request. Please try again.