diff options
author | Matthew Heon <matthew.heon@gmail.com> | 2018-07-13 16:34:51 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-13 16:34:51 -0400 |
commit | a689639a6502bab3f49b853bc2983c1b44363b2f (patch) | |
tree | 75ba256d70545d79aa61d7c57c20df886be1555f /contrib/python/podman/examples | |
parent | 14a6d51a8432fc0c3324fec02e8729d3032f2af2 (diff) | |
parent | 74ccd9ce5f29a1df4ffe70b4d8bd00c29d5d9d15 (diff) | |
download | podman-a689639a6502bab3f49b853bc2983c1b44363b2f.tar.gz podman-a689639a6502bab3f49b853bc2983c1b44363b2f.tar.bz2 podman-a689639a6502bab3f49b853bc2983c1b44363b2f.zip |
Merge pull request #1081 from jwhonce/wip/client
remote python client for podman
Diffstat (limited to 'contrib/python/podman/examples')
-rw-r--r-- | contrib/python/podman/examples/eg_attach.py | 18 | ||||
-rw-r--r-- | contrib/python/podman/examples/eg_containers_by_image.py | 16 | ||||
-rw-r--r-- | contrib/python/podman/examples/eg_image_list.py | 10 | ||||
-rw-r--r-- | contrib/python/podman/examples/eg_inspect_fedora.py | 16 | ||||
-rw-r--r-- | contrib/python/podman/examples/eg_latest_containers.py | 19 | ||||
-rw-r--r-- | contrib/python/podman/examples/eg_new_image.py | 32 | ||||
-rwxr-xr-x | contrib/python/podman/examples/run_example.sh | 43 |
7 files changed, 154 insertions, 0 deletions
diff --git a/contrib/python/podman/examples/eg_attach.py b/contrib/python/podman/examples/eg_attach.py new file mode 100644 index 000000000..f5070dc53 --- /dev/null +++ b/contrib/python/podman/examples/eg_attach.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +"""Example: Run top on Alpine container.""" + +import podman + +print('{}\n'.format(__doc__)) + +with podman.Client() as client: + id = client.images.pull('alpine:latest') + img = client.images.get(id) + cntr = img.create(detach=True, tty=True, command=['/usr/bin/top']) + cntr.attach(eot=4) + + try: + cntr.start() + print() + except (BrokenPipeError, KeyboardInterrupt): + print('\nContainer disconnected.') diff --git a/contrib/python/podman/examples/eg_containers_by_image.py b/contrib/python/podman/examples/eg_containers_by_image.py new file mode 100644 index 000000000..bf4fdebf1 --- /dev/null +++ b/contrib/python/podman/examples/eg_containers_by_image.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +"""Example: Show containers grouped by image id.""" + +from itertools import groupby + +import podman + +print('{}\n'.format(__doc__)) + +with podman.Client() as client: + ctnrs = sorted(client.containers.list(), key=lambda k: k.imageid) + for key, grp in groupby(ctnrs, key=lambda k: k.imageid): + print('Image: {}'.format(key)) + for c in grp: + print(' : container: {} created at: {}'.format( + c.id[:12], podman.datetime_format(c.createdat))) diff --git a/contrib/python/podman/examples/eg_image_list.py b/contrib/python/podman/examples/eg_image_list.py new file mode 100644 index 000000000..ef31fd708 --- /dev/null +++ b/contrib/python/podman/examples/eg_image_list.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python3 +"""Example: Show all images on system.""" + +import podman + +print('{}\n'.format(__doc__)) + +with podman.Client() as client: + for img in client.images.list(): + print(img) diff --git a/contrib/python/podman/examples/eg_inspect_fedora.py b/contrib/python/podman/examples/eg_inspect_fedora.py new file mode 100644 index 000000000..b5bbba46d --- /dev/null +++ b/contrib/python/podman/examples/eg_inspect_fedora.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +"""Example: Pull Fedora and inspect image and container.""" + +import podman + +print('{}\n'.format(__doc__)) + +with podman.Client() as client: + id = client.images.pull('registry.fedoraproject.org/fedora:28') + img = client.images.get(id) + print(img.inspect()) + + cntr = img.create() + print(cntr.inspect()) + + cntr.remove() diff --git a/contrib/python/podman/examples/eg_latest_containers.py b/contrib/python/podman/examples/eg_latest_containers.py new file mode 100644 index 000000000..446f670dd --- /dev/null +++ b/contrib/python/podman/examples/eg_latest_containers.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +"""Example: Show all containers created since midnight.""" + +from datetime import datetime, time, timezone + +import podman + +print('{}\n'.format(__doc__)) + + +midnight = datetime.combine(datetime.today(), time.min, tzinfo=timezone.utc) + +with podman.Client() as client: + for c in client.containers.list(): + created_at = podman.datetime_parse(c.createdat) + + if created_at > midnight: + print('{}: image: {} createdAt: {}'.format( + c.id[:12], c.image[:32], podman.datetime_format(created_at))) diff --git a/contrib/python/podman/examples/eg_new_image.py b/contrib/python/podman/examples/eg_new_image.py new file mode 100644 index 000000000..21e076dcb --- /dev/null +++ b/contrib/python/podman/examples/eg_new_image.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +"""Example: Create new image from container.""" + +import sys + +import podman + + +def print_history(details): + """Format history data from an image, in a table.""" + for i, r in enumerate(details): + print( + '{}: {} {} {}'.format(i, r.id[:12], + podman.datetime_format(r.created), r.tags), + sep='\n') + print("-" * 25) + + +print('{}\n'.format(__doc__)) + +with podman.Client() as client: + ctnr = next( + (c for c in client.containers.list() if 'alpine' in c['image']), None) + + if ctnr: + print_history(client.images.get(ctnr.imageid).history()) + + # Make changes as we save the container to a new image + id = ctnr.commit('alpine-ash', changes=['CMD=/bin/ash']) + print_history(client.images.get(id).history()) + else: + print('Unable to find "alpine" container.', file=sys.stderr) diff --git a/contrib/python/podman/examples/run_example.sh b/contrib/python/podman/examples/run_example.sh new file mode 100755 index 000000000..0f6575073 --- /dev/null +++ b/contrib/python/podman/examples/run_example.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +export PYTHONPATH=.. + +function examples { + for file in $@; do + python3 -c "import ast; f=open('"${file}"'); t=ast.parse(f.read()); print(ast.get_docstring(t) + ' -- "${file}"')" + done +} + +while getopts "lh" arg; do + case $arg in + l ) examples $(ls eg_*.py); exit 0 ;; + h ) echo 1>&2 $0 [-l] [-h] filename ; exit 2 ;; + esac +done +shift $((OPTIND-1)) + +# podman needs to play some games with resources +if [[ $(id -u) != 0 ]]; then + echo 1>&2 $0 must be run as root. + exit 2 +fi + +if ! systemctl --quiet is-active io.projectatomic.podman.socket; then + echo 1>&2 'podman is not running. systemctl enable --now io.projectatomic.podman.socket' + exit 1 +fi + +function cleanup { + podman rm $1 >/dev/null 2>&1 +} + +# Setup storage with an image and container +podman pull alpine:latest >/tmp/podman.output 2>&1 +CTNR=$(podman create alpine) +trap "cleanup $CTNR" EXIT + +if [[ -f $1 ]]; then + python3 $1 +else + python3 $1.py +fi |