Skip to content

Instantly share code, notes, and snippets.

@marijus-ravickas
Last active February 2, 2022 09:25
Show Gist options
  • Save marijus-ravickas/284f5a5d3be607abd1a2033e66f0c658 to your computer and use it in GitHub Desktop.
Save marijus-ravickas/284f5a5d3be607abd1a2033e66f0c658 to your computer and use it in GitHub Desktop.
Possible migration from sysctl cookbook

sysctl cookbok is deprecated but still widely used. Chef core already has sysctl resource

Below is the short example how could we migrate from sysctl cookbook, without changing attributes structure.

# Code taken from https://github.com/Sous-Chefs-Boneyard/sysctl/blob/v0.6.2/libraries/sysctl.rb and a bit modified

def compile_attr(prefix, v)
  case v
  when Array
    return {prefix => "#{v.join(' ')}"}
  when String, Fixnum, Bignum, Float, Symbol
    {prefix => "#{v}"}
  when Hash, Chef::Node::Attribute
    prefix += '.' unless prefix.empty?
    return v.map { |key, value| compile_attr("#{prefix}#{key}", value) }.flatten
  else
    fail Chef::Exceptions::UnsupportedAction, "Sysctl can't handle values of type: #{v.class}"
  end
end

params = {}
params['sysctl'] = {}
params['sysctl']['params'] = {}
params['sysctl']['params']['vm'] = {}
params['sysctl']['params']['vm']['max_map_count'] = 262_144
params['sysctl']['params']['vm']['swappiness'] = 1

compiled_params = compile_attr('',params)

compiled_params.each do |entry|
  entry.each do |k, v|
    sysctl "#{k}" do
      value #{v}
    end
  end
end

code above is just an example, which could help in writing own common helper to apply systcl configuration from the same attributes branch.

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