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.

249 lines
7.3 KiB

3 years ago
# Command File
A command 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.
```ini
[this becomes the command comment]
; ...
```
With YAML, each command is a list item and the item name becomes the command comment:
```yaml
- 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.
```ini
[restart the postfix service]
restart: postfix
```
```yaml
- 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.
1 year ago
- 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.
3 years ago
- 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`.
```ini
[create a python virtual environment]
virtualenv: python
cd: /path/to/project
```
### comment
1 year ago
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`.
3 years ago
```ini
[this becomes the comment]
; ...
```
```yaml
- 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.
```yaml
- 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.
1 year ago
The attribute name is always normalized to `environments`.
3 years ago
### prefix
The `prefix` option is used to define a statement to be executed before the main statement is executed.
```ini
[migrate the database]
1 year ago
django.migrate:
3 years ago
cd: /path/to/project/source
prefix: source ../python/bin/activate
```
### register
1 year ago
`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`.
3 years ago
```yaml
- check apache configuration:
apache: test
register: apache_ok
```
### shell
1 year ago
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.
3 years ago
```ini
[run django checks]
1 year ago
django.check:
3 years ago
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
1 year ago
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](#register) is defined, this option will use the result of the command to determine success. This option is also useful for programmatic execution.
3 years ago
```yaml
- check apache configuration:
apache: test
register: apache_ok
stop: yes
```
!!! warning
1 year ago
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.
3 years ago
### sudo
The `sudo` option may be defined as `yes` or a username. This will cause the statement to be generated with sudo.
```ini
[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 (INI) or list (YAML) of tag names. These may be used for filtering.
```yaml
- install apache:
install: apache2
tags: [apache, web]
- enable wsgi:
apache.enable: mod_wsgi
tags: [apache, web]
- restart apache:
apache.restart:
tags: [apache, web]
- run django checks:
django: check
tags: [django, python]
- apply database migrations:
django: migrate
tags: [django, python]
```
1 year ago
With YAML, tags may also be specified like so:
```yaml
- install apache:
install: apache2
tags:
- apache
- web
```
## Ad Hoc Parameters
3 years ago
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.
```ini
[run tests to validate the system]
run: make tests
local: yes
stop: yes
[install apache]
install: apache2
remote: yes
; and so on ...
```
1 year ago
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](../cli.md#context-variables-may-be-provided-on-the-command-line), using a [variables file](variables.md), or both.
In the configuration file:
```ini
; 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:
```ini
[deploy_user]
value: deploy
[domain_tld]
value: example_com
[webserver_user]
value: www-data
```