Scripts
Scripts are in fact just regular python scripts executed for each host. Surprising, I know. In a script, Fora behaves like a regular library. Just remember that your inventory will already have been been loaded, and a connection to the current host will have been established.
Scripts are always executed in their containing folder. This makes it easy to bundle files related to the script in folders relative to it.
Accessing global state
You can access the inventory and the current host via the exposed global state in the main fora
module. You can use these to directly execute commands on the host, or to retrieve variables from a host or inventory.
Using operations
The most important part of your deploy scripts will be calling operations to modify the remote host. Operations examine the host's current state and execute just the neccessary commands to bring it to the target state. Operations are idempotent functions, so calling them multiple times doesn't affect the final outcome.
All operations return a
OperationResult
object, which can be used to examine the initial and target state of the host regarding this operation.If an operation fails and
check=False
has not been passed to the operation, the script is automatically aborted.All operations support an optional
name="Description of what is being done"
parameter, which will be printed on execution so it is easier to follow what is being done.
An overview of all available operations can be found in the https://github.com/oddlama/fora-docs/blob/main/usage/introduction/api/index_operations.md section. You may of course also write your own operations. For this I recommend reading the implementation of some existing operations.
Variables and fallback values
You can customize your script's behavior based on host variables. Usually, scripts will have a set of variables that can be customized by the host, but also need a fallback value. For this, you can define a global variable in your script with the same name as the expected host variable. If the host has no such variable, accessing host.myvariable
will automatically return your global fallback value in that case.
Parameters
Instead of customizing script behavior based on host variables, you might want to write a script that can be reused in your deploy and should accept parameters.
Remote defaults
If parameters like owner=
, group=
or mode=
are not given, they will default to some value initially specified by the inventory as base_remote_settings()
.
When configuring services, you often need to create many files with the same specific owner, group and mode. Scripts provide a context manager defaults()
to temporarily change these defaults to avoid repetition. These defaults also specify process information, such as which user is used to run commands on the remote. Here is an overview over what can be modified:
owner
- the owner for new files and directoriesgroup
- the group for new files and directoriesfile_mode
- the mode for new files (determinesmode=
on operations such asfiles.upload
)dir_mode
- the mode for new directories (determinesmode=
on operations such asfiles.directory
)umask
- the effective umask for remote commandscwd
- the working directory for remote commandsas_user
- the user as which the remote commands are executedas_group
- the group as which the remote commands are executed
Last updated