From 9c93fc0f578fcca23a063fe91fb5b3a58e461fb6 Mon Sep 17 00:00:00 2001 From: Feng Shengqin Date: Tue, 27 Mar 2018 15:13:40 +0800 Subject: [PATCH] rebuild a container with a different image rebuild a container with a different image. by this way, you can upgrade the container. For example, rebuild a ubuntu:14.04 container to ubuntu:16.04 for upgrade. Change-Id: Id08f7dca5de019da33695561f69976aaed6a022c Implements:blueprint replace-image --- zunclient/osc/v1/containers.py | 16 ++++++++++++++++ zunclient/tests/unit/v1/test_containers.py | 8 +++++--- zunclient/v1/containers.py | 5 +++-- zunclient/v1/containers_shell.py | 18 +++++++++++++++++- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/zunclient/osc/v1/containers.py b/zunclient/osc/v1/containers.py index afc45751..d99a64e8 100644 --- a/zunclient/osc/v1/containers.py +++ b/zunclient/osc/v1/containers.py @@ -1134,6 +1134,18 @@ class RebuildContainer(command.Command): metavar='', nargs='+', help='ID or name of the (container)s to rebuild.') + parser.add_argument( + '--image', + metavar='', + help='The image for specified container to update.') + parser.add_argument( + '--image-driver', + metavar='', + help='The image driver to use to update container image. ' + 'It can have following values: ' + '"docker": update the image from Docker Hub. ' + '"glance": update the image from Glance. ' + 'The default value is source container\'s image driver ') return parser def take_action(self, parsed_args): @@ -1141,6 +1153,10 @@ class RebuildContainer(command.Command): for container in parsed_args.containers: opts = {} opts['id'] = container + if parsed_args.image: + opts['image'] = parsed_args.image + if parsed_args.image_driver: + opts['image_driver'] = parsed_args.image_driver try: client.containers.rebuild(**opts) print(_('Request to rebuild container %s has ' diff --git a/zunclient/tests/unit/v1/test_containers.py b/zunclient/tests/unit/v1/test_containers.py index e0f17746..f671a2d8 100644 --- a/zunclient/tests/unit/v1/test_containers.py +++ b/zunclient/tests/unit/v1/test_containers.py @@ -336,7 +336,7 @@ fake_responses = { None, ), }, - '/v1/containers/%s/rebuild' + '/v1/containers/%s/rebuild?image=cirros' % (CONTAINER1['id']): { 'POST': ( @@ -711,10 +711,12 @@ class ContainerManagerTest(testtools.TestCase): self.assertTrue(containers) def test_containers_rebuild(self): - self.mgr.rebuild(CONTAINER1['id']) + opts = {'image': 'cirros'} + self.mgr.rebuild(CONTAINER1['id'], **opts) expect = [ ('POST', - '/v1/containers/%s/rebuild' % (CONTAINER1['id']), + '/v1/containers/%s/rebuild?image=cirros' + % (CONTAINER1['id']), {'Content-Length': '0'}, None) ] self.assertEqual(expect, self.api.calls) diff --git a/zunclient/v1/containers.py b/zunclient/v1/containers.py index a506ea9a..3be684a0 100644 --- a/zunclient/v1/containers.py +++ b/zunclient/v1/containers.py @@ -130,8 +130,9 @@ class ContainerManager(base.Manager): return self._action(id, '/stop', qparams={'timeout': timeout}) - def rebuild(self, id): - return self._action(id, '/rebuild') + def rebuild(self, id, **kwargs): + return self._action(id, '/rebuild', + qparams=kwargs) def restart(self, id, timeout): return self._action(id, '/reboot', diff --git a/zunclient/v1/containers_shell.py b/zunclient/v1/containers_shell.py index a0e21f70..7cf57a84 100644 --- a/zunclient/v1/containers_shell.py +++ b/zunclient/v1/containers_shell.py @@ -262,11 +262,27 @@ def do_show(cs, args): metavar='', nargs='+', help='ID of the (container)s to rebuild.') +@utils.arg('--image', + metavar='', + help='The image for specified container to update.') +@utils.arg('--image-driver', + metavar='', + help='The image driver to use to pull container image. ' + 'It can have following values: ' + '"docker": pull the image from Docker Hub. ' + '"glance": pull the image from Glance. ' + 'The default value is source container\'s image driver ') def do_rebuild(cs, args): """Rebuild specified containers.""" for container in args.containers: + opts = {} + opts['id'] = container + if args.image: + opts['image'] = args.image + if args.image_driver: + opts['image_driver'] = args.image_driver try: - cs.containers.rebuild(container) + cs.containers.rebuild(**opts) print("Request to rebuild container %s has been accepted." % container) except Exception as e: