add code-server clean

This commit is contained in:
CakeCN
2025-09-12 14:55:08 +08:00
parent baf7271ce2
commit e025ac6a9c
3 changed files with 133 additions and 23 deletions

View File

@@ -11,20 +11,12 @@ def desktop(user_uuid, course_id, chapter_name, lesson_name):
username2uuid = current_app.extensions["username2uuid"]
uuid2username = current_app.extensions["uuid2username"]
userid_recorder = current_app.extensions["userid_recorder"]
# session 中放 uuid访客也能进则给一个
if "user_uuid" not in session:
user_uuid = "user_" + str(uuid.uuid4())
session["user_uuid"] = user_uuid
uuid2username[user_uuid] = "guest"+str(uuid.uuid4())[0:5]
username2uuid[uuid2username[user_uuid]] = user_uuid
return redirect(url_for("auth.login"))
# 全局映射
user_id = uuid2username[user_uuid]
userid_recorder[f"{user_id}&{course_id}"] = session["user_uuid"]
# 按课程/章节创建工作目录
base_root = current_app.config["STUDENT_WORKSPACE_ROOT"]
user_home = os.path.join("/home", user_id)
try:
subprocess.run(["sudo", "useradd", "-m", "-d", user_home, user_id]) # 创建用户
@@ -40,19 +32,13 @@ def desktop(user_uuid, course_id, chapter_name, lesson_name):
print(f"Error creating directory {path_dir} details {str(e)}")
chatmanager = current_app.extensions["user_uuid2chatmanager"][user_uuid]
code_server_port = 10000 + int(uuid.uuid4().int % 10000)
try:
subprocess.Popen([
"sudo", "-u", user_id,
"code-server", "--bind-addr", f"0.0.0.0:{code_server_port}",
"--auth", "none", path_dir,
"--extensions-dir", "/share/extension/"
])
except subprocess.CalledProcessError as e:
print(f"Error start code-server at {code_server_port} with path{path_dir} {e}")
chatmanager.start_code_server(user_uuid, user_id, path_dir, code_server_port)
caddy_conf = f"""
handle_path /vsc{user_uuid}/* {{
reverse_proxy localhost:{code_server_port}