- 12 Nov 2024
-
DarkLight
-
PDF
Python Service
- Updated on 12 Nov 2024
-
DarkLight
-
PDF
Python Script
This guide demonstrates the basic concepts around creating and executing a Python script service within IAG5.
Prerequisites
-
You will need a Git repository setup with a Python script within it.
-
Review the Create Repository command to understand how to create a repository.
Create Python Script
The command iagctl create python-script
will create the Python script service.
➡ More detail on all creation
options is available here.
The command shown below creates a Python script service within IAG5 called simple-python
that will leverage a previously configured repository called gateway-resources
.
>_ iagctl create python-script simple-python --repository gateway-resources --working-dir pythonscripts --filename main.py
Python Scripts Directory
Important information to understand the structure of gateway-resources
is presented below.
gateway-resources
has been specified for use via the --repository
flag
├── README.md
├── ansibleplaybooks
├── pythonscripts
│ ├── main.py
│ └── requirements.txt
└── opentofuplans
Notice that the Python script exists in a directory called pythonscripts
. This is denoted using the --working-dir
flag.
Inside the pythonscripts
directory there is an actual script called main.py
. This is denoted with the --filename
flag.
If the Python script requires external libraries, place a requirements.txt
file within the working directory. IAG5 will automatically manage the dependencies.
Verify Python Script Service
Details about the previously created Python script service can be viewed by running the describe
command.
➡ More detail on all iagctl-describe
commands is available here.
>_ iagctl describe python-script simple-python
Output:
Name: simple-python
Repo Name: gateway-resources
Working Dir: pythonscripts
FileName: main.py
Decorator:
Description:
Tags:
Execute Python Script
Executing a Python script is simple from IAG5 by utilizing the run python-script
command.
Review the contents of main.py
in the Python script shown below.
# main.py
from netmiko import ConnectHandler
import argparse
def main():
parser = argparse.ArgumentParser(description="Netmiko Command Service")
parser.add_argument('--command', required=True, help="Command to run")
parser.add_argument('--host', required=True, help="Host to connect to")
args = parser.parse_args()
# YOUR EXECUTION CODE GOES BELOW THIS LINE
cisco1 = {
"device_type": "cisco_ios",
"host": args.host
}
with ConnectHandler(**cisco1) as net_connect:
output = net_connect.send_command(args.command)
print(output)
if __name__ == "__main__":
main()
This script requires the netmiko
library. This is specified in the requirements.txt
file located in the working-dir
when creating the service.
netmiko==4.3.0
When executing a Python script using the run python-script
command, IAG5 will automatically look for the requirements.txt
file in the working directory that was specified during the creation of the service.
IAG5 will then create the virtual environment with all the dependencies specified in requirements.txt
and execute the script within that virtual environment. This feature allows for easy management of Python dependencies.
The Python script takes in two inputs: command
and host
. They can be specified using the --set
flag.
>_ iagctl run python-script simple-python --set host=10.0.0.1 --set command="show version"
Output:
Start Time: 2024-01-01T12:00:00Z
End Time: 2024-01-01T12:00:01Z
Elapsed Time: 1.372672s
Return Code: 0
================================================================================
System Version Information
================================================================================
...truncated for document
Stderr:
IAG5 will take the key=values
defined via --set
command and then pass it to the Python service.
For this example, the following command is run by IAG5 on the backend:
<python-executable> main.py --host=10.0.0.1 --command="show version"
Barebones Script With Inputs
The most important part of working with a Python script is how to get the arguments into the script. A key integration between IAG5 and a Python service is the ability to read key=value
from the command line.
Every Python script that is written will have something that looks like the Python code below. You can follow the barebones example to get started with accepting input arguments.
# main.py
import argparse
def main():
parser = argparse.ArgumentParser(description="iagctl example")
parser.add_argument('--name', required=True, help="Name to greet")
args = parser.parse_args()
name = args.name
# YOUR EXECUTION CODE GOES BELOW THIS LINE
print(f"Hello, {name}")
if __name__ == "__main__":
main()
Managing Python Services
For reference and understanding, below is a flow outline depicting the steps IAG5 moves through as it manages the dependencies for a Python based service.
- Start Python Service →
- Does
requirements.txt
exist? - If Yes, get contents of
requirements.txt
. Go to step 4. - Does virtual environment ("venv") exist with the contents?
- If Yes, go to step 5.
- If No, complete these steps:
- Find a Python interpreter.
- Create virtual environment.
- Install requirements.
- Use existing venv. Go to step 6.
- Run Python Script with venv.
Decorators
It is possible to put restrictions around the inputs that are accepted by a Python script by utilizing decorators. For more information on decorators, please refer to this guide.
CLI Reference
For command references see → Command References Index
-
iagctl create service python-script
-
iagctl run python-script
-
iagctl get services
-
iagctl describe service
-
iagctl delete service