Merge "Get "zun cp" command to work"

This commit is contained in:
Jenkins 2017-03-13 10:22:22 +00:00 committed by Gerrit Code Review
commit 9ce8248844
4 changed files with 35 additions and 20 deletions

View File

@ -11,8 +11,11 @@
# under the License.
import argparse
from contextlib import closing
import io
import logging
import os
import tarfile
import time
from osc_lib.command import command
@ -760,12 +763,12 @@ class CopyContainer(command.Command):
parser.add_argument(
'source',
metavar='<source>',
help='The source should be copied to the container or localhost.'
help='The source should be copied to the container or localhost. '
'The format of this parameter is [container:]src_path.')
parser.add_argument(
'destination',
metavar='<destination>',
help='The directory destination where save the source.'
help='The directory destination where save the source. '
'The format of this parameter is [container:]dest_path.')
return parser
@ -780,19 +783,23 @@ class CopyContainer(command.Command):
opts['path'] = container_path
res = client.containers.get_archive(**opts)
filename = os.path.split(container_path)[1]
dest_path = parsed_args.destination + '/' + filename
with open(dest_path, 'w') as outfile:
outfile.write(res[0][0])
dest_path = parsed_args.destination
tardata = io.BytesIO(res['data'].encode())
with closing(tarfile.open(fileobj=tardata)) as tar:
tar.extractall(dest_path)
elif ':' in parsed_args.destination:
dest_parts = parsed_args.destination.split(':', 1)
container_id = dest_parts[0]
container_path = dest_parts[1]
filename = os.path.split(parsed_args.source)[1]
opts = {}
opts['id'] = container_id
opts['path'] = container_path
opts['data'] = parsed_args.source
tardata = io.BytesIO()
with closing(tarfile.open(fileobj=tardata, mode='w')) as tar:
tar.add(parsed_args.source, arcname=filename)
opts['data'] = tardata.getvalue()
client.containers.put_archive(**opts)
else:
print("Please check the parameters for zun copy!")

View File

@ -257,11 +257,11 @@ fake_responses = {
),
},
'/v1/containers/%s/put_archive?%s'
% (CONTAINER1['id'], parse.urlencode({'path': path, 'data': data})):
% (CONTAINER1['id'], parse.urlencode({'path': path})):
{
'POST': (
{},
None,
{'data': data},
),
},
}
@ -553,9 +553,9 @@ class ContainerManagerTest(testtools.TestCase):
containers = self.mgr.put_archive(CONTAINER1['id'], path, data)
expect = [
('POST', '/v1/containers/%s/put_archive?%s'
% (CONTAINER1['id'], parse.urlencode({'path': path,
'data': data})),
{'Content-Length': '0'}, None)
% (CONTAINER1['id'], parse.urlencode({'path': path})),
{'Content-Length': '0'},
{'data': data})
]
self.assertEqual(expect, self.api.calls)
self.assertTrue(containers)

View File

@ -183,4 +183,5 @@ class ContainerManager(base.Manager):
def put_archive(self, id, path, data):
return self._action(id, '/put_archive',
qparams={'path': path, 'data': data})
qparams={'path': path},
body={'data': data})

View File

@ -13,8 +13,11 @@
# under the License.
import argparse
from contextlib import closing
import io
import json
import os
import tarfile
import time
import yaml
@ -550,11 +553,11 @@ def do_top(cs, args):
@utils.arg('source',
metavar='<source>',
help='The source should be copied to the container or localhost.'
help='The source should be copied to the container or localhost. '
'The format of this parameter is [container:]src_path.')
@utils.arg('destination',
metavar='<destination>',
help='The directory destination where save the source.'
help='The directory destination where save the source. '
'The format of this parameter is [container:]dest_path.')
def do_cp(cs, args):
"""Copy files/tars between a container and the local filesystem."""
@ -567,19 +570,23 @@ def do_cp(cs, args):
opts['path'] = container_path
res = cs.containers.get_archive(**opts)
filename = os.path.split(container_path)[1]
dest_path = args.destination + '/' + filename
with open(dest_path, 'w') as outfile:
outfile.write(res[0][0])
dest_path = args.destination
tardata = io.BytesIO(res['data'].encode())
with closing(tarfile.open(fileobj=tardata)) as tar:
tar.extractall(dest_path)
elif ':' in args.destination:
dest_parts = args.destination.split(':', 1)
container_id = dest_parts[0]
container_path = dest_parts[1]
filename = os.path.split(args.source)[1]
opts = {}
opts['id'] = container_id
opts['path'] = container_path
opts['data'] = args.source
tardata = io.BytesIO()
with closing(tarfile.open(fileobj=tardata, mode='w')) as tar:
tar.add(args.source, arcname=filename)
opts['data'] = tardata.getvalue()
cs.containers.put_archive(**opts)
else: