#!/usr/bin/python -u # Copyright (c) 2010-2012 OpenStack, LLC. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or # implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import print_function, unicode_literals import argparse import getpass import io import json import logging import signal import socket import warnings from os import environ, walk, _exit as os_exit from os.path import isfile, isdir, join from six import text_type, PY2 from six.moves.urllib.parse import unquote, urlparse from sys import argv as sys_argv, exit, stderr, stdin from time import gmtime, strftime from swiftclient import RequestException from swiftclient.utils import config_true_value, generate_temp_url, \ prt_bytes, JSONableIterable from swiftclient.multithreading import OutputManager from swiftclient.exceptions import ClientException from swiftclient import __version__ as client_version from swiftclient.client import logger_settings as client_logger_settings, \ parse_header_string from swiftclient.service import SwiftService, SwiftError, \ SwiftUploadObject, get_conn, process_options from swiftclient.command_helpers import print_account_stats, \ print_container_stats, print_object_stats try: from shlex import quote as sh_quote except ImportError: from pipes import quote as sh_quote BASENAME = 'swift' commands = ('delete', 'download', 'list', 'post', 'copy', 'stat', 'upload', 'capabilities', 'info', 'tempurl', 'auth', 'bash_completion') def immediate_exit(signum, frame): stderr.write(" Aborted\n") os_exit(2) st_delete_options = '''[--all] [--leave-segments] [--object-threads ] [--container-threads ] [--header ] [--prefix ] [--versions] [ [] [--version-id ] [...]] ''' st_delete_help = ''' Delete a container or objects within a container. Positional arguments: [] Name of container to delete from. [] Name of object to delete. Specify multiple times for multiple objects. Optional arguments: -a, --all Delete all containers and objects. --versions Delete all versions --leave-segments Do not delete segments of manifest objects. -H, --header Adds a custom request header to use for deleting objects or an entire container . --object-threads Number of threads to use for deleting objects. Default is 10. --container-threads Number of threads to use for deleting containers. Default is 10. --prefix Only delete objects beginning with . --version-id Delete specific version of a versioned object. '''.strip("\n") def st_delete(parser, args, output_manager, return_parser=False): parser.add_argument( '-a', '--all', action='store_true', dest='yes_all', default=False, help='Delete all containers and objects.') parser.add_argument('--versions', action='store_true', help='delete all versions') parser.add_argument( '-p', '--prefix', dest='prefix', help='Only delete items beginning with .') parser.add_argument( '--version-id', action='store', default=None, help='Delete a specific version of a versioned object') parser.add_argument( '-H', '--header', action='append', dest='header', default=[], help='Adds a custom request header to use for deleting objects ' 'or an entire container.') parser.add_argument( '--leave-segments', action='store_true', dest='leave_segments', default=False, help='Do not delete segments of manifest objects.') parser.add_argument( '--object-threads', type=int, default=10, help='Number of threads to use for deleting objects. ' 'Its value must be a positive integer. Default is 10.') parser.add_argument( '--container-threads', type=int, default=10, help='Number of threads to use for deleting containers. ' 'Its value must be a positive integer. Default is 10.') # We return the parser to build up the bash_completion if return_parser: return parser (options, args) = parse_args(parser, args) args = args[1:] if (not args and not options['yes_all']) or (args and options['yes_all']): output_manager.error('Usage: %s delete %s\n%s', BASENAME, st_delete_options, st_delete_help) return if options['versions'] and len(args) >= 2: exit('--versions option not allowed for object deletes') if options['version_id'] and len(args) < 2: exit('--version-id option only allowed for object deletes') if options['object_threads'] <= 0: output_manager.error( 'ERROR: option --object-threads should be a positive integer.' '\n\nUsage: %s delete %s\n%s', BASENAME, st_delete_options, st_delete_help) return if options['container_threads'] <= 0: output_manager.error( 'ERROR: option --container-threads should be a positive integer.' '\n\nUsage: %s delete %s\n%s', BASENAME, st_delete_options, st_delete_help) return options['object_dd_threads'] = options['object_threads'] with SwiftService(options=options) as swift: try: if not args: del_iter = swift.delete() else: container = args[0] if '/' in container: output_manager.error( 'WARNING: / in container name; you ' "might have meant '%s' instead of '%s'." % (container.replace('/', ' ', 1), container) ) return objects = args[1:] if objects: del_iter = swift.delete(container=container, objects=objects) else: del_iter = swift.delete(container=container) for r in del_iter: c = r.get('container', '') o = r.get('object', '') a = (' [after {0} attempts]'.format(r.get('attempts')) if r.get('attempts', 1) > 1 else '') if r['action'] == 'bulk_delete': if r['success']: objs = r.get('objects', []) for o, err in r.get('result', {}).get('Errors', []): # o will be of the form quote("//") o = unquote(o) if PY2: # In PY3, unquote(unicode) uses utf-8 like we # want, but PY2 uses latin-1 o = o.encode('latin-1').decode('utf-8') output_manager.error('Error Deleting: {0}: {1}' .format(o[1:], err)) try: objs.remove(o[len(c) + 2:]) except ValueError: # shouldn't happen, but ignoring it won't hurt pass for o in objs: if options['yes_all']: p = '{0}/{1}'.format(c, o) else: p = o output_manager.print_msg('{0}{1}'.format(p, a)) else: for o in r.get('objects', []): output_manager.error('Error Deleting: {0}/{1}: {2}' .format(c, o, r['error'])) else: if r['success']: if options['verbose']: if r['action'] == 'delete_object': if options['yes_all']: p = '{0}/{1}'.format(c, o) else: p = o elif r['action'] == 'delete_segment': p = '{0}/{1}'.format(c, o) elif r['action'] == 'delete_container': p = c output_manager.print_msg('{0}{1}'.format(p, a)) else: p = '{0}/{1}'.format(c, o) if o else c output_manager.error('Error Deleting: {0}: {1}' .format(p, r['error'])) except SwiftError as err: output_manager.error(err.value) st_download_options = '''[--all] [--marker ] [--prefix ] [--output ] [--output-dir ] [--object-threads ] [--ignore-checksum] [--container-threads ] [--no-download] [--skip-identical] [--remove-prefix] [--version-id ] [--header ] [--no-shuffle] [ [] [...]] ''' st_download_help = ''' Download objects from containers. Positional arguments: [] Name of container to download from. To download a whole account, omit this and specify --all. [] Name of object to download. Specify multiple times for multiple objects. Omit this to download all objects from the container. Optional arguments: -a, --all Indicates that you really want to download everything in the account. -m, --marker Marker to use when starting a container or account download. -p, --prefix Only download items beginning with -r, --remove-prefix An optional flag for --prefix , use this option to download items without -o, --output For a single file download, stream the output to . Specifying "-" as will redirect to stdout. -D, --output-dir An optional directory to which to store objects. By default, all objects are recreated in the current directory. --object-threads Number of threads to use for downloading objects. Default is 10. --container-threads Number of threads to use for downloading containers. Default is 10. --no-download Perform download(s), but don't actually write anything to disk. -H, --header Adds a customized request header to the query, like "Range" or "If-Match". This option may be repeated. Example: --header "content-type:text/plain" --skip-identical Skip downloading files that are identical on both sides. --version-id Download specific version of a versioned object. --ignore-checksum Turn off checksum validation for downloads. --no-shuffle By default, when downloading a complete account or container, download order is randomised in order to reduce the load on individual drives when multiple clients are executed simultaneously to download the same set of objects (e.g. a nightly automated download script to multiple servers). Enable this option to submit download jobs to the thread pool in the order they are listed in the object store. --ignore-mtime Ignore the 'X-Object-Meta-Mtime' header when downloading an object. Instead, create atime and mtime with fresh timestamps. '''.strip("\n") def st_download(parser, args, output_manager, return_parser=False): parser.add_argument( '-a', '--all', action='store_true', dest='yes_all', default=False, help='Indicates that you really want to download ' 'everything in the account.') parser.add_argument( '-m', '--marker', dest='marker', default='', help='Marker to use when starting a container or ' 'account download.') parser.add_argument( '-p', '--prefix', dest='prefix', help='Only download items beginning with the .') parser.add_argument( '-o', '--output', dest='out_file', help='For a single ' 'download, stream the output to . ' 'Specifying "-" as will redirect to stdout.') parser.add_argument( '-D', '--output-dir', dest='out_directory', help='An optional directory to which to store objects. ' 'By default, all objects are recreated in the current directory.') parser.add_argument( '-r', '--remove-prefix', action='store_true', dest='remove_prefix', default=False, help='An optional flag for --prefix , ' 'use this option to download items without .') parser.add_argument( '--object-threads', type=int, default=10, help='Number of threads to use for downloading objects. ' 'Its value must be a positive integer. Default is 10.') parser.add_argument( '--container-threads', type=int, default=10, help='Number of threads to use for downloading containers. ' 'Its value must be a positive integer. Default is 10.') parser.add_argument( '--no-download', action='store_true', default=False, help="Perform download(s), but don't actually write anything to disk.") parser.add_argument( '-H', '--header', action='append', dest='header', default=[], help='Adds a customized request header to the query, like "Range" or ' '"If-Match". This option may be repeated. ' 'Example: --header "content-type:text/plain"') parser.add_argument( '--skip-identical', action='store_true', dest='skip_identical', default=False, help='Skip downloading files that are identical on ' 'both sides.') parser.add_argument( '--version-id', action='store', default=None, help='Download a specific version of a versioned object') parser.add_argument( '--ignore-checksum', action='store_false', dest='checksum', default=True, help='Turn off checksum validation for downloads.') parser.add_argument( '--no-shuffle', action='store_false', dest='shuffle', default=True, help='By default, download order is randomised in order ' 'to reduce the load on individual drives when multiple clients are ' 'executed simultaneously to download the same set of objects (e.g. a ' 'nightly automated download script to multiple servers). Enable this ' 'option to submit download jobs to the thread pool in the order they ' 'are listed in the object store.') parser.add_argument( '--ignore-mtime', action='store_true', dest='ignore_mtime', default=False, help='By default, the object-meta-mtime header is used ' 'to store the access and modified timestamp for the downloaded file. ' 'With this option, the header is ignored and the timestamps are ' 'created freshly.') # We return the parser to build up the bash_completion if return_parser: return parser (options, args) = parse_args(parser, args) args = args[1:] if options['out_file'] == '-': options['verbose'] = 0 if options['out_file'] and len(args) != 2: exit('-o option only allowed for single file downloads') if not options['prefix']: options['remove_prefix'] = False if options['out_directory'] and len(args) == 2: exit('Please use -o option for single file downloads and renames') if (not args and not options['yes_all']) or (args and options['yes_all']): output_manager.error('Usage: %s download %s\n%s', BASENAME, st_download_options, st_download_help) return if options['version_id'] and len(args) < 2: exit('--version-id option only allowed for object downloads') if options['object_threads'] <= 0: output_manager.error( 'ERROR: option --object-threads should be a positive integer.\n\n' 'Usage: %s download %s\n%s', BASENAME, st_download_options, st_download_help) return if options['container_threads'] <= 0: output_manager.error( 'ERROR: option --container-threads should be a positive integer.' '\n\nUsage: %s download %s\n%s', BASENAME, st_download_options, st_download_help) return options['object_dd_threads'] = options['object_threads'] with SwiftService(options=options) as swift: try: if not args: down_iter = swift.download() else: container = args[0] if '/' in container: output_manager.error( 'WARNING: / in container name; you ' "might have meant '%s' instead of '%s'." % (container.replace('/', ' ', 1), container) ) return objects = args[1:] if not objects: down_iter = swift.download(container) else: down_iter = swift.download(container, objects) for down in down_iter: if options['out_file'] == '-' and 'contents' in down: contents = down['contents'] for chunk in contents: output_manager.print_raw(chunk) else: if down['success']: if options['verbose']: start_time = down['start_time'] headers_receipt = \ down['headers_receipt'] - start_time auth_time = down['auth_end_time'] - start_time finish_time = down['finish_time'] read_length = down['read_length'] attempts = down['attempts'] total_time = finish_time - start_time down_time = total_time - auth_time _mega = 1000000 if down['pseudodir']: time_str = ( 'auth %.3fs, headers %.3fs, total %.3fs, ' 'pseudo' % ( auth_time, headers_receipt, total_time ) ) else: speed = float(read_length) / down_time / _mega time_str = ( 'auth %.3fs, headers %.3fs, total %.3fs, ' '%.3f MB/s' % ( auth_time, headers_receipt, total_time, speed ) ) path = down['path'] if attempts > 1: output_manager.print_msg( '%s [%s after %d attempts]', path, time_str, attempts ) else: output_manager.print_msg( '%s [%s]', path, time_str ) else: error = down['error'] path = down['path'] container = down['container'] obj = down['object'] if isinstance(error, ClientException): if error.http_status == 304 and \ options['skip_identical']: output_manager.print_msg( "Skipped identical file '%s'", path) continue if error.http_status == 404: output_manager.error( "Object '%s/%s' not found", container, obj) continue output_manager.error( "Error downloading object '%s/%s': %s", container, obj, error) except SwiftError as e: output_manager.error(e.value) except Exception as e: output_manager.error(e) st_list_options = '''[--long] [--lh] [--totals] [--prefix ] [--delimiter ] [--header ] [--versions] [] ''' st_list_help = ''' Lists the containers for the account or the objects for a container. Positional arguments: [] Name of container to list object in. Optional arguments: -l, --long Long listing format, similar to ls -l. --lh Report sizes in human readable format similar to ls -lh. -t, --totals Used with -l or --lh, only report totals. -p , --prefix Only list items beginning with the prefix. -d , --delimiter Roll up items with the given delimiter. For containers only. See OpenStack Swift API documentation for what this means. -j, --json Display listing information in json --versions Display listing information for all versions -H, --header Adds a custom request header to use for listing. '''.strip('\n') def st_list(parser, args, output_manager, return_parser=False): def _print_stats(options, stats, human): total_count = total_bytes = 0 container = stats.get("container", None) for item in stats["listing"]: item_name = item.get('name') if not options['long'] and not human and not options['versions']: output_manager.print_msg(item.get('name', item.get('subdir'))) else: if not container: # listing containers item_bytes = item.get('bytes') byte_str = prt_bytes(item_bytes, human) count = item.get('count') total_count += count try: meta = item.get('meta') utc = gmtime(float(meta.get('x-timestamp'))) datestamp = strftime('%Y-%m-%d %H:%M:%S', utc) except TypeError: datestamp = '????-??-?? ??:??:??' if not options['totals']: output_manager.print_msg( "%5s %s %s %s", count, byte_str, datestamp, item_name) else: # list container contents subdir = item.get('subdir') content_type = item.get('content_type') if subdir is None: item_bytes = item.get('bytes') byte_str = prt_bytes(item_bytes, human) date, xtime = item.get('last_modified').split('T') xtime = xtime.split('.')[0] else: item_bytes = 0 byte_str = prt_bytes(item_bytes, human) date = xtime = '' item_name = subdir if not options['totals']: if options['versions']: output_manager.print_msg( "%s %10s %8s %16s %24s %s", byte_str, date, xtime, item.get('version_id', 'null'), content_type, item_name) else: output_manager.print_msg( "%s %10s %8s %24s %s", byte_str, date, xtime, content_type, item_name) total_bytes += item_bytes # report totals if options['long'] or human: if not container: output_manager.print_msg( "%5s %s", prt_bytes(total_count, True), prt_bytes(total_bytes, human)) else: output_manager.print_msg( prt_bytes(total_bytes, human)) parser.add_argument( '-l', '--long', dest='long', action='store_true', default=False, help='Long listing format, similar to ls -l.') parser.add_argument( '--lh', dest='human', action='store_true', default=False, help='Report sizes in human readable format, ' "similar to ls -lh.") parser.add_argument( '-t', '--totals', dest='totals', help='used with -l or --lh, only report totals.', action='store_true', default=False) parser.add_argument( '-p', '--prefix', dest='prefix', help='Only list items beginning with the prefix.') parser.add_argument( '-d', '--delimiter', dest='delimiter', help='Roll up items with the given delimiter. For containers ' 'only. See OpenStack Swift API documentation for ' 'what this means.') parser.add_argument('-j', '--json', action='store_true', help='print listing information in json') parser.add_argument('--versions', action='store_true', help='display all versions') parser.add_argument( '-H', '--header', action='append', dest='header', default=[], help='Adds a custom request header to use for listing.') # We return the parser to build up the bash_completion if return_parser: return parser options, args = parse_args(parser, args) args = args[1:] if options['delimiter'] and not args: exit('-d option only allowed for container listings') if options['versions'] and not args: exit('--versions option only allowed for container listings') human = options.pop('human') if human: options['long'] = True if options['totals'] and not options['long']: output_manager.error( "Listing totals only works with -l or --lh.") return with SwiftService(options=options) as swift: try: if not args: stats_parts_gen = swift.list() else: container = args[0] args = args[1:] if "/" in container or args: output_manager.error( 'Usage: %s list %s\n%s', BASENAME, st_list_options, st_list_help) return else: stats_parts_gen = swift.list(container=container) if options.get('json', False): def listing(stats_parts_gen=stats_parts_gen): for stats in stats_parts_gen: if stats["success"]: for item in stats['listing']: yield item else: raise stats["error"] json.dump( JSONableIterable(listing()), output_manager.print_stream, sort_keys=True, indent=2) output_manager.print_msg('') return for stats in stats_parts_gen: if stats["success"]: _print_stats(options, stats, human) else: raise stats["error"] except SwiftError as e: output_manager.error(e.value) st_stat_options = '''[--lh] [--header ] [--version-id ] [ []] ''' st_stat_help = ''' Displays information for the account, container, or object. Positional arguments: [] Name of container to stat from. [] Name of object to stat. Optional arguments: --lh Report sizes in human readable format similar to ls -lh. --version-id Report stat of specific version of a versioned object. -H, --header Adds a custom request header to use for stat. '''.strip('\n') def st_stat(parser, args, output_manager, return_parser=False): parser.add_argument( '--lh', dest='human', action='store_true', default=False, help='Report sizes in human readable format similar to ls -lh.') parser.add_argument( '--version-id', action='store', default=None, help='Report stat of a specific version of a versioned object') parser.add_argument( '-H', '--header', action='append', dest='header', default=[], help='Adds a custom request header to use for stat.') # We return the parser to build up the bash_completion if return_parser: return parser options, args = parse_args(parser, args) args = args[1:] if options['version_id'] and len(args) < 2: exit('--version-id option only allowed for object stats') with SwiftService(options=options) as swift: try: if not args: stat_result = swift.stat() if not stat_result['success']: raise stat_result['error'] items = stat_result['items'] headers = stat_result['headers'] print_account_stats(items, headers, output_manager) else: container = args[0] if '/' in container: output_manager.error( 'WARNING: / in container name; you might have ' "meant '%s' instead of '%s'." % (container.replace('/', ' ', 1), container)) return args = args[1:] if not args: stat_result = swift.stat(container=container) if not stat_result['success']: raise stat_result['error'] items = stat_result['items'] headers = stat_result['headers'] print_container_stats(items, headers, output_manager) else: if len(args) == 1: objects = [args[0]] stat_results = swift.stat( container=container, objects=objects) for stat_result in stat_results: # only 1 result if stat_result["success"]: items = stat_result['items'] headers = stat_result['headers'] print_object_stats( items, headers, output_manager ) else: raise(stat_result["error"]) else: output_manager.error( 'Usage: %s stat %s\n%s', BASENAME, st_stat_options, st_stat_help) except SwiftError as e: output_manager.error(e.value) st_post_options = '''[--read-acl ] [--write-acl ] [--sync-to ] [--sync-key ] [--meta ] [--header
] [ []] ''' st_post_help = ''' Updates meta information for the account, container, or object. If the container is not found, it will be created automatically. Positional arguments: [] Name of container to post to. [] Name of object to post. Optional arguments: -r, --read-acl Read ACL for containers. Quick summary of ACL syntax: .r:*, .r:-.example.com, .r:www.example.com, account1 (v1.0 identity API only), account1:*, account2:user2 (v2.0+ identity API). -w, --write-acl Write ACL for containers. Quick summary of ACL syntax: account1 (v1.0 identity API only), account1:*, account2:user2 (v2.0+ identity API). -t, --sync-to Sync To for containers, for multi-cluster replication. -k, --sync-key Sync Key for containers, for multi-cluster replication. -m, --meta Sets a meta data item. This option may be repeated. Example: -m Color:Blue -m Size:Large -H, --header Adds a customized request header. This option may be repeated. Example -H "content-type:text/plain" -H "Content-Length: 4000" '''.strip('\n') def st_post(parser, args, output_manager, return_parser=False): parser.add_argument( '-r', '--read-acl', dest='read_acl', help='Read ACL for containers. ' 'Quick summary of ACL syntax: .r:*, .r:-.example.com, ' '.r:www.example.com, account1, account2:user2') parser.add_argument( '-w', '--write-acl', dest='write_acl', help='Write ACL for ' 'containers. Quick summary of ACL syntax: account1, ' 'account2:user2') parser.add_argument( '-t', '--sync-to', dest='sync_to', help='Sets the ' 'Sync To for containers, for multi-cluster replication.') parser.add_argument( '-k', '--sync-key', dest='sync_key', help='Sets the ' 'Sync Key for containers, for multi-cluster replication.') parser.add_argument( '-m', '--meta', action='append', dest='meta', default=[], help='Sets a meta data item. This option may be repeated. ' 'Example: -m Color:Blue -m Size:Large') parser.add_argument( '-H', '--header', action='append', dest='header', default=[], help='Adds a customized request header. ' 'This option may be repeated. ' 'Example: -H "content-type:text/plain" ' '-H "Content-Length: 4000"') # We return the parser to build up the bash_completion if return_parser: return parser (options, args) = parse_args(parser, args) args = args[1:] if (options['read_acl'] or options['write_acl'] or options['sync_to'] or options['sync_key']) and not args: exit('-r, -w, -t, and -k options only allowed for containers') with SwiftService(options=options) as swift: try: if not args: result = swift.post() else: container = args[0] if '/' in container: output_manager.error( 'WARNING: / in container name; you might have ' "meant '%s' instead of '%s'." % (args[0].replace('/', ' ', 1), args[0])) return args = args[1:] if args: if len(args) == 1: objects = [args[0]] results_iterator = swift.post( container=container, objects=objects ) result = next(results_iterator) else: output_manager.error( 'Usage: %s post %s\n%s', BASENAME, st_post_options, st_post_help) return else: result = swift.post(container=container) if not result["success"]: raise(result["error"]) except SwiftError as e: output_manager.error(e.value) st_copy_options = '''[--destination ] [--fresh-metadata] [--meta ] [--header
] [] [...] ''' st_copy_help = ''' Copies object to new destination, optionally updates objects metadata. If destination is not set, will update metadata of object Positional arguments: Name of container to copy from. Name of object to copy. Specify multiple times for multiple objects Optional arguments: -d, --destination The container and name of the destination object. Name of destination object can be omitted, then will be same as name of source object. Supplying multiple objects and destination with object name is invalid. -M, --fresh-metadata Copy the object without any existing metadata, If not set, metadata will be preserved or appended -m, --meta Sets a meta data item. This option may be repeated. Example: -m Color:Blue -m Size:Large -H, --header Adds a customized request header. This option may be repeated. Example -H "content-type:text/plain" -H "Content-Length: 4000" '''.strip('\n') def st_copy(parser, args, output_manager, return_parser=False): parser.add_argument( '-d', '--destination', help='The container and name of the ' 'destination object') parser.add_argument( '-M', '--fresh-metadata', action='store_true', help='Copy the object without any existing metadata', default=False) parser.add_argument( '-m', '--meta', action='append', dest='meta', default=[], help='Sets a meta data item. This option may be repeated. ' 'Example: -m Color:Blue -m Size:Large') parser.add_argument( '-H', '--header', action='append', dest='header', default=[], help='Adds a customized request header. ' 'This option may be repeated. ' 'Example: -H "content-type:text/plain" ' '-H "Content-Length: 4000"') # We return the parser to build up the bash_completion if return_parser: return parser (options, args) = parse_args(parser, args) args = args[1:] with SwiftService(options=options) as swift: try: if len(args) >= 2: container = args[0] if '/' in container: output_manager.error( 'WARNING: / in container name; you might have ' "meant '%s' instead of '%s'." % (args[0].replace('/', ' ', 1), args[0])) return objects = [arg for arg in args[1:]] for r in swift.copy( container=container, objects=objects, options=options): if r['success']: if options['verbose']: if r['action'] == 'copy_object': output_manager.print_msg( '%s/%s copied to %s' % ( r['container'], r['object'], r['destination'] or '')) if r['action'] == 'create_container': output_manager.print_msg( 'created container %s' % r['container'] ) else: error = r['error'] if 'action' in r and r['action'] == 'create_container': # it is not an error to be unable to create the # container so print a warning and carry on output_manager.warning( 'Warning: failed to create container ' "'%s': %s", container, error ) else: output_manager.error("%s" % error) else: output_manager.error( 'Usage: %s copy %s\n%s', BASENAME, st_copy_options, st_copy_help) return except SwiftError as e: output_manager.error(e.value) st_upload_options = '''[--changed] [--skip-identical] [--segment-size ] [--segment-container ] [--leave-segments] [--object-threads ] [--segment-threads ] [--meta ] [--header
] [--use-slo] [--ignore-checksum] [--object-name ] [] [...] ''' st_upload_help = ''' Uploads specified files and directories to the given container. Positional arguments: Name of container to upload to. Name of file or directory to upload. Specify multiple times for multiple uploads. If "-" is specified, reads content from standard input (--object-name is required in this case). Optional arguments: -c, --changed Only upload files that have changed since the last upload. --skip-identical Skip uploading files that are identical on both sides. -S, --segment-size Upload files in segments no larger than (in Bytes) and then create a "manifest" file that will download all the segments as if it were the original file. --segment-container Upload the segments into the specified container. If not specified, the segments will be uploaded to a _segments container to not pollute the main listings. --leave-segments Indicates that you want the older segments of manifest objects left alone (in the case of overwrites). --object-threads Number of threads to use for uploading full objects. Default is 10. --segment-threads Number of threads to use for uploading object segments. Default is 10. -m, --meta Sets a meta data item. This option may be repeated. Example: -m Color:Blue -m Size:Large -H, --header Adds a customized request header. This option may be repeated. Example: -H "content-type:text/plain" -H "Content-Length: 4000". --use-slo When used in conjunction with --segment-size it will create a Static Large Object instead of the default Dynamic Large Object. --object-name Upload file and name object to or upload dir and use as object prefix instead of folder name. --ignore-checksum Turn off checksum validation for uploads. '''.strip('\n') def st_upload(parser, args, output_manager, return_parser=False): DEFAULT_STDIN_SEGMENT = 10 * 1024 * 1024 parser.add_argument( '-c', '--changed', action='store_true', dest='changed', default=False, help='Only upload files that have changed since ' 'the last upload.') parser.add_argument( '--skip-identical', action='store_true', dest='skip_identical', default=False, help='Skip uploading files that are identical on ' 'both sides.') parser.add_argument( '-S', '--segment-size', dest='segment_size', help='Upload files ' 'in segments no larger than (in Bytes) and then create a ' '"manifest" file that will download all the segments as if it were ' 'the original file. Sizes may also be expressed as bytes with the ' 'B suffix, kilobytes with the K suffix, megabytes with the M suffix ' 'or gigabytes with the G suffix.') parser.add_argument( '-C', '--segment-container', dest='segment_container', help='Upload the segments into the specified container. ' 'If not specified, the segments will be uploaded to a ' '_segments container to not pollute the main ' ' listings.') parser.add_argument( '--leave-segments', action='store_true', dest='leave_segments', default=False, help='Indicates that you want ' 'the older segments of manifest objects left alone (in the case of ' 'overwrites).') parser.add_argument( '--object-threads', type=int, default=10, help='Number of threads to use for uploading full objects. ' 'Its value must be a positive integer. Default is 10.') parser.add_argument( '--segment-threads', type=int, default=10, help='Number of threads to use for uploading object segments. ' 'Its value must be a positive integer. Default is 10.') parser.add_argument( '-m', '--meta', action='append', dest='meta', default=[], help='Sets a meta data item. This option may be repeated. ' 'Example: -m Color:Blue -m Size:Large') parser.add_argument( '-H', '--header', action='append', dest='header', default=[], help='Set request headers with the syntax header:value. ' ' This option may be repeated. Example: -H "content-type:text/plain" ' '-H "Content-Length: 4000"') parser.add_argument( '--use-slo', action='store_true', default=False, help='When used in conjunction with --segment-size, it will ' 'create a Static Large Object instead of the default ' 'Dynamic Large Object.') parser.add_argument( '--object-name', dest='object_name', help='Upload file and name object to or upload dir and ' 'use as object prefix instead of folder name.') parser.add_argument( '--ignore-checksum', dest='checksum', default=True, action='store_false', help='Turn off checksum validation for uploads.') # We return the parser to build up the bash_completion if return_parser: return parser options, args = parse_args(parser, args) args = args[1:] if len(args) < 2: output_manager.error( 'Usage: %s upload %s\n%s', BASENAME, st_upload_options, st_upload_help) return else: container = args[0] files = args[1:] from_stdin = '-' in files if from_stdin and len(files) > 1: output_manager.error( 'upload from stdin cannot be used along with other files') return if options['object_name'] is not None: if len(files) > 1: output_manager.error('object-name only be used with 1 file or dir') return else: orig_path = files[0] elif from_stdin: output_manager.error( 'object-name must be specified with uploads from stdin') return if options['segment_size']: try: # If segment size only has digits assume it is bytes int(options['segment_size']) except ValueError: try: size_mod = "BKMG".index(options['segment_size'][-1].upper()) multiplier = int(options['segment_size'][:-1]) except ValueError: output_manager.error("Invalid segment size") return options['segment_size'] = str((1024 ** size_mod) * multiplier) if int(options['segment_size']) <= 0: output_manager.error("segment-size should be positive") return if options['object_threads'] <= 0: output_manager.error( 'ERROR: option --object-threads should be a positive integer.' '\n\nUsage: %s upload %s\n%s', BASENAME, st_upload_options, st_upload_help) return if options['segment_threads'] <= 0: output_manager.error( 'ERROR: option --segment-threads should be a positive integer.' '\n\nUsage: %s upload %s\n%s', BASENAME, st_upload_options, st_upload_help) return if from_stdin: if not options['use_slo']: options['use_slo'] = True if not options['segment_size']: options['segment_size'] = DEFAULT_STDIN_SEGMENT options['object_uu_threads'] = options['object_threads'] with SwiftService(options=options) as swift: try: objs = [] dir_markers = [] for f in files: if f == '-': fd = io.open(stdin.fileno(), mode='rb') objs.append(SwiftUploadObject( fd, object_name=options['object_name'])) # We ensure that there is exactly one "file" to upload in # this case -- stdin break if isfile(f): objs.append(f) elif isdir(f): for (_dir, _ds, _fs) in walk(f): if not (_ds + _fs): dir_markers.append(_dir) else: objs.extend([join(_dir, _f) for _f in _fs]) else: output_manager.error("Local file '%s' not found" % f) # Now that we've collected all the required files and dir markers # build the tuples for the call to upload if options['object_name'] is not None and not from_stdin: objs = [ SwiftUploadObject( o, object_name=o.replace( orig_path, options['object_name'], 1 ) ) for o in objs ] dir_markers = [ SwiftUploadObject( None, object_name=d.replace( orig_path, options['object_name'], 1 ), options={'dir_marker': True} ) for d in dir_markers ] for r in swift.upload(container, objs + dir_markers): if r['success']: if options['verbose']: if 'attempts' in r and r['attempts'] > 1: if 'object' in r: output_manager.print_msg( '%s [after %d attempts]' % (r['object'], r['attempts']) ) else: if 'object' in r: output_manager.print_msg(r['object']) elif 'for_object' in r: output_manager.print_msg( '%s segment %s' % (r['for_object'], r['segment_index']) ) else: error = r['error'] if 'action' in r and r['action'] == "create_container": # it is not an error to be unable to create the # container so print a warning and carry on if isinstance(error, ClientException): if (r['headers'] and 'X-Storage-Policy' in r['headers']): msg = ' with Storage Policy %s' % \ r['headers']['X-Storage-Policy'].strip() else: msg = ' '.join(str(x) for x in ( error.http_status, error.http_reason) ) if error.http_response_content: if msg: msg += ': ' msg += (error.http_response_content .decode('utf8')[:60]) msg = ': %s' % msg else: msg = ': %s' % error output_manager.warning( 'Warning: failed to create container ' "'%s'%s", r['container'], msg ) else: output_manager.error("%s" % error) too_large = (isinstance(error, ClientException) and error.http_status == 413) if too_large and options['verbose'] > 0: output_manager.error( "Consider using the --segment-size option " "to chunk the object") except SwiftError as e: output_manager.error(e.value) st_capabilities_options = '''[--json] [] ''' st_info_options = st_capabilities_options st_capabilities_help = ''' Retrieve capability of the proxy. Optional positional arguments: Proxy URL of the cluster to retrieve capabilities. Optional arguments: --json Print the cluster capabilities in JSON format. '''.strip('\n') st_info_help = st_capabilities_help def st_capabilities(parser, args, output_manager, return_parser=False): def _print_compo_cap(name, capabilities): for feature, options in sorted(capabilities.items(), key=lambda x: x[0]): output_manager.print_msg("%s: %s" % (name, feature)) if options: output_manager.print_msg(" Options:") for key, value in sorted(options.items(), key=lambda x: x[0]): output_manager.print_msg(" %s: %s" % (key, value)) parser.add_argument('--json', action='store_true', help='print capability information in json') # We return the parser to build up the bash_completion if return_parser: return parser (options, args) = parse_args(parser, args) if args and len(args) > 2: output_manager.error('Usage: %s capabilities %s\n%s', BASENAME, st_capabilities_options, st_capabilities_help) return with SwiftService(options=options) as swift: try: if len(args) == 2: url = args[1] capabilities_result = swift.capabilities(url) capabilities = capabilities_result['capabilities'] else: capabilities_result = swift.capabilities() capabilities = capabilities_result['capabilities'] if options['json']: output_manager.print_msg( json.dumps(capabilities, sort_keys=True, indent=2)) else: capabilities = dict(capabilities) _print_compo_cap('Core', {'swift': capabilities['swift']}) del capabilities['swift'] _print_compo_cap('Additional middleware', capabilities) except SwiftError as e: output_manager.error(e.value) st_info = st_capabilities st_auth_help = ''' Display auth related authentication variables in shell friendly format. Commands to run to export storage url and auth token into OS_STORAGE_URL and OS_AUTH_TOKEN: swift auth Commands to append to a runcom file (e.g. ~/.bashrc, /etc/profile) for automatic authentication: swift auth -v -U test:tester -K testing \ -A http://localhost:8080/auth/v1.0 '''.strip('\n') def st_auth(parser, args, thread_manager, return_parser=False): # We return the parser to build up the bash_completion if return_parser: return parser (options, args) = parse_args(parser, args) if options['verbose'] > 1: if options['auth_version'] in ('1', '1.0'): print('export ST_AUTH=%s' % sh_quote(options['auth'])) print('export ST_USER=%s' % sh_quote(options['user'])) print('export ST_KEY=%s' % sh_quote(options['key'])) else: print('export OS_IDENTITY_API_VERSION=%s' % sh_quote( options['auth_version'])) print('export OS_AUTH_VERSION=%s' % sh_quote( options['auth_version'])) print('export OS_AUTH_URL=%s' % sh_quote(options['auth'])) for k, v in sorted(options.items()): if v and k.startswith('os_') and \ k not in ('os_auth_url', 'os_options'): print('export %s=%s' % (k.upper(), sh_quote(v))) else: conn = get_conn(options) url, token = conn.get_auth() print('export OS_STORAGE_URL=%s' % sh_quote(url)) print('export OS_AUTH_TOKEN=%s' % sh_quote(token)) st_tempurl_options = '''[--absolute] [--prefix-based] [--iso8601]