70 lines
3.1 KiB
Plaintext
70 lines
3.1 KiB
Plaintext
ns = params_kw.copy()
|
|
for k,v in list(ns.items()):
|
|
if v == 'NaN' or v == 'null': ns[k] = None
|
|
if k.endswith('_text'): ns.pop(k, None)
|
|
|
|
id = params_kw.get('id','')
|
|
if not id or len(str(id)) > 32: id = uuid()
|
|
ns['id'] = id
|
|
|
|
userorgid = await get_userorgid()
|
|
if not userorgid:
|
|
return {'widgettype':'Error','options':{'title':'Authorization Error','timeout':3,'cwidth':16,'cheight':9,'message':'Please login'}}
|
|
|
|
pool_id = ns.get('pool_id','')
|
|
control_node_id = ns.get('control_node_id','')
|
|
storage_node_id = ns.get('storage_node_id','')
|
|
compute_node_id = ns.get('compute_node_id','')
|
|
|
|
if not control_node_id:
|
|
return {'widgettype':'Error','options':{'title':'Error','message':'必须选择控制节点','cwidth':16,'cheight':9,'timeout':3}}
|
|
if not storage_node_id:
|
|
return {'widgettype':'Error','options':{'title':'Error','message':'必须选择存储节点','cwidth':16,'cheight':9,'timeout':3}}
|
|
|
|
ns['resellerid'] = userorgid
|
|
ns['created_at'] = datetime.datetime.now().isoformat()
|
|
ns['updated_at'] = ns['created_at']
|
|
|
|
# 从 ns 移除节点选择字段(非 cluster 表字段)
|
|
for k in ('control_node_id','storage_node_id','compute_node_id'):
|
|
ns.pop(k, None)
|
|
|
|
async with DBPools().sqlorContext(get_module_dbname('pcc')) as sor:
|
|
# 校验节点可用性
|
|
async def check_node(nid, pool_id):
|
|
if not nid:
|
|
return None
|
|
existing = await sor.R('cluster_node', {'node_id': nid})
|
|
if existing:
|
|
return '节点 ' + nid + ' 已被其他集群占用'
|
|
if pool_id:
|
|
node = await sor.R('compute_node', {'id': nid})
|
|
if node and getattr(node[0],'pool_id','') != pool_id:
|
|
return '节点 ' + nid + ' 不属于所选算力池'
|
|
return None
|
|
|
|
err = await check_node(control_node_id, pool_id)
|
|
if err: return {'widgettype':'Error','options':{'title':'Error','message':err,'cwidth':16,'cheight':9,'timeout':3}}
|
|
err = await check_node(storage_node_id, pool_id)
|
|
if err: return {'widgettype':'Error','options':{'title':'Error','message':err,'cwidth':16,'cheight':9,'timeout':3}}
|
|
err = await check_node(compute_node_id, pool_id)
|
|
if err: return {'widgettype':'Error','options':{'title':'Error','message':err,'cwidth':16,'cheight':9,'timeout':3}}
|
|
|
|
# 创建集群
|
|
await sor.C('cluster', ns)
|
|
|
|
# 自动分配节点
|
|
now = datetime.datetime.now().isoformat()
|
|
assignments = []
|
|
if control_node_id:
|
|
assignments.append({'id': uuid(), 'cluster_id': id, 'node_id': control_node_id, 'role': 'control', 'status': 'joining', 'assigned_at': now})
|
|
if storage_node_id:
|
|
assignments.append({'id': uuid(), 'cluster_id': id, 'node_id': storage_node_id, 'role': 'storage', 'status': 'joining', 'assigned_at': now})
|
|
if compute_node_id:
|
|
assignments.append({'id': uuid(), 'cluster_id': id, 'node_id': compute_node_id, 'role': 'compute', 'status': 'joining', 'assigned_at': now})
|
|
|
|
for a in assignments:
|
|
await sor.C('cluster_node', a)
|
|
|
|
return {'widgettype':'Message','options':{'cwidth':16,'cheight':9,'title':'Success','timeout':3,'message':'集群已创建,已分配 ' + str(len(assignments)) + ' 个节点'}}
|