simple_automation.task.Task

class simple_automation.task.Task(manager)

Bases: object

A base class for tasks. A task executes on a host context and runs a set of transactions on this context.

Examples

Every tasks needs an identifier and a description. The identifier can be used as a namespace for related variables.

from simple_automation import Task

class TaskZshConfig(Task):
    identifier = "zsh"
    description = "Installs a global zsh configuration"

    def run(self, context):
        with context.defaults(umask=0o022, dir_mode=0o755, file_mode=0o644):
            # Copy configuration
            directory(context, path="/etc/zsh")
            template(context, src="templates/zsh/zshrc.j2", dst="/etc/zsh/zshrc")
            template(context, src="templates/zsh/zprofile.j2", dst="/etc/zsh/zprofile")

Methods

enabled

Returns true, if our enable variable is set to true in the given context.

exec

Executes the actual task, as well as the pre and post functions in respective order, if the task is enabled for the current context.

post_run

Called after self.run() is called.

pre_run

Called before self.run() is called.

run

To be overwritten by a subclass.

set_defaults

Optional callback for subclasses.

Attributes

description

A short description of what the task does.

identifier

The identifier of this task.

description = None

A short description of what the task does.

enabled(context)

Returns true, if our enable variable is set to true in the given context.

exec(context)

Executes the actual task, as well as the pre and post functions in respective order, if the task is enabled for the current context.

identifier = None

The identifier of this task.

post_run(context)

Called after self.run() is called. No-op by default.

pre_run(context)

Called before self.run() is called. Prints the task’s title by default.

run(context)

To be overwritten by a subclass. Contain’s the task’s logic.

set_defaults()

Optional callback for subclasses. Called when the task may define its global defaults. Default variable keys should be named like “tasks.{self.identifier}.variable_name”.

Examples

Define variables so your task can be customized easily for your different systems.

from simple_automation import Task

class TaskZshConfig(Task):
    identifier = "zsh"
    description = "Installs a global zsh configuration"

    def set_defaults(self):
        self.manager.set("tasks.zsh.config_folder", "/etc/zsh")

    def run(self, context):
        # ...
        template(context, src="templates/zsh/zshrc.j2", dst="{{ tasks.zsh.config_folder }}/zshrc")