-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbindings.py
More file actions
98 lines (84 loc) · 2.95 KB
/
bindings.py
File metadata and controls
98 lines (84 loc) · 2.95 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#Stats
import os
import subprocess
program = 'docker'
stop_command = 'stop'
def createProcess(options):
process = subprocess.Popen(options,stderr=subprocess.PIPE,stdout=subprocess.PIPE)
stdout,stderr = process.communicate()
return (stdout.decode('UTF-8'),stderr.decode('UTF-8'),len(stderr.decode('UTF-8'))>0)
def createProcessSingleine(options):
process = subprocess.Popen(options,stderr=subprocess.PIPE,stdout=subprocess.PIPE)
result = process.stdout.readline().decode('UTF-8')
result = process.stdout.readline().decode('UTF-8')
process.terminate()
return result
class Container:
def __init__(self,l):
self.cid = l[0]
self.image = l[1]
self.status = l[4]
#self.port = l[5]
self.name = l[1]
def stop(self,options=[]):
options = [program,stop_command]+options+[self.cid]
process = createProcess(options)
return process
def start(self,options=[]):
options = [program,'start']+options+[self.cid]
process = createProcess(options)
return process
def delete(self,options=[]):
self.stop()
options = [program,'rm']+options+[self.cid]
process = createProcess(options)
return process
def stats(self):
options = [program,'stats']+[self.cid]
text = createProcessSingleine(options)
text = text.split(' ')
text = list(filter(lambda w : w!='' and w!=' ',text))
return {
"name" : text[1],
"cpu" : text[2],
"ram_limit" : text[3],
"ram" : text[4],
"io" :text[5],
"block_io":text[6],
"pid" : text[7]
}
class Docker:
def __init__(self):
self.name="docker"
def getContainers(self):
result = createProcess(['docker','ps','-a'])
objs = []
if not result[2]:
text = result[0]
text = text.split('\n')
text = text[1::]
for index,line in enumerate(text):
text[index] = text[index].split(' ')
text[index] = list(filter(lambda element : element.rstrip()!='' and element.lstrip!='',text[index]))
if(len(text[index])>0):
objs.append(Container(text[index]))
return objs
def findContainer(self,cid):
containers = self.getContainers()
for container in containers:
if container.cid == cid:
return container
return None
def createContainer(self,options):
options = ['docker','run','-d']+options.split(' ')
result = createProcess(options)
return result
docker = Docker()
container = docker.findContainer("fa4a09c8d092")
stats = container.stats()
for key,value in stats.items():
print("{} : {}".format(key,value))
container.start()
stats = container.stats()
for key,value in stats.items():
print("{} : {}".format(key,value))