-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker_commands.py
53 lines (47 loc) · 1.98 KB
/
docker_commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import sh
error = {"error":" "}
def start1(usrID, proID):
try:
sh.docker("run","-itd", "--name",proID + usrID, proID)#创建容器
sh.docker("exec",proID + usrID,'service','ssh','start')#容器开启ssh服务
#以下为返回容器id,用户id,容器ip的操作
containerID = str(sh.docker("inspect","-f","{{.Config.Hostname}}",proID + usrID)).replace('\n','').replace('\r','')
containerIP = str(sh.docker("inspect","-f","{{.NetworkSettings.IPAddress}}",proID + usrID)).replace('\n','').replace('\r','')
return {'containerID':containerID,'containerIP': containerIP}
except Exception as e:
error["error"] = str(e)
return error
#用户非第一次开始:
def start2(usrID,proID):
try:
sh.docker("start", proID + usrID)#创建容器
sh.docker("exec",proID + usrID,'service','ssh','start')#容器开启ssh服务
#以下为返回容器id,用户id,容器ip的操作
containerID = str(sh.docker("inspect","-f","{{.Config.Hostname}}",proID + usrID)).replace('\n','').replace('\r','')
containerIP = str(sh.docker("inspect","-f","{{.NetworkSettings.IPAddress}}",proID + usrID)).replace('\n','').replace('\r','')
return {'containerID':containerID,'containerIP': containerIP}
except Exception as e:
error["error"] = str(e)
# print(error)
return error
#用户退出实验,弹出是否保存按钮时
#选择是:
def exit1(usrID,proID):
try:
sh.docker("stop",proID + usrID) #关闭容器
return {"status" : "succeed"}
except Exception as e:
error["error"] = str(e)
# print(error)
return error
#选择否:
def exit2(usrID,proID):
try:
sh.docker("stop",proID + usrID)
sh.docker("rm",proID + usrID) #关闭并删除容器
print("hahahahahahha")
return {"status" : "succeed"}
except Exception as e:
error["error"] = str(e)
# print(error)
return error