Run Terminal Commands in a Deployment¶
This how-to will explain how to run terminal commands inside a deployment environment through Python.
Deployment¶
In order to run commands inside a deployment environment, a deployment instance must be created that will run these commands. Luckily, this is very easy to do with the subprocess module. The following code can be used to run commands:
import subprocess
class Deployment:
def __init__(self, base_directory, context):
pass
def request(self, data):
subprocess.run(data['command'], shell=True)
print("Finished deployment")
return {'output': 1}
Create a new deployment with the above code inside the deployment.py file.
The input/output of the deployment should be as follows:
Input:¶
Name: | Type: |
---|---|
command | Array of strings |
Output:¶
Name: | Type: |
---|---|
output | Integer |
A command can now be run inside the deployment by sending the command as an array of strings to the deployment e.g. through the UbiOps web interface. For example ["ls"]
will list all files in the deployment directory. The result of the commands can be found in the logs of the deployment.