Skip to content
This repository has been archived by the owner on Mar 24, 2022. It is now read-only.

Uses the shell-function-invoker with Python

License

Notifications You must be signed in to change notification settings

vmware-archive/shell-python2-uppercase

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

riff Sample: Shell Python2 Uppercase

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.

Deployment

  1. Setup a running riff install (tested with riff 0.0.4)

    See riff's Getting Started guide. Skip if you already have riff running.

  2. Clone this repo

    git clone https://github.com/projectriff-samples/shell-python2-uppercase.git
    cd shell-python2-uppercase
  3. 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
  4. 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 by riff init after the FROM 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 rename uppercase.sh to uppercase.py make sure to also update references in the Dockerfile.

  5. Deploy the function to riff

    riff update
  6. Invoke the function

    riff publish -d hello -r

    Resulting in:

    Posting to http://127.0.0.1:31970/requests/shell-python2-uppercase
    HELLO
    

How it works

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.

About

Uses the shell-function-invoker with Python

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages