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
This commit is contained in:
Feng Shengqin
2018-03-27 15:13:40 +08:00
committed by JiWei
parent 2ffe454e7d
commit 9c93fc0f57
4 changed files with 41 additions and 6 deletions

View File

@@ -1134,6 +1134,18 @@ class RebuildContainer(command.Command):
metavar='<container>',
nargs='+',
help='ID or name of the (container)s to rebuild.')
parser.add_argument(
'--image',
metavar='<image>',
help='The image for specified container to update.')
parser.add_argument(
'--image-driver',
metavar='<image_driver>',
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 '

View File

@@ -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)

View File

@@ -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',

View File

@@ -262,11 +262,27 @@ def do_show(cs, args):
metavar='<container>',
nargs='+',
help='ID of the (container)s to rebuild.')
@utils.arg('--image',
metavar='<image>',
help='The image for specified container to update.')
@utils.arg('--image-driver',
metavar='<image_driver>',
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: