Page History
Table of Contents |
---|
Introduction
The JS7 can be used to execute any scripts, executables and programs including Python® scripts.
- Python® is a frequently used scripting language available for Linux, MacOS, Windows and other platforms.
- Docker® offers a Python® SDK to manage containers, see https://docs.docker.com/engine/api/sdk/
- This article explains how to use the Python® SDK for Docker® with JS7 job scripts.
...
Install the Python® SDK as available from https://github.com/docker/docker-py
Code Block language bash title Install docker-py plugin pip install docker
- Explanation:
- Python® has to be available on the machine that the JS7 Agent is operated for. This includes Agents operated on premises and with Docker® containers.
- This is just an example, there are more ways how to install of installing the Python® SDK for Docker®.
- Explanation:
Create a workflow with a shell job that imports the Python® SDK like this:
Code Block language bash title Import docker-py plugin with Agents operated for Unix #!/usr/bin/python import docker client = docker.from_env() ...
Code Block language bash title Import docker-py plugin with Agents operated for Windows @@findstr/v "^@@f.*&" "%~f0"|python.exe -&goto:eof import docker client = docker.from_env() ...
Explanation:For details on how to invoke Python® from shell jobs see see the JS7 - How to run Python scripts from jobs article.
- Create JS7 - Job Resources for your containers.
- Best practice is to use one a Job Resource per container.
- This allows to specify specification of any properties of a container such as image name, host name, container name, mounts etc. from variables of a Job Resource.
Examples
Examples The examples make use of a Job Resource and a number of Workflows that are managed with the Configuration view like this:
...
Download (upload .json): agent-2-0-standalone.jobresource.json
When adding managing a Job Resource this view offers two sub-views:
- The Arguments sub-view that allows to specify specifying variables holding which hold pairs of variable names and values.
- Arguments can be used for parameterization of JVM jobs and they can be used in workflow instructions such as the JS7 - If Instruction.
- The Environment Variables sub-view that allows to specify specifying environment variables from pairs of names and values that are forwarded to shell jobs.
- Environment Variables can be used of for parameterization of shell jobs.
- The below following names and values are an example for examples of frequently used parameters when setting up containers, for example with the
docker run
command:repository_name
: Identifier of the repositoryimage_name
: Identifier of the image that is used to instantiate the container.container_name
: Name assigned the container.host_name
: Hostname (FQDN) assigned the container.- etc.
Consider the following example of the Environment Variables sub-view:
- Names of environment variables can be freely chosen within the limits of the operating system. Names are stored with uppercase letters.
- The values of environment variables reference variables previously configured with the Arguments sub-view.
- This allows to the use of variables from both JVM jobs and shell jobs.
- As an alternative it is possible to directly assign strings to the values of environment variables and not to use the Arguments sub-view.
...
Workflow to start a Container
The example makes use of uses a single job that runs on an Agent with access to the Docker® daemon.
- Download (upload .json): pdStartContainer.workflow.json
- The job is assigned the
agent-2-0-standalone
Job Resource that holds the parameterization of the container that should be started.
The job script of the single job in this workflow looks like this:
...
Explanation:
- The job script looks fairly complex uses a number of parameters as it maps all options used to run a JS7 Agent from a Docker® container as explained with in the JS7 - Agent Installation for Docker Containersarticle.
- The relevant part is the
client.containers.run()
function that which is parameterized with frequently used options to specify for specifying the image name, the hostname, network name etc. - Environment variables are available from the assigned Job Resource.
- The optional part includes to display displaying log output created on start-up of the container using the
container.logs()
function.
Workflow to stop a Container
The example makes use of uses a single job that which runs on an Agent with access to the Docker® daemon.
- Download (upload .json): pdStopContainer.workflow.json
- The job is assigned the
agent-2-0-standalone
Job Resource that which holds the parameters that identify the container to be stopped.
The job script of the single job in this workflow looks like this:
...
- Download (upload .json): pdExecContainer.workflow.json
- The job is assigned the
agent-2-0-standalone
Job Resource that holds the parameters that which identify the container to be stopped. - Consider the
command
workflow variable that which holds the command to be executed in the container. Adding a variable from a workflow offers the increases flexibility that by allowing such variables can to be updated for use with different values from each order that executes the workflow.
...