Skip to content
This repository has been archived by the owner on Oct 10, 2020. It is now read-only.

Commit

Permalink
Refactor containers verb
Browse files Browse the repository at this point in the history
With the exception of fstrim, the containers verb has now been
refactored.  It primarily now uses the containers object in its
implementation.

Closes: #792
Approved by: rhatdan
  • Loading branch information
baude authored and rh-atomic-bot committed Dec 9, 2016
1 parent 42aa9df commit e405c6a
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 191 deletions.
28 changes: 23 additions & 5 deletions Atomic/backends/_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from requests import exceptions
from Atomic.trust import Trust
from Atomic.objects.layer import Layer

from dateutil.parser import parse as dateparse

class DockerBackend(Backend):
def __init__(self):
Expand Down Expand Up @@ -99,22 +99,39 @@ def _make_image(self, image, img_struct, deep=False, remote=False):
return img_obj

def _make_container(self, container, con_struct, deep=False):
con_obj = Container(container)
con_obj = Container(container, backend=self)
con_obj.id = con_struct['Id']
con_obj.created = con_struct['Created']
try:
con_obj.created = float(con_struct['Created'])
except ValueError:
con_obj.created = dateparse(con_struct['Created']).strftime("%F %H:%M") # pylint: disable=no-member
con_obj.original_structure = con_struct
try:
con_obj.name = con_struct['Names'][0]
except KeyError:
con_obj.name = con_struct['Name']
con_obj.input_name = container
con_obj.backend = self
try:
con_obj.command = con_struct['Command']
except KeyError:
con_obj.command = con_struct['Config']['Cmd']

con_obj.state = con_struct.get('State', None) or con_struct.get['State'].get('Status', None)
if isinstance(con_obj.state, dict):
con_obj.state = con_obj.state['Status']
con_obj.running = True if con_obj.state.lower() in ['true', 'running'] else False

if deep:
# Add in the deep inspection stuff
con_obj.status = con_struct['State']['Status']
con_obj.running = con_struct['State']['Running']
con_obj.image = con_struct['Image']
con_obj.image_name = con_struct['Config']['Image']

else:
con_obj.status = con_struct['Status']
con_obj.image = con_struct['ImageID']
con_obj.image_id = con_struct['ImageID']
con_obj.image_name = con_struct['Image']

return con_obj

Expand Down Expand Up @@ -276,3 +293,4 @@ def get_layers(self, image):
layers.append(layer)
return layers


15 changes: 11 additions & 4 deletions Atomic/backends/_ostree.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ def backend(self):

def _make_container(self, info):
container_id = info['Id']

runtime = self.syscontainers.get_container_runtime_info(container_id)

container = Container(container_id, backend=self)
container.name = container_id
container.command = info['Command']
container.id = container_id
container.image_name = info['Image']
container.image_id = info['ImageID']
container.created = info['Created']
container.status = runtime['status']
container.status = container.state = runtime['status']
container.input_name = container_id
container.original_structure = info
container.deep = True
container.image = info['Image']
container.running = False if container.status == 'inactive' else True

return container

Expand Down Expand Up @@ -142,7 +144,8 @@ def get_layers(self, image):
layers.append(layer)
return layers

def get_dangling_images(self):
@staticmethod
def get_dangling_images():
return []

def make_remote_image(self, image):
Expand All @@ -152,3 +155,7 @@ def make_remote_image(self, image):

def _make_remote_image(self, image):
return self._make_image(image, None, remote=True)

def delete_container(self, container, force=False):
return self.syscontainers.uninstall(container)

4 changes: 4 additions & 0 deletions Atomic/backends/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ def delete_image(self, image, force=False):
"""
pass

@abstractmethod
def delete_container(self, container, force=False):
pass

@abstractmethod
def start_container(self, name):
pass
Expand Down
30 changes: 17 additions & 13 deletions Atomic/backendutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ def backend_has_image(backend, img):
def backend_has_container(backend, container):
return True if backend.has_container(container) else False

def get_backend_and_image(self, img, str_preferred_backend=None):
def get_backend_and_image_obj(self, img, str_preferred_backend=None):
"""
Given an image name (str) and optionally a str reference to a backend,
this method looks for the image firstly on the preferred backend and
then on the alternate backends. It returns a backend object.
then on the alternate backends. It returns a backend object and an
image object.
:param img: name of image to look for
:param str_preferred_backend: i.e. 'docker'
:return: backend object
:return: backend object and image object
"""
backends = list(self.BACKENDS)
# Check preferred backend first
Expand Down Expand Up @@ -62,35 +63,38 @@ def get_backend_and_image(self, img, str_preferred_backend=None):
raise ValueError("Found {} in multiple storage backends: {}".
format(img, ', '.join([x.backend for x in img_in_backends])))

def get_backend_for_container(self, container, str_preferred_backend=None):
def get_backend_and_container_obj(self, container_name, str_preferred_backend=None):
"""
Given a container name (str) and optionally a str reference to a backend,
this method looks for the container firstly on the preferred backend and
then on the alternate backends. It returns a backend object.
:param container: name of image to look for
then on the alternate backends. It returns a backend object and a container
object.
:param container_name: name of image to look for
:param str_preferred_backend: i.e. 'docker'
:return: backend object
:return: backend object and container object
"""
backends = list(self.BACKENDS)
# Check preferred backend first
if str_preferred_backend:
be = self.get_backend_from_string(str_preferred_backend)
if be.has_container(container):
return be
con_obj = be.has_container(container_name)
if con_obj:
return be, con_obj
# Didnt find in preferred, need to remove it from the list now
del backends[self._get_backend_index_from_string(str_preferred_backend)]

container_in_backends = []
for backend in backends:
be = backend()
if be.has_container(container):
container_in_backends.append(be)
con_obj = be.has_container(container_name)
if con_obj:
container_in_backends.append((be, con_obj))
if len(container_in_backends) == 1:
return container_in_backends[0]
if len(container_in_backends) == 0:
raise ValueError("Unable to find backend associated with container '{}'".format(container))
raise ValueError("Unable to find backend associated with container '{}'".format(container_name))
raise ValueError("Found {} in multiple storage backends: {}".
format(container, ', '.join([x.backend for x in container_in_backends])))
format(container_name, ', '.join([x.backend for x in container_in_backends])))

def get_images(self, get_all=False):
backends = self.BACKENDS
Expand Down
Loading

0 comments on commit e405c6a

Please sign in to comment.