Package: opentaskpy
Open Task Framework (opentaskpy)InstallationExample DeploymentConfigurationCommand Line Argumentsotf-batch-validatorEnvironment VariablesLoggingVariablesRuntime OverridesLookup pluginsAdding your ownExample VariablesTask DefinitionsTransfersExecutionsBatchesDevelopmentQuickstart for developmentBuilding and uploading to PyPiOfficial Addons/Pluginsotf-addons-awsDeveloping your own addon/pluginLookup PluginsAddonsOpen Task Framework (OTF) is a Python based framework to make it easy to run predefined file transfers and scripts/commands on remote machines.Currently the framework is primarily based around being able to use SSH or SFTP to communicate with remote hosts in order to manipulate files and run commands. This is done via the use of SSH keys, which must be set up in advance.OTF has 3 main concepts for tasks. These are:TransfersExecutionsBatchesFor more details, see thetask typesdocInstallationOTF can be run either as an installed script, or via a docker containerInstall via pip:pipinstallopentaskpyThetask-runscript will be added to your PATH, and you can invoke it directly.To run via docker, use theDockerfileto create your own base image using just the standard opentaskpy library. However if you want to install addons, you'll need to customise this, to install the additional packages first, before bundling it as a Docker image.dockerbuild-topentaskpy-fDockerfile.# Build the imagedockerrun--rm--volume/opt/otf/cfg:/cfg--volume/var/log/otf:/logs--volume/home/<USER>/.ssh/id_rsa:/id_rsa-eOTF_SSH_KEY=/id_rsa-eOTF_LOG_DIRECTORY=/logstask-run-t<TASKNAME>-c/cfg# Run a taskThe defaultopentaskpylibrary is only really designed to use SSH for executions and file transfers. To do this, you need to make sure that the host/container that is running thetask-runscript has a private RSA key, that is trusted on all remote hosts that you're running against.An environment variableOTF_SSH_KEYcan be used to define a default SSH key to use for all SSH connectivity. This can be overridden at the transfer/execution level by specifying akeyFilein thecredentialssection of the protocol definition.Example DeploymentThis is an example deployment for using OTF in an AWS environment, using BMC Control-M as the job scheduler. Control-M could be replaced with any job scheduler, AWS EventBridge for example, depending on your requirements."Admin/Status Portal" currently does not exist, and is a placeholder for a potential further project to visualise the OTF configs, as well as including real-time job statuses from the job scheduler.ConfigurationThere are several ways to customise the running of tasks.Command Line ArgumentsThe following details the syntax of thetask-runcommand:usage: task-run [-h] -t TASKID [-r RUNID] [-v VERBOSITY] [-c CONFIGDIR]

options:
  -h, --help            show this help message and exit
  --noop                Do not attempt to run anything. Only load the config files to validate that they're OK
  -t TASKID, --taskId TASKID
                        Name of the JSON config to run
  -r RUNID, --runId RUNID
                        Unique identifier to correlate logs with. e.g. if being triggered by an external scheduler
  -v VERBOSITY, --verbosity VERBOSITY
                        Increase verbosity
  -c CONFIGDIR, --configDir CONFIGDIR
                        Directory containing task configurations--noopThis is useful for validating the configuration is valid. It will not actually do anything, other than load the configuration files and validate that they are valid. N.B. This only works for executions and transfers.-t, --taskIdThis relates to the specific task that you want to run. It is the name of the configuration file to load (without the.jsonor.json.j2suffix), contained under theCONFIGDIR-r, --runIdA log aggregation mechanism to bundle logs for several steps in a batch together. e.g. if you pass in-r some_job_name, the log files for all executions and transfers will be placed under<logdir>/some_job_name/-v, --verbosityThere are 3 levels of logging, beyond the default INFO level. These are VERBOSE1 (1), VERBOSE2 (2), and DEBUG (3).VERBOSITY is an integer; 1, 2 or 3-c, --configDirThe directory containing all of the config files. These are the task definition JSON files, as well as the variables Jinja2 template file.In order for the process to run, you must have at least one task, and avariables.json.j2file, even if it's just an empty object definitionotf-batch-validatorThis is a script that can be used to validate the configuration of a batch. It will check that all tasks are defined consecutively, and that there are valid dependencies. It will also check that all tasks are defined in theconfigDirdirectory.It is run as follows:otf-batch-validator-c/path/to/configDir-tbatch-task-nameFull list of arguments are as follows:usage: otf-batch-validator [-h] -t TASKID [-v VERBOSITY] [-c CONFIGDIR]

