actions

Actions module.

class wmflib.actions.Actions(name: Hashable)[source]

Bases: object

Class to keep track and log a set of actions performed and their result with a nice string representation.

The instance gets initialized with the given name, that can represent a host or any other identifier.

It allows to save actions performed on the given name with their status (success, warning, failure). It exposes the following public properties:

  • name: the name passed to the instance at instantiation time.

  • has_warnings: a bool that is True when at least one warning action was registered, False otherwise.

  • has_failures: a bool that is True when at least one failed action was registered, False otherwise.

When converted to a string it returns a nicely formatted representation of the instance and all its actions that can be used as-is in Phabricator:

<name> (**<status>**)
  - <action1>
  - <action2>
  - ...

The status is enclosed in double asterisks to be rendered as bold in Phabricator.

Examples

actions = Actions('host1001')
actions.success('Downtimed on Icinga')
actions.success('Restarted ntp')
print(actions)

The above code will print:

host1001 (**PASS**)
  - Downtimed on Icinga
  - Restarted ntp
Parameters

name (typing.Hashable) – the name of the set of actions to be registered.

property status: str

Return the current status of the actions based on the worst result recorded.

Returns

  • FAIL: if there was at least one failure action registered

  • WARN if there was at least one warning action and no FAIL actions registered

  • PASS if only success actions were registered

Return type

str

success(message: str) None[source]

Register a successful action, it gets also logged with info level.

Parameters

message (str) – the action description.

failure(message: str) None[source]

Register a failed action, it gets also logged with error level.

Parameters

message (str) – the action description.

warning(message: str) None[source]

Register an action that require some attention, it gets also logged with warning level.

Parameters

message (str) – the action description.

class wmflib.actions.ActionsDict[source]

Bases: dict

Custom dictionary with defaultdict capabilities for the wmflib.actions.Action class.

Automatically instantiate and returns a new instance of the wmflib.actions.Actions class for every missing key like a collections.defaultdict.

When accessing a missing key, the key itself is passed to the new wmflib.actions.Actions instance as name.

When converted to string returns a nicely formatted representation of the instance and all its items, that can be used as-is in Phabricator.

Examples

actions = ActionsDict()
actions['host1001'].success('Downtimed on Icinga')
actions['host1001'].failure('**Failed to restart ntp**')  # Will be rendered in bold in Phabricator
actions['host2001'].warning('//Host with alerts on Icinga//')  # Will be rendered in italic in Phabricator
print(actions)

The above code will print:

- host1001 (**FAIL**)
  - Downtimed on Icinga
  - **Failed to restart ntp**

- host2001 (**WARN**)
  - //Host not optimal on Icinga//

It will also include a final newline at the end of the block that doesn’t get rendered in this documentation.