def get_node_status(self): config_api = ConfigAPI() root_path = config_api.get_consul_nodes_path() node_status = JoiningStatus.not_joined if os.path.exists(config_api.get_replace_file_path()): return node_status try: # Config try: config = configuration() node_name = config.get_node_info().name except Exception as config_exc: logger.exception( "Config file error. The PetaSAN os maybe just installed.") return node_status try: cluster_info = config.get_cluster_info().management_nodes mgmt_nodes_count = cluster_info.__len__() if mgmt_nodes_count < 3: raise Exception( "Cluster is not completed, PetaSAN will check node join status." ) consul_base = BaseAPI() data = consul_base.read_value(root_path + node_name) if data is not None and configuration( ).are_all_mgt_nodes_in_cluster_config(): return JoiningStatus.node_joined else: return JoiningStatus.not_joined except Exception as exc: cluster_info = config.get_cluster_info().management_nodes mgmt_nodes_count = cluster_info.__len__() logger.exception(exc.message) if mgmt_nodes_count < 3 and not config.is_node_in_cluster_config( ): return JoiningStatus.not_joined elif mgmt_nodes_count is 3 and config.is_node_in_cluster_config( ): return JoiningStatus.node_joined if mgmt_nodes_count is 1: return JoiningStatus.one_node_exists elif mgmt_nodes_count is 2: return JoiningStatus.two_node_exist except Exception as e: return JoiningStatus.not_joined return node_status
def replace(self, ip, password): config = configuration() ssh_obj = ssh() config_api = ConfigAPI() logger.info("Starting replace.") if os.path.exists(config_api.get_cluster_info_file_path()): os.remove(config_api.get_cluster_info_file_path()) if ssh_obj.copy_public_key_from_host(ip, password): logger.info("Successfully copied public keys.") if ssh_obj.copy_private_key_from_host(ip, password): ssh_obj.create_authorized_key_file() logger.info("Successfully copied private keys.") else: raise SSHKeyException("Error copying keys") out, err = ssh_obj.exec_command( ip, "python {}".format(config_api.get_cluster_status_for_join_path())) out = int(out) if out == -1: raise ReplaceException("core_deploy_replace_mon_not_healthy_err") elif out == 0: raise ReplaceException( "core_deploy_replace_cluster_in_progress_err") elif out == 1: raise ReplaceException( "core_deploy_replace_two_management_node_down_err") elif out == 3: raise ReplaceException("core_deploy_replace_cluster_running_err") if not os.listdir( os.path.dirname(config_api.get_cluster_info_file_path())): os.makedirs( os.path.dirname(config_api.get_cluster_info_file_path())) logger.info("Starting to copy config file") if not ssh_obj.copy_file_from_host( ip, config_api.get_cluster_info_file_path()): raise Exception("Error copying config file") logger.info("Successfully copied config file.") cluster_name = config.get_cluster_name(True) logger.info("Successfully joined to cluster {}".format(cluster_name)) wrong_name = True wrong_ip = True for node_info in config.get_management_nodes_config(): if node_info.name == config.get_node_name( ) or node_info.management_ip == Network().get_node_management_ip(): if node_info.name == config.get_node_name(): wrong_name = False if node_info.management_ip == Network().get_node_management_ip( ): wrong_ip = False if not wrong_name and not wrong_ip: config.set_node_info(node_info, True) open(config_api.get_replace_file_path(), 'w+').close() break if wrong_name and wrong_ip: os.remove(config_api.get_cluster_info_file_path()) raise ReplaceException("core_deploy_replace_node_do_not_match_err") elif wrong_name: os.remove(config_api.get_cluster_info_file_path()) raise ReplaceException( "core_deploy_replace_node_do_not_match_name_err") elif wrong_ip: os.remove(config_api.get_cluster_info_file_path()) raise ReplaceException( "core_deploy_replace_node_do_not_match_ip_err") config.set_password(password) logger.info("password set successfully.") self.__copy_current_tunings(ip) return cluster_name