Exemple #1
0
    def get_rest_nodes(self, method, group=None, host=None, ex_myself=False):
        nodes = self.get_nodes(path=path_join(ZK_REST_PATH_NAME, method))
        r_nodes = []
        for n in nodes:
            n_group = re.search("\[.*?\]", n)[0][1:-1]
            n_host = re.search(r'\].*$', n)[0][1:]
            if group:
                if re.match(group, n_group):
                    r_nodes.append(n_host)
            else:
                r_nodes.append(n_host)

        h_nodes = []
        for n in r_nodes:
            if host:
                if re.match(host, n):
                    if ex_myself:
                        if n == f"{self.ip}:{self.port}":
                            continue
                        h_nodes.append(n)
                    else:
                        if ex_myself:
                            if n == f"{self.ip}:{self.port}":
                                continue
                        h_nodes.append(n)
            else:
                if n == f"{self.ip}:{self.port}":
                    continue
                h_nodes.append(n)

        return h_nodes
Exemple #2
0
    def find_master_start(self, sleep_time=1):
        try:
            # Master增加转发Nat
            secret_key = str(uuid.uuid4())
            result = self.get(path_join(self.rest_base_url, PART_API.ADD_NAT),
                              data={
                                  "nat_port": self.nat_port,
                                  "secret_key": secret_key,
                                  "target_addr": self.target_addr
                              })
            if result["is_success"]:
                data_port = result['data']["data_port"]
                secret_key = result['data']["secret_key"]
                master_ip = result['data']["master_ip"]
            else:
                raise Exception("ADD_NAT ERROR" + str(result["msg"]))
            communicate_addr = (master_ip, int(data_port))
            time.sleep(1)
            self.log.info(
                f"communicate_addr:{communicate_addr}-target_addr:{self.target_addr}"
            )
            run_slaver(communicate_addr=communicate_addr,
                       target_addr=split_host(self.target_addr),
                       secret_key=secret_key,
                       max_spare_count=2)
            sleep_time = 1

        except Exception as e:
            self.log.exception(f"连接Master异常{str(e)}")
            time.sleep(sleep_time)
            if sleep_time < 600:
                sleep_time = sleep_time * 2
            self.find_master_start(sleep_time=sleep_time)
Exemple #3
0
 def set(self, path, value, absolute=False):
     value = value.encode("utf-8")
     path = path_join(self.zk_name, path) if not absolute else path
     if not self.zk.exists(path=path):
         self.zk.create(path=path, value=value, makepath=True)
         return
     self.zk.set(path, value)
Exemple #4
0
 def get_nodes(self, path="/") -> list:
     try:
         path = path_join(self.zk_name, path)
         nodes = self.zk.get_children(path)
     except NoNodeError:
         self.log.debug("NoNodeError--->" + path)
         nodes = []
     return nodes
Exemple #5
0
 def decorate(fn):
     # 加上默认路径
     a = '/' + path_join(self.rest_base_url, rest_path)
     if a.startswith("//"):
         a = a[1:]
     if self.services.get(a, None) is None:
         self.services[a] = fn
     return fn
Exemple #6
0
    def register_rest_service(self, services):
        for key in list(services.keys()):
            path = path_join(ZK_REST_PATH_NAME, key)
            self.mkdir(path)
            temp_path = path_join("/", path, f"[{self.group}]{self.ip}:{self.port}")
            http_url = f"""http://{self.ip}:{self.port}/{path.lstrip("/" + ZK_REST_PATH_NAME)}"""
            nat_http_url = f"""http://nat_address:{self.nat_port}/{path.lstrip("/" + ZK_REST_PATH_NAME)}"""
            self.setTemp(temp_path, yaml.dump({
                "PID": self.pid,
                "IP": self.ip,
                "Rest Port": self.port,
                "NAT Rest Port": self.nat_port,
                "Rest URL": http_url,
                "NAT Rest URL": nat_http_url
            }), pass_error=True)

        self.set(ZK_REST_PATH_NAME, date_to_str())
Exemple #7
0
 def config_watch(self, event):
     if event.type in ("CREATED", "CHANGED"):
         if event.path == path_join("/", self.zk.zk_name, self.config_path):
             self.load_config()
         else:
             txt = self.zk.get(event.path,
                               watch=self.config_watch,
                               absolute=True)
             self.load_node(os.path.basename(event.path), txt)
Exemple #8
0
    def get(self, path, watch=None, default=None, absolute=False):
        path = path_join(self.zk_name, path) if not absolute else path
        if not self.zk.exists(path=path):
            return default
        if watch:
            data = self.zk.get(path=path, watch=watch)
        else:
            data = self.zk.get(path=path)

        return str(data[0], encoding="utf-8")
Exemple #9
0
 def setTemp(self, path, value: str, pass_error=False):
     """设置临时节点"""
     try:
         value = value.encode("utf-8")
         path = path_join(self.root, self.zk_name, path)
         if not self.zk.exists(path=path):
             self.zk.create(path=path,
                            value=value,
                            ephemeral=True,
                            makepath=True)
             return True
         else:
             if not pass_error:
                 return False
             self.zk.set(path=path, value=value)
             return True
     except Exception as e:
         if pass_error:
             return False
         else:
             raise e
Exemple #10
0
 def exists(self, path, absolute=False):
     path = path_join(self.zk_name, path) if not absolute else path
     return self.zk.exists(path=path)
Exemple #11
0
 def delete(self, path):
     path = path_join(self.zk_name, path)
     if self.zk.exists(path=path):
         self.zk.delete(path)