Skip to content

Instantly share code, notes, and snippets.

@tsabat
Created July 31, 2013 22:05
Show Gist options
  • Save tsabat/a8f27ae6ac7d1fd3b6f7 to your computer and use it in GitHub Desktop.
Save tsabat/a8f27ae6ac7d1fd3b6f7 to your computer and use it in GitHub Desktop.
Problem with nginx recipe

I'm having trouble with the nginx recipe from opscode. It seems like it never picks up on a variable I set in my attributes.

I have a "base" recipe, with an attributes/default.rb file that looks like this:

default.nginx.install_method = 'source'
default.nginx.source.version = '1.5.3'

That base recipe eventualy calls include_recipe "nginx::source"

But it fails, as shown here:

Resource Declaration:
---------------------
# In /tmp/vagrant-chef-1/chef-solo-1/cookbooks/nginx/recipes/source.rb

 84: bash "compile_nginx_source" do
 85:   cwd ::File.dirname(src_filepath)
 86:   code <<-EOH
 87:     tar zxf #{::File.basename(src_filepath)} -C #{::File.dirname(src_filepath)} &&
 88:     cd nginx-#{node['nginx']['source']['version']} &&
 89:     ./configure #{node.run_state['nginx_configure_flags'].join(" ")} &&
 90:     make && make install
 91:   EOH
 92:
 93:   not_if do
 94:     nginx_force_recompile == false &&
 95:       node.automatic_attrs['nginx'] &&
 96:       node.automatic_attrs['nginx']['version'] == node['nginx']['source']['version'] &&
 97:       node.automatic_attrs['nginx']['configure_arguments'].sort == configure_flags.sort
 98:   end
 99:
100:   notifies :restart, "service[nginx]"
101: end
102:



Compiled Resource:
------------------
# Declared in /tmp/vagrant-chef-1/chef-solo-1/cookbooks/nginx/recipes/source.rb:84:in `from_file'

bash("compile_nginx_source") do
  action "run"
  retries 0
  retry_delay 2
  command "\"bash\"  \"/tmp/chef-script20130731-14469-f1tw0d\""
  backup 5
  cwd "/var/chef/cache"
  returns 0
  code "    tar zxf nginx-1.5.3.tar.gz -C /var/chef/cache &&\n    cd nginx-1.5.3 &&\n    ./configure --prefix=/opt/nginx-1.2.6 --conf-path=/etc/nginx/nginx.conf --sbin-path=/opt/ngi
nx-1.2.6/sbin/nginx --with-http_ssl_module --with-http_gzip_static_module &&\n    make && make install\n"
  interpreter "bash"
  cookbook_name :nginx
  recipe_name "source"
  not_if { #code block }
end

which fails with cd: nginx-1.5.3: No such file or directory

Some weirdness:

  • the --prefix is nginx-1.2.6, which does not make any sense, since the prefix is built from the version here
  • the nginx download url seems to fail too. It's built here

So, basically it seems I'm maybe setting my attributes incorrectly?

@shawnzhu
Copy link

shawnzhu commented Aug 1, 2013

It uses attributes/default.rb where it overrides the attributes default.nginx.source.version by the attribute default.nginx.version, so you got the version 1.2.6 in --prefix compiling flag. So the solution is, provide another attribute default.nginx.version = '1.5.3'

@tsabat
Copy link
Author

tsabat commented Aug 1, 2013

Even with that overridden, I still end up with the funky prefix.

my attributes:

default.nginx.install_method = 'source'
default.nginx.source.version = '1.5.3'
default.nginx.version = '1.5.3'

The line with the bad output:

Compiled Resource:
------------------
# Declared in /tmp/vagrant-chef-1/chef-solo-1/cookbooks/nginx/recipes/source.rb:84:in `from_file'

bash("compile_nginx_source") do
  action "run"
...snip
  code "    tar zxf nginx-1.5.3.tar.gz -C /var/chef/cache &&\n    cd nginx-1.5.3 &&\n    ./configure --prefix=/opt/nginx-1.2.6 --conf-pnx-1.2.6/sbin/nginx --with-http_ssl_module --with-http_gzip_static_module &&\n    make && make install\n"
...snip
end

@justsee
Copy link

justsee commented Aug 19, 2013

You need to override additional variables: prefix, url, sbin_path, and default_configure_flags:

set['nginx']['version'] = "1.5.3"
set['nginx']['source']['version'] = "1.5.3"
set['nginx']['source']['checksum'] = "edcdf2030750b4eb1ba8cd79365c16a3e33e6136b7fdd8a1a7b4082397f4e92b"
set['nginx']['source']['prefix'] = "/opt/nginx-#{node['nginx']['source']['version']}"
set['nginx']['source']['url'] = "http://nginx.org/download/nginx-#{node['nginx']['source']['version']}.tar.gz"
set['nginx']['source']['sbin_path'] = "#{node['nginx']['source']['prefix']}/sbin/nginx"
set['nginx']['source']['default_configure_flags'] = [
  "--prefix=#{node['nginx']['source']['prefix']}",
  "--conf-path=#{node['nginx']['dir']}/nginx.conf",
  "--sbin-path=#{node['nginx']['source']['sbin_path']}"
]

This is because these variables embed other variables, and they are set using values that have not yet been overriden by your values.

By specifying these additional variables in your wrapper cookbook, you ensure they are using all the updated values you expect.

Also, because the default values are already set in the nginx cookbook, it's better to use something like set (an alias for normal) as that more accurately describes what you are doing and has a higher attribute precedence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment