Skip to content

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")

Create a new deployment with the above code inside the deployment.py file.
The input of the deployment should be as follows:

Input:

Name: Type:
command Array of strings

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. Note that no output is given back, the result of the commands can be found in the logs of the deployment.