Skip to content

Commit

Permalink
修复部署显示问题
Browse files Browse the repository at this point in the history
  • Loading branch information
data-infra committed Aug 9, 2024
1 parent 506877d commit c42f326
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 17 deletions.
5 changes: 4 additions & 1 deletion myapp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@

# 在这个文件里面只创建app,不要做view层面的事情。
APP_DIR = os.path.dirname(__file__)
CONFIG_MODULE = os.environ.get("MYAPP_CONFIG", "myapp.config")


# app = Flask(__name__,static_url_path='/static',static_folder='static',template_folder='templates')
app = Flask(__name__) # ,static_folder='/mnt',static_url_path='/mnt'
app.json.sort_keys=False # 返回字典乱序问题
app.json.ensure_ascii = False # 返回 中文乱码问题

CONFIG_MODULE = os.environ.get("MYAPP_CONFIG", "myapp.config")
app.config.from_object(CONFIG_MODULE)
conf = app.config

Expand Down
1 change: 0 additions & 1 deletion myapp/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ class MyappModelBase():
"deploy_time": _("部署时间"),
"host": _("域名"),
"host_url": _("域名"),
"inference_host_url": _("域名:测试(test.xx)/调试(debug.xx)"),
"deploy": _("部署"),
"test_deploy": _("测试部署"),
"prod_deploy": _("生产部署"),
Expand Down
2 changes: 2 additions & 0 deletions myapp/models/model_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def clone(self):
session_num=self.session_num,
chat_type=self.chat_type,
hello=self.hello,
tips=self.tips,
knowledge=self.knowledge,
prompt=self.prompt,
service_type=self.service_type,
service_config=self.service_config,
owner=self.owner,
Expand Down
1 change: 0 additions & 1 deletion myapp/utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1806,7 +1806,6 @@ def hive_create_sql_demo():
return sql



import subprocess


Expand Down
18 changes: 18 additions & 0 deletions myapp/utils/py/py_k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ def get_running_pods(self, namespace=None):
pod = self.v1.read_namespaced_pod(name=pod_name_temp, namespace=namespace)
all_pods.append(pod)

def exist_hold_resource(self,pod):
try:
exist_hold_resource=False
if pod['status'] == 'Running' or pod['status_more'].get('phase', '') == 'Running':
exist_hold_resource = True
if pod['status'] == 'Pending':
for condition in pod['status_more']['conditions']:
if condition['type'] == 'PodScheduled' and str(condition['status']).lower() == 'true':
exist_hold_resource = True
break
except Exception as e:
print(e)
exist_hold_resource=True

return exist_hold_resource

# @pysnooper.snoop()
def get_pods(self, namespace=None, service_name=None, pod_name=None, labels={},status=None):
# print(namespace)
Expand Down Expand Up @@ -106,6 +122,8 @@ def get_pods(self, namespace=None, service_name=None, pod_name=None, labels={},s
# print(pod)
metadata = pod.metadata
status = pod.status.phase if pod and hasattr(pod, 'status') and hasattr(pod.status, 'phase') else ''
# Pending Running Succeeded Failed Unknown
# 辅助状态 ContainerCreating CrashLoopBackOff ImagePullBackOff ErrImagePull Terminating
# 如果是running 也分为重启运行中
if status.lower()=='running':
status = 'Running' if [x.status for x in pod.status.conditions if x.type == 'Ready' and x.status == 'True'] else 'CrashLoopBackOff'
Expand Down
30 changes: 23 additions & 7 deletions myapp/views/baseApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,7 @@ def api_edit(self, pk):
if isinstance(item, dict):
return self.response_error(422, message=item.errors)
self.pre_update(item)

import traceback
try:
self.datamodel.edit(item, raise_exception=True)
if self.post_update:
Expand All @@ -1366,8 +1366,12 @@ def api_edit(self, pk):
**back_data,
)
except IntegrityError as e:
traceback.print_exc()
return self.response_error(422, message=str(e.orig))

