programme.py 5.0 KB

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