From 76a4bd92607d05c16c63ccc4c1dd91e35cb4d6b0 Mon Sep 17 00:00:00 2001 From: RealAnna <89971034+RealAnna@users.noreply.github.com> Date: Wed, 31 May 2023 14:05:40 +0200 Subject: [PATCH] feat: add python-runtime (#1496) Signed-off-by: realanna --- python-runtime/Dockerfile | 18 ++++++++ python-runtime/README.md | 68 +++++++++++++++++++++++++++++++ python-runtime/entrypoint.sh | 12 ++++++ python-runtime/samples/args.py | 12 ++++++ python-runtime/samples/hellopy.py | 4 ++ 5 files changed, 114 insertions(+) create mode 100644 python-runtime/Dockerfile create mode 100644 python-runtime/README.md create mode 100755 python-runtime/entrypoint.sh create mode 100644 python-runtime/samples/args.py create mode 100644 python-runtime/samples/hellopy.py diff --git a/python-runtime/Dockerfile b/python-runtime/Dockerfile new file mode 100644 index 0000000000..062f7301a4 --- /dev/null +++ b/python-runtime/Dockerfile @@ -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 diff --git a/python-runtime/README.md b/python-runtime/README.md new file mode 100644 index 0000000000..506be18caa --- /dev/null +++ b/python-runtime/README.md @@ -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} +``` + + + diff --git a/python-runtime/entrypoint.sh b/python-runtime/entrypoint.sh new file mode 100755 index 0000000000..d1628f229e --- /dev/null +++ b/python-runtime/entrypoint.sh @@ -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 diff --git a/python-runtime/samples/args.py b/python-runtime/samples/args.py new file mode 100644 index 0000000000..0dc3aba098 --- /dev/null +++ b/python-runtime/samples/args.py @@ -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:]) diff --git a/python-runtime/samples/hellopy.py b/python-runtime/samples/hellopy.py new file mode 100644 index 0000000000..0d973a7557 --- /dev/null +++ b/python-runtime/samples/hellopy.py @@ -0,0 +1,4 @@ +import os + +print("Hello, World!") +print(os.environ)