Created
November 29, 2016 11:36
-
-
Save tehasdf/0c7fb323f8035f636d32fc8c5a2b082f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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