forked from SeeTheC/ContainerProvisioning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpbo.py
141 lines (128 loc) · 4.24 KB
/
cpbo.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/python3.5 -W ignore
from pylxd import Client
from json import load
from constants import Constants
'''
-------------------------------------------
Class: Loads the Configuration class
----------------------------------------------
'''
class Config:
server_config=None;
cp_config=None;
@staticmethod
def init():
Config.loadCpJson();
Config.loadServerJson();
@staticmethod
def loadCpJson():
cp_file=open("cp.config","r")
Config.cp_config=load(cp_file);
@staticmethod
def loadServerJson():
s_file=open("server.config","r");
Config.server_config=load(s_file);
'''
-------------------------------------------
Class: Class obj will info about the server
----------------------------------------------
'''
class ServerBO:
'''
---------------------------------------------
Desc: Constructor
----------------------------------------------
'''
def __init__(self,_host,_uri):
self.host=_host;
self.uri=_uri;
self.client=self.creatClient();
'''
----------------------------------------------
Desc: Creates the lxd client obj
return : Obj is successfull else None
----------------------------------------------
'''
def creatClient(self):
client= Client(endpoint=self.uri, cert=(Config.cp_config["cert"],Config.cp_config["key"]),verify=False);
client.authenticate(Config.cp_config["trustPassword"]);
if(client.trusted):
return client;
else:
return None;
'''
----------------------------------------------
Class: Class obj will info about the server
----------------------------------------------
'''
class ContainerBO:
'''
----------------------------------------------
Desc: Constructor
----------------------------------------------
'''
def __init__(self):
self.sname=None;
self.name=None;
self.cpu=None;
self.memory=None;
self.container=None;
self.expectedMemSize=None;
self.isRunning=(lambda: True if self.container.status=="Running" else False);
'''
----------------------------------------------
Desc: gets the Cpu Limit
----------------------------------------------
'''
def getCpuLimit(self):
if self.container == None:
return None;
cpu=self.container.config.get("limits.cpu");
return int(cpu) if cpu!=None and len(cpu)>0 else None;
'''
----------------------------------------------
Desc: gets the mem Limit
----------------------------------------------
'''
def getMemoryLimit(self):
if self.container == None:
return None;
mem=self.container.config.get("limits.memory");
return int(mem[:-2])*Constants.MBtoKB if mem!=None and len(mem)>0 else None;
'''
----------------------------------------------
Desc: sets the mem Limit
----------------------------------------------
'''
def setMemoryLimit(self,sizeInKB):
if self.container == None:
return False;
sizeInMB=sizeInKB//Constants.MBtoKB;
sizeInMB=int(sizeInMB);
self.container.config.update({"limits.memory":str(sizeInMB)+"MB"});
self.container.save();
return True;
'''
----------------------------------------------
Desc: get running status
----------------------------------------------
'''
def getRunningStatus(self):
if self.container == None or not self.isRunning():
return None;
status=self.container.execute(["vmstat","1","-n","5"])[0];
lines=status.split("\n");
frequency=0;
cpu_usage=0;
mem_usage=0;
memLimit=self.getMemoryLimit();
for lno in range(0,len(lines)):
if lno >1 and len(lines[lno])>0:
col=lines[lno].split();
cpu_usage+=100-int(col[14]);
mem_usage+=int(col[3]);
#print(col[3]+" - "+col[14]);
frequency+=1;
avg_mem_usage=int(mem_usage/(frequency if frequency !=0 else 1));
avg_cpu_usage=int(cpu_usage/(frequency if frequency !=0 else 1));
return {"cpu":avg_cpu_usage,"mem":memLimit-avg_mem_usage};