def swap_master_and_slave(instance, dry_run):
""" Swap a master and slave in zk. Warning: this does not sanity checks
and does nothing more than update zk. YOU HAVE BEEN WARNED!
Args:
instance - An instance in the replica set. This function will figure
everything else out.
dry_run - If set, do not modify configuration.
"""
zk_local = MysqlZookeeper()
kazoo_client = get_kazoo_client()
if not kazoo_client:
raise Exception('Could not get a zk connection')
log.info('Instance is {inst}'.format(inst=instance))
(replica_set, version) = zk_local.get_replica_set_from_instance(instance)
log.info('Detected replica_set as '
'{replica_set}'.format(replica_set=replica_set))
(zk_node, parsed_data,
version) = get_zk_node_for_replica_set(kazoo_client, replica_set)
log.info('Replica set {replica_set} is held in zk_node '
'{zk_node}'.format(
zk_node=zk_node, replica_set=replica_set))
log.info('Existing config:')
log.info(pprint.pformat(remove_auth(parsed_data[replica_set])))
new_data = copy.deepcopy(parsed_data)
new_data[replica_set][REPLICA_ROLE_MASTER] = \
parsed_data[replica_set][REPLICA_ROLE_SLAVE]
new_data[replica_set][REPLICA_ROLE_SLAVE] = \
parsed_data[replica_set][REPLICA_ROLE_MASTER]
log.info('New config:')
log.info(pprint.pformat(remove_auth(new_data[replica_set])))
if new_data == parsed_data:
raise Exception('No change would be made to zk, '
'will not write new config')
elif dry_run:
log.info('dry_run is set, therefore not modifying zk')
else:
log.info('Pushing new configuration for '
'{replica_set}:'.format(replica_set=replica_set))
kazoo_client.set(zk_node, simplejson.dumps(new_data), version)
评论列表
文章目录