programme.py 5.0 KB

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