sous-chefs / mysql

Development repository for the mysql cookbook
https://supermarket.chef.io/cookbooks/mysql
Apache License 2.0
337 stars 691 forks source link

Unable to create mysql service on ubuntu "undefined method `[]' for nil:NilClass" #660

Closed jburns24 closed 3 years ago

jburns24 commented 3 years ago

:speaking_head: 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.

:ghost: Brief Description

Unable to create mysql service v5.7 on Ubuntu 16.04

:pancakes: Cookbook version

10.0.1

:woman_cook: Chef-Infra Version

14.15.6

:tophat: Platform details

ubuntu 16.04

Steps To Reproduce

Steps to reproduce the behavior: 1) Generate a new test cookbook chef generate cookbook mysql_test -b 2) Add the following dependencies to the berksfile and the depends statement to metadata.rb

provisioner: name: chef_zero product_name: chef product_version: 14 always_update_cookbooks: false

verifier: name: inspec

platforms:

suites:

4) Edit the `recipes/default.rb`

include_recipe 'apt'

mysql_service 'default' do port '3306' version '5.7' initial_root_password 'changeme' action [:create, :start] end

5) Run `kitchen converge`
6) Converge fails with the following error
   Recipe: mysql_test::default
     * mysql_service[default] action create
       * mysql_server_installation_package[default] action install
         * apt_package[mysql-server-5.7] action install
    - install version 5.7.33-0ubuntu0.16.04.1 of package mysql-server-5.7
         * apt_package[perl-Sys-Hostname-Long] action nothing (skipped due to action :nothing)
         * execute[Initial DB setup script] action nothing (skipped due to action :nothing)

       * mysql_service_manager_systemd[default] action create

         ================================================================================
         Error executing action `create` on resource 'mysql_service_manager_systemd[default]'
         ================================================================================

         NoMethodError
         -------------
         undefined method `[]' for nil:NilClass

         Cookbook Trace:
         ---------------
         /tmp/kitchen/cache/cookbooks/mysql/libraries/mysql_service_base.rb:126:in `configure_apparmor'
         /tmp/kitchen/cache/cookbooks/mysql/libraries/mysql_service_manager_systemd.rb:15:in `block in <class:MysqlServiceManagerSystemd>'

         Resource Declaration:
         ---------------------
         # In /tmp/kitchen/cache/cookbooks/mysql/libraries/mysql_service.rb

   51:           svc = mysql_service_manager(new_resource.name, &block)
   52:         when 'sysvinit'

         Compiled Resource:
         ------------------
         # Declared in /tmp/kitchen/cache/cookbooks/mysql/libraries/mysql_service.rb:51:in `svc_manager'

         mysql_service_manager_systemd("default") do
    action [:create]
    default_guard_interpreter :default
    declared_type :mysql_service_manager
    cookbook_name "mysql_test"
    version "5.7"
    initial_root_password "changeme"
    port "3306"
    instance "default"
    include_dir "/etc/mysql/conf.d"
    data_dir "/var/lib/mysql"
         end

         System Info:
         ------------
         chef_version=14.15.6
         platform=ubuntu
         platform_version=16.04
         ruby=ruby 2.5.8p224 (2020-03-31 revision 67882) [x86_64-linux]
         program_name=/opt/chef/bin/chef-client
         executable=/opt/chef/bin/chef-client

       ================================================================================
       Error executing action `create` on resource 'mysql_service[default]'
       ================================================================================

       NoMethodError
       -------------
       mysql_service_manager_systemd[default] (/tmp/kitchen/cache/cookbooks/mysql/libraries/mysql_service.rb line 51) had an error: NoMethodError: undefined method `[]' for nil:NilClass

       Cookbook Trace:
       ---------------
       /tmp/kitchen/cache/cookbooks/mysql/libraries/mysql_service_base.rb:126:in `configure_apparmor'
       /tmp/kitchen/cache/cookbooks/mysql/libraries/mysql_service_manager_systemd.rb:15:in `block in <class:MysqlServiceManagerSystemd>'

       Resource Declaration:
       ---------------------
       # In /tmp/kitchen/cache/cookbooks/mysql_test/recipes/default.rb

         9: mysql_service 'default' do
        10:     port '3306'
        11:     version '5.7'
        12:     initial_root_password 'changeme'
        13:     action [:create, :start]
        14: end

       Compiled Resource:
       ------------------
       # Declared in /tmp/kitchen/cache/cookbooks/mysql_test/recipes/default.rb:9:in `from_file'

       mysql_service("default") do
         action [:create, :start]
         updated true
         updated_by_last_action true
         default_guard_interpreter :default
         declared_type :mysql_service
         cookbook_name "mysql_test"
         recipe_name "default"
         port "3306"
         version "5.7"
         initial_root_password "changeme"
       end

       System Info:
       ------------
       chef_version=14.15.6
       platform=ubuntu
       platform_version=16.04
       ruby=ruby 2.5.8p224 (2020-03-31 revision 67882) [x86_64-linux]
       program_name=/opt/chef/bin/chef-client
       executable=/opt/chef/bin/chef-client

   Running handlers:
   [2021-02-23T18:48:35+00:00] ERROR: Running exception handlers
   Running handlers complete
   [2021-02-23T18:48:35+00:00] ERROR: Exception handlers complete
   Chef Client failed. 9 resources updated in 01 minutes 30 seconds
   [2021-02-23T18:48:35+00:00] FATAL: Stacktrace dumped to /tmp/kitchen/cache/chef-stacktrace.out
   [2021-02-23T18:48:35+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
   [2021-02-23T18:48:35+00:00] FATAL: NoMethodError: mysql_service[default] (mysql_test::default line 9) had an error: NoMethodError: mysql_service_manager_systemd[default] (/tmp/kitchen/cache/cookbooks/mysql/libraries/mysql_service.rb line 51) had an error: NoMethodError: undefined method `[]' for nil:NilClass


## :police_car: Expected behavior

I would expect this to converge as README states version 5.7 is supported on ubuntu 16.04. 

## :heavy_plus_sign: Additional context

I tried digging into the the attribute that was not set and it appears this was the `new_resource.instance` property which according to `mysql_service_base.rb` this is the `name_property` so I am not sure how this is Nil.
ramereth commented 3 years ago

We actually no longer support Ubuntu 16.04 since it's going EOL very soon. I just updated the README to ensure that's now stated.

mehagar commented 3 years ago

This happens on Ubuntu 20.04 as well. If default['apparmor']['disable'] is not set in the consumer's attributes, node['apparmor'] will evaluate to nil and cause this undefined method error.

ramereth commented 3 years ago

@mehagar unfortunately I'm not able to replicate the issue on my end, even following your detailed (thanks!) instructions. Also, I see you have Chef 14 listed, but this cookbook only supports >= 15. I'm not sure how you were able to run this cookbook with Chef 14.

cgunther commented 3 years ago

The metadata allows Chef >= 12.7: https://github.com/sous-chefs/mysql/blob/f7c92af098bca2936a6189aec1344c63f8ad5c2d/metadata.rb#L8

I'm also hitting this with Chef 14.15.6 and temporarily solved it by adding the following before calling mysql_service:

node.default['apparmor']['disable'] = true

The fact that some tests have this same "fix" is a bit suspect to me: https://github.com/sous-chefs/mysql/blob/844036bde95721c401ce5309007787babdfc412d/test/cookbooks/test/recipes/smoke.rb#L9-L10

I don't have access to Chef 15 at the moment to confirm, but if it's working there, that'd imply to me that either Chef 15 changed nodes to allow deep access through undefined keys (ie. node['foo']['bar']['baz'] doesn't error even if node['foo'] is never set), or Chef 15 started setting a node['apparmor'] key on it's own.

ramereth commented 3 years ago

Apologies, I had forgotten that my cookbook generator template adds a requirement for Chef >=15. I also realized I was testing this using dokken which bypasses the problem completely. I was able to reproduce the problem and will work on a PR to address it soon.

ramereth commented 3 years ago

@cgunther I believe #662 resolves this. Can you please verify?