options:
  -h, --help            show this help message and exit
  -t TASKID, --taskId TASKID
                        Name of the JSON config to run
  -v VERBOSITY, --verbosity VERBOSITY
                        Increase verbosity: 3 - DEBUG 2 - VERBOSE2 1 - VERBOSE1
  -c CONFIGDIR, --configDir CONFIGDIR
                        Directory containing task configurationsEnvironment VariablesThese are some environment variables that can be used to customise the behaviour of the application. There are some internally used variables too, but changing them without a full understanding of the code is not advised.OTF_NO_LOG- Disable logging to file. Only log to stderr. Set to1to enableOTF_LOG_JSON- Stderr logging will be in JSON format. Set to1to enableOTF_LOG_DIRECTORY- Path under which log files are writtenOTF_RUN_ID- (meant for internal use) An aggregator for log files. When set, all log files for a run will go under this sub directory. E.g. running a batch, all execution and transfer logs will be dropped into this sub directory, rather than a directory for each task name. This is equivalent to using-ror--runIdcommand line arguments, which is generally preferred.OTF_SSH_KEY- The private SSH key to use by default for all SSH connections. This is essential when using a basic docker container to trigger OTF. If not specified, it will default to use any private SSH keys available to the user executing the application.OTF_STAGING_DIR- Staging base directory to place files before they're dropped into their final location. Default is/tmpOTF_BATCH_RESUME_LOG_DATE- Allow resuming of batch runs from a specific date. This is useful if you want to rerun a batch from a specific date, especially if the failure happens just after midnight and the date is no longer the same as the original run. Date format isYYYYMMDDOTF_VARIABLES_FILE- Override the default variables file. This is useful when you want to use the same job definitions, but point at a different environment with different for example.LoggingBy default, OTF will log to a directory calledlogsin the current working directory. For the docker containers, unless overridden byOTF_LOG_DIRECTORY, it will write to/logsusing a symlink at/app/logsVariablesVariables can exist at a global level, and also on a per-task basis.You must always have avariables.json.j2file defined at the root of yourconfigDir.Variables can be used using the Jinja2 template syntax within task definitions. They can also be nested.Individual tasks can have their own local variables tooRuntime OverridesSometimes you might want to override the current date, or something specific about a file transfer when you manually run a task. This can be done using environment variables.Standard global variables can be overridden simply by setting an environment variable that matches the name of the variable you want to override e.g.export DD=01In the log output, you'll see something like this:Overriding global variable (DD: 05) with environment variable (01)To override task specific values, you can use the following format in the environment variable name:OTF_OVERRIDE_<TASK_TYPE>_<ATTRIBUTE>_<ATTRIBUTE>_<ATTRIBUTE>e.g.OTF_OVERRIDE_TRANSFER_SOURCE_HOSTNAMECase doesn't matter here. For attributes that are nested within an array, you can specify the array indexe.g.OTF_OVERRIDE_TRANSFER_DESTINATION_0_PROTOCOL_CREDENTIALS_USERNAMEAgain this will be logged to show you that the override is being applied.Lookup pluginsStatic variables are useful, however sometimes you need to look up something a bit more dynamic, or secret, that you don't want to hard code into the variables file.There are 2 default lookup plugins available:FileHTTP JSONThe file plugin will load the content of a file into the variable e.g."{{lookup('file',path='/tmp/variable_lookup.txt')}}"The HTTP JSON plugin will perform a very basic HTTP GET request, expecting a JSON response. The value to extract is defined by a jsonpath e.g."{{lookup('http_json',url='https://jsonplaceholder.typicode.com/posts/1',jsonpath='$.title')}}"This will hit the typicode.com side, and extract the title attribute from from the returned JSON fileAdding your ownOTF will look for plugins that are either available as an installed module (under theopentaskpy.plugins.lookupnamespace), or dropped in apluginsunder the config directory.An example Python module might be:opentaskpy.plugins.lookup.aws.ssm. This can then be referenced as a variable in a template like so:"{{lookup('aws.ssm',name='my_test_param')}}"Alternatively a lookup plugin could be placed undercfg/pluginsnamedmy_lookup.py, and used in a template:"{{lookup('my_lookup',name='my_param')}}"Example VariablesBelow are examples of some useful variables to start with:"YYYY":"{{ now().strftime('%Y') }}","MM":"{{ now().strftime('%m') }}","DD":"{{ now().strftime('%d') }}","TODAY":"{{ YYYY }}{{ MM }}{{ DD }}","MONTH_SHORT":"{{ now().strftime('%b') }}","DAY_SHORT":"{{ now().strftime('%a') }}","PREV_DD":"{{ (now()|delta_days(-1)).strftime('%d') }}","PREV_MM":"{{ (now()|delta_days(-1)).strftime('%m') }}","PREV_YYYY":"{{ (now()|delta_days(-1)).strftime('%Y') }}"Usage:"fileRegex":"somefile.*{{ YYYY }}\\.txt"Task DefinitionsTask definitions are validated using the JSON Schemas defined withinsrc/opentaskpy/config/schemas/. These are split up to make them more readable. The top level schema for each task type is defined within theschemas.pyfile, one level above.At a later date, I plan to automate the creation of the JSON schema documentation.The task definitions themselves live under thecfgdirectory, and any number of sub directories under there to allow for grouping of tasks by whatever you like. Definitions can be either a plain JSON file, or a Jinja2 template. If a template is used, it must have the.j2suffix. JSON files may also use internal variables, this is not supported when using.j2task definitions.TransfersTransfers consist of asourcedefinition, and an optionaldestination.The easiest way to see usage is to look at the examples under theexamplesdirectoryBelow is an example of all the options available for a transfer:{"type":"transfer","source":{"hostname":"{{ HOST_A }}","directory":"/tmp/testFiles/src","fileRegex":".*\\.txt","fileWatch":{"timeout":15,"directory":"/tmp/testFiles/src","fileRegex":"fileWatch\\.txt"},"logWatch":{"timeout":15,"directory":"/tmp/testFiles/src","log":"log{{ YYYY }}Watch1.log","contentRegex":"someText[0-9]","tail":true},"conditionals":{"size":{"gt":10,"lt":20},"age":{"gt":60,"lt":600}},"postCopyAction":{"action":"move","destination":"/tmp/testFiles/archive"},"protocol":{"name":"ssh","credentials":{"username":"{{ SSH_USERNAME }}"}}},"destination":[{"hostname":"{{ HOST_B }}","directory":"/tmp/testFiles/dest","permissions":{"group":"operator"},"protocol":{"name":"ssh","credentials":{"username":"{{ SSH_USERNAME }}"}}},{"hostname":"{{ HOST_B }}","directory":"/tmp/testFiles/dest","transferType":"pull","rename":{"pattern":"^(.*)\\.txt$","sub":"\\1-2.txt"},"protocol":{"name":"ssh","credentials":{"username":"{{ SSH_USERNAME }}","transferUsername":"{{ SSH_USERNAME }}"}}}]}An explanation of what's going on in the order it will handled:Tail the log file matching named:/tmp/testFiles/src/log{{ YYYY }}Watch1.logfor lines matching containing the regexsomeText[0-9], for up to 15 seconds, before giving up.Poll for a file matching the regex/tmp/testFiles/src/fileWatch\.txtfor up to 15 seconds.Find all files matching the regex/tmp/testFiles/src/.*\.txt, with the conditions that the are >10B and <20B in size, as well as being older than 60 seconds, but newer than 600 seconds since last modificationTransfer the files that were found to 2 destinations.The first destination is a simple SCP fromHOST_AtoHOST_Bwhere the file is placed under/tmp/testFiles/dest. The group ownership of the file(s) is then set tooperatorThe second destination is done via a pull from the destination server into the same directory. The SCP connects toHOST_AastransferUsername. Once the file has been retrieved, it is renamed using the following regex match^(.*)\.txt$and substitution\1-2.txtTransferred files are moved into/tmp/testFiles/archiveonHOST_AExecutionsExecutions are the simplest task to configure. They consist of a list of hosts to run on, the username to run/connect as, and the command to run.Executions do not currently have a timeout, so can in theory run forever, or until they are killed. If a timeout is required, either use a wrapper script on the host the command is being executed on, or they should be wrapped inside a batch.{"type":"execution","hosts":["{{ HOST_A }}"],"directory":"/tmp/testFiles/src","command":"touch touchedFile.txt","protocol":{"name":"ssh","credentials":{"username":"{{ SSH_USERNAME }}"}}}The above is running the commandtouch touchedFile.txton{{ HOST_A }}, from within the/tmp/testFiles/srcdirectory.If multiplehostsare defined, a thread is spawned in parallel for each host. If the command fails on any of the hosts in the list, it will cause the task run to fail, once all processes have returned a result.BatchesBatches are a little more complex. They do not contain any task definitions, only the list, and order of execution for each task.A batch task can have multiple options set that determine the execution order and conditions, as well as how failures and task reruns are handled.Each task in a batch has anorder_id, this is a unique ID for each task, and is primarily used to define dependencies.dependenciescan be applied to any task, and are simply a list of other tasks that must be completed before it is triggered.continue_on_failis a boolean that defines whether a failure would cause the whole batch to fail immediately (after existing tasks have finished executing), or whether following steps get run. This defaults to false if not defined.retry_on_rerunis a boolean that determines whether a successful task is run a second time following a failed run. If a batch exits with a failure, and then the script is reun later on that same day, by default only the steps that failed will be run. All steps can be forced to run by setting this to truetimeoutspecifies the number of seconds a task is allowed to be running before it gets terminated. This counts as a failure. The default timeout, if not specified is 300 seconds.As a batch task runs, it writes out the status of each sub task to it's log file. If a failure occurs, and the batch is rerun with the same arguments, it will attempt to resume from the point of failure. To determine the previous state, the batch handler will look at only logs that are from the current date. This is tp ensure that if something failed at 1am yesterday, but hasn't been rerun, we won't try to recover from the point of failure. Sometimes you might want to recover regardless, this can be done by passing in the date of the log files that you want to recover from, using the environment variableOTF_BATCH_RESUME_LOG_DATEin the formatYYYYMMDD. This will instruct the batch handler to look at logs with that date instead.DevelopmentThis repo has been primarily configured to work with GitHub Codespaces devcontainers, though it can obviously be used directly on your machine too.Dev and runtime packages are defined via pipenv, with arequirements.txtfor the runtime package requirementsQuickstart for developmentClone this repopip install pipenvpipenv --python 3.10 && pipenv install && cd src && pipenv install --editable .Building and uploading to PyPipython3-mbuild
