Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nginx_site resource doesn't work because it fails to reload the nginx service #505

Closed
krozett opened this issue Aug 15, 2019 · 5 comments
Closed
Assignees

Comments

@krozett
Copy link

krozett commented Aug 15, 2019

🗣️ Foreword

Thank for taking the time to fill this bug report fully. Without it we may not be able to fix the bug, and the issue may be closed without resolution.

👻 Brief Description

Reloading the nginx service fails as part of the nginx_site process

🥞 Cookbook version

10.0.1

👩‍🍳 Chef-Infra Version

15.2.20

🎩 Platform details

Ubuntu 18.04, running on VirtualBox.
Ruby 2.6.3p62

Steps To Reproduce

Steps to reproduce the behavior:

  1. Create and bootstrap a VM.
  2. Create a new cookbook: chef generate cookbook monster
  3. Edit monster/metadata.rb, adding the nginx dependency: depends 'nginx', '~> 10.0.1'
  4. Edit monster/recipes/default.rb, and add these 2 lines at the end:
nginx_install 'distro'
nginx_site 'donuts' do
  cookbook 'monster'
  template 'yumyum.rb'
end
  1. Add a minimal template file called yumyum.rb with the following content:
server {
  server_name localhost;
  listen 80;
  root /var/www/;
  index index.html;
}
  1. Upload the nginx cookbook (version 10.0.1) onto the Chef server (I'm using Hosted Chef).
  2. Upload your monster cookbook: knife cookbook upload monster
  3. Set the VM node's run list: knife node run_list set vagrant 'recipe[monster]
  4. SSH into the VM: vagrant ssh
  5. Run Chef Client: sudo chef-client

🚓 Expected behavior

nginx should be installed and a bare-bones site called 'donuts' should be set up. The nginx service should also be running.

➕ Additional context

Here's what I get instead of a successful run:

Starting Chef Infra Client, version 15.2.20
resolving cookbooks for run list: ["monster"]
Synchronizing Cookbooks:
  - ohai (5.2.0)
  - nginx (10.0.1)
  - monster (0.1.7)
Installing Cookbook Gems:
Compiling Cookbooks...
[2019-08-15T07:47:32+00:00] WARN: Resource ohai_hint from the client is overriding the resource from a cookbook. Please upgrade your cookbook or remove the cookbook from your run_list.
Converging 2 resources
Recipe: monster::default
  * nginx_install[distro] action install
    * ohai_plugin[nginx] action create
      * directory[/etc/chef/ohai/plugins] action create (skipped due to not_if)
      * template[/etc/chef/ohai/plugins/nginx.rb] action create (up to date)
      * ohai[nginx] action nothing (skipped due to action :nothing)
       (up to date)
    * ohai[reload_nginx] action nothing (skipped due to action :nothing)
    * ohai_plugin[nginx] action create
      * directory[/etc/chef/ohai/plugins] action create (skipped due to not_if)
      * template[/etc/chef/ohai/plugins/nginx.rb] action create (up to date)
      * ohai[nginx] action nothing (skipped due to action :nothing)
       (up to date)
    * log[Using distro provided packages.] action write
    
    * apt_package[nginx] action install (up to date)
    * directory[/etc/nginx] action create (up to date)
    * directory[/var/log/nginx] action create (up to date)
    * directory[/run] action create (up to date)
    * directory[/etc/nginx/sites-available] action create (up to date)
    * directory[/etc/nginx/sites-enabled] action create (up to date)
    * directory[/etc/nginx/conf.d] action create (up to date)
    * directory[/etc/nginx/streams-available] action create (up to date)
    * directory[/etc/nginx/streams-enabled] action create (up to date)
    * template[/usr/sbin/nxensite] action create (up to date)
    * template[/usr/sbin/nxdissite] action create (up to date)
    * template[/usr/sbin/nxenstream] action create (up to date)
    * template[/usr/sbin/nxdisstream] action create (up to date)
    * nginx_config[/etc/nginx/nginx.conf] action create
      * template[/etc/nginx/nginx.conf] action create (up to date)
       (up to date)
    * template[/etc/nginx/sites-available/default] action create (up to date)
    * nginx_site[default] action enable
      * execute[nxensite default] action run (skipped due to not_if)
       (up to date)
    * service[nginx] action start (up to date)
    * service[nginx] action enable (up to date)
  
  * nginx_site[donuts] action enable
    
    ================================================================================
    Error executing action `enable` on resource 'nginx_site[donuts]'
    ================================================================================
    
    Chef::Exceptions::ResourceNotFound
    ----------------------------------
    resource template[/etc/nginx/sites-available/donuts] is configured to notify resource service[nginx] with action reload, but service[nginx] cannot be found in the resource collection. template[/etc/nginx/sites-available/donuts] is defined in /var/chef/cache/cookbooks/nginx/resources/site.rb:18:in `block in class_from_file'
    
    Resource Declaration:
    ---------------------
    # In /var/chef/cache/cookbooks/monster/recipes/default.rb
    
      8: nginx_site 'donuts' do
      9:   cookbook 'monster'
     10:   template 'yumyum.rb'
     11: end
     12: 
    
    Compiled Resource:
    ------------------
    # Declared in /var/chef/cache/cookbooks/monster/recipes/default.rb:8:in `from_file'
    
    nginx_site("donuts") do
      action [:enable]
      default_guard_interpreter :default
      declared_type :nginx_site
      cookbook_name "monster"
      recipe_name "default"
      cookbook "monster"
      template "yumyum.rb"
      site_name "donuts"
    end
    
    System Info:
    ------------
    chef_version=15.2.20
    platform=ubuntu
    platform_version=18.04
    ruby=ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
    program_name=/usr/bin/chef-client
    executable=/opt/chef/bin/chef-client
    

Running handlers:
[2019-08-15T07:47:32+00:00] ERROR: Running exception handlers
Running handlers complete
[2019-08-15T07:47:32+00:00] ERROR: Exception handlers complete
Chef Infra Client failed. 2 resources updated in 04 seconds
[2019-08-15T07:47:33+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
[2019-08-15T07:47:33+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
[2019-08-15T07:47:33+00:00] FATAL: Chef::Exceptions::ResourceNotFound: nginx_site[donuts] (monster::default line 8) had an error: Chef::Exceptions::ResourceNotFound: resource template[/etc/nginx/sites-available/donuts] is configured to notify resource service[nginx] with action reload, but service[nginx] cannot be found in the resource collection. template[/etc/nginx/sites-available/donuts] is defined in /var/chef/cache/cookbooks/nginx/resources/site.rb:18:in `block in class_from_file'

I can verify nginx is running, but it has no /etc/nginx/sites-enabled/donuts.

What's strange is that if you look just above the first error, you can see this line:
* service[nginx] action enable (up to date)
So the script clearly knows that service[nginx] exists at some level.

Skimming the code I found this line:

service 'nginx' do

A perfect declaration of the nginx service resource! So why isn't the nginx_site resource seeing it? Is it a scope issue with Chef?

BTW, I discovered I can work around this bug by copy/pasting that exact service resource block into my default recipe. Everything works fine then... but that seems like it's the nginx_site resource's responsibility to set that up correctly.

@Lasering
Copy link

I can confirm. nginx_site complains:
resource template[/etc/nginx/sites-available/site] is configured to notify resource service[nginx] with action reload, but service[nginx] cannot be found in the resource collection. template[/etc/nginx/sites-available/site] is defined in /tmp/kitchen/cache/cookbooks/nginx/resources/site.rb:18:in block in class_from_file'`

@ITJamie
Copy link
Contributor

ITJamie commented Aug 26, 2019

This appears to be the same issue as the apache2 cookbook.

I just tested this and it worked add this to your wrapper cookbook after your nginx_install section

# needed block
service 'nginx' do
  extend Nginx::Cookbook::Helpers
  supports restart: true, status: true, reload: true
  action :nothing
end

# test restarting to make sure it works
service 'nginx' do
  action :restart
end

@Lasering
Copy link

Yes, I know that works. But this cookbook should fix it.

@rdoddi
Copy link

rdoddi commented Aug 29, 2019

This appears to be the same issue as the apache2 cookbook.

I just tested this and it worked add this to your wrapper cookbook after your nginx_install section

# needed block
service 'nginx' do
  extend Nginx::Cookbook::Helpers
  supports restart: true, status: true, reload: true
  action :nothing
end

# test restarting to make sure it works
service 'nginx' do
  action :restart
end

This saved my research. Hoping the maintainers will update the code, so that we don't have to add this. Thanks for adding this snippet.

@zmaupin
Copy link
Contributor

zmaupin commented Sep 26, 2019

fixed in #513

@zmaupin zmaupin self-assigned this Oct 6, 2019
@zmaupin zmaupin closed this as completed Oct 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants