Class: CFA::SysctlConfig

Inherits:
Object
  • Object
show all
Includes:
Yast::Logger
Defined in:
library/general/src/lib/cfa/sysctl_config.rb

Overview

CFA based API to adjust the sysctl tool configuration

This class does not modify the running kernel configuration. It just writes the desired values into the configuration file (PATH). Despite the class Sysctl this class also takes care about entries in /run/sysctl.d, /etc/sysctl.d /usr/local/lib/sysctl.d /usr/lib/sysctl.d /lib/sysctl.d /etc/sysctl.conf and inform the user if his settings will be overruled by setting in other files.

Examples:

Enabling IPv4 forwarding

sysctl = SysctlConfig.new
sysctl.forward_ipv4 = true
sysctl.save

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_attr(attr) ⇒ Object


63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'library/general/src/lib/cfa/sysctl_config.rb', line 63

def define_attr(attr)
  define_method attr do
    file = files.reverse.find do |f|
      f.present?(attr)
    end
    return file.public_send(attr) if file

    yast_config_file.public_send(attr)
  end

  define_method "#{attr}=" do |value|
    yast_config_file.public_send("#{attr}=", value)
  end
end

Instance Method Details

#conflict?(only: [], show_information: true) ⇒ Boolean

Whether there is a conflict with given attributes

Parameters:

  • only (Array<Symbol>) (defaults to: [])

    attributes to check

  • show_information (Boolean) (defaults to: true)

    showing a popup if it is conflicting

Returns:

  • (Boolean)

    true if any conflict is found; false otherwise


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'library/general/src/lib/cfa/sysctl_config.rb', line 96

def conflict?(only: [], show_information: true)
  return false if yast_config_file.empty?

  conflicting_attrs = Sysctl::ATTRIBUTES.keys
  conflicting_attrs &= only unless only.empty?
  conflicts = {}
  higher_precedence_files.each do |file|
    # Checking all "higher" files if their values overrule the current
    # YAST settings.
    conflict_values = yast_config_file.conflicts(file) & conflicting_attrs
    conflicts[file.file_path] = conflict_values unless conflict_values.empty?
  end

  # Transform into real tags
  conflicts.each do |file, tags|
    conflicts[file] = tags.map { |t| Sysctl::ATTRIBUTES[t.to_sym] }
  end

  if !conflicts.empty?
    log.warn("It could be that #{YAST_CONFIG_PATH} will not be written.")
    log.warn("There are conflicts in sysctl files: #{conflicts}.")
    ConfictReport.report(conflicts) if show_information
  end

  !conflicts.empty?
end

#filesObject


123
124
125
# File 'library/general/src/lib/cfa/sysctl_config.rb', line 123

def files
  @files ||= config_paths.map { |file| Sysctl.new(file_path: file) }
end

#loadObject


81
82
83
# File 'library/general/src/lib/cfa/sysctl_config.rb', line 81

def load
  files.each(&:load)
end

#saveObject

Saving all sysctl settings


87
88
89
# File 'library/general/src/lib/cfa/sysctl_config.rb', line 87

def save
  yast_config_file&.save
end