Inspection

The inspection module contains a set of utilities for introspection of the deployment tree. This can be either from inside an action, or externally to reflect on a given tree.

Suppose that we already have the following node instantiated:

from deployer.node import Node

class Setup(Node):
    def say_hello(self):
        self.hosts.run('echo "Hello world"')

setup = Setup()

Now we can ask for the list of actions that this node has:

from deployer.inspection import Inspector

insp = Inspector(setup)
print insp.get_actions()
print insp.get_childnodes()

Some usecases:

  • Suppose that you have a huge deployment tree, covering dozens of projects, each having both a staging and production set-up, and all of them are doing a git checkout. Now you want to list all the current checkouts of all the repositories on all your machines. This is easy by traversing the nodes, filtering on the type gitnode and calling git show in there.
  • Suppose you have an nginx node, which generates the configuration according to the childnodes in there. One childnode could for instance define a back-end, another one could define the location of static files, etc... By using this inspection module, you cat find the childnodes that contain a configuration section and combine these.
  • Internally, the whole interactive shell is also using quite a lot of reflection.

Inspector

Reflexion/introspection on a deployer.node.Node

class deployer.inspection.inspector.PathType

Types for displaying the Node address in a tree. It’s an options for Inspector.get_path()

NAME_ONLY = 'NAME_ONLY'

A list of names.

NODE_AND_NAME = 'NODE_AND_NAME'

A list of (Node, name) tuples.

NODE_ONLY = 'NODE_ONLY'

A list of nodes.

class deployer.inspection.inspector.Inspector(node)

Introspection of a Node instance.

get_action(name)

Return the Action with this name or raise AttributeError.

get_actions(include_private=True)

Return a list of Action instances for the actions in this node.

Parameters:include_private (bool) – Include actions starting with an underscore.
get_childnode(name)

Return the childnode with this name or raise AttributeError.

get_childnodes(include_private=True, verify_parent=True)

Return a list of childnodes.

Parameters:
  • include_private (bool) – ignore names starting with underscore.
  • verify_parent (bool) – check that the parent matches the current node.
get_group()

Return the deployer.groups.Group to which this node belongs.

get_name()

Return the name of this node.

Note: when a node is nested in a parent node, the name becomes the attribute name of this node in the parent.

get_parent()

Return the parent Node or raise AttributeError.

get_path(path_type='NAME_ONLY')

Return a (name1, name2, ...) tuple, defining the path from the root until here.

Parameters:path_type (PathType) – Path formatting.
get_properties(include_private=True)

Return the attributes that are properties.

This are the members of this node that were wrapped in @property :returns: A list of Action instances.

get_property(name)

Returns the property with this name or raise AttributeError. :returns: Action instance.

get_queries(include_private=True)

Return the attributes that are deployer.query.Query instances.

get_query(name)

Returns the Action object that wraps the Query with this name or raise AttributeError.

Returns:An Action instance.
get_root()

Return the root Node of the tree.

has_action(name)

Returns True when this node has an action called name.

has_childnode(name)

Returns True when this node has a childnode called name.

has_property(name)

Returns True when the attribute name is a @property.

has_query(name)

Returns True when the attribute name of this node is a Query.

is_callable()

Return True when this node implements __call__.

suppress_result_for_action(name)

True when deployer.node.suppress_action_result() has been applied to this action.

walk(filter=None)

Recursively walk (topdown) through the nodes and yield them.

It does not split SimpleNodes nodes in several isolations.

Parameters:filter – A filters.Filter instance.
Returns:A NodeIterator instance.

Filters for NodeIterator

NodeIterator is the iterator that Inspector.walk() returns. It supports filtering to limit the yielded nodes according to certain conditions.

A filter is a Filter instance or an AND or OR operation of several filters. For instance:

from deployer.inspection.filters import HasAction, PublicOnly
Inspector(node).walk(HasAction('my_action') & PublicOnly & ~ InGroup(Staging))
class deployer.inspection.filters.Filter

Base class for Inspector.walk filters.

deployer.inspection.filters.PublicOnly = PublicOnly

Filter on public nodes.

deployer.inspection.filters.PrivateOnly = PrivateOnly

Filter on private nodes.

class deployer.inspection.filters.IsInstance(node_class)

Filter on the nodes which are an instance of this Node class.

Parameters:node_class – A deployer.node.Node subclass.
class deployer.inspection.filters.HasAction(action_name)

Filter on the nodes which implement this action.

class deployer.inspection.filters.InGroup(group)

Filter nodes that are in this group.

Parameters:group – A deployer.groups.Group subclass.