programme.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. # send_util.stop_jupyter({'namespace': namespace,'release_name': db_item.release_name})
  71. raise Exception("启动结构化编码失败")
  72. url = f"http://{j_data['host']}{j_data['base_url']}/lab"
  73. db_item.status = 0
  74. db.commit()
  75. db.flush()
  76. db.refresh(db_item)
  77. return db_item
  78. def stop_jupyter(db: Session, item: schemas.ProgrammeId):
  79. db_item: models.Programme = db.query(models.Programme).filter(models.Programme.id == item.programme_id).first()
  80. if not db_item:
  81. raise Exception("未找到该编程")
  82. send_util.stop_jupyter({'namespace': namespace,'release_name': db_item.release_name})
  83. db_item.status = 0
  84. db.commit()
  85. db.flush()
  86. db.refresh(db_item)
  87. return db_item
  88. def update_jupyter_password(db: Session, item: schemas.ProgrammeUpdate):
  89. db_item: models.Programme = db.query(models.Programme).filter(models.Programme.id == item.programme_id).first()
  90. if not db_item:
  91. raise Exception("未找到该编程")
  92. if db_item.status == 1:
  93. raise Exception("程序正在运行,请先停止再修改密码")
  94. check_password(item.password)
  95. p_res = send_util.get_jupyter_password({"password": item.password})
  96. password = p_res['data']
  97. db_item.password = password
  98. db.commit()
  99. db.flush()
  100. db.refresh(db_item)
  101. return db_item
  102. def get_programme(db: Session):
  103. db_items: List[models.Programme] = db.query(models.Programme).filter(models.Programme.project_id == g.project_id).all()
  104. res = []
  105. for item in db_items:
  106. complete_url =f'http://{host}{item.base_url}/lab'
  107. jupyter_status = get_programme_status(complete_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. res_header= send_util.get_jupyter_html(url)
  118. # 若为启动状态,则Server中包含Tornado,否则没有启动
  119. print(res_header)
  120. jupyter_status = 0 if res_header['Server'].find('Tornado') < 0 else 1
  121. return jupyter_status