programme.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import re
  2. import time
  3. from typing import List
  4. from app import models, schemas
  5. from sqlalchemy.orm import Session
  6. import app.utils.send_util as send_util
  7. from configs.globals import g
  8. from configs.settings import DefaultOption, config
  9. special_project_id = config.get('PERMISSIONS', 'special_project_id')
  10. namespace = config.get('PROGRAMME', 'namespace')
  11. super_image = config.get('PROGRAMME', 'super_image')
  12. ordinary_image = config.get('PROGRAMME', 'ordinary_image')
  13. tag = config.get('PROGRAMME', 'tag')
  14. host = config.get('PROGRAMME', 'host')
  15. chart = config.get('PROGRAMME', 'chart')
  16. path_type = config.get('PROGRAMME', 'path_type')
  17. cpu = config.get('PROGRAMME', 'cpu', vars=DefaultOption(config, 'PROGRAMME', CPU = 2000))
  18. memory = config.get('PROGRAMME', 'memory', vars=DefaultOption(config, 'PROGRAMME', MEMORY = 8192))
  19. ingress_class = config.get('PROGRAMME', 'ingress_class', vars=DefaultOption(config, 'PROGRAMME', INGRESS_CLASS = None))
  20. image_pull_secret = config.get('PROGRAMME', 'image_pull_secret', vars=DefaultOption(config, 'PROGRAMME', IMAGE_PULL_SECRET = ""))
  21. node_selector = config.get('PROGRAMME', 'node_selector', vars=DefaultOption(config, 'PROGRAMME', NODE_SELECTOR = ""))
  22. def check_password(password: str):
  23. p_bool = bool(re.search(r"(?:,|/|\$)", password))
  24. if p_bool:
  25. raise Exception("密码中存在特殊符号(,或/或$)")
  26. def create_programme(db: Session, item: schemas.ProgrammeCreate):
  27. db_item = db.query(models.Programme).filter(models.Programme.project_id == g.project_id).first()
  28. if db_item:
  29. raise Exception("该项目已存在编程,不可重复创建")
  30. check_password(item.password)
  31. p_res = send_util.get_jupyter_password({"password": item.password})
  32. password = p_res['data']
  33. db_item = models.Programme(**{
  34. 'name': item.name,
  35. 'password': password,
  36. 'workspace': f"workspace_{g.project_id}",
  37. 'base_url': f"/nbss_{g.project_id}",
  38. 'image': super_image if g.project_id == special_project_id else ordinary_image,
  39. 'path': f"/nbss_{g.project_id}",
  40. 'release_name': f"aihub-dag-jpt-{g.project_id}",
  41. 'status': 0,
  42. 'user_id': g.user_id,
  43. 'user_name': g.user_name,
  44. 'project_id': g.project_id,
  45. 'create_time': int(time.time()),
  46. 'cpu': cpu,
  47. 'memory': memory,
  48. 'node_selector': node_selector
  49. })
  50. db.add(db_item)
  51. db.commit()
  52. db.refresh(db_item)
  53. return db_item
  54. def start_jupyter(db: Session, item: schemas.ProgrammeId):
  55. db_item: models.Programme = db.query(models.Programme).filter(models.Programme.id == item.programme_id).first()
  56. if not db_item:
  57. raise Exception("未找到该编程")
  58. jupyter_create_data = {
  59. 'password': db_item.password,
  60. 'namespace': namespace,
  61. 'workspace': db_item.workspace,
  62. 'base_url': db_item.base_url,
  63. 'image': db_item.image,
  64. 'tag': tag,
  65. 'path': db_item.path,
  66. 'host': host,
  67. 'release_name': db_item.release_name,
  68. 'chart': chart,
  69. 'path_type': path_type,
  70. 'ingress_class': ingress_class if ingress_class else "",
  71. 'cpu': str(db_item.cpu)+'m' if db_item.cpu != 0 else '',
  72. 'memory': str(db_item.memory)+'Mi' if db_item.memory != 0 else '',
  73. 'image_pull_secret': image_pull_secret,
  74. 'node_selector': db_item.node_selector if db_item.node_selector != None and db_item.node_selector != '' else node_selector
  75. }
  76. c_res = send_util.create_jupyter(jupyter_create_data)
  77. j_data = c_res['data'] if 'data' in c_res.keys() else None
  78. if not j_data:
  79. raise Exception("启动结构化编码失败")
  80. db_item.status = 0
  81. db.commit()
  82. db.flush()
  83. db.refresh(db_item)
  84. return db_item
  85. def stop_jupyter(db: Session, item: schemas.ProgrammeId):
  86. db_item: models.Programme = db.query(models.Programme).filter(models.Programme.id == item.programme_id).first()
  87. if not db_item:
  88. raise Exception("未找到该编程")
  89. send_util.stop_jupyter({'namespace': namespace,'release_name': db_item.release_name})
  90. db_item.status = 0
  91. db.commit()
  92. db.flush()
  93. db.refresh(db_item)
  94. return db_item
  95. def update_jupyter_password(db: Session, item: schemas.ProgrammeUpdate):
  96. db_item: models.Programme = db.query(models.Programme).filter(models.Programme.id == item.programme_id).first()
  97. if not db_item:
  98. raise Exception("未找到该编程")
  99. if db_item.status == 1:
  100. raise Exception("程序正在运行,请先停止再修改密码")
  101. check_password(item.password)
  102. p_res = send_util.get_jupyter_password({"password": item.password})
  103. password = p_res['data']
  104. db_item.password = password
  105. db.commit()
  106. db.flush()
  107. db.refresh(db_item)
  108. return db_item
  109. def get_programme(db: Session):
  110. db_items: List[models.Programme] = db.query(models.Programme).filter(models.Programme.project_id == g.project_id).all()
  111. res = []
  112. for item in db_items:
  113. # 前端与本地访问使用
  114. complete_url = f'http://{host}{item.base_url}/lab'
  115. # 集群内部访问使用
  116. request_url = f'http://{item.release_name}-jupyterlab:8888{item.base_url}/lab'
  117. jupyter_status = get_programme_status(request_url)
  118. item.status = jupyter_status
  119. db.commit()
  120. db.flush()
  121. db.refresh(item)
  122. i_dict = item.to_dict()
  123. i_dict.update({'complete_url': complete_url})
  124. res.append(i_dict)
  125. return res
  126. def get_programme_status(url: str):
  127. try:
  128. res_header= send_util.get_jupyter_html(url)
  129. # 若为启动状态, 则Server中包含Tornado,否则没有启动
  130. jupyter_status = 0 if res_header['Server'].find('Tornado') < 0 else 1
  131. except Exception as e:
  132. jupyter_status = 0
  133. return jupyter_status