Skip to main content

A module for working with Cloud Native Application Bundles in Python

Project description

Python CNAB Library

Work-in-progress library for working with CNAB in Python.

There are probably three main areas of interest for a CNAB client:

  1. Handling the bundle.json format (101)
  2. Building invocation images (102)
  3. Running actions against a CNAB (103)

Claims and Signing are optional but will be worked on once the above are stable.

Installation

The module is published on PyPi and can be installed from there.

pip install cnab

Parsing bundle.json

Nothing too fancy here, the Bundle class has a from_dict static method which builds a full Bundle object.

import json
from cnab import Bundle

with open("bundle.json") as f:
    data = json.load(f)

bundle = Bundle.from_dict(data)

This could for example be used for validation purposes, or for building user interfaces for bundle.json files.

Describing bundle.json in Python

You can also describe the bundle.json file in Python. This will correctly validate the structure based on the current specification and would allow for building a custom DSL or other user interface for generating bundle.json files.

from cnab import Bundle, InvocationImage

bundle = Bundle(
    name="hello",
    version="0.1.0",
    invocation_images=[
        InvocationImage(
            image_type="docker",
            image="technosophos/helloworld:0.1.0",
            digest="sha256:aaaaaaa...",
        )
    ],
)

print(bundle.to_json())

Running CNABs

The module supports running actions on a CNAB, using the docker driver.

from cnab import CNAB

# The first argument can be a path to a bundle.json file, a dictionary
# or a full `Bundle` object
app = CNAB("fixtures/helloworld/bundle.json")

# list available actions
print(app.actions)

# list available parameters
print(app.parameter)

# run the install action
print(app.run("install"))

# run the install action specifying a parameters
print(app.run("install", parameters={"port": 9090}))

# Many applications will require credentials
app = CNAB("fixtures/hellohelm/bundle.json")

# list required credentials
print(app.credentials)

# Here we pass the value for the required credential
# in this case by reading the existing configuration from disk
with open("/home/garethr/.kube/config") as f:
    print(app.run("status", credentials={"kubeconfig": f.read()}))

Note that error handling for this is very work-in-progress.

Thanks

Thanks to QuickType for bootstrapping the creation of the Python code for manipulating bundle.json based on the current JSON Schema.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cnab-0.1.4.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

cnab-0.1.4-py3-none-any.whl (31.6 kB view details)

Uploaded Python 3

File details

Details for the file cnab-0.1.4.tar.gz.

File metadata

  • Download URL: cnab-0.1.4.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.10 CPython/3.7.2 Linux/4.18.0-13-generic

File hashes

Hashes for cnab-0.1.4.tar.gz
Algorithm Hash digest
SHA256 0580b14ec8cd2bc49362e996f31efb44ff81c678caae475da82d64cee701fbad
MD5 d439502aa91a726b00cd571a90050076
BLAKE2b-256 799b0b054ef836aa122e4ff4eabbe353d4d78570a918f0aae0fab8bdef0b1f05

See more details on using hashes here.

File details

Details for the file cnab-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: cnab-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 31.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.10 CPython/3.7.2 Linux/4.18.0-13-generic

File hashes

Hashes for cnab-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e7a42a4b7034e77c2fe3157bfdfc3b314be6fdd5c2626daf432ec168de1a7b28
MD5 a9124460cb5998f7ecbd0add1e30a7e2
BLAKE2b-256 83abe1d9e2e183e7049acbc8a51475580a73cd650b432f4d646beacda85951b7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page