-
Notifications
You must be signed in to change notification settings - Fork 0
/
beans.py
129 lines (114 loc) · 3.69 KB
/
beans.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
from django.conf import settings
from db import connection
from public import NOVA_DB,NEUTRON_DB,NOVA,NEUTRON
class ComputeNode:
def __init__(self,vcpus,memory_mb,vcpus_used,memory_mb_used,hypervisor_hostname,running_vms,deleted=0,host_ip=None,id=None):
self.vcpus=vcpus
self.memory_mb=memory_mb
self.vcpus_used=vcpus_used
self.memory_mb_used=memory_mb_used
self.hypervisor_hostname=hypervisor_hostname
self.running_vms=running_vms
self.rest_vcpus=vcpus*4-vcpus_used
self.rest_memory_mb=memory_mb-memory_mb_used
self.deleted=deleted
self.host_ip=host_ip
self.id=id
def __str__(self):
return "--host:%s,rest_vcpus:%s,rest_mem:%s-- " % (self.hypervisor_hostname,self.rest_vcpus,self.rest_memory_mb)
def __repr__(self):
return "--host:%s,rest_vcpus:%s,rest_mem:%s-- " % (self.hypervisor_hostname,self.rest_vcpus,self.rest_memory_mb)
def availability(self,cpu,mem):
return self.rest_vcpus>cpu and self.rest_memory_mb>mem
ALL_PHYSICAL="SELECT vcpus,memory_mb,vcpus_used,memory_mb_used,hypervisor_hostname,running_vms,deleted,host_ip,id FROM compute_nodes"
GET_SALT_PHYSICAL="SELECT compute_node_ip,compute_node_host,region,running_vms,node_deleted,id FROM salt_nodes WHERE region=%s"
ADD_Minion="""
INSERT INTO salt_nodes(id,compute_node_ip,compute_node_host,region,running_vms,salt_state,node_deleted,update_time) VALUES (%s,%s,%s,%s,%s,"INIT",%s,now())
"""
UPDATE_MINION_VMS="""
UPDATE salt_nodes SET running_vms=%s,node_deleted=%s WHERE id=%s AND region=%s
"""
UPDATE_MINION_STATE="""
UPDATE salt_nodes SET salt_state=%s WHERE id=%s AND region=%s
"""
SALT_LOG="""
INSERT INTO salt_thread_log(log,create_time,type) VALUES (%s,now(),%s)
"""
class ComputeNodeMana:
def updateMinion(self,vms,deleted,_id,region):
conn=connection()
cursor=conn.cursor()
cursor.execute(UPDATE_MINION_VMS,(vms,deleted,_id,region))
conn.commit()
cursor.close()
conn.close()
def updateMinionState(self,state,_id,region):
conn=connection()
cursor=conn.cursor()
cursor.execute(UPDATE_MINION_STATE,(state,_id,region))
conn.commit()
cursor.close()
conn.close()
def addMinion(self,n,region):
conn=connection()
cursor=conn.cursor()
try:
cursor.execute(ADD_Minion,(n.id,n.host_ip,n.hypervisor_hostname,region,n.running_vms,n.deleted))
except Exception,ex:
print Exception,":",ex
return False
finally:
conn.commit()
cursor.close()
conn.close()
return True
def addSaltLog(self,log,Type):
conn=connection()
cursor=conn.cursor()
try:
cursor.execute(SALT_LOG,(log,Type,))
except Exception,ex:
print Exception,":",ex
return False
finally:
conn.commit()
cursor.close()
conn.close()
return True
def getAllComputeNodes(self,db):
cursor=db.cursor()
cursor.execute(ALL_PHYSICAL)
results=cursor.fetchall()
nodes=[]
for line in results:
vcpus=line[0]
memory_mb=line[1]
vcpus_used=line[2]
memory_mb_used=line[3]
hypervisor_hostname=line[4]
running_vms=line[5]
deleted=line[6]
host_ip=line[7]
_id=line[8]
nodes.append(ComputeNode(vcpus,memory_mb,vcpus_used,memory_mb_used,hypervisor_hostname,running_vms,deleted,host_ip,_id))
cursor.close()
db.close()
return nodes
def getSaltComputeNodes(self,region):
conn=connection()
cursor=conn.cursor()
cursor.execute(GET_SALT_PHYSICAL,(region,))
results=cursor.fetchall()
cursor.close()
conn.close()
nodes={}
for line in results:
minion={}
minion["compute_node_ip"]=line[0]
minion["compute_node_host"]=line[1]
minion["region"]=line[2]
minion["running_vms"]=line[3]
minion["node_deleted"]=line[4]
minion["id"]=line[5]
nodes["%s_%d" % (line[1],minion["id"])]=minion
return nodes