diff options
author | Jhon Honce <jhonce@redhat.com> | 2018-05-24 13:44:04 -0700 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2018-05-25 09:31:21 +0000 |
commit | 0a4ade1c175d3188ad55d22d751a86c96e060a44 (patch) | |
tree | 9bfb3414119e931fd715c6d3002c0e357fce7e18 /contrib/python/podman/libs/images.py | |
parent | 684b544e9c45129a3d8112cfab22526440d8fd13 (diff) | |
download | podman-0a4ade1c175d3188ad55d22d751a86c96e060a44.tar.gz podman-0a4ade1c175d3188ad55d22d751a86c96e060a44.tar.bz2 podman-0a4ade1c175d3188ad55d22d751a86c96e060a44.zip |
Implement python podman create and start
- Added alias 'container()' to image model for CreateContainer()
- Fixed return in containers_create.go to wrap error in varlink
exception
- Added a wait time to container.kill(), number of seconds to wait
for the container to change state
- Refactored cached_property() to use system libraries
- Refactored tests to speed up performance
Signed-off-by: Jhon Honce <jhonce@redhat.com>
Closes: #821
Approved by: rhatdan
Diffstat (limited to 'contrib/python/podman/libs/images.py')
-rw-r--r-- | contrib/python/podman/libs/images.py | 53 |
1 files changed, 42 insertions, 11 deletions
diff --git a/contrib/python/podman/libs/images.py b/contrib/python/podman/libs/images.py index d6fd7946d..d617a766b 100644 --- a/contrib/python/podman/libs/images.py +++ b/contrib/python/podman/libs/images.py @@ -3,6 +3,9 @@ import collections import functools import json +from . import Config +from .containers import Container + class Image(collections.UserDict): """Model for an Image.""" @@ -25,8 +28,42 @@ class Image(collections.UserDict): """Get items from parent dict.""" return super().__getitem__(key) + def _split_token(self, values=None, sep='='): + mapped = {} + if values: + for var in values: + k, v = var.split(sep, 1) + mapped[k] = v + return mapped + + def create(self, *args, **kwargs): + """Create container from image. + + Pulls defaults from image.inspect() + """ + # Inialize config from parameters + with self._client() as podman: + details = self.inspect() + + # TODO: remove network settings once defaults implemented on service side + config = Config(image_id=self.id, **kwargs) + config['command'] = details.containerconfig['cmd'] + config['env'] = self._split_token(details.containerconfig['env']) + config['image'] = details.repotags[0] + config['labels'] = self._split_token(details.labels) + config['net_mode'] = 'bridge' + config['network'] = 'bridge' + config['work_dir'] = '/tmp' + + with self._client() as podman: + id = podman.CreateContainer(config)['container'] + cntr = podman.GetContainer(id) + return Container(self._client, id, cntr['container']) + + container = create + def export(self, dest, compressed=False): - """Write image to dest, return True on success.""" + """Write image to dest, return id on success.""" with self._client() as podman: results = podman.ExportImage(self.id, dest, compressed) return results['image'] @@ -50,8 +87,8 @@ class Image(collections.UserDict): """Retrieve details about image.""" with self._client() as podman: results = podman.InspectImage(self.id) - obj = json.loads(results['image'], object_hook=self._lower_hook()) - return collections.namedtuple('ImageInspect', obj.keys())(**obj) + obj = json.loads(results['image'], object_hook=self._lower_hook()) + return collections.namedtuple('ImageInspect', obj.keys())(**obj) def push(self, target, tlsverify=False): """Copy image to target, return id on success.""" @@ -89,12 +126,6 @@ class Images(object): for img in results['images']: yield Image(self._client, img['id'], img) - def create(self, *args, **kwargs): - """Create image from configuration.""" - with self._client() as podman: - results = podman.CreateImage() - return results['image'] - def build(self, *args, **kwargs): """Build container from image. @@ -125,9 +156,9 @@ class Images(object): def search(self, id, limit=25): """Search registries for id.""" with self._client() as podman: - results = podman.SearchImage(id) + results = podman.SearchImage(id, limit) for img in results['images']: - yield img + yield collections.namedtuple('ImageSearch', img.keys())(**img) def get(self, id): """Get Image from id.""" |