Adjust swift_rings.py to work on specified regions

This patch allows the swift_ring.py to only adjust/add/remove nodes from
a specified region, leaving the other regions that are already in the
ring unmodified.

This will allow multi-region swift to be managed by separate locations
each managing their own region's nodes and leaving other regions to
handle their own nodes.

The default is to manage all regions, so not specifying a region will
work the same as it does now and the script's functionality remains
unchanged.

Change-Id: I1cf73be20f27c437450c0181bb247c86e0f46bc6
Partially-Implements: blueprint multi-region-swift
This commit is contained in:
Andy McCrae 2015-07-09 15:12:41 +01:00 committed by Steve Lewis
parent 94e399e351
commit c927f4205e

View File

@ -24,7 +24,7 @@ import sys
import threading
USAGE = "usage: %prog -f <swift_ring.contents>"
USAGE = "usage: %prog -f <swift_ring.contents> -r <managed_region>"
DEVICE_KEY = "%(ip)s:%(port)d/%(device)s"
@ -159,7 +159,7 @@ def get_build_file_data(build_file):
def build_ring(build_name, repl, min_part_hours, part_power, hosts,
validate=False):
region=None, validate=False):
# Create the build file
build_file = "%s.builder" % build_name
build_file_data = get_build_file_data(build_file)
@ -179,16 +179,18 @@ def build_ring(build_name, repl, min_part_hours, part_power, hosts,
if update:
for i, dev in enumerate(build_file_data['devs']):
if dev is not None:
old_hosts[DEVICE_KEY % dev] = i
if region is None or int(region) == int(dev['region']):
old_hosts[DEVICE_KEY % dev] = i
for host in hosts:
host_key = DEVICE_KEY % host
if host_key in old_hosts:
old_host = build_file_data['devs'][old_hosts[host_key]]
update_host_in_ring(build_file, host, old_host,
validate=validate)
old_hosts.pop(host_key)
else:
add_host_to_ring(build_file, host, validate=validate)
if region is None or int(region) == int(host['region']):
if host_key in old_hosts:
old_host = build_file_data['devs'][old_hosts[host_key]]
update_host_in_ring(build_file, host, old_host,
validate=validate)
old_hosts.pop(host_key)
else:
add_host_to_ring(build_file, host, validate=validate)
if old_hosts and not validate:
# There are still old hosts, these hosts must've been removed
@ -207,7 +209,7 @@ def build_ring(build_name, repl, min_part_hours, part_power, hosts,
)
def main(setup):
def main(setup, region):
# load the json file
try:
with open(setup) as json_stream:
@ -217,7 +219,7 @@ def main(setup):
return 1
hosts = _contents_file['drives']
kargs = {'validate': True, 'hosts': hosts}
kargs = {'validate': True, 'hosts': hosts, 'region': region}
ring_call = [
_contents_file['builder_file'],
_contents_file['repl_number'],
@ -245,6 +247,14 @@ if __name__ == "__main__":
help="Specify the swift ring contents file.",
metavar="FILE"
)
parser.add_option(
"-r",
"--region",
help="Specify the region to manage for the ring file.",
dest="region",
type='int',
metavar="REGION"
)
options, _args = parser.parse_args(sys.argv[1:])
if options.setup and not exists(options.setup):
@ -252,4 +262,4 @@ if __name__ == "__main__":
parser.print_help()
sys.exit(1)
sys.exit(main(options.setup))
sys.exit(main(options.setup, options.region))