From 2c5dc64d2562030ba4976b1c88462e1ebf8717c4 Mon Sep 17 00:00:00 2001 From: Tim Burke Date: Wed, 26 Jun 2024 12:15:07 -0700 Subject: [PATCH] Pull swift-*-info scripts into swift.cli.info Related-Change: Ifcc8138e7b55d5b82bea0d411ec6bfcca2c77c83 Change-Id: I2718d8b96193d13f913824fc1091d72737d755ea --- bin/swift-account-info | 39 +-------------- bin/swift-container-info | 46 +----------------- bin/swift-object-info | 44 +---------------- setup.cfg | 6 +-- swift/cli/info.py | 102 ++++++++++++++++++++++++++++++++++++++- 5 files changed, 109 insertions(+), 128 deletions(-) diff --git a/bin/swift-account-info b/bin/swift-account-info index 41563bcc95..54bde7b70a 100755 --- a/bin/swift-account-info +++ b/bin/swift-account-info @@ -11,42 +11,7 @@ # License for the specific language governing permissions and limitations # under the License. -import sqlite3 -import sys -from optparse import OptionParser - -from swift.cli.info import print_info, InfoSystemExit -from swift.common.exceptions import LockTimeout - - -def run_print_info(args, opts): - try: - print_info('account', *args, **opts) - except InfoSystemExit: - sys.exit(1) - except (sqlite3.OperationalError, LockTimeout) as e: - if not opts.get('stale_reads_ok'): - opts['stale_reads_ok'] = True - print('Warning: Possibly Stale Data') - run_print_info(args, opts) - sys.exit(2) - else: - print('Account info failed: %s' % e) - sys.exit(1) - +from swift.cli import info if __name__ == '__main__': - parser = OptionParser('%prog [options] ACCOUNT_DB_FILE') - parser.add_option( - '-d', '--swift-dir', default='/etc/swift', - help="Pass location of swift directory") - parser.add_option( - '--drop-prefixes', default=False, action="store_true", - help="When outputting metadata, drop the per-section common prefixes") - - options, args = parser.parse_args() - - if len(args) != 1: - sys.exit(parser.print_help()) - - run_print_info(args, vars(options)) + info.account_main() diff --git a/bin/swift-container-info b/bin/swift-container-info index 7d9efecba3..362d0ab3e5 100755 --- a/bin/swift-container-info +++ b/bin/swift-container-info @@ -11,49 +11,7 @@ # License for the specific language governing permissions and limitations # under the License. -import sqlite3 -import sys -from optparse import OptionParser - -from swift.cli.info import print_info, InfoSystemExit -from swift.common.exceptions import LockTimeout - - -def run_print_info(args, opts): - try: - print_info('container', *args, **opts) - except InfoSystemExit: - sys.exit(1) - except (sqlite3.OperationalError, LockTimeout) as e: - if not opts.get('stale_reads_ok'): - opts['stale_reads_ok'] = True - print('Warning: Possibly Stale Data') - run_print_info(args, opts) - sys.exit(2) - else: - print('Container info failed: %s' % e) - sys.exit(1) - +from swift.cli import info if __name__ == '__main__': - parser = OptionParser('%prog [options] CONTAINER_DB_FILE') - parser.add_option( - '-d', '--swift-dir', default='/etc/swift', - help="Pass location of swift directory") - parser.add_option( - '--drop-prefixes', default=False, action="store_true", - help="When outputting metadata, drop the per-section common prefixes") - parser.add_option( - '-v', '--verbose', default=False, action="store_true", - help="Show all shard ranges. By default, only the number of shard " - "ranges is displayed if there are many shards.") - parser.add_option( - '--sync', '-s', default=False, action="store_true", - help="Output the contents of the incoming/outging sync tables") - - options, args = parser.parse_args() - - if len(args) != 1: - sys.exit(parser.print_help()) - - run_print_info(args, vars(options)) + info.container_main() diff --git a/bin/swift-object-info b/bin/swift-object-info index 29f59c100b..b43cedc898 100755 --- a/bin/swift-object-info +++ b/bin/swift-object-info @@ -14,47 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import codecs -import sys -from optparse import OptionParser - -import six - -from swift.common.storage_policy import reload_storage_policies -from swift.common.utils import set_swift_dir -from swift.cli.info import print_obj, InfoSystemExit - +from swift.cli import info if __name__ == '__main__': - if not six.PY2: - # Make stdout able to write escaped bytes - sys.stdout = codecs.getwriter("utf-8")( - sys.stdout.detach(), errors='surrogateescape') - - parser = OptionParser('%prog [options] OBJECT_FILE') - parser.add_option( - '-n', '--no-check-etag', default=True, - action="store_false", dest="check_etag", - help="Don't verify file contents against stored etag") - parser.add_option( - '-d', '--swift-dir', default='/etc/swift', dest='swift_dir', - help="Pass location of swift directory") - parser.add_option( - '--drop-prefixes', default=False, action="store_true", - help="When outputting metadata, drop the per-section common prefixes") - parser.add_option( - '-P', '--policy-name', dest='policy_name', - help="Specify storage policy name") - - options, args = parser.parse_args() - - if len(args) != 1: - sys.exit(parser.print_help()) - - if set_swift_dir(options.swift_dir): - reload_storage_policies() - - try: - print_obj(*args, **vars(options)) - except InfoSystemExit: - sys.exit(1) + info.obj_main() diff --git a/setup.cfg b/setup.cfg index 66a1149d9e..70e2036802 100644 --- a/setup.cfg +++ b/setup.cfg @@ -43,13 +43,10 @@ packages = [files] scripts = bin/swift-account-audit - bin/swift-account-info bin/swift-config - bin/swift-container-info bin/swift-reconciler-enqueue bin/swift-drive-audit bin/swift-get-nodes - bin/swift-object-info bin/swift-oldies bin/swift-orphans @@ -67,11 +64,13 @@ keystone = [entry_points] console_scripts = swift-account-auditor = swift.account.auditor:main + swift-account-info = swift.cli.info:account_main swift-account-reaper = swift.account.reaper:main swift-account-replicator = swift.account.replicator:main swift-account-server = swift.account.server:main swift-container-auditor = swift.container.auditor:main swift-container-deleter = swift.cli.container_deleter:main + swift-container-info = swift.cli.info:container_main swift-container-replicator = swift.container.replicator:main swift-container-reconciler = swift.container.reconciler:main swift-container-server = swift.container.server:main @@ -85,6 +84,7 @@ console_scripts = swift-manage-shard-ranges = swift.cli.manage_shard_ranges:main swift-object-auditor = swift.obj.auditor:main swift-object-expirer = swift.obj.expirer:main + swift-object-info = swift.cli.info:obj_main swift-object-reconstructor = swift.obj.reconstructor:main swift-object-relinker = swift.cli.relinker:main swift-object-replicator = swift.obj.replicator:main diff --git a/swift/cli/info.py b/swift/cli/info.py index 4d2e777983..1a9fabc886 100644 --- a/swift/cli/info.py +++ b/swift/cli/info.py @@ -10,15 +10,21 @@ # License for the specific language governing permissions and limitations # under the License. + from __future__ import print_function +import codecs import itertools import json +from optparse import OptionParser import os import sqlite3 +import sys from collections import defaultdict +import six from six.moves import urllib +from swift.common.exceptions import LockTimeout from swift.common.utils import hash_path, storage_directory, \ Timestamp, is_valid_ipv6 from swift.common.ring import Ring @@ -29,10 +35,10 @@ from swift.account.backend import AccountBroker, DATADIR as ABDATADIR from swift.container.backend import ContainerBroker, DATADIR as CBDATADIR from swift.obj.diskfile import get_data_dir, read_metadata, DATADIR_BASE, \ extract_policy -from swift.common.storage_policy import POLICIES +from swift.common.storage_policy import POLICIES, reload_storage_policies from swift.common.swob import wsgi_to_str from swift.common.middleware.crypto.crypto_utils import load_crypto_meta -from swift.common.utils import md5 +from swift.common.utils import md5, set_swift_dir class InfoSystemExit(Exception): @@ -713,3 +719,95 @@ def print_item_locations(ring, ring_name=None, account=None, container=None, print('Object \t%s\n\n' % urllib.parse.quote(obj)) print_ring_locations(ring, loc, account, container, obj, part, all_nodes, policy_index=policy_index) + + +def obj_main(): + if not six.PY2: + # Make stdout able to write escaped bytes + sys.stdout = codecs.getwriter("utf-8")( + sys.stdout.detach(), errors='surrogateescape') + + parser = OptionParser('%prog [options] OBJECT_FILE') + parser.add_option( + '-n', '--no-check-etag', default=True, + action="store_false", dest="check_etag", + help="Don't verify file contents against stored etag") + parser.add_option( + '-d', '--swift-dir', default='/etc/swift', dest='swift_dir', + help="Pass location of swift directory") + parser.add_option( + '--drop-prefixes', default=False, action="store_true", + help="When outputting metadata, drop the per-section common prefixes") + parser.add_option( + '-P', '--policy-name', dest='policy_name', + help="Specify storage policy name") + + options, args = parser.parse_args() + + if len(args) != 1: + sys.exit(parser.print_help()) + + if set_swift_dir(options.swift_dir): + reload_storage_policies() + + try: + print_obj(*args, **vars(options)) + except InfoSystemExit: + sys.exit(1) + + +def run_print_info(db_type, args, opts): + try: + print_info(db_type, *args, **opts) + except InfoSystemExit: + sys.exit(1) + except (sqlite3.OperationalError, LockTimeout) as e: + if not opts.get('stale_reads_ok'): + opts['stale_reads_ok'] = True + print('Warning: Possibly Stale Data') + run_print_info(db_type, args, opts) + sys.exit(2) + else: + print('%s info failed: %s' % (db_type.title(), e)) + sys.exit(1) + + +def container_main(): + parser = OptionParser('%prog [options] CONTAINER_DB_FILE') + parser.add_option( + '-d', '--swift-dir', default='/etc/swift', + help="Pass location of swift directory") + parser.add_option( + '--drop-prefixes', default=False, action="store_true", + help="When outputting metadata, drop the per-section common prefixes") + parser.add_option( + '-v', '--verbose', default=False, action="store_true", + help="Show all shard ranges. By default, only the number of shard " + "ranges is displayed if there are many shards.") + parser.add_option( + '--sync', '-s', default=False, action="store_true", + help="Output the contents of the incoming/outging sync tables") + + options, args = parser.parse_args() + + if len(args) != 1: + sys.exit(parser.print_help()) + + run_print_info('container', args, vars(options)) + + +def account_main(): + parser = OptionParser('%prog [options] ACCOUNT_DB_FILE') + parser.add_option( + '-d', '--swift-dir', default='/etc/swift', + help="Pass location of swift directory") + parser.add_option( + '--drop-prefixes', default=False, action="store_true", + help="When outputting metadata, drop the per-section common prefixes") + + options, args = parser.parse_args() + + if len(args) != 1: + sys.exit(parser.print_help()) + + run_print_info('account', args, vars(options))