Sous-Chefs-Boneyard / sysctl

Development repository for the sysctl cookbook
https://supermarket.chef.io/cookbooks/sysctl
Apache License 2.0
42 stars 79 forks source link

Feature: Ignore errors when keys do not exist on system. #110

Closed mkristan closed 6 years ago

mkristan commented 6 years ago

Cookbook version

1.0.0

Chef-client version

14.0.59

Platform Details

Amazon Linux 2017.09

Scenario:

Feature request for a way to define in the resource to ignore errors when a given key does not exist on a platform, this will make wrapper cookbooks easier to implement as there won't be a need to introduce complex guards in each resource.

Steps to Reproduce:

Declare a sysctl paramter that does not exist on the system. For example net.ipv4.tcp_syncookies. See travis output at https://travis-ci.org/sous-chefs/sysctl/jobs/342817880

Expected Result:

Previous versions of the cookbook would silently fail if the key does not exist on a target system.

Actual Result:

The cookbook throws an exception because the sysctl -p command returns an error 255. Full details below

Mixlib::ShellOut::ShellCommandFailed
------------------------------------
execute[sysctl -p] (/opt/kitchen/cache/cookbooks/sysctl/resources/param.rb line 62) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '255'
damacus commented 6 years ago

yeah this is super bloody annoying, and it's currently limited to travis.

I actually think this is an OS configuration error rather than a sysctl issue.

But if you want to add in an ignore_errors switch a PR would be welcomed! 👍

mkristan commented 6 years ago

I can look into a PR.

mkristan commented 6 years ago

I'm still looking into this. This appears to be in the code a while back but seems to have gotten lost when we switched from attributes to resources.

mkristan commented 6 years ago

Branch created and I'm working this in my own forked repo https://github.com/mkristan/sysctl/tree/issue_110_ignore_when_keys_do_not_exist

isuftin commented 6 years ago

Hitting up against this issue. I am confused. In the last release of the cookbook, does ignore_error not work?

         * sysctl[net.ipv6.route.flush] action apply

           ================================================================================
           Error executing action `apply` on resource 'sysctl[net.ipv6.route.flush]'
           ================================================================================

           RuntimeError
           ------------
           Unknown sysctl key net.ipv6.route.flush!

           Resource Declaration:
           ---------------------
           # In /tmp/kitchen/cache/cookbooks/stig/recipes/proc_hard.rb

            26:   sysctl_param param do
            27:     key param
            28:     value value
            29:     ignore_error true
            30:     # ignore_error node['sysctl']['ignore_error']
            31:   end
            32: end

           Compiled Resource:
           ------------------
           # Declared in /tmp/kitchen/cache/cookbooks/stig/recipes/proc_hard.rb:26:in `block in from_file'

           sysctl("net.ipv6.route.flush") do
             action [:apply]
             default_guard_interpreter :default
             declared_type :sysctl_param
             cookbook_name "stig"
             recipe_name "proc_hard"
             key "net.ipv6.route.flush"
             value "1"
             ignore_error true
           end

           System Info:
           ------------
           chef_version=14.0.202
           platform=centos
           platform_version=6.9
           ruby=ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
           program_name=/opt/chef/bin/chef-client
           executable=/opt/chef/bin/chef-client
cpoma commented 6 years ago

The "quick" fix in resource/param.rb is to allow 255 to be a valid return code. You get 255 for keys that don't exist on the system:

action :apply do
  converge_if_changed do
    # set it temporarily
    set_sysctl_param(new_resource.key, new_resource.value)

    directory new_resource.conf_dir

    file "#{new_resource.conf_dir}/99-chef-#{new_resource.key}.conf" do
      content "#{new_resource.key} = #{new_resource.value}"
    end

# Redhat will return 255 if sysctl -p has invalid values.
# Adding 255 to valid return codes if ignore_error set to true
    valid_return_codes = [0]
    valid_return_codes.insert(0,255) if new_resource.ignore_error 

    execute 'sysctl -p' do
      command 'sysctl -p'
      action :run
      returns valid_return_codes
    end
  end
end
lock[bot] commented 5 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.