149 lines
5.2 KiB
Plaintext
149 lines
5.2 KiB
Plaintext
async def new_cluster(params_kw={}):
|
||
# params_kw:
|
||
# cpcid
|
||
# cluster_type
|
||
# ctl_nodeid
|
||
# cluster_name
|
||
debug(f'{params_kw=}')
|
||
dbname = get_module_dbname('cpcc')
|
||
dbname = 'kboss'
|
||
db = DBPools()
|
||
cpcid = params_kw.cpcid
|
||
nodeid = params_kw.ctl_nodeid
|
||
cluster_type = params_kw.cluster_type
|
||
cluster_name = params_kw.cluster_name
|
||
enable_date = params_kw.enable_date
|
||
export_date = params_kw.export_date
|
||
# debug(f"=====> {cluster_type=} {cluster_name=} {nodeid=} {cpcid=}")
|
||
|
||
async with db.sqlorContext(dbname) as sor:
|
||
cpcs = await sor.R('cpclist', {'id':cpcid})
|
||
if len(cpcs) < 1:
|
||
e = Exception(f'cpclist {cpcid=} not exists')
|
||
exception(f'{e}')
|
||
raise e
|
||
cpc = cpcs[0]
|
||
nodes = await sor.R('cpcnode', {'id':nodeid})
|
||
# 这里有问题:没有匹配到节点信息!!!
|
||
if len(nodes) < 1:
|
||
e = Exception(f'cpcnode {nodeid=} 节点基础信息不匹配')
|
||
exception(f'{e}')
|
||
raise e
|
||
node = nodes[0]
|
||
url = cpc.pcapi_url + "/pcapi/api/v1/cluster/common/new_cluster"
|
||
debug(f"请求url: {url=}")
|
||
debug(f"目标IP认证信息: {node.ip=} {node.sshport=} {node.adminuser=} {node.adminpwd=}")
|
||
# 请求方式待定,取决于获取参数值方式
|
||
debug(333)
|
||
headers = basic_auth_headers('ysh', 'Kyy@123456')
|
||
debug(22)
|
||
hc = HttpClient()
|
||
print(444)
|
||
import requests
|
||
params = {
|
||
'cluster_type': cluster_type,
|
||
'host':node.ip,
|
||
'port':node.sshport,
|
||
'user':node.adminuser,
|
||
'password':node.adminpwd,
|
||
'role':"master"
|
||
}
|
||
debug(f'{params=}')
|
||
debug(1111111)
|
||
#resp = await hc.request(url, method='POST',
|
||
# headers = headers,
|
||
# data=params
|
||
#)
|
||
# 框架不支持超时时间
|
||
resp = requests.post(url,
|
||
headers = headers,
|
||
data=params,
|
||
timeout=500,
|
||
verify=False
|
||
)
|
||
resp = json.dumps(resp.json()) #这里模拟hc.request返回的结果写后续逻辑
|
||
debug(f'{type(resp)=}->{resp=}')
|
||
debug(f"pcapi返回值: {json.loads(resp)=}")
|
||
2datas = json.loads(resp).get('data')
|
||
2datas = 2datas.split("\n")
|
||
clusterjoin = 2datas[0]
|
||
kubeconfig_context = 2datas[1]
|
||
if json.loads(resp).get("status") == True:
|
||
# new cluster info write to database
|
||
# update node_status to '1' cpcnode record identify by ctl_nodeid ctl_node
|
||
clusterid = uuid()
|
||
ns = {
|
||
'id':clusterid,
|
||
'clustertype':cluster_type,
|
||
'cpcid':cpcid,
|
||
'controllerid':nodeid,
|
||
'name':cluster_name,
|
||
'enable_date':enable_date,
|
||
'export_date':export_date,
|
||
'clusterjoin':clusterjoin,
|
||
'kubeconfig':kubeconfig_context
|
||
}
|
||
debug(f"新集群元数据: {ns=}")
|
||
await sor.C('cpccluster', ns)
|
||
node_ns = {
|
||
'id':nodeid,
|
||
'node_status':'1',
|
||
'clusterid':clusterid,
|
||
'cpcid':cpcid,
|
||
'role':'master'
|
||
}
|
||
debug(f"更新控制节点元数据 {node_ns=}")
|
||
await sor.U('cpcnode', node_ns)
|
||
debug(f"###集群新增数据盘部件库存(目前采用控制节点上提供NFS共享存储服务)")
|
||
# 1.选定控制节点disk中SYS和DATA子类型中较大的值作为集群数据盘存储的库存量
|
||
|
||
# -------------------- 构建参数化SQL(带库存非负检查)--------------------
|
||
update_data = [
|
||
('adsfadsfa', 'A', 'disk', 'AA', 'SYS', 5),
|
||
('fdgdsgfd', 'A', 'cpu', 'AA', 'INTER', 10)
|
||
]
|
||
case_clauses = []
|
||
params = []
|
||
|
||
for info in update_data:
|
||
id_val, cpcid_val, type_val, clusterid_val, model_val, delta = info
|
||
case_clauses.append(
|
||
"WHEN id = %s AND cpcid = %s AND type = %s AND clusterid = %s AND model = %s "
|
||
"THEN GREATEST(stock + %s, 0)" # 确保库存非负
|
||
)
|
||
params.extend([id_val, cpcid_val, type_val, clusterid_val, model_val, delta])
|
||
|
||
sql = """
|
||
UPDATE cpcwidget
|
||
SET stock = CASE
|
||
{case_clauses}
|
||
ELSE stock
|
||
END
|
||
""".format(case_clauses=' '.join(case_clauses))
|
||
|
||
# 执行更新
|
||
rows_affected = cursor.execute(sql, params)
|
||
|
||
|
||
# 第二种写法
|
||
update cpcwidget
|
||
set stock = case
|
||
when id = 'adsfadsfa' and cpcid = 'A' and type = 'disk' and clusterid = 'AA' and model = 'SYS'
|
||
then greatest(stock + 5, 0) -- 增加 5,但若结果为负则设为 0
|
||
when id = 'fdgdsgfd' and cpcid = 'A' and type = 'cpu' and clusterid = 'AA' and model = 'INTER'
|
||
then greatest(stock + 10, 0) -- 增加 10,但若结果为负则设为 0
|
||
else stock
|
||
end;
|
||
await sor.sqlExe(bsql, {})
|
||
# -------------------------- 库存操作结束 ---------------------------
|
||
|
||
return {'status': True, 'msg': f'操作成功!\n加入集群凭证: {clusterjoin}', 'data': node_ns}
|
||
else:
|
||
return {'status': False, 'msg': '算力中心服务操作失败'}
|
||
|
||
return {'status': False, 'msg': '其他错误'}
|
||
|
||
ret = await new_cluster(params_kw)
|
||
return ret
|
||
|