confctl

Confctl module to abstract Conftool.

exception spicerack.confctl.ConfctlError[source]

Bases: SpicerackError

Custom exception class for errors of this module.

class spicerack.confctl.Confctl(config: str = '/etc/conftool/config.yaml', schema: str = '/etc/conftool/schema.yaml', dry_run: bool = True) None[source]

Bases: object

Confctl class to abstract conftool operations.

Initialize the instance.

Parameters:
  • config (str, default: '/etc/conftool/config.yaml') -- the path to the configuration file to load.

  • schema (str, default: '/etc/conftool/schema.yaml') -- the path to the Conftool schema to load.

  • dry_run (bool, default: True) -- whether this is a DRY-RUN.

entity(entity_name: str) spicerack.confctl.ConftoolEntity[source]

Get the Conftool specific entity class.

Parameters:

entity_name (str) -- the name of the entity..

Return type:

spicerack.confctl.ConftoolEntity

class spicerack.confctl.ConftoolEntity(entity: conftool.kvobject.Entity, dry_run: bool = True) None[source]

Bases: object

ConftoolEntity class to perform operations on a specific Conftool entity.

Initialize the instance.

Parameters:
  • entity (conftool.kvobject.Entity) -- an instance of Conftool entity.

  • dry_run (bool, default: True) -- whether this is a DRY-RUN.

change_and_revert(field: str, original: bool | str | int | float, changed: bool | str | int | float, **tags: str) collections.abc.Iterator[collections.abc.Iterable[conftool.kvobject.Entity]][source]

Context manager to perform actions with a changed value in conftool.

This method will only act on objects that had the original value.

Warning

If the code executed within the contextmanager raises an unhandled exception, the original state of the objects will NOT be restored.

Parameters:
  • field (str) -- The field to change the value of.

  • original (typing.Union[bool, str, int, float]) -- the original value.

  • changed (typing.Union[bool, str, int, float]) -- changed value.

  • tags (str) -- Appropriate conftool tags for the chosen entity to select objects.

Yields:

generator -- conftool.kvObject.Entity the objects that were acted upon.

Return type:

collections.abc.Iterator[collections.abc.Iterable[conftool.kvobject.Entity]]

filter_objects(filter_expr: dict[str, bool | str | int | float], **tags: str) collections.abc.Iterator[conftool.kvobject.Entity][source]

Filters objects coming from conftool based on values.

A generator will be returned which will contain only objects that match all filters.

Parameters:
  • filter_expr (dict[str, typing.Union[bool, str, int, float]]) -- a set of desired field names and values.

  • **tags (str) -- arbitrary Conftool tags as keyword arguments.

Yields:

conftool.kvobject.Entity -- the selected object.

Raises:

spicerack.confctl.ConfctlError -- if no object corresponds to the tags.

Return type:

collections.abc.Iterator[conftool.kvobject.Entity]

get(**tags: str) collections.abc.Iterator[conftool.kvobject.Entity][source]

Generator that yields conftool objects corresponding to the selection.

Parameters:

**tags (str) -- arbitrary Conftool tags as keyword arguments.

Yields:

conftool.kvobject.Entity -- the selected object.

Return type:

collections.abc.Iterator[conftool.kvobject.Entity]

set_and_verify(key: str, value: bool | str | int | float, **tags: str) None[source]

Set and verify a single Conftool value.

Parameters:
  • key (str) -- the key in Conftool to modify.

  • value (typing.Union[bool, str, int, float]) -- the value to set.

  • **tags (str) -- arbitrary Conftool tags as keyword arguments.

Raises:

spicerack.confctl.ConfctlError -- on etcd or Conftool errors or failing to verify the changes.

Return type:

None

update(changed: dict[str, bool | str | int | float], **tags: str) None[source]

Updates the value of conftool objects corresponding to the selection done with tags.

Parameters:
  • changed (dict[str, typing.Union[bool, str, int, float]]) -- the new values to set for the selected objects.

  • **tags (str) -- arbitrary Conftool tags as keyword arguments.

Raises:

spicerack.confctl.ConfctlError -- on etcd or Conftool errors.

Return type:

None

Examples

>>> confctl.update({'pooled': 'no'}, service='appservers-.*', name='eqiad')
update_objects(changed: dict[str, bool | str | int | float], objects: collections.abc.Iterable[conftool.kvobject.Entity]) None[source]

Updates the value of the provided conftool objects.

Examples

>>> inactive = confctl.filter_objects({'pooled': 'inactive'}, service='appservers-.*', name='eqiad')
>>> confctl.update_objects({'pooled': 'no'}, inactive)
Parameters:
  • changed (dict[str, typing.Union[bool, str, int, float]]) -- the new values to set for the selected objects.

  • query -- an iterator of conftool objects.

Raises:

spicerack.confctl.ConfctlError -- on etcd or Conftool errors.

Return type:

None