A sample function that uppercases the input text using Python. Unlike the traditional shell invoker, this sample also installs Python and is implemented in Python instead of shell.
-
Setup a running riff install (tested with riff 0.0.4)
See riff's Getting Started guide. Skip if you already have riff running.
-
Clone this repo
git clone https://github.com/projectriff-samples/shell-python2-uppercase.git cd shell-python2-uppercase
-
Initialize the function definition
Since there are multiple files in the directory, we need to tell
riff
which file to use.riff init -a uppercase.sh
-
Install Python
The shell-function-invoker does not have Python installed by default. We can copy Python from another Docker image.
Add this line to the
Dockerfile
generated byriff init
after theFROM
command:COPY --from=python:2.7-alpine /usr/local /usr/local
Resulting in a
Dockerfile
like:FROM projectriff/shell-function-invoker:latest COPY --from=python:2.7-alpine /usr/local /usr/local ARG FUNCTION_URI="/uppercase.sh" ADD uppercase.sh / ENV FUNCTION_URI $FUNCTION_URI
NOTE: The script has a
.sh
file extension even though the content is Python. If you'd like to renameuppercase.sh
touppercase.py
make sure to also update references in theDockerfile
. -
Deploy the function to riff
riff update
-
Invoke the function
riff publish -d hello -r
Resulting in:
Posting to http://127.0.0.1:31970/requests/shell-python2-uppercase HELLO
The riff shell-function-invoker executes an arbitrary file. While a shell script is most common, any executable file will work.
The first line of uppercase.sh
is a shebang which instructs the system how to interpret the file. In this case we tell the interpreter that the rest of the file is Python.
#!/usr/bin/env python2
But Python isn't available within the shell-function-invoker container by default. We install Python to the container by copying the bits from another Docker image that has Python installed, in this case python:2.7-alpine
.
COPY --from=python:2.7-alpine /usr/local /usr/local
You can be more precise with the exact version of Python or upgrade to Python3 by changing the version number of the Docker container the bits are copied from.
The script itself consumes standard input, converts the test to uppercase and prints to standard output.
for line in fileinput.input():
sys.stdout.write(line.upper())
When the input stream ends, the script exits.