This commit is contained in:
jxxghp 2023-09-06 15:29:52 +08:00
parent 08f046f059
commit f14d8bec1b
2 changed files with 14 additions and 12 deletions

View File

@ -208,5 +208,6 @@ def restart_system(_: schemas.TokenPayload = Depends(verify_token)):
""" """
if not SystemUtils.can_restart(): if not SystemUtils.can_restart():
return schemas.Response(success=False, message="当前运行环境不支持重启操作!") return schemas.Response(success=False, message="当前运行环境不支持重启操作!")
SystemUtils.restart_docker() # 执行重启
SystemUtils.restart()
return schemas.Response(success=True) return schemas.Response(success=True)

View File

@ -300,20 +300,21 @@ class SystemUtils:
""" """
判断是否可以内部重启 判断是否可以内部重启
""" """
if not SystemUtils.is_docker():
return False
return Path("/var/run/docker.sock").exists() return Path("/var/run/docker.sock").exists()
@staticmethod @staticmethod
def restart_docker(): def restart():
""" """
执行Docker重启操作 执行Docker重启操作
""" """
# 创建 Docker 客户端 try:
client = docker.from_env() # 创建 Docker 客户端
# 获取当前容器的 ID client = docker.from_env()
container_id = open("/proc/self/cgroup", "r").read().split("/")[-1] # 获取当前容器的 ID
if not container_id: container_id = open("/proc/self/cgroup", "r").read().split("/")[-1]
return if not container_id:
# 重启当前容器 return
client.containers.get(container_id.strip()).restart() # 重启当前容器
client.containers.get(container_id.strip()).restart()
except Exception as err:
print(str(err))