Packaging source code

Create source and deployment packages with fission package, build source archives on the cluster with a builder, and attach packages to functions.

A Fission package is the deployable artifact behind a function: it holds your code as either a source archive (built on the cluster by a builder) or a pre-built deployment archive. This page shows how to create both kinds of package and attach them to a function. For the concepts behind builders and the build pipeline, read Packages and builds.

Creating a Source Package

Before you create a package, you need to create an environment with an associated builder image:

$ fission env create --name pythonsrc --image ghcr.io/fission/python-env \
                     --builder ghcr.io/fission/python-builder \
                     --mincpu 40 --maxcpu 80 \
                     --minmemory 64 --maxmemory 128 \
                     --version 3 --poolsize 2

environment 'pythonsrc' created

Let’s take a simple python function which has a dependency on the pyyaml module. We can specify the dependencies in requirements.txt and a simple command to build from source. The directory structure and file contents look like this:

sourcepkg/
├── __init__.py
├── build.sh
├── requirements.txt
└── user.py

The file contents:

  • user.py
import sys
import yaml

document = """
  a: 1
  b:
    c: 3
    d: 4
"""

def main():
    return yaml.dump(yaml.load(document))
  • requirements.txt
pyyaml
  • build.sh
#!/bin/sh
pip3 install -r ${SRC_PKG}/requirements.txt -t ${SRC_PKG} && cp -r ${SRC_PKG} ${DEPLOY_PKG}

Make sure the build.sh file is executable:

$ chmod +x build.sh

Now create an archive before creating the package:

$zip -jr demo-src-pkg.zip sourcepkg/
  adding: __init__.py (stored 0%)
  adding: build.sh (deflated 24%)
  adding: requirements.txt (stored 0%)
  adding: user.py (deflated 25%)

Using the source archive created in previous step, you can create a package in Fission:

$ fission package create --sourcearchive demo-src-pkg.zip --env pythonsrc --buildcmd "./build.sh"
Package 'demo-src-pkg-zip-8lwt' created

This is a source package, so the create command above included a build command. Once you create the package, the build process will start and you can see the build logs with the fission package info command:

$ fission pkg info --name demo-src-pkg-zip-8lwt

Name:        demo-src-pkg-zip-8lwt
Environment: pythonsrc
Status:      succeeded
Build Logs:
Collecting pyyaml (from -r /packages/demo-src-pkg-zip-8lwt-v57qil/requirements.txt (line 1))
  Using cached PyYAML-3.12.tar.gz
Installing collected packages: pyyaml
  Running setup.py install for pyyaml: started
    Running setup.py install for pyyaml: finished with status 'done'
Successfully installed pyyaml-3.12

Create the function from this package. The package already carries its source archive, environment, and build command, so fission fn create ignores those flags if you pass them. You only need to supply the entrypoint:

$ fission fn create --name srcpy --pkg demo-src-pkg-zip-8lwt --entrypoint "user.main"

function 'srcpy' created

# Run the function:
$ fission fn test --name srcpy
a: 1
b: {c: 3, d: 4}

Creating a Deployment Package

Before you create a package, you need to create an environment with the builder image:

$ fission env create --name pythondeploy --image ghcr.io/fission/python-env \
                     --builder ghcr.io/fission/python-builder \
                     --mincpu 40 --maxcpu 80 \
                     --minmemory 64 --maxmemory 128 \
                     --version 3 --poolsize 2

environment 'pythondeploy' created

We’ll create a deployment archive from a directory containing a simple Python script that outputs “Hello, world!”:

$ cat testDir/hello.py

def main():
    return "Hello, world!"

$ zip -jr demo-deploy-pkg.zip testDir/

Using the archive and environments created previously, you can create a package:

$ fission package create --deployarchive demo-deploy-pkg.zip --env pythondeploy

Package 'demo-deploy-pkg-zip-whzl' created

A deployment archive skips the build step, so its build logs are empty:

$ fission package info --name demo-deploy-pkg-zip-whzl

Name:        demo-deploy-pkg-zip-xlaw
Environment: pythondeploy2
Status:      succeeded
Build Logs:

Finally you can create a function with the package and test the function:

$ fission fn create --name deploypy --pkg demo-deploy-pkg-zip-whzl --entrypoint "hello.main"

$ fission fn test --name deploypy

Hello, world!

While these examples illustrate how to use packages, you don’t have to use them every time you need to build your source code. A better way is to use Specifications.