Skip to content

Commit

Permalink
feat: add python-runtime (#1496)
Browse files Browse the repository at this point in the history
Signed-off-by: realanna <anna.reale@dynatrace.com>
  • Loading branch information
RealAnna committed May 31, 2023
1 parent 9801e5d commit 76a4bd9
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
18 changes: 18 additions & 0 deletions python-runtime/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM python:3.9 AS production

LABEL org.opencontainers.image.source="https://github.com/keptn/lifecycle-toolkit" \
org.opencontainers.image.url="https://keptn.sh" \
org.opencontainers.image.title="Keptn Python Runtime" \
org.opencontainers.image.vendor="Keptn" \
org.opencontainers.image.licenses="Apache-2.0"

RUN pip install -q --disable-pip-version-check pyyaml GitPython requests

COPY entrypoint.sh /entrypoint.sh

USER 1000:1000

ENV CMD_ARGS=""
ENV SCRIPT=""

ENTRYPOINT /entrypoint.sh
68 changes: 68 additions & 0 deletions python-runtime/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Keptn Lifecycle Controller - Function Runtime

## Build

```shell
docker build -t lifecycle-toolkit/python-runtime:${VERSION} .
```

## Usage

The Keptn python runtime uses python3, and enables the follwing packages: requests, json, git, yaml

The Keptn Lifecycle Toolkit uses this runtime to run [KeptnTask](https://lifecycle.keptn.sh/docs/tasks/write-tasks/)
for pre- and post-checks.

`KeptnTask`s can be tested locally with the runtime using the following commands.
Replace `${VERSION}` with the KLT version of your choice.
`SCRIPT` should refer to either a python file mounted locally in the container or to a url containing the file.

### mounting a python file

```shell
docker run -v $(pwd)/samples/hellopy.py:/hellopy.py -e "SCRIPT=hellopy.py" -it lifecycle-toolkit/python-runtime:${VERSION}
```

Where the file in sample/hellopy.py contains python3 code:

```python3
import os

print("Hello, World!")
print(os.environ)
```

This should print in your shell, something like:

```shell
Hello, World!
environ({'HOSTNAME': 'myhost', 'PYTHON_VERSION': '3.9.16', 'PWD': '/', 'CMD_ARGS': '','SCRIPT': 'hellopy.py', ...})
```

### Pass command line arguments to the python command

You can pass python command line arguments by specifying `CMD_ARGS`.
The following example will print the help of python3:

```shell
docker run -e "CMD_ARGS= -help" -it lifecycle-toolkit/python-runtime:${VERSION}
```

### Pass arguments to your python script

In this example we pass one argument (-i test.txt) to the script

```shell
docker run -v $(pwd)/samples/args.py:/args.py -e "SCRIPT=args.py -i test.txt" -it lifecycle-toolkit/python-runtime:${VERSION}
```

### Use a script from url

We can call the hellopy.py script downloading it directly from github

```shell
docker run -e "SCRIPT=https://raw.githubusercontent.com/keptn/lifecycle-toolkit/main/python-runtime/samples/hellopy.py" -it lifecycle-toolkit/python-runtime:${VERSION}
```

<!-- markdownlint-disable-next-line MD033 MD013 -->
<img referrerpolicy="no-referrer-when-downgrade" src="https://static.scarf.sh/a.png?x-pxid=858843d8-8da2-4ce5-a325-e5321c770a78" />
12 changes: 12 additions & 0 deletions python-runtime/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

set -eu

regex='(https?|ftp|file)://[-[:alnum:]\+&@#/%?=~_|!:,.;]*[-[:alnum:]\+&@#/%=~_|]'

if [[ $SCRIPT =~ $regex ]]
then
curl -s $SCRIPT | python3 $CMD_ARGS -
else
python3 $CMD_ARGS $SCRIPT
fi
12 changes: 12 additions & 0 deletions python-runtime/samples/args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sys, getopt

def main(argv):
inputfile = ''
opts, _ = getopt.getopt(argv,"i:",["ifile="])
for opt, arg in opts:
if opt in ("-i", "--ifile"):
inputfile = arg
print ('Input file is ', inputfile)

if __name__ == "__main__":
main(sys.argv[1:])
4 changes: 4 additions & 0 deletions python-runtime/samples/hellopy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import os

print("Hello, World!")
print(os.environ)

0 comments on commit 76a4bd9

Please sign in to comment.