Class: CFA::MultiFileConfig

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

Overview

API to handle configuration of services that are split between /usr/etc + /etc directories

This class is intended to work as base to implement support for that kind of configuration. It abstracts the details about which files are read/written, precedence, etc.

Individual files are handled through a separate class (usually CFA based).

Examples:

Defining a class to handle a Foo service configuration

class FooConfig < MultiFileConfig
  self.file_name = "foo.conf"
  self.yast_file_name = "70-yast.conf"
  self.file_class = CFA::Foo
end

Using the previous Foo configuration handler class

config = FooConfig.load
config.some_param = "some_value1"
config.save

Detecting conflicts

config = FooConfig.load
config.conflicts #=> [:conflicting_param]

See Also:

  • LoginDefsConfig

Direct Known Subclasses

ShadowConfig

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.file_classObject

Returns the value of attribute file_class


84
85
86
# File 'library/general/src/lib/cfa/multi_file_config.rb', line 84

def file_class
  @file_class
end

.file_nameString

Returns Base file name (like 'login.defs' or 'sysctl.conf').

Returns:

  • (String)

    Base file name (like 'login.defs' or 'sysctl.conf')


76
77
78
# File 'library/general/src/lib/cfa/multi_file_config.rb', line 76

def file_name
  @file_name
end

.yast_file_nameString

Returns YaST specific configuration file name (like '70-yast.conf').

Returns:

  • (String)

    YaST specific configuration file name (like '70-yast.conf')


80
81
82
# File 'library/general/src/lib/cfa/multi_file_config.rb', line 80

def yast_file_name
  @yast_file_name
end

Class Method Details

.define_attr(attr) ⇒ Object

Define an attribute

Parameters:

  • attr (Symbol)

    Attribute name


61
62
63
64
65
66
67
68
69
70
71
72
# File 'library/general/src/lib/cfa/multi_file_config.rb', line 61

def define_attr(attr)
  define_method attr do
    file = files.reverse.find { |f| f.present?(attr) }
    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

.loadMultiFileConfig

Instantiates and loads configuration

Convenience method to create instance and load the configuration in just one call.

Returns:


54
55
56
# File 'library/general/src/lib/cfa/multi_file_config.rb', line 54

def load
  new.tap(&:load)
end

Instance Method Details

#conflictsArray<Symbol>

Returns the conflicting attributes

Conflicting attributes are the ones which override some value from the YaST specific configuration file.

Returns:

  • (Array<Symbol>)

117
118
119
120
121
# File 'library/general/src/lib/cfa/multi_file_config.rb', line 117

def conflicts
  higher_precedence_files.each_with_object([]) do |file, attrs|
    attrs.concat(yast_config_file.conflicts(file))
  end
end

#loadObject

Loads the configuration


101
102
103
# File 'library/general/src/lib/cfa/multi_file_config.rb', line 101

def load
  files.each(&:load)
end

#saveObject

Save changes to the YaST specific file


106
107
108
109
# File 'library/general/src/lib/cfa/multi_file_config.rb', line 106

def save
  log_conflicts
  yast_config_file.save
end