text: import json
  from datetime import datetime
  
  from flask import Blueprint
  
  from anubis.models import db, TheiaSession
  from anubis.rpc.theia import reap_theia_sessions_in_course
  from anubis.utils.auth.http import require_admin
  from anubis.utils.auth.user import current_user
  from anubis.utils.data import req_assert
  from anubis.utils.http.decorators import json_response, json_endpoint
  from anubis.utils.http import error_response, success_response
  from anubis.lms.courses import course_context
  from anubis.utils.rpc import enqueue_ide_initialize
  from anubis.utils.rpc import rpc_enqueue, enqueue_ide_stop
  from anubis.utils.config import get_config_int
  
  ide = Blueprint("admin-ide", __name__, url_prefix="/admin/ide")
  
  
  @ide.route("/settings")
  @require_admin()
  @json_response
  def admin_ide_admin_settings():
      return success_response(
          {
              "settings": {
                  "image": "registry.digitalocean.com/anubis/theia-admin",
                  "repo_url": course_context.autograde_tests_repo,
                  # Options
                  "privileged": True,
                  "network_locked": False,
                  "network_policy": "admin",
                  "resources": '{"limits": {"cpu": "2", "memory": "2Gi"}, "requests": {"cpu": "1", "memory": "500Mi"}}',
                  "autosave": True,
                  "credentials": True,
                  "persistent_storage": False,
              }
          }
      )
  
  
  @ide.route("/initialize", methods=["POST"])
  @ide.route("/initialize-custom", methods=["POST"])
  @require_admin()
  @json_endpoint([("settings", dict)])
  def admin_ide_initialize_custom(settings: dict, **_):
      """
      Initialize a new management ide with options.
  
      :param settings:
      :param _:
      :return:
      """
  
      # Check to see if there is already a management session
      # allocated for the current user
      session: TheiaSession = TheiaSession.query.filter(
          TheiaSession.active,
          TheiaSession.owner_id == current_user.id,
          TheiaSession.course_id == course_context.id,
          TheiaSession.assignment_id == None,
      ).first()
  
      # If there is already a session, then stop
      if session is not None:
          return success_response({"session": session.data})
  
      # Read the options out of the posted data
      image = settings.get("image", "registry.digitalocean.com/anubis/theia-admin")
      repo_url = settings.get(
          "repo_url", "https://github.com/os3224/anubis-assignment-tests"
      )
      resources_str = settings.get("resources", '{"limits":{"cpu":"4","memory":"4Gi"}}')
      network_locked = settings.get("network_locked", False)
      network_policy = settings.get("network_policy", "admin")
      autosave = settings.get("autosave", True)
      credentials = settings.get("credentials", True)
      privileged = settings.get("privileged", True)
      persistent_storage = settings.get("persistent_storage", False)
  
      # Attempt to load the options_str into a dict object
      try:
          resources = json.loads(resources_str)
      except json.JSONDecodeError:
          return error_response("Can not parse JSON options")
  
      # Get the config value for if ide starts are allowed.
      theia_starts_enabled = get_config_int("THEIA_STARTS_ENABLED", default=1) == 1
  
      # Assert that new ide starts are allowed. If they are not, then
      # we return a status message to the user saying they are not able
      # to start a new ide.
      req_assert(
          theia_starts_enabled,
          message="Starting new IDEs is currently disabled by an Anubis administrator. "
          "Please try again later.",
      )
  
      # Create a new session
      session = TheiaSession(
          owner_id=current_user.id,
          assignment_id=None,
          course_id=course_context.id,
          image=image,
          repo_url=repo_url,
          active=True,
          state="Initializing",
          # Options
          network_locked=network_locked,
          network_policy=network_policy,
          autosave=autosave,
          resources=resources,
          credentials=credentials,
          privileged=privileged,
          persistent_storage=persistent_storage,
      )
      db.session.add(session)
      db.session.commit()
  
      # Send kube resource initialization rpc job
      enqueue_ide_initialize(session.id)
  
      return success_response(
          {
              "session": session.data,
              "settings": session.settings,
              "status": "Admin IDE Initialized.",
          }
      )
  
  
  @ide.route("/active")
  @require_admin()
  @json_response
  def admin_ide_active():
      """
      Get the list of all active Theia ides within
      the current course context.
  
      :return:
      """
  
      # Query for an active theia session within this course context
      session = TheiaSession.query.filter(
          TheiaSession.active,
          TheiaSession.owner_id == current_user.id,
          TheiaSession.course_id == course_context.id,
          TheiaSession.assignment_id == None,
      ).first()
  
      # If there was no session, then stop
      if session is None:
          return success_response({"session": None})
  
      # Return the active session information
      return success_response(
          {
              "session": session.data,
              "settings": session.settings,
          }
      )
  
  
  @ide.route("/list")
  @require_admin()
  @json_response
  def admin_ide_list():
      """
      List all active ide sessions
  
      :return:
      """
  
      # Get all active sessions
      sessions = TheiaSession.query.filter(
          TheiaSession.active == True,
          TheiaSession.course_id == course_context.id,
      ).all()
  
      # Hand back response
      return success_response({"sessions": [session.data for session in sessions]})
  
  
  @ide.route("/stop/<string:id>")
  @require_admin()
  @json_response
  def admin_ide_stop_id(id: str):
      """
      Stop a specific IDE
  
      :return:
      """
  
      # Search for the theia session
      session = TheiaSession.query.filter(
          TheiaSession.id == id,
          TheiaSession.course_id == course_context.id,
      ).first()
  
      # Verify it exists
      req_assert(session is not None, message="session does not exist")
  
      # Set all the things as stopped
      session.active = False
      session.ended = datetime.now()
      session.state = "Ending"
  
      # Commit the stop
      db.session.commit()
  
      # Enqueue the theia stop cleanup
      enqueue_ide_stop(session.id)
  
      # Hand back response
      return success_response({"status": "Session Killed."})
  
  
  @ide.route("/reap-all")
  @require_admin()
  @json_response
  def private_ide_reap_all():
      """
      Enqueue a job for the rpc workers to reap all the active
      theia submissions. They will end all active sessions in the
      database, then schedule all the kube resources for deletion.
  
      :return:
      """
  
      # Send reap job to rpc cluster
      rpc_enqueue(reap_theia_sessions_in_course, "theia", args=(course_context.id,))
  
      # Hand back status
      return success_response(
          {"status": "Reap job enqueued. Session cleanup will take a minute."}
      )
  