-
Notifications
You must be signed in to change notification settings - Fork 7
/
modifydb.py
181 lines (171 loc) · 5.63 KB
/
modifydb.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
"""
This module allows modification to be made to the database.
"""
import os
from get_property import get_home_directory
from get_property import get_resolve_hostname
from get_property import timestamp
from search import search_node
from processdb import process_nodes
from snmp import snmp
import yaml
def append(args):
argument_node = args.node
database = process_nodes()
index = 0
match_node = []
mgmt_ip4 = get_resolve_hostname(argument_node)
# try:
"""
Check if new node currently exist in database.
"""
# print(database)
for node in database:
if mgmt_ip4 == node['mgmt_ip4']:
match_node.append('MATCH')
break
else:
continue
try:
if 'MATCH' in match_node:
print('+ Node currently present in database.')
else:
device = snmp(argument_node)
new_node = yaml.dump(device,default_flow_style = False)
with open('{}/superloop_code/database/nodes.yaml'.format(get_home_directory()),'a') as file:
file.write(new_node)
database = process_nodes()
sorted_database = sortdb(database)
updated_database = yaml.dump(sorted_database,default_flow_style = False)
with open('{}/superloop_code/database/nodes.yaml'.format(get_home_directory()),'w') as file:
file.write('---\n')
file.write(updated_database)
print('+ SNMP discovery successful.')
print('+ New node appended to database.')
except FileNotFoundError as error:
print('FileNotFoundError: file cannot be found')
print(error)
# except Exception as error:
# print('SNMP query timeout. Please ensure that FQDN or IP address is reachable via SNMP.')
return None
def remove(args):
database = process_nodes()
index = 0
try:
for element in database:
if element['name'] == args.node or element['mgmt_ip4'] == args.node:
break
else:
index = index + 1
"""
Delete node from list.
"""
database.pop(index)
updated_database = yaml.dump(database,default_flow_style = False)
try:
with open('{}/superloop_code/database/nodes.yaml'.format(get_home_directory()),'w') as file:
file.write('---\n')
file.write(updated_database)
print('- Node successfully removed from database.')
except FileNotFoundError as error:
print("FileNotFoundError: file cannot be found")
print(error)
except IndexError as error:
print('+ Node does not exist in database.')
return None
def update(args):
argument_node = args.node
argument_attribute = args.attribute
argument_amend = args.amend
database = process_nodes()
index = 0
try:
for element in database:
if element['name'] == argument_node or element['mgmt_ip4'] == argument_node:
break
else:
index = index + 1
"""
Identified node from list.
"""
try:
if argument_attribute == 'data':
return print('+ Attribute \'data\' cannot be modified via host update.')
else:
check = str(input('Please confirm you would like to change the value from {} : {} : {} to {} : {} : {}. [y/N]: '.format(database[index]['name'],argument_attribute,database[index][argument_attribute],database[index]['name'],argument_attribute,argument_amend)))
if check[0] == 'y':
database[index][argument_attribute] = argument_amend
database[index]['updated_at'] = timestamp()
database[index]['updated_by'] = '{}'.format(os.environ.get('USER'))
updated_database = yaml.dump(database,default_flow_style = False)
try:
with open('{}/superloop_code/database/nodes.yaml'.format(get_home_directory()),'w') as file:
file.write('---\n')
file.write(updated_database)
print('+ Amendment to database was successful.')
except FileNotFoundError as error:
print("FileNotFoundError: file cannot be found")
print(error)
elif check[0] == 'N':
return False
else:
print("RuntimeError: aborted at user request")
except Exception as error:
print('+ Invalid attribute \'{}\' for \'{}\'. Please check node details via \'superloop node list {}\''.format(argument_attribute,database[index]['name'],database[index]['name']))
except IndexError as error:
print('+ Node does not exist in database.')
def discover(args):
argument_node = args.node
database = process_nodes()
match_node = search_node(argument_node,database)
"""
:param node_object: All node(s) in the database with all attributes.
:type node_object: list
:param match_node: Nodes that matches the arguements passed in by user.
:type match_node: list
"""
if len(match_node) == 0:
print('+ No matching node(s) found in database.')
return None
else:
for node in match_node:
print('{}'.format(node))
index = 0
try:
for element in database:
if element['name'] == node or element['mgmt_ip4'] == node:
break
else:
index = index + 1
"""
Identified node from list.
"""
device = snmp(node)
print('')
except Exception as error:
print('SNMP query timeout. Please ensure that FQDN or IP address is reachable via SNMP.')
for attribute in database[index]:
if 'created_at' == attribute or 'created_by' == attribute:
continue
else:
database[index][attribute] = device[0][attribute]
database[index]['updated_at'] = timestamp()
database[index]['updated_by'] = '{}'.format(os.environ.get('USER'))
updated_database = yaml.dump(database,default_flow_style = False)
with open('{}/superloop_code/database/nodes.yaml'.format(get_home_directory()),'w') as file:
file.write('---\n')
file.write(updated_database)
print('+ SNMP discovery successful.')
def sortdb(database):
sorted_database = []
names = []
for node in database:
names.append(node['name'])
names.sort()
for name in names:
for node in database:
if name == node['name']:
sorted_database.append(node)
else:
continue
return sorted_database