except Exception as e:
print("An error occurred:", e)
traceback.print_exc() # 打印完整的错误堆栈跟踪
return self.response_error(500, message="An unexpected error occurred.")
@event_logger.log_this
@expose("/<int:pk>", methods=["DELETE"])
# @pysnooper.snoop()
Expand All @@ -1381,10 +1385,9 @@ def api_delete(self, pk):
if not has_permission:
flash('no permission to delete',category='warning')
return self.response_error(422, message='no permission to delete')

if self.pre_delete:
self.pre_delete(item)
try:
if self.pre_delete:
self.pre_delete(item)
self.datamodel.delete(item, raise_exception=True)
self.post_delete(item)
back_data = {
Expand All @@ -1395,7 +1398,13 @@ def api_delete(self, pk):
return self.response(200, **back_data)
except IntegrityError as e:
return self.response_error(422, message=str(e.orig))

except Exception as e:
back = {
"status": -1,
"message": str(e),
"result": {}
}
return self.response(500, **back)
@event_logger.log_this
@expose("/action/<string:name>/<int:pk>", methods=["GET"])
def single_action(self, name, pk):
Expand Down Expand Up @@ -1860,11 +1869,18 @@ def make_ui_info(self, ret_src):
if choices:
values = []
for choice in choices:
if choice and len(choice) == 2:
if choice and type(choice)==list and (len(choice) == 1 or len(choice) == 2):
choice = choice if len(choice)==2 else [choice,choice]
values.append({
"id": choice[0],
"value": choice[1]
})
# 多级选择
if choice and type(choice)==dict:
values.append(choice)
ret['ui-type']='cascader'
ret['type'] = 'cascader'

ret['values'] = values
if not ret.get('ui-type', ''):
ret['ui-type'] = 'select2' if 'SelectMultiple' in ret['type'] else 'select'
Expand Down
4 changes: 2 additions & 2 deletions myapp/views/baseFormApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,7 @@ def echart(self):

return jsonify({})



@event_logger.log_this
@expose("/action/<string:name>/<int:pk>", methods=["GET"])
def single_action(self, name, pk):
"""
Expand All @@ -339,6 +338,7 @@ def single_action(self, name, pk):
}
return self.response(200, **back)

@event_logger.log_this
@expose("/multi_action/<string:name>", methods=["POST"])
def multi_action(self, name):
"""
Expand Down
2 changes: 1 addition & 1 deletion myapp/views/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def menu(self):
"children": [
{
"name": 'pod',
"title": __('计量计费'),
"title": __('计量明细'),
"icon": '<svg t="1704356401904" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4270" width="200" height="200"><path d="M512 61.866667C262.4 61.866667 61.866667 262.4 61.866667 512c0 249.6 202.666667 450.133333 450.133333 450.133333 249.6 0 450.133333-202.666667 450.133333-450.133333 0-249.6-200.533333-450.133333-450.133333-450.133333z m0 814.933333c-200.533333 0-364.8-164.266667-364.8-364.8 0-200.533333 164.266667-364.8 364.8-364.8S876.8 311.466667 876.8 512c0 200.533333-164.266667 364.8-364.8 364.8z" p-id="4271"></path><path d="M680.533333 492.8c23.466667 0 42.666667-19.2 42.666667-42.666667s-19.2-42.666667-42.666667-42.666666h-106.666666L661.333333 320c17.066667-17.066667 17.066667-42.666667 0-59.733333-17.066667-17.066667-42.666667-17.066667-59.733333 0l-91.733333 91.733333-91.733334-91.733333c-17.066667-17.066667-42.666667-17.066667-59.733333 0-17.066667 17.066667-17.066667 42.666667 0 59.733333l87.466667 87.466667h-102.4c-23.466667 0-42.666667 19.2-42.666667 42.666666s19.2 42.666667 42.666667 42.666667H469.333333v46.933333h-125.866666c-23.466667 0-42.666667 19.2-42.666667 42.666667s19.2 42.666667 42.666667 42.666667H469.333333v106.666666c0 23.466667 19.2 42.666667 42.666667 42.666667s42.666667-19.2 42.666667-42.666667v-106.666666h125.866666c23.466667 0 42.666667-19.2 42.666667-42.666667s-19.2-42.666667-42.666667-42.666667H554.666667v-46.933333h125.866666z" p-id="4272"></path></svg>',
"menu_type": "api",
"url": "/pod_modelview/api/",
Expand Down
2 changes: 1 addition & 1 deletion myapp/views/view_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ class Chat_View_Base():
description= _('svg格式图标,图标宽高设置为50*50,<a target="_blank" href="https://www.iconfont.cn/">iconfont</a>'),
widget=BS3TextFieldWidget(),
# choices=[[str(x),Markup(icon_choices[x])] for x in range(len(icon_choices))],
validators=[DataRequired()]
validators=[DataRequired(),Regexp("^<svg.*")]
),
"owner": StringField(
label= _('责任人'),
Expand Down
2 changes: 1 addition & 1 deletion myapp/views/view_inferenceserving.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class InferenceService_ModelView_base():
images += item
service_type_choices = ['serving', 'tfserving', 'torch-server', 'onnxruntime', 'triton-server', 'ml-server(企业版)','llm-server(企业版)',]
spec_label_columns = {
# "host": __("域名:测试环境test.xx,调试环境 debug.xx"),
"inference_host_url": _("域名:需要泛域名支持,测试(test.xx)/调试(debug.xx)"),
}
service_type_choices = [x.replace('_','-') for x in service_type_choices]
host_rule=",<br>".join([cluster+"cluster:*."+conf.get('CLUSTERS')[cluster].get("SERVICE_DOMAIN",conf.get('SERVICE_DOMAIN','')) for cluster in conf.get('CLUSTERS') if conf.get('CLUSTERS')[cluster].get("SERVICE_DOMAIN",conf.get('SERVICE_DOMAIN',''))])
Expand Down
2 changes: 1 addition & 1 deletion myapp/views/view_serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Service_ModelView_base():
"name":StringField(_('名称'), description= _('英文名(小写字母、数字、- 组成),最长50个字符'),widget=BS3TextFieldWidget(), validators=[DataRequired(),Regexp("^[a-z][a-z0-9\-]*[a-z0-9]$"),Length(1,54)]),
"label":StringField(_('标签'), description= _('中文名'), widget=BS3TextFieldWidget(),validators=[DataRequired()]),
"images": StringField(_('镜像'), description= _('镜像全称'), widget=BS3TextFieldWidget(), validators=[DataRequired()]),
"volume_mount":StringField(_('挂载'),description= _('外部挂载,格式:$pvc_name1(pvc):/$container_path1,$hostpath1(hostpath):/$container_path2,4G(memory):/dev/shm,注意pvc会自动挂载对应目录下的个人rtx子目录'),widget=BS3TextFieldWidget(),default=''),
"volume_mount":StringField(_('挂载'),description= _('外部挂载,格式:$pvc_name1(pvc):/$container_path1,$hostpath1(hostpath):/$container_path2,4G(memory):/dev/shm,注意pvc会自动挂载对应目录下的个人username子目录'),widget=BS3TextFieldWidget(),default=''),
"working_dir": StringField(_('工作目录'),description= _('工作目录,容器启动的初始所在目录,不填默认使用Dockerfile内定义的工作目录'),widget=BS3TextFieldWidget()),
"command":StringField(_('启动命令'), description= _('启动命令,支持多行命令'),widget=MyBS3TextAreaFieldWidget(rows=3)),
"node_selector":StringField(_('机器选择'), description= _('运行当前服务所在的机器'),widget=BS3TextFieldWidget(),default='cpu=true,serving=true'),
Expand Down
2 changes: 1 addition & 1 deletion myapp/views/view_total_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def pod_resource():
org = pod['node_selector'].get("org", 'public')
if org not in all_tasks_json[cluster_name][namespace]:
all_tasks_json[cluster_name][namespace][org] = {}
if pod['status'] == 'Running' or pod['status_more'].get('phase','')=='Running':
if k8s_client.exist_hold_resource(pod):
user = pod['labels'].get('user', pod['labels'].get('username', pod['labels'].get('run-rtx',pod['labels'].get('rtx-user','admin'))))
if user:
request_gpu = 0
Expand Down

0 comments on commit c42f326

Please sign in to comment.