TenantDrive/utils/tools.py

37 lines
922 B
Python

import requests
import time
import re
s = requests.Session()
def get_cnb_weburl(port):
"""
获取CNB项目的Web URL
参数:
port: 应用运行端口
返回:
str: 外部可访问的URL
"""
# 设置认证头
s.headers = {"Authorization": "2hk178fffIx8tdXLD9YjYJot0gA"}
# 获取工作区列表
res = s.get("https://api.cnb.cool/workspace/list?branch=main&status=running").json()
info = res["list"][0]
# 获取工作区详细信息
urlinfo = s.get(f"https://api.cnb.cool/{info['slug']}/-/workspace/detail/{info['sn']}").json()
print(urlinfo)
# 使用正则表达式提取cnb-id
pattern = r'cnb-[a-z0-9]+-[a-z0-9]+-\d+'
match = re.search(pattern, urlinfo["webide"])
if match:
cnb_id = match.group(0)
else:
cnb_id = None
# 返回格式化的URL
return f"https://{cnb_id}-{port}.cnb.run/"