run python-script

Execute a Python script service

Synopsis

Run

This command will execute a Python script service and display the script's resulting stdout, stderr, return code, as well as some additional execution time information.

When running a Python script service, one can specify CLI arguments that the Python script accepts using Python's argparse.ArgumentParser using the --set flag. The --set flag takes in a key=value syntax. Any variables passed in using --set will be validated against the decorator if one was defined during the service's creation. This then will be passed to the python script that was created.

To view helpful information about what inputs are accepted by a particular service, run this command with just the service name and the --use flag.

Stub Code For Taking In Arguments

When running your python script, it is important to understand how torero will send values defined by the --set flag to the script. On execution, torero will send arguments in the format of --key=value. For example, running

>_ torero run python-script my-script \
--set device=10.0.0.1 \
--set commands='["show ver"]'

will essentially cause torero to run the following command within your virtual environment: python main.py --device=10.0.0.1 --commands='["show ver"]'

See the example stub code below that would parse these inputs.

main.py

import argparse
import json

def main():
    parser = argparse.ArgumentParser(description="Run commands on a network device.")
    parser.add_argument('--device', required=True, help="Device IP address or hostname")
    parser.add_argument('--commands', required=True, help="Commands to run.")
    args = parser.parse_args()

    device = args.device
    commands_input = args.commands

    try:
         Try to parse commands as JSON. Used when the value would be JSON.
        commands = json.loads(commands_input)
        print("Debug: Commands parsed as JSON")
    except json.JSONDecodeError:
         If it's not valid JSON, treat it as a single command string
        commands = [commands_input]
        print("Debug: Commands parsed as single string command")

    print(device)
    print(commands)

     YOUR EXECUTION CODE GOES BELOW THIS LINE    

if __name__ == "__main__":
    main()
torero run python-script <service-name> [flags]

Examples

Simple Example

Run a simple Python script service called my-python-service.

>_ torero run python-script my-python-service

Setting Arguments

Run a Python script service called my-python-service that takes in arguments of device and commands. Ensure that you understand how arguments will be fed into your script by reading the Stub Code For Taking In Arguments section in this command's help menu.

>_ torero run python-script my-python-service \
--set device=10.0.0.1 \
--set commands='["show ver"]'

Options

  -h, --help              help for python-script
      --set stringArray   Sets an input argument to be passed into the script via the CLI when executed.
                          Arguments are sent in the order that the are defined here and are appended to
                          the arguments already defined on the pythonscript service. They must follow the
                          key=value syntax and the service must support the inputs. If a decorator is not
                          set and you want to use positional arguments you may pass in the key of 'arg='
                          as in '--set arg=first_argument --set arg=second_argument'
      --use               Display the possible inputs of the script via the decorator.

Options inherited from parent commands

      --config string   Path to the configuration file
      --raw             Displays the result of the command in its raw format
      --verbose         Enable verbose output

SEE ALSO