2.3. libmace.files

Process configuration files.

libmace.files.load(src_dir, providers)

Load tree of configuration sources.

Parameters:
  • src_dir – path to directories tree with configuration sources
  • providers – list of libmace.providers.BaseProvider objects to scan configuration for
Returns:

a tuple of ({provider: Conftext}, {var_name: var_value}); the first item is for found sources and the last one is for found variables

libmace.files.commit(src_dir, dst_dir, sources, envars, force=False, dry_services=False)

Apply configuration to final environment.

Parameters:
  • src_dir – path to directories tree with configuration sources
  • dst_dir – path to directories tree to write configuration to
  • sources – dictionary returned by load() method
  • force – force to rewrite configuration and reapply it
  • dry_services – do not ask providers to apply configuration, just write configuration files
Returns:

list of providers that configuration was applied

libmace.files.stat(sources, cb)

Return configuration sources statistic.

Parameters:
  • sources – dictionary returned by load() function
  • cb – callback that will be called with SourceFile object and should return True if this file needs to be included to result statistic
Returns:

a tuple of (total, merged, overriden, hidden) numbers of files

libmace.files.uid_by_name(name)

Get uid by user name.

Should be used instead of pwd.getpwnam. Was introduced only to support unittests.

Parameters:name – user name
Returns:uid integer value
libmace.files.gid_by_name(name)

Get gid by group name.

Should be used instead of grp.getgrnam. Was introduced only to support unittests.

Parameters:name – user name
Returns:uid integer value
libmace.files.chown(path, uid, gid)

Change file ownership.

Should be used instead of os.chown. Was introduced only to support unittests.

Parameters:
  • uid – user id
  • gid – group id
class libmace.files.Conftext

Collect configuration sources for the same provider.

by_confile(confile)

SourceFile objects for the same confile.

Parameters:confile – the name of a confile
Returns:list of SourceFile objects
class libmace.files.SourceFile(provider, src_dir, confile, confile_prio, path)

Source file representation.

This class describes only source configuration file, i.e., it is a part of source confile and there are might be several files for the same confile in sources tree.

Parameters:
  • providerlibmace.providers.BaseProvider object that this configuration file belongs to
  • src_dir – the sources root directory
  • confile – provider’s confile name
  • confile_prio – confile priority; less value means higher priority
  • path – path to this source file
override(other)

Check if this configuration file is being overriden by another one.

Parameters:otherSourceFile that substitutes this one
class libmace.files.Transaction(root, envars=None, artificial_config=None)

Configuration application transation.

These objects will be used while applying final configuration. All changes need to be done via Transaction methods to let it rollback changes on fails.

Parameters:root – path to the root directory with final configuration, e.g., /
config

Artificial provider configuration. Some providers might support artificial configuration, i.e., if there is no singular configuration syntax. For these providers config will contain configuration parsed from .ini files.

envars

Variables exported by all providers

empty

Are there any changes made within this transation.

invalidate()

Make transation dirty and force it applying.

path(path)

Arbitrary path starting from final configuration root directory.

Parameters:path – relative path
Returns:specified path with prefixing root directory
ensure_absence(path, abs_path=False)

Make sure that path doesn’t exist.

Parameters:
  • path – configuration file within the root directory to track
  • abs_path – if False, path will be prefixed with final configuration root directory
Returns:

True if something was changed

ensure_file(path, content=None, owner=None, mode=None, create_missed=None, abs_path=False, content_cb=None)

Change a file.

Parameters:
  • path – configuration file within the root directory to track
  • content – if not None, content that confpath file should have
  • owner – what the owner of file should be; the singular value will define user and group, but it might be a tuple; atomic values might be a string to define names or ints to define ids
  • mode – mode that file should have
  • create_missed – create files if they don’t exist
  • abs_path – if False, path will be prefixed with final configuration root directory
  • content_cb – callback to call to get content lines, the calling format is content_cb(lines)
Returns:

True if something was changed

ensure_dir(path, owner=None, mode=None, abs_path=False)

Change a directory.

Parameters:
  • path – configuration file within the root directory to track
  • owner – what the owner of directory should be; the singular value will define user and group, but it might be a tuple; atomic values might be a string to define names or ints to define ids
  • mode – mode that directory should have
  • abs_path – if False, path will be prefixed with final configuration root directory
Returns:

True if something was changed

Create a symlink.

Parameters:
  • src – configuration file within the root directory to link from
  • dst – configuration file within the root directory to link to
  • abs_path – if False, dst will be prefixed with final configuration root directory
Returns:

True if something was changed

rollback()

Revert all previsly made changes within this transation.

checkpoint()

Cleanup rollback history, so rollback() call will do nothing.

Previous topic

2.2. libmace.providers

Next topic

2.4. libmace.distro

This Page