Skip to content

Commit

Permalink
Fix initial list_directory function.
Browse files Browse the repository at this point in the history
docker-registry has a local database which can be queried using a simple
REST API.  If you trash this database, it is supposed to be recreated
when you restart docker-registry.  This fix the recreation of the db which
happens thanks to results of the the list_directory function.  Previously
this was empty.

To test the problem and review the solution:

 1. assuming a working swift and docker registry with swift driver installed;
 2. push some images;
 3. stop docker-registry;
 4. check sqlalchemy_index_database in docker registry conf and "rm" the database (by default a sqlite file);
 5. restart docker-registry;

With the old code the database will be empty.  With the new one the db
will be filled with the already pushed images.  The main problem come
from the facts that:

    $ curl -i -X GET -H "X-Auth-Token: AUTH_token" 'http://localhost:8080/v1/AUTH_test/docker?prefix=lvl_1/&delimiter=/&format=json'

    [{"hash": "a83a7dcf68a3dd1d83584abd8e230829", "last_modified": "2014-09-17T15:26:25.585390", "bytes": 29, "name": "lvl_1/file_2", "content_type": "application/octet-stream"}, {"subdir": "lvl_1/lvl_2/"}]

and

    $ curl -i -X GET -H "X-Auth-Token: AUTH_token" 'http://localhost:8080/v1/AUTH_test/docker?path=lvl_1/&format=json'

    [{"hash": "a83a7dcf68a3dd1d83584abd8e230829", "last_modified": "2014-09-17T15:26:25.585390", "bytes": 29, "name": "lvl_1/file_2", "content_type": "application/octet-stream"}]

do not return the same thing.  The main difference being that the
subdirectories are not return when using the "path" syntax.

I tested with swift 1.8.0 and check that the Pseudo-hierarchical folders
and directories behaviour was the same on swift 2.2.0.

This would close bacongobbler#14 and bacongobbler#15.  The "fill it if not there" mechanism is
described in this issue  docker-archive/docker-registry#516
  • Loading branch information
sathlan committed Sep 17, 2014
1 parent 8643222 commit 1b93877
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
25 changes: 18 additions & 7 deletions docker_registry/drivers/swift.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ def _init_path(self, path=None):
path = path[:-1]
return path

def _clean_up_name(self, name):
if self._root_path != '/':
name = name.replace(
self._init_path() + '/', '', 1)
# trim extra trailing slashes
if name.endswith('/'):
name = name[:-1]
return name

def content_redirect_url(self, path):
path = self._init_path(path)
return '/'.join([
Expand Down Expand Up @@ -95,17 +104,19 @@ def list_directory(self, path=None):
path += '/'
_, directory = self._swift_connection.get_container(
container=self._swift_container,
path=path)
prefix=path, delimiter='/')
if not directory:
raise
param = ''
for inode in directory:
if 'name' in inode:
param = 'name'
elif 'subdir' in inode:
param = 'subdir'
else:
raise
# trim extra trailing slashes
if inode['name'].endswith('/'):
inode['name'] = inode['name'][:-1]
if self._root_path != '/':
inode['name'] = inode['name'].replace(
self._init_path() + '/', '', 1)
yield inode['name']
yield self._clean_up_name(inode[param])
except Exception:
raise exceptions.FileNotFoundError('%s is not there' % path)

Expand Down
5 changes: 5 additions & 0 deletions tests/mocker.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def get_container(self, container, marker=None, limit=None, prefix=None,
delimiter=None, end_marker=None, path=None,
full_listing=False):
lst = []
if path is None:
if prefix[-1] != '/':
path = prefix + '/'
else:
path = prefix
for key, value in self._swift_containers[container].iteritems():
if key.startswith(path):
lst.append({'name': key})
Expand Down

0 comments on commit 1b93877

Please # to comment.