Skip to content

Instantly share code, notes, and snippets.

@tehasdf
Created November 29, 2016 11:36
Show Gist options
  • Save tehasdf/0c7fb323f8035f636d32fc8c5a2b082f to your computer and use it in GitHub Desktop.
Save tehasdf/0c7fb323f8035f636d32fc8c5a2b082f to your computer and use it in GitHub Desktop.
class ClusterHTTPClient(HTTPClient):
def __init__(self, *args, **kwargs):
super(ClusterHTTPClient, self).__init__(*args, **kwargs)
if not profile.cluster:
raise ValueError('Cluster client invoked for an empty cluster!')
self._cluster = list(profile.cluster)
def do_request(self, *args, **kwargs):
for node in profile.cluster:
self._use_node(node)
try:
return super(ClusterHTTPClient, self).do_request(*args,
**kwargs)
except (NotClusterMaster, requests.exceptions.ConnectionError):
continue
raise CloudifyClientError('No active node in the cluster!')
def _use_node(self, node):
if node['manager_ip'] == self.host:
return
self.host = node['manager_ip']
self.port = node.get('rest_port', self.port)
self.protocol = node.get('rest_protocol', self.protocol)
self._update_profile(node)
def _update_profile(self, node):
for attr in CLUSTER_NODE_ATTRS:
if attr in node:
setattr(profile, attr, node[attr])
# set node as the first one in .cluster
profile.cluster.remove(node)
profile.cluster = [node] + profile.cluster
profile.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment