-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.py
215 lines (198 loc) · 7.14 KB
/
sql.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python3
import MySQLdb
import re
import sys
# Local module, see config.py file
import config
'''
Functions for low-level interaction with database.
'''
def db_connect():
'''Connect to the database, return MySQLdb.connect object and cursor.'''
try:
dbconn = MySQLdb.connect(
host=config.host,
port=config.port,
passwd=config.passwd,
db=config.db,
user=config.user)
dbcursor = dbconn.cursor()
return (dbconn, dbcursor)
except:
print("ERROR: Can't connect to database. Check that MySQL is "
"accessible via 3306 port with requisites from config.py file.")
sys.exit(30)
def db_close(dbconn, dbcursor):
'''Close cursor and database connection.'''
dbcursor.close()
dbconn.close()
def db_commit(query):
'''SQL INSERT or UPDATE function.'''
(dbconn, dbcursor) = db_connect()
dbcursor.execute(query)
dbconn.commit()
db_close(dbconn, dbcursor)
def db_query_all(query):
'''SQL query function, return all rows.'''
(dbconn, dbcursor) = db_connect()
dbcursor.execute(query)
sqlresult = dbcursor.fetchall()
result = []
for i in sqlresult:
result.append(i[0])
db_close(dbconn, dbcursor)
return result
def get_servers():
'''Get all servers from racktables objects page.'''
return db_query_all("SELECT name FROM Object WHERE objtype_id=4")
def get_server_id(name):
'''Translate server name to its id.'''
sql = "SELECT id FROM Object WHERE name = '{}'".format(name)
result = db_query_all(sql)
return result
def get_attr_ids(name):
'''Return ids of given attribute name.'''
if name == 'OS':
name = "SW type"
elif name == 'STORAGE':
name = 'HDD'
elif name == 'CPU':
name = 'CPU%Model'
elif name == 'RAM':
name = 'DRAM'
sql = "SELECT id FROM Attribute WHERE name LIKE '%{}%'".format(name)
result = db_query_all(sql)
return result
def get_empty_attr_id(name, server):
'''Return id of the empty attribute slot for a given server.'''
for attr_id in get_attr_ids(name):
try:
server_id = get_server_id(server)[0]
sql = ("SELECT uint_value FROM AttributeValue WHERE "
"object_id = {} AND attr_id = {}".format(server_id, attr_id))
query = db_query_all(sql)
if query == []:
return attr_id
except:
pass
def get_attr_values(attr, server):
'''Return all values for a given attribute for the server.'''
result = []
for attr_id in get_attr_ids(attr):
try:
server_id = get_server_id(server)[0]
sql = ("SELECT uint_value FROM AttributeValue WHERE "
"object_id = {} AND attr_id = {}".format(server_id, attr_id))
dict_key = db_query_all(sql)[0]
sql = ("SELECT dict_value FROM Dictionary WHERE dict_key = '{}'"
.format(dict_key))
query = db_query_all(sql)
if query != ['EMPTY']:
result.extend(db_query_all(sql))
except:
pass
if attr == 'OS':
result = clean_OS_output(result)
if attr == 'RAM':
return dict_key
if attr == 'FQDN':
sql = ("SELECT string_value FROM AttributeValue WHERE "
"object_id = {} AND attr_id = {}".format(server_id, attr_id))
result = db_query_all(sql)
return result
return result
def clean_OS_output(array):
'''Remove unnecessary information from OS names.'''
result = [re.sub(".*%GSKIP%", "", elem) for elem in array]
result = [re.sub(" \|.*", "", elem) for elem in result]
return result
def get_available_values(attr):
'''Return all possible values for a given attribute.'''
result = []
if attr == 'STORAGE':
chapter_id = 10001
if attr == 'CPU':
chapter_id = 10000
if attr == 'OS':
chapter_id = 13
if attr == 'STATUS':
chapter_id = 10003
if attr == 'SLA':
chapter_id = 10004
sql = ("SELECT dict_value FROM Dictionary WHERE chapter_id = '{}'"
.format(chapter_id))
result.extend(db_query_all(sql))
if attr == 'OS':
result = clean_OS_output(result)
return result
def add_attr_value(attr, server, value):
'''Add a hardware into the server.'''
try:
attr_id = get_empty_attr_id(attr, server)
sql = ("SELECT dict_key FROM Dictionary WHERE dict_value = '{}'"
.format(value))
uint_value = db_query_all(sql)[0]
server_id = get_server_id(server)[0]
sql = ("INSERT INTO AttributeValue "
"(object_id, object_tid, attr_id, uint_value) "
"VALUES ({}, 4, {}, {})".format(server_id, attr_id, uint_value))
db_commit(sql)
print('OK')
except Exception as e:
print('ERROR: {}'.format(e))
exit(40)
def set_attr_value(attr, server, value):
'''Update the value of the specified attribute.'''
try:
attr_id = get_attr_ids(attr)[0]
server_id = get_server_id(server)[0]
if attr == 'RAM':
sql = ("UPDATE AttributeValue SET uint_value = {} "
"WHERE object_id = {} AND attr_id = {}"
.format(value, server_id, attr_id))
elif attr == 'FQDN':
sql = ("UPDATE AttributeValue SET string_value = '{}' "
"WHERE object_id = {} AND attr_id = {}"
.format(value, server_id, attr_id))
else:
sql = ("SELECT dict_key FROM Dictionary "
"WHERE BINARY dict_value LIKE '%{}%'"
.format(value))
dict_key = db_query_all(sql)[0]
sql = ("UPDATE AttributeValue SET uint_value = {} "
"WHERE object_id = {} AND attr_id = {}"
.format(dict_key, server_id, attr_id))
db_commit(sql)
print('OK')
except Exception as e:
print('ERROR: {}'.format(e))
exit(40)
def get_last_nonempty_attr_id(attr, server, value):
'''Return id of the last nonempty attribute for a given server.'''
for attr_id in reversed(get_attr_ids(attr)):
try:
sql = ("SELECT dict_key FROM Dictionary WHERE dict_value = '{}'"
.format(value))
uint_value = db_query_all(sql)[0]
server_id = get_server_id(server)[0]
sql = ("SELECT * FROM AttributeValue WHERE "
"object_id = {} AND attr_id= {} AND uint_value = {}"
.format(server_id, attr_id, uint_value))
query = db_query_all(sql)
if query != []:
return attr_id
except:
pass
def del_attr_value(attr, server, value):
'''Remove a hardware from the server.'''
try:
attr_id = get_last_nonempty_attr_id(attr, server, value)
server_id = get_server_id(server)[0]
sql = ("DELETE FROM AttributeValue "
"WHERE attr_id = {} AND object_id = {}"
.format(attr_id, server_id))
db_commit(sql)
print('OK')
except Exception as e:
print('ERROR: {}'.format(e))
exit(40)