From c58253a0a05e6209ab82232ebdd151c771389238 Mon Sep 17 00:00:00 2001 From: Mich Date: Tue, 4 Nov 2014 21:07:12 -0500 Subject: [PATCH] Functions to fix nodes with same alias --- starcluster/cluster.py | 35 +++++++++++++++++++++++++++++++++++ starcluster/node.py | 16 ++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/starcluster/cluster.py b/starcluster/cluster.py index bbbaa127c..c95de9eb6 100644 --- a/starcluster/cluster.py +++ b/starcluster/cluster.py @@ -25,6 +25,8 @@ import iptools +from collections import Counter + from starcluster import utils from starcluster import static from starcluster import sshutils @@ -1739,6 +1741,39 @@ def ssh_to_node(self, alias, user='root', command=None, forward_x11=False, pseudo_tty=pseudo_tty, command=command) + def _get_duplicate_aliases(self): + aliases = [_n.alias for _n in self.nodes] + tmp_aliases = Counter(aliases) + return filter(lambda k: tmp_aliases[k] > 1, tmp_aliases) + + def _recover_duplicate_aliases(self): + aliases = self._get_duplicate_aliases() + if not aliases: + return + + log.error("Nodes with same alias detected! {}".format(aliases)) + etc_hosts = self._master.ssh.get_remote_file_lines('/etc/hosts') + for alias in aliases: + ip = None + for line in etc_hosts: + if re.findall(alias, line): + ip, _ = line.split(" ", 1) + log.debug("Found ip: " + ip) + break + else: + log.debug("No ip found") + for node in self.nodes: + if node.alias != alias: + continue + if node.private_ip_address != ip: + new_alias = self._make_alias(self._get_next_node_num()) + log.info("Renaming {} from {} to {}" + .format(node, node.alias, new_alias)) + node.rename(new_alias) + # force a memory refresh + self._nodes = [] + self.nodes + class ClusterValidator(validators.Validator): diff --git a/starcluster/node.py b/starcluster/node.py index a7e9306c9..cb8454b90 100644 --- a/starcluster/node.py +++ b/starcluster/node.py @@ -1190,3 +1190,19 @@ def package_install(self, pkgs): def __del__(self): if self._ssh: self._ssh.close() + + def rename(self, new_name): + """ + Used to reset the name and alias when there is a conflict + """ + self.remove_tag("alias") + self.remove_tag("Name") + self.add_tag("Name", new_name) + self.add_tag("alias", new_name) + while True: + tags = self.tags + if tags.get("Name", "") == new_name \ + and tags.get("alias", "") == new_name: + break + log.info("Waiting for new name to propagate") + time.sleep(5)