From a54095e562c2a14d48164d0ad48987cd6e85a379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Nov=C3=BD?= Date: Tue, 31 May 2016 17:00:30 +0200 Subject: [PATCH] swift-ring-builder --yes option This option assume a yes response to all questions. It is usefull for scripts. Change-Id: I28ca1a44507e0f1265afd36e6ac1e7c6c176428f --- doc/manpages/swift-ring-builder.1 | 5 ++++ swift/cli/ringbuilder.py | 43 +++++++++++++++++++++---------- swift/common/ring/utils.py | 2 ++ test/unit/cli/test_ringbuilder.py | 4 +-- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/doc/manpages/swift-ring-builder.1 b/doc/manpages/swift-ring-builder.1 index 6bff7e8e67..e4c12b6958 100644 --- a/doc/manpages/swift-ring-builder.1 +++ b/doc/manpages/swift-ring-builder.1 @@ -97,6 +97,11 @@ Nerd explanation: .PD +.SH OPTIONS +.TP +.I "\-y, \-\-yes" +Assume a yes response to all questions + .SH COMMANDS .PD 0 diff --git a/swift/cli/ringbuilder.py b/swift/cli/ringbuilder.py index fa8c425409..8f52849a93 100644 --- a/swift/cli/ringbuilder.py +++ b/swift/cli/ringbuilder.py @@ -181,7 +181,7 @@ def _parse_add_values(argvish): return parsed_devs -def _set_weight_values(devs, weight): +def _set_weight_values(devs, weight, opts): if not devs: print('Search value matched 0 devices.\n' 'The on-disk ring builder is unchanged.') @@ -191,8 +191,9 @@ def _set_weight_values(devs, weight): print('Matched more than one device:') for dev in devs: print(' %s' % format_device(dev)) - if input('Are you sure you want to update the weight for ' - 'these %s devices? (y/N) ' % len(devs)) != 'y': + if not opts.yes and \ + input('Are you sure you want to update the weight for ' + 'these %s devices? (y/N) ' % len(devs)) != 'y': print('Aborting device modifications') exit(EXIT_ERROR) @@ -222,7 +223,7 @@ def _parse_set_weight_values(argvish): devs.extend(builder.search_devs( parse_search_value(devstr)) or []) weight = float(weightstr) - _set_weight_values(devs, weight) + _set_weight_values(devs, weight, opts) else: if len(args) != 1: print(Commands.set_weight.__doc__.strip()) @@ -231,13 +232,13 @@ def _parse_set_weight_values(argvish): devs.extend(builder.search_devs( parse_search_values_from_opts(opts)) or []) weight = float(args[0]) - _set_weight_values(devs, weight) + _set_weight_values(devs, weight, opts) except ValueError as e: print(e) exit(EXIT_ERROR) -def _set_info_values(devs, change): +def _set_info_values(devs, change, opts): if not devs: print("Search value matched 0 devices.\n" @@ -248,8 +249,9 @@ def _set_info_values(devs, change): print('Matched more than one device:') for dev in devs: print(' %s' % format_device(dev)) - if input('Are you sure you want to update the info for ' - 'these %s devices? (y/N) ' % len(devs)) != 'y': + if not opts.yes and \ + input('Are you sure you want to update the info for ' + 'these %s devices? (y/N) ' % len(devs)) != 'y': print('Aborting device modifications') exit(EXIT_ERROR) @@ -354,11 +356,11 @@ def _parse_set_info_values(argvish): if change_value or not change: raise ValueError('Invalid set info change value: %s' % repr(argvish[1])) - _set_info_values(devs, change) + _set_info_values(devs, change, opts) else: devs = builder.search_devs(parse_search_values_from_opts(opts)) change = parse_change_values_from_opts(opts) - _set_info_values(devs, change) + _set_info_values(devs, change, opts) def _parse_remove_values(argvish): @@ -382,7 +384,7 @@ def _parse_remove_values(argvish): devs.extend(builder.search_devs( parse_search_values_from_opts(opts))) - return devs + return (devs, opts) except ValueError as e: print(e) exit(EXIT_ERROR) @@ -641,6 +643,7 @@ swift-ring-builder add """ swift-ring-builder set_weight [ set_weight --region --zone --ip --port --replication-ip --replication-port --device --meta --weight + [--yes] Where , and are replication ip, hostname and port. @@ -656,6 +660,8 @@ swift-ring-builder set_weight Resets the devices' weights. No partitions will be reassigned to or from the device until after running 'rebalance'. This is so you can make multiple device changes and rebalance them all just once. + + Option --yes assume a yes response to all questions. """ # if len(argv) < 5 or len(argv) % 2 != 1: if len(argv) < 5: @@ -675,6 +681,7 @@ swift-ring-builder set_weight swift-ring-builder set_info :[R:]/_ [ :[R:]/_] ... + [--yes] or @@ -687,6 +694,7 @@ swift-ring-builder set_info --change-replication-port --change-device --change-meta + [--yes] Where , and are replication ip, hostname and port. @@ -699,6 +707,8 @@ swift-ring-builder set_info :/_ parameter; just give what you want to change. For instance set_info d74 _"snet: 5.6.7.8" would just update the meta data for device id 74. + + Option --yes assume a yes response to all questions. """ if len(argv) < 5: print(Commands.set_info.__doc__.strip()) @@ -719,6 +729,7 @@ swift-ring-builder set_info def remove(): """ swift-ring-builder remove [search-value ...] + [--yes] or @@ -726,6 +737,7 @@ swift-ring-builder remove --region --zone --ip --port --replication-ip --replication-port --device --meta --weight + [--yes] Where , and are replication ip, hostname and port. @@ -737,6 +749,8 @@ swift-ring-builder remove remove command. This will not take effect until after running 'rebalance'. This is so you can make multiple device changes and rebalance them all just once. + + Option --yes assume a yes response to all questions. """ if len(argv) < 4: print(Commands.remove.__doc__.strip()) @@ -744,7 +758,7 @@ swift-ring-builder remove print(parse_search_value.__doc__.strip()) exit(EXIT_ERROR) - devs = _parse_remove_values(argv[3:]) + devs, opts = _parse_remove_values(argv[3:]) if not devs: print('Search value matched 0 devices.\n' @@ -755,8 +769,9 @@ swift-ring-builder remove print('Matched more than one device:') for dev in devs: print(' %s' % format_device(dev)) - if input('Are you sure you want to remove these %s ' - 'devices? (y/N) ' % len(devs)) != 'y': + if not opts.yes and \ + input('Are you sure you want to remove these %s ' + 'devices? (y/N) ' % len(devs)) != 'y': print('Aborting device removals') exit(EXIT_ERROR) diff --git a/swift/common/ring/utils.py b/swift/common/ring/utils.py index 1b48acc3c8..830dbfef22 100644 --- a/swift/common/ring/utils.py +++ b/swift/common/ring/utils.py @@ -586,6 +586,8 @@ def parse_args(argvish): help="Device name (e.g. md0, sdb1) for change") parser.add_option('-M', '--change-meta', type="string", default="", help="Extra device info (just a string) for change") + parser.add_option('-y', '--yes', default=False, action="store_true", + help="Assume a yes response to all questions") return parser.parse_args(argvish) diff --git a/test/unit/cli/test_ringbuilder.py b/test/unit/cli/test_ringbuilder.py index 35b1f6461c..955e421ab8 100644 --- a/test/unit/cli/test_ringbuilder.py +++ b/test/unit/cli/test_ringbuilder.py @@ -233,7 +233,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): # Test no devices # _set_weight_values doesn't take argv-like arguments self.assertSystemExit( - EXIT_ERROR, ringbuilder._set_weight_values, [], 100) + EXIT_ERROR, ringbuilder._set_weight_values, [], 100, {}) def test_parse_set_weight_values_number_of_arguments(self): # Test Number of arguments abnormal @@ -249,7 +249,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): # Test no devices # _set_info_values doesn't take argv-like arguments self.assertSystemExit( - EXIT_ERROR, ringbuilder._set_info_values, [], 100) + EXIT_ERROR, ringbuilder._set_info_values, [], 100, {}) def test_parse_set_info_values_number_of_arguments(self): # Test Number of arguments abnormal