Merge branch 'feature/image-import' of github.com:tarnfeld/docker-py into tarnfeld-feature/image-import

This commit is contained in:
shin- 2014-01-16 20:22:33 +01:00
commit 8a5cd6ae65
2 changed files with 44 additions and 15 deletions

View File

@ -453,27 +453,35 @@ class Client(requests.Session):
return [x['Id'] for x in res]
return res
def import_image(self, src, data=None, repository=None, tag=None):
def import_image(self, src=None, data=None, repository=None, tag=None,
image=None):
u = self._url("/images/create")
params = {
'repo': repository,
'tag': tag
}
try:
# XXX: this is ways not optimal but the only way
# for now to import tarballs through the API
fic = open(src)
data = fic.read()
fic.close()
src = "-"
except IOError:
# file does not exists or not a file (URL)
data = None
if isinstance(src, six.string_types):
params['fromSrc'] = src
return self._result(self._post(u, data=data, params=params))
return self._result(self._post(u, data=src, params=params))
if src:
try:
# XXX: this is ways not optimal but the only way
# for now to import tarballs through the API
fic = open(src)
data = fic.read()
fic.close()
src = "-"
except IOError:
# file does not exists or not a file (URL)
data = None
if isinstance(src, six.string_types):
params['fromSrc'] = src
return self._result(self._post(u, data=data, params=params))
return self._result(self._post(u, data=src, params=params))
if image:
params['fromImage'] = image
return self._result(self._post(u, data=None, params=params))
raise Exception("Must specify a src or image")
def info(self):
return self._result(self._get(self._url("/info")),

View File

@ -868,6 +868,27 @@ class DockerClientTest(unittest.TestCase):
buf.close()
os.remove(buf.name)
def test_import_image_from_image(self):
try:
self.client.import_image(
image=fake_api.FAKE_IMAGE_NAME,
repository=fake_api.FAKE_REPO_NAME,
tag=fake_api.FAKE_TAG_NAME
)
except Exception as e:
self.fail('Command should not raise exception: {0}'.format(e))
fake_request.assert_called_with(
'unix://var/run/docker.sock/v1.6/images/create',
params={
'repo': fake_api.FAKE_REPO_NAME,
'tag': fake_api.FAKE_TAG_NAME,
'fromImage': fake_api.FAKE_IMAGE_NAME
},
data=None,
timeout=docker.client.DEFAULT_TIMEOUT_SECONDS
)
def test_inspect_image(self):
try:
self.client.inspect_image(fake_api.FAKE_IMAGE_NAME)