Skip to content

Commit

Permalink
Added has_image method to client-side Docker manager (fixes #316) (#317)
Browse files Browse the repository at this point in the history
* added head method to API

* added has_image to client-side Docker manager

* added HEAD /docker/images/:name

* bug fix: bad var name

* updated changelog

* bug fix: use .get and catch error
  • Loading branch information
ChrisTimperley authored Jan 28, 2019
1 parent c55bf9d commit 033e160
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* Added `__contains__` to client- and server-side `BugManager`.
* Added `__delitem__` to client- and server-side `BugManager`.
* Added `head` method to client API interface.
* Added `has_image` method to client-side Docker manager.


## 2.1.23 (2019-02-28)
Expand Down
6 changes: 6 additions & 0 deletions bugzoo/client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from ..exceptions import ConnectionFailure, UnexpectedResponse, BugZooException

logger = logging.getLogger(__name__) # type: logging.Logger
logger.setLevel(logging.DEBUG)

__all__ = ['APIClient']

Expand Down Expand Up @@ -109,6 +110,11 @@ def put(self, path: str, **kwargs) -> requests.Response:
logger.debug('PUT: %s', url)
return requests.put(url, **kwargs)

def head(self, path: str, **kwargs) -> requests.Response:
url = self._url(path)
logger.debug('HEAD: %s', url)
return requests.head(url, **kwargs)

def patch(self, path: str, data, **kwargs ) -> requests.Response:
url = self._url(path)
logger.debug('PATCH: %s', url)
Expand Down
15 changes: 14 additions & 1 deletion bugzoo/client/dockerm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .api import APIClient

logger = logging.getLogger(__name__) # type: logging.Logger
logger.setLevel(logging.DEBUG)


class DockerManager(object):
Expand All @@ -14,14 +15,26 @@ class DockerManager(object):
def __init__(self, api: APIClient) -> None:
self.__api = api

def has_image(self, name: str) -> bool:
"""
Determines whether the server has a Docker image with a given name.
"""
path = "docker/images/{}".format(name)
r = self.__api.head(path)
if r.status_code == 204:
return True
elif r.status_code == 404:
return False
self.__api.handle_erroneous_response(r)

def delete_image(self, name: str) -> None:
"""
Deletes a Docker image with a given name.
Parameters:
name: the name of the Docker image.
"""
logger.info("deleting Docker image: %s", name)
logger.debug("deleting Docker image: %s", name)
path = "docker/images/{}".format(name)
response = self.__api.delete(path)
if response.status_code != 204:
Expand Down
12 changes: 12 additions & 0 deletions bugzoo/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,18 @@ def docker_images(name: str):
return UnexpectedServerError.from_exception(ex), 500


@app.route('/docker/images/<path:name>', methods=['GET', 'HEAD'])
@throws_errors
def has_docker_image(name: str):
try:
daemon.docker.images.get(name)
return '', 204
except docker.errors.ImageNotFound:
return '', 404
except Exception as ex:
return UnexpectedServerError.from_exception(ex), 500


@app.route('/bugs/<uid>', methods=['DELETE'])
@throws_errors
def deregister_bug(uid: str):
Expand Down

0 comments on commit 033e160

Please sign in to comment.