A collection of classes and commands for automated command line scripting using Python.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

6.7 KiB

Steps File

A steps file contains the metadata about the commands to be generated. INI and YAML formats are supported.

In an INI file, each section is a command. With YAML, each top-level list item is a command.

The Comment/Description

With INI files, the section name is the command comment.

[this becomes the command comment]
; ...

With YAML, each command is a list item and the item name becomes the command comment:

- this becomes the command comment:
  # ...

First Option and Arguments

The first variable in the section is the command name. It's value contains the required arguments.

[restart the postfix service]
restart: postfix
- restart the postfix service:
  restart: postfix

Formatting Notes

With both INI and YAML files, the formatting rules are:

  • The first part of each command is the INI section or YAML item and is used as the comment.
  • The command name must be the first option in an INI section.
  • The arguments for the command appear as the value of the first option in the section. Arguments are separated by a space.
  • Arguments that should be treated as a single value should be enclosed in double quotes.
  • yes and no are interpreted as boolean values. maybe is interpreted as None.
  • List values, where required, are separated by commas when appearing in INI files, but are a [standard, list, of, values] in a YAML file.

Additional Attributes

Additional variables in the section are generally optional parameters that inform or control how the command should be executed and are sometimes used to add switches to the statement.

!!! warning

This is not always the case, so consult the documentation for the command in question, because some parameters that appear after the first line are actually required.

Common Attributes

A number of common options are recognized. Some of these have no bearing on statement generation but may be used for filtering. Others may be optionally included, and a few may only be used programmatically.

cd

The cd option sets the directory (path) from which the statement should be executed. It is included by default when the statement is generated, but may be suppressed using cd=False.

[create a python virtual environment]
virtualenv: python
cd: /path/to/project

comment

The comment comes from the section name (INI) or list name (YAML). It is included by default when the statement is generated, but may be suppressed using include_comment=False.

[this becomes the comment]
; ...

env

The env option indicates the target environment (or environments) in which the statement should run. This is not used in command generation, but may be used for filtering.

- set up the database:
  pgsql.create: example_com
  env: [staging, live]

This option may be given as environments, environs, envs, or simply env. It may be a list or CSV string.

The attribute name is always normalized to environments.

prefix

The prefix option is used to define a statement to be executed before the main statement is executed.

[migrate the database]
django.migrate:
cd: /path/to/project/source
prefix: source ../python/bin/activate

register

register defines the name of a variable to which the result of the statement should be saved. For some commands, it may be included by default when the statement is generated, but may be suppressed using include_register=False.

- check apache configuration:
  apache: test
  register: apache_ok

shell

The shell defines the shell to be used for command execution. It is not used for statement generation, but may be used programmatically -- for example, with Python's subprocess module. Some commands (such as Django management commands) may need a shell to be explicitly defined.

[run django checks]
django.check:
cd: /path/to/project/source
prefix: source ../python/bin/activate
shell: /bin/bash

!!! note

As this option is intended for programmatic use, it would be better to define a default shell for all command execution and use this option only when the default should be overridden. 

stop

A yes indicates processing should stop if the statement fails to execute with success. If provided, it is included by default when the statement is generated, but may be suppressed. Additionally, when register is defined, this option will use the result of the command to determine success. This option is also useful for programmatic execution.

!!! warning

Some commands do not provide a zero or non-zero exit code on success or failure. You should verify that the `stop` will actually be used.

sudo

The sudo option may be defined as yes or a username. This will cause the statement to be generated with sudo.

[install apache]
install: apache2
sudo: yes

!!! note

When present, sudo is always generated as part of the statement. For programmatic use, it may be better to control how and when sudo is applied using some other mechanism. If sudo should be used for all statements, it can be passed as a global option.

tags

tags is a comma separated list of tag names. These may be used for filtering.

Ad Hoc Parameters

Options that are not recognized as common or as part of those specific to a command are still processed by the loader. This makes it possible to define your own options based on the needs of a given implementation.

For example, suppose you are implementing a deployment system where some commands should run locally, but most should run on the remote server.

[run tests to validate the system]
run: make tests
local: yes
stop: yes

[install apache]
install: apache2
remote: yes

; and so on ...

This will be of no use as a generated script since the generator does not know about local and remote, but these could (for example) be used programmatically to control whether Python subprocess or an SSH client is invoked.

Template Processing

Commands often need to be specific to an environment or installation. To facilitate this, the configuration file may be parsed as a Jinja2 template prior to processing and generating the statements. This happens automatically when variables are provided.

Template context may be provided on the command line, using a variables file, or both.

In the configuration file:

; commands.ini
[create the document root of the website]
dir: /var/www/{{ domain_tld }}/www
owner: {{ deploy_user }}
group: {{ webserver_user }}
recursive: yes

In the variables file:

[deploy_user]
value: deploy

[domain_tld]
value: example_com

[webserver_user]
value: www-data