simple_automation.task.TrackedTask

class simple_automation.task.TrackedTask(manager)

Bases: simple_automation.task.Task

A base class for tasks which want to track changes in a git repository. All tracking will be done after run() has finished.

Examples

Generally, to track files or directories, you have to inherit from TrackedTask instead of Task. It may be beneficial to create your own base class for all tracked tasks, to set a common tracking repository. You will then only have to add all files and directories you want to track to tracking_paths in the actual task.

from simple_automation import TrackedTask

class MyTrackedTask(TrackedTask):
    tracking_repo_url = "{{ tracking.repo_url }}"
    tracking_local_dst = "/var/lib/root/tracking"

To track a config file, simply extend your task with the correct tracking path:

class TaskZshConfig(MyTrackedTask):
    tracking_paths = ["/etc/zsh"]
    # ...

To track dynamic information, first save it to a file:

# Track installed packages from portage
class TaskTrackInstalledPackages(MyTrackedTask):
    identifier = "track_installed_packages"
    description = "Tracks all installed packages"
    tracking_paths = ["/var/lib/root/installed_packages"]

    def run(self, context):
        save_output(context, command=["qlist", "-CIv"],
                    dst="/var/lib/root/installed_packages",
                    desc="Query installed packages")

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.

tracking_git_commit_opts

Extra options to ‘git commit’.

tracking_group

The group which will own tracking related files.

tracking_local_dst

The path where the local clone of the repository will be.

tracking_paths

A list of directories and/or files that should be tracked.

tracking_repo_configs

A dictionary of git configs to be set locally when the repository is first created.

tracking_repo_url

The remote url to the repository which will be used as the tracking repo.

tracking_subpath

The subpath in the repository where the tracked files will be held.

tracking_user

The user to execute all tracking commands as, and the user which will own all related files.

class TaskInitializeTracking(tracked_task)

Bases: simple_automation.task.Task

A sub-task used to initialize the tracking repository.

We remember the tracked parent task, so so we have access to the tracking specific variables later.

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.

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")
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")
tracking_git_commit_opts = []

Extra options to ‘git commit’. Will be templated by the currently executed context.

tracking_group = 'root'

The group which will own tracking related files.

tracking_local_dst = None

The path where the local clone of the repository will be. Will be templated by the currently executed context.

tracking_paths = []

A list of directories and/or files that should be tracked. Will be templated by the currently executed context.

tracking_repo_configs = {'user.email': 'root@localhost', 'user.name': '{{ context.host.identifier }}'}

A dictionary of git configs to be set locally when the repository is first created. Values will be templated by the currently executed context.

By default, it sets user.name to the host identifier, and user.email to root@localhost. (for each entry, ‘git config –local {key} {value}’ is executed). Useful to set name and email, and maybe a gpg signing key.

tracking_repo_url = None

The remote url to the repository which will be used as the tracking repo. Will be templated by the currently executed context. For example: - (via ssh) “git@github.com:myuser/tracked-system-settings” - (via https) “https://{{ personal_access_token }}@github.com/myuser/tracked-system-settings” Remember to put secrets into a vault so they aren’t checked into your repository in plain text. We recommend using ssh, as secrets in the url will be printed to the terminal when executed.

tracking_subpath = '{{ context.host.identifier }}'

The subpath in the repository where the tracked files will be held. Will be templated by the currently executed context. It defaults to the identifier of the machine.

tracking_user = 'root'

The user to execute all tracking commands as, and the user which will own all related files. The rsync will always be called as root so it may access arbitrary paths.