parent
6de3956dc6
commit
0d93018a00
24 changed files with 558 additions and 37 deletions
@ -0,0 +1,171 @@ |
|||||||
|
--- |
||||||
|
title: CLI |
||||||
|
--- |
||||||
|
|
||||||
|
The `tease` command may be used to parse a configuration file, providing additional utilities for working with commands. |
||||||
|
|
||||||
|
## Getting Help |
||||||
|
|
||||||
|
Use `tease -h` to get started. There are three (3) sub-commands: [docs](#the-docs-sub-command), [inventory](#the-inventory-sub-command), and [script](#the-script-sub-command). |
||||||
|
|
||||||
|
```text |
||||||
|
usage: tease [-h] [-v] [--version] docs, inventory, script ... |
||||||
|
|
||||||
|
positional arguments: |
||||||
|
docs, inventory, script |
||||||
|
Commands |
||||||
|
docs Output documentation instead of code. |
||||||
|
inventory (inv) Copy an inventory item to a local directory. |
||||||
|
script Output the commands. |
||||||
|
|
||||||
|
optional arguments: |
||||||
|
-h, --help show this help message and exit |
||||||
|
-v Show version number and exit. |
||||||
|
--version Show verbose version information and exit. |
||||||
|
|
||||||
|
NOTES |
||||||
|
|
||||||
|
This command is used to parse configuration files and output the commands. |
||||||
|
|
||||||
|
``` |
||||||
|
|
||||||
|
## The Docs Sub-Command |
||||||
|
|
||||||
|
A nice benefit of using configuration for commands is that the information may be used to output documentation. This allows the automatic creation of an install guide or tutorial using exactly the same commands that would be used for the actual work. |
||||||
|
|
||||||
|
Additionally, Script Tease provides the [explain](commands/messages.md#explain) and [screenshot](commands/messages.md#screenshot) commands that help provide extra content for documentary output. |
||||||
|
|
||||||
|
```text |
||||||
|
usage: tease docs [-h] [-o= {html,md,plain,rst}] [-C= VARIABLES] [-i= COMMAND_FILE] [-O= OPTIONS] [-P= {centos,ubuntu}] |
||||||
|
[-T= TEMPLATE_LOCATIONS] [-w= OUTPUT_FILE] [-V= VARIABLES_FILE] [-D] [-p] |
||||||
|
|
||||||
|
optional arguments: |
||||||
|
-h, --help show this help message and exit |
||||||
|
-o= {html,md,plain,rst}, --output-format= {html,md,plain,rst} |
||||||
|
The output format; HTML, Markdown, plain text, or ReStructuredText. |
||||||
|
-C= VARIABLES, --context= VARIABLES |
||||||
|
Context variables for use in pre-parsing the config and templates. In the form of: name:value |
||||||
|
-i= COMMAND_FILE, --input-file= COMMAND_FILE |
||||||
|
The path to the configuration file. |
||||||
|
-O= OPTIONS, --option= OPTIONS |
||||||
|
Common command options in the form of: name:value |
||||||
|
-P= {centos,ubuntu}, --profile= {centos,ubuntu} |
||||||
|
The OS profile to use. |
||||||
|
-T= TEMPLATE_LOCATIONS, --template-path= TEMPLATE_LOCATIONS |
||||||
|
The location of template files that may be used with the template command. |
||||||
|
-w= OUTPUT_FILE, --write= OUTPUT_FILE |
||||||
|
Write the output to disk. |
||||||
|
-V= VARIABLES_FILE, --variables-file= VARIABLES_FILE |
||||||
|
Load variables from a file. |
||||||
|
-D, --debug Enable debug mode. Produces extra output. |
||||||
|
-p Preview mode. |
||||||
|
|
||||||
|
``` |
||||||
|
|
||||||
|
## The Inventory Sub-Command |
||||||
|
|
||||||
|
Script Tease ships with few pre-defined configurations that may be copied to a local directory. |
||||||
|
|
||||||
|
Use `tease inv ?` to list available inventory items. |
||||||
|
|
||||||
|
```text |
||||||
|
usage: tease inventory [-h] [-P= TO_PATH] [-D] [-p] name |
||||||
|
|
||||||
|
positional arguments: |
||||||
|
name The name of the inventory item. Use ? to list available items. |
||||||
|
|
||||||
|
optional arguments: |
||||||
|
-h, --help show this help message and exit |
||||||
|
-P= TO_PATH, --path= TO_PATH |
||||||
|
The path to where the item should be copied. Defaults to the current working directory. |
||||||
|
-D, --debug Enable debug mode. Produces extra output. |
||||||
|
-p Preview mode. |
||||||
|
|
||||||
|
``` |
||||||
|
|
||||||
|
!!! note "Road Map" |
||||||
|
A future release will include support for multiple inventory locations that may be defined by the user. |
||||||
|
|
||||||
|
## The Script Sub-Command |
||||||
|
|
||||||
|
The `script` sub-command exports command configuration to actual Bash statements. Minimum usage is |
||||||
|
|
||||||
|
```bash |
||||||
|
tease -i commands.ini |
||||||
|
``` |
||||||
|
|
||||||
|
This will output the statements represented in the specified configuration file. There are quite a few other parameters. |
||||||
|
|
||||||
|
```text |
||||||
|
usage: tease script [-h] [-c] [-s] [-C= VARIABLES] [-i= COMMAND_FILE] [-O= OPTIONS] [-P= {centos,ubuntu}] [-T= TEMPLATE_LOCATIONS] |
||||||
|
[-w= OUTPUT_FILE] [-V= VARIABLES_FILE] [-D] [-p] |
||||||
|
|
||||||
|
optional arguments: |
||||||
|
-h, --help show this help message and exit |
||||||
|
-c, --color Enable code highlighting for terminal output. |
||||||
|
-s, --shebang Add the shebang to the beginning of the output. |
||||||
|
-C= VARIABLES, --context= VARIABLES |
||||||
|
Context variables for use in pre-parsing the config and templates. In the form of: name:value |
||||||
|
-i= COMMAND_FILE, --input-file= COMMAND_FILE |
||||||
|
The path to the configuration file. |
||||||
|
-O= OPTIONS, --option= OPTIONS |
||||||
|
Common command options in the form of: name:value |
||||||
|
-P= {centos,ubuntu}, --profile= {centos,ubuntu} |
||||||
|
The OS profile to use. |
||||||
|
-T= TEMPLATE_LOCATIONS, --template-path= TEMPLATE_LOCATIONS |
||||||
|
The location of template files that may be used with the template command. |
||||||
|
-w= OUTPUT_FILE, --write= OUTPUT_FILE |
||||||
|
Write the output to disk. |
||||||
|
-V= VARIABLES_FILE, --variables-file= VARIABLES_FILE |
||||||
|
Load variables from a file. |
||||||
|
-D, --debug Enable debug mode. Produces extra output. |
||||||
|
-p Preview mode. |
||||||
|
|
||||||
|
``` |
||||||
|
|
||||||
|
### Context Variables May be Provided on the Command Line |
||||||
|
|
||||||
|
To supply context variables on the command line: |
||||||
|
|
||||||
|
```bash |
||||||
|
tease -C domain_name:example.com -C domain_tld:example_com |
||||||
|
``` |
||||||
|
|
||||||
|
!!! important |
||||||
|
Variables provided on the command will always override those provided in a file. |
||||||
|
|
||||||
|
### Loading Context Variables from a File |
||||||
|
|
||||||
|
Context variables may be loaded from a file: |
||||||
|
|
||||||
|
```ini |
||||||
|
[domain_name] |
||||||
|
value = example.com |
||||||
|
|
||||||
|
[domain_tld] |
||||||
|
value = example_com |
||||||
|
``` |
||||||
|
|
||||||
|
The variables above are available as template variables in a commands file. |
||||||
|
|
||||||
|
For example, ``{{ domain_name }}`` becomes ``example.com``. |
||||||
|
|
||||||
|
To load the variables, use the `-V` switch: |
||||||
|
|
||||||
|
```bash |
||||||
|
tease -V variables.ini |
||||||
|
|
||||||
|
``` |
||||||
|
|
||||||
|
### Setting Common Options for All Commands |
||||||
|
|
||||||
|
Rather than include a common parameter in the configuration file, it is possible to specify a common option on the command line. |
||||||
|
|
||||||
|
```bash |
||||||
|
tease -O sudo:yes |
||||||
|
|
||||||
|
``` |
||||||
|
|
||||||
|
### The Difference Between Variables and Options |
||||||
|
|
||||||
|
Variables are used to pre-process configuration files as templates, while common options are passed to *all* command instances. |
@ -0,0 +1,24 @@ |
|||||||
|
# Getting Started |
||||||
|
|
||||||
|
## System Requirements |
||||||
|
|
||||||
|
Python 3.6 or greater is required. |
||||||
|
|
||||||
|
## Install |
||||||
|
|
||||||
|
To install: |
||||||
|
|
||||||
|
```bash |
||||||
|
pip install python-scripttease; |
||||||
|
``` |
||||||
|
|
||||||
|
## Configuration |
||||||
|
|
||||||
|
See [Commands File](topics/command-file.md) for creating a configuration file. |
||||||
|
|
||||||
|
## FAQs |
||||||
|
|
||||||
|
Have a question? `Just ask`_! |
||||||
|
|
||||||
|
.. _Just ask: https://develmaycare.com/contact/?product=Script%20Tease |
||||||
|
|
@ -0,0 +1,14 @@ |
|||||||
|
# Create an Executable Script |
||||||
|
|
||||||
|
To create a script that is ready to execute: |
||||||
|
|
||||||
|
```bash |
||||||
|
tease -i commands.ini -s -w script.sh |
||||||
|
chmod +x script.sh |
||||||
|
``` |
||||||
|
|
||||||
|
The `-s` switch causes the [shebang](https://linuxhandbook.com/shebang/) to be included at the top of the output. This defaults to: |
||||||
|
|
||||||
|
`#! /usr/bin/env bash` |
||||||
|
|
||||||
|
The script is now ready to run: `./script.sh` |
@ -0,0 +1,51 @@ |
|||||||
|
# Define A Custom Command |
||||||
|
|
||||||
|
!!! note |
||||||
|
It is not currently possible to define a custom command which may be used with the `tease` command. |
||||||
|
|
||||||
|
## 1) Create A Function |
||||||
|
|
||||||
|
Create a function that does what you want: |
||||||
|
|
||||||
|
```python |
||||||
|
# mycommands.py |
||||||
|
from scripttease.lib.commands.base import Command |
||||||
|
|
||||||
|
def do_something_impressive(arg1, **kwargs): |
||||||
|
return Command("ls -ls %s" % arg1, **kwargs) |
||||||
|
``` |
||||||
|
|
||||||
|
!!! important |
||||||
|
kwargs are *always* required. |
||||||
|
|
||||||
|
## 2) Create A Mapping |
||||||
|
|
||||||
|
```python |
||||||
|
# mycommands.py |
||||||
|
MAPPINGS = { |
||||||
|
'impressive': do_something_impressive, |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
`impressive` is now mapped to the function which creates the command. |
||||||
|
|
||||||
|
## 3) Supply The Mapping to the Command Factory |
||||||
|
|
||||||
|
```python |
||||||
|
from scripttease.lib.factories import command_factory |
||||||
|
from scripttease.lib.loaders import INILoader |
||||||
|
from .mycommands import MAPPINGS |
||||||
|
|
||||||
|
ini = INILoader("path/to/commands.ini") |
||||||
|
ini.load() |
||||||
|
|
||||||
|
commands = command_factory(ini, mappings=MAPPINGS) |
||||||
|
|
||||||
|
``` |
||||||
|
|
||||||
|
## 4) Include the Custom Command |
||||||
|
|
||||||
|
```ini |
||||||
|
[this is my custom command] |
||||||
|
impressive: testing |
||||||
|
``` |
After Width: | Height: | Size: 301 KiB |
After Width: | Height: | Size: 393 KiB |
After Width: | Height: | Size: 33 KiB |
After Width: | Height: | Size: 111 KiB |
After Width: | Height: | Size: 181 KiB |
@ -0,0 +1,41 @@ |
|||||||
|
# Post a Message to Slack |
||||||
|
|
||||||
|
The `slack` command may be used to send a message to a Slack channel. This uses the Incoming Webhooks feature, which requires some additional setup. |
||||||
|
|
||||||
|
!!! note |
||||||
|
The following steps were accurate as of September 2020. |
||||||
|
|
||||||
|
## 1) Log in to Slack |
||||||
|
|
||||||
|
Log in to Slack and go to [Your Apps](https://api.slack.com/apps). |
||||||
|
|
||||||
|
## 2) Create New Slack App |
||||||
|
|
||||||
|
Create a new Slack app. |
||||||
|
|
||||||
|
## 3) Select Incoming Webhooks |
||||||
|
|
||||||
|
On the next page, select Incoming Webhooks and then toggle activation. |
||||||
|
|
||||||
|
![incoming webhooks](images/slack-1.jpg) |
||||||
|
|
||||||
|
## 4) Select Channel |
||||||
|
|
||||||
|
Next, click "Add new Webhook to Workspace" and select the channel to which the message will be posted. |
||||||
|
|
||||||
|
![slack 2](images/slack-2.jpg) |
||||||
|
|
||||||
|
![slack 3](images/slack-3.jpg) |
||||||
|
|
||||||
|
## 5) Copy the URL |
||||||
|
|
||||||
|
Copy the URL for the new webhook to use as the ``url`` parameter for the Slack command. |
||||||
|
|
||||||
|
```ini |
||||||
|
[send a message to slack] |
||||||
|
slack: "This is a test message." |
||||||
|
url: the URL you created goes here |
||||||
|
``` |
||||||
|
|
||||||
|
!!! tip |
||||||
|
Define this URL in a `variables.ini` file if you need to send multiple messages to the same channel. |
@ -0,0 +1,35 @@ |
|||||||
|
# Post a Message to Twist |
||||||
|
|
||||||
|
The `twist` command may be used to send a message to Twist, which requires some additional setup. |
||||||
|
|
||||||
|
!!! note |
||||||
|
The following steps were accurate as of September 2020. |
||||||
|
|
||||||
|
## 1) Log In To Twist |
||||||
|
|
||||||
|
Log in to Twist and from the profile menu go to Add Integrations. Then click on Build and "Add a new integration". |
||||||
|
|
||||||
|
## 2) Provide Requested Information |
||||||
|
|
||||||
|
Provide the requested info. |
||||||
|
|
||||||
|
![twist 1](images/twist-1.png) |
||||||
|
|
||||||
|
## 3) Install Integration |
||||||
|
|
||||||
|
After submitting this info, go to Installation. Select a channel and who to notify. Then click "Install integration". |
||||||
|
|
||||||
|
![twist 2](images/twist-2.png) |
||||||
|
|
||||||
|
## 4) Copy the URL |
||||||
|
|
||||||
|
Copy the "Post content manually" URL for use in your configuration file. |
||||||
|
|
||||||
|
```ini |
||||||
|
[post a message to twist] |
||||||
|
twist: "This is a test message." |
||||||
|
url: the URL you created goes here |
||||||
|
``` |
||||||
|
|
||||||
|
!!! tip |
||||||
|
Define this URL in a `variables.ini` file if you need to send multiple messages to the same channel. |
@ -0,0 +1,50 @@ |
|||||||
|
# Use Script Tease With Common Kit |
||||||
|
|
||||||
|
Since the focus of Script Tease is to convert plain text instructions into valid command line statements, it does *not* provide support for executing those statements either locally or remotely. However, The shell component of [python-commonkit](https://docs.develmaycare.com/en/python-commonkit/stable/components/#module-commonkit.shell) *does* provide support for executing commands in local POSIX environments. |
||||||
|
|
||||||
|
Here is an example of how to use these packages together: |
||||||
|
|
||||||
|
```python |
||||||
|
from commonkit.shell import Command |
||||||
|
from scripttease.lib.factories import command_factory |
||||||
|
from scripttease.lib.loaders import INILoader |
||||||
|
|
||||||
|
def execute(step): |
||||||
|
command = Command( |
||||||
|
step.statement, |
||||||
|
comment=step.comment, |
||||||
|
path=step.cd, |
||||||
|
prefix=step.prefix, |
||||||
|
shell=step.shell |
||||||
|
) |
||||||
|
|
||||||
|
# Sudo is a different class, but identical in behavior. |
||||||
|
command.sudo = step.sudo |
||||||
|
|
||||||
|
if command.run(): |
||||||
|
print("[success] %s" % step.comment) |
||||||
|
else: |
||||||
|
print("[failure] %s" % step.comment) |
||||||
|
if step.stop: |
||||||
|
print("I can't go on: %s" % command.error) |
||||||
|
exit(command.code) |
||||||
|
|
||||||
|
|
||||||
|
ini = INILoader("path/to/commands.ini") |
||||||
|
ini.load() |
||||||
|
steps = command_factory(ini) |
||||||
|
|
||||||
|
# A failure to load results in None. |
||||||
|
if steps is None: |
||||||
|
print("Failed to load steps. Bummer.") |
||||||
|
exit(1) |
||||||
|
|
||||||
|
# Iterate through each step to create a COMMON KIT command instance. |
||||||
|
for step in steps: |
||||||
|
|
||||||
|
# To preview ... |
||||||
|
# print(step.get_statement(cd=True)) |
||||||
|
execute(step) |
||||||
|
``` |
||||||
|
|
||||||
|
Common Kit is already a dependency of Script Tease, so it is installed by default. The ``execute()`` function provides the interface between Script Tease command instances and Common Kit command instances. |
@ -0,0 +1,14 @@ |
|||||||
|
# Itemized Commands |
||||||
|
|
||||||
|
It is sometimes useful to create a single command entry that does the same thing with different input. Script Tease facilitates this with the "itemized" command. |
||||||
|
|
||||||
|
```ini |
||||||
|
[install common utilities] |
||||||
|
install: $item |
||||||
|
items: curl, git, lftp, wget |
||||||
|
``` |
||||||
|
|
||||||
|
In the example above, statements will be generated to install each of the packages named in `items`. |
||||||
|
|
||||||
|
!!! note |
||||||
|
Content commands (`explain` and `screenshot`) and Template commands cannot be itemized. |
@ -0,0 +1,37 @@ |
|||||||
|
# Templates |
||||||
|
|
||||||
|
Script Tease supports processing of template files using Jinja2. |
||||||
|
|
||||||
|
## Location of Templates |
||||||
|
|
||||||
|
By default, the location of template files are reckoned as relative to the commands configuration file. |
||||||
|
|
||||||
|
```text |
||||||
|
package_name/ |
||||||
|
|-- commands.ini |
||||||
|
|-- templates |
||||||
|
| `-- httpd.conf |
||||||
|
``` |
||||||
|
|
||||||
|
Upon loading the `commands.ini` file, any reference to a template file is assumed to be in the `templates/` directory in the same location. |
||||||
|
|
||||||
|
!!! tip |
||||||
|
The `-T` switch of the `tease` command may be used to add template locations. |
||||||
|
|
||||||
|
## Template Context |
||||||
|
|
||||||
|
The context variables available to parse the template are: |
||||||
|
|
||||||
|
- Variables received on the command line are included in the context. |
||||||
|
- Variables loaded from a file. |
||||||
|
- Options passed to the loader from the command line are also included. |
||||||
|
|
||||||
|
Additionally, any unrecognized parameters specified in the command are included in the context. For example: |
||||||
|
|
||||||
|
```ini |
||||||
|
[create the httpd.conf file] |
||||||
|
template: httpd.conf /etc/apache/sites-available/example.com.conf |
||||||
|
ssl_enabled: yes |
||||||
|
``` |
||||||
|
|
||||||
|
`ssl_enabled` is not a normal parameter for the template command, so it is included in the context. |
Loading…
Reference in new issue