Merge "Fix default replication options for ring-builder add"

This commit is contained in:
Jenkins 2013-10-07 16:09:34 +00:00 committed by Gerrit Code Review
commit 22372eadfe
3 changed files with 79 additions and 43 deletions

View File

@ -14,7 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import optparse
from array import array
from errno import EEXIST
from itertools import islice, izip
@ -29,7 +28,8 @@ from swift.common import exceptions
from swift.common.ring import RingBuilder
from swift.common.ring.builder import MAX_BALANCE
from swift.common.utils import lock_parent_directory
from swift.common.ring.utils import parse_search_value
from swift.common.ring.utils import parse_search_value, parse_args, \
build_dev_from_opts
MAJOR_VERSION = 1
MINOR_VERSION = 3
@ -60,26 +60,7 @@ def _parse_add_values(argvish):
:returns: array of device dicts
"""
parser = optparse.OptionParser()
parser.add_option('-r', '--region', type="int",
help="Region")
parser.add_option('-z', '--zone', type="int",
help="Zone")
parser.add_option('-i', '--ip', type="string",
help="IP address")
parser.add_option('-p', '--port', type="int",
help="Port number")
parser.add_option('-j', '--replication-ip', type="string",
help="Replication IP address")
parser.add_option('-q', '--replication-port', type="int",
help="Replication port number")
parser.add_option('-d', '--device', type="string",
help="Device name (e.g. md0, sdb1)")
parser.add_option('-w', '--weight', type="float",
help="Device weight")
parser.add_option('-m', '--meta', type="string", default="",
help="Extra device info (just a string)")
opts, args = parser.parse_args(argvish)
opts, args = parse_args(argvish)
# We'll either parse the all-in-one-string format or the --options format,
# but not both. If both are specified, raise an error.
@ -208,26 +189,13 @@ def _parse_add_values(argvish):
'weight': weight, 'meta': meta})
return parsed_devs
else:
for attribute, shortopt, longopt in (['region', '-r', '--region'],
['zone', '-z', '--zone'],
['ip', '-i', '--ip'],
['port', '-p', '--port'],
['device', '-d', '--device'],
['weight', '-w', '--weight']):
if not getattr(opts, attribute, None):
print 'Required argument %s/%s not specified.' % \
(shortopt, longopt)
print "The on-disk ring builder is unchanged.\n"
exit(EXIT_ERROR)
replication_ip = getattr(opts, 'replication_ip', opts.ip)
replication_port = getattr(opts, 'replication_port', opts.port)
return [{'region': opts.region, 'zone': opts.zone, 'ip': opts.ip,
'port': opts.port, 'device': opts.device, 'meta': opts.meta,
'replication_ip': replication_ip,
'replication_port': replication_port,
'weight': opts.weight}]
try:
dev = build_dev_from_opts(opts)
except ValueError as e:
print e
print "The on-disk ring builder is unchanged.\n"
exit(EXIT_ERROR)
return [dev]
class Commands:

View File

@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from collections import defaultdict
import optparse
def tiers_for_dev(dev):
@ -240,3 +241,52 @@ def parse_search_value(search_value):
raise ValueError('Invalid <search-value>: %s' %
repr(orig_search_value))
return match
def parse_args(argvish):
"""
Build OptionParser and evaluate command line arguments.
"""
parser = optparse.OptionParser()
parser.add_option('-r', '--region', type="int",
help="Region")
parser.add_option('-z', '--zone', type="int",
help="Zone")
parser.add_option('-i', '--ip', type="string",
help="IP address")
parser.add_option('-p', '--port', type="int",
help="Port number")
parser.add_option('-j', '--replication-ip', type="string",
help="Replication IP address")
parser.add_option('-q', '--replication-port', type="int",
help="Replication port number")
parser.add_option('-d', '--device', type="string",
help="Device name (e.g. md0, sdb1)")
parser.add_option('-w', '--weight', type="float",
help="Device weight")
parser.add_option('-m', '--meta', type="string", default="",
help="Extra device info (just a string)")
return parser.parse_args(argvish)
def build_dev_from_opts(opts):
"""
Convert optparse stype options into a device dictionary.
"""
for attribute, shortopt, longopt in (['region', '-r', '--region'],
['zone', '-z', '--zone'],
['ip', '-i', '--ip'],
['port', '-p', '--port'],
['device', '-d', '--device'],
['weight', '-w', '--weight']):
if not getattr(opts, attribute, None):
raise ValueError('Required argument %s/%s not specified.' %
(shortopt, longopt))
replication_ip = opts.replication_ip or opts.ip
replication_port = opts.replication_port or opts.port
return {'region': opts.region, 'zone': opts.zone, 'ip': opts.ip,
'port': opts.port, 'device': opts.device, 'meta': opts.meta,
'replication_ip': replication_ip,
'replication_port': replication_port, 'weight': opts.weight}

View File

@ -16,7 +16,8 @@
import unittest
from swift.common.ring.utils import (build_tier_tree, tiers_for_dev,
parse_search_value)
parse_search_value, parse_args,
build_dev_from_opts)
class TestUtils(unittest.TestCase):
@ -121,6 +122,23 @@ class TestUtils(unittest.TestCase):
self.assertEqual(res, {'meta': 'meta1'})
self.assertRaises(ValueError, parse_search_value, 'OMGPONIES')
def test_replication_defaults(self):
args = '-r 1 -z 1 -i 127.0.0.1 -p 6010 -d d1 -w 100'.split()
opts, _ = parse_args(args)
device = build_dev_from_opts(opts)
expected = {
'device': 'd1',
'ip': '127.0.0.1',
'meta': '',
'port': 6010,
'region': 1,
'replication_ip': '127.0.0.1',
'replication_port': 6010,
'weight': 100.0,
'zone': 1,
}
self.assertEquals(device, expected)
if __name__ == '__main__':
unittest.main()