Skip to content

Commit

Permalink
Make swift-dispersion-report importable
Browse files Browse the repository at this point in the history
This patch allows to import the dispersion report tool, and thus making
it more easily usable within other Python tools. This can be also used
in a follow up patch to add some tests for the report tool.

It also fixes a bug when using the "--dump-json" option - until now it
returned the policy name and made the JSON invalid.

Change-Id: Ie0d52a1a54fc152bb72cbb3f84dcc36a8dad972a
  • Loading branch information
cschwede authored and tipabu committed Sep 13, 2017
1 parent 097a908 commit b77de53
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
4 changes: 3 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ scripts =
bin/swift-container-reconciler
bin/swift-reconciler-enqueue
bin/swift-dispersion-populate
bin/swift-dispersion-report
bin/swift-drive-audit
bin/swift-form-signature
bin/swift-get-nodes
Expand All @@ -63,6 +62,9 @@ scripts =
bin/swift-ring-builder-analyzer
bin/swift-temp-url

console_scripts =
swift-dispersion-report = swift.cli.dispersion_report:main

[extras]
kms_keymaster =
oslo.config>=4.0.0,!=4.3.0,!=4.4.0 # Apache-2.0
Expand Down
61 changes: 41 additions & 20 deletions bin/swift-dispersion-report → swift/cli/dispersion_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def missing_string(partition_count, missing_copies, copy_count):
)


if __name__ == '__main__':
def main():
patcher.monkey_patch()
hubs.get_hub().debug_exceptions = False

Expand All @@ -335,43 +335,60 @@ def missing_string(partition_count, missing_copies, copy_count):
help="Specify storage policy name")

options, args = parser.parse_args()

if args:
conffile = args.pop(0)

if options.debug:
global debug
debug = True

c = ConfigParser()
if not c.read(conffile):
exit('Unable to read config file: %s' % conffile)
conf = dict(c.items('dispersion'))

if options.policy_name is None:
if options.dump_json:
conf['dump_json'] = 'yes'
if options.object_only:
conf['container_report'] = 'no'
if options.container_only:
conf['object_report'] = 'no'
if options.insecure:
conf['keystone_api_insecure'] = 'yes'
if options.partitions:
conf['partitions'] = 'yes'

output = generate_report(conf, options.policy_name)

if json_output:
print(json.dumps(output))


def generate_report(conf, policy_name=None):
global json_output
json_output = config_true_value(conf.get('dump_json', 'no'))
if policy_name is None:
policy = POLICIES.default
else:
policy = POLICIES.get_by_name(options.policy_name)
policy = POLICIES.get_by_name(policy_name)
if policy is None:
exit('Unable to find policy: %s' % options.policy_name)
print('Using storage policy: %s ' % policy.name)
exit('Unable to find policy: %s' % policy_name)
if not json_output:
print('Using storage policy: %s ' % policy.name)

swift_dir = conf.get('swift_dir', '/etc/swift')
retries = int(conf.get('retries', 5))
concurrency = int(conf.get('concurrency', 25))
endpoint_type = str(conf.get('endpoint_type', 'publicURL'))
region_name = str(conf.get('region_name', ''))
if options.dump_json or config_true_value(conf.get('dump_json', 'no')):
json_output = True
container_report = config_true_value(conf.get('container_report', 'yes')) \
and not options.object_only
object_report = config_true_value(conf.get('object_report', 'yes')) \
and not options.container_only
container_report = config_true_value(conf.get('container_report', 'yes'))
object_report = config_true_value(conf.get('object_report', 'yes'))
if not (object_report or container_report):
exit("Neither container or object report is set to run")
user_domain_name = str(conf.get('user_domain_name', ''))
project_domain_name = str(conf.get('project_domain_name', ''))
project_name = str(conf.get('project_name', ''))
insecure = options.insecure \
or config_true_value(conf.get('keystone_api_insecure', 'no'))
if options.debug:
debug = True
insecure = config_true_value(conf.get('keystone_api_insecure', 'no'))

coropool = GreenPool(size=concurrency)

Expand Down Expand Up @@ -402,10 +419,14 @@ def missing_string(partition_count, missing_copies, copy_count):
if container_report:
output['container'] = container_dispersion_report(
coropool, connpool, account, container_ring, retries,
options.partitions, policy)
conf.get('partitions'), policy)
if object_report:
output['object'] = object_dispersion_report(
coropool, connpool, account, object_ring, retries,
options.partitions, policy)
if json_output:
print(json.dumps(output))
conf.get('partitions'), policy)

return output


if __name__ == '__main__':
main()

0 comments on commit b77de53

Please sign in to comment.