python3-mtwineupload--repositorytestpypidist/*Official Addons/PluginsHere's a list of official addons:otf-addons-awsProvides transfer and execution addons:Remote handler for interacting with AWS S3 bucketsRemote handler for executing AWS Lambda functionsLookup plugins:Support for AWS SSM Parameter Store for retrieving global variablesDeveloping your own addon/pluginLookup PluginsPlugins are very simple. They simply need arunfunction, and to return the required variable based on a list of kwargs provided within the config template.3 examples are bundled by default. The simplest of which is therandomplugin, which takes 2 numbers and returns a random number between the 2.They should either be installed as a python package and accessible to load as a module, or stored within the config directory under apluginsdirectory.They must sit under theopentaskpy.plugins.lookupnamespace. The filename must match the name of the plugin being references from the template lookup function.AddonsAddons allow you to write your own interfaces with other types of remote systems. This could be a custom database provider to allow you to run stored procedures on demand, or something like AWS to perform custom transfersAddons can either be a transfer type, or execution, and must follow the same rules. They should implement all of the functions in the abstractRemoteTransferHandlerorRemoteExecutionHandlerclass, or return aNotImplementedErrorexception.You should also ensure that you define an appropriate JSON schema, and include those under theopentaskpy.addons.XXXX.remotehandlers.schemasdirectoryAddons can technically be named under any namespace, but must be referred to using the full package name in the task definition e.g.opentaskpy.addons.aws.remotehandlers.lambda.LambdaExecution