-
Notifications
You must be signed in to change notification settings - Fork 1
/
migrate.py
222 lines (198 loc) · 9.8 KB
/
migrate.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
216
217
218
219
220
221
222
#!/usr/bin/env python
"""
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
#############################################################################################################
# v1.0.0 of python script that connects to rackmonkey DB and migrates data to Device42 appliance using APIs
# This is for mysql based installation. Should be similar for postgres or sqlite based installations.
# Just change the DB connector for other DB types
# Required MySQLdb to be installed. Refer to README for further instructions
#############################################################################################################
import urllib2
import urllib
from base64 import b64encode
import json
import csv
import MySQLdb
D42_API_URL = 'https://D42_IP_or_FQDN' # make sure to NOT to end in /
D42_USERNAME = 'D42USER'
D42_PASSWORD = 'D42PASS'
host = 'localhost' # Hostname for RackMonkey DB. This and values below should be available in Rackmonkey config file
user = 'rackmonkey' # Username to connect to DB
passwd = '' # Password to connect to DB
dbname = 'rackmonkey' # DB Name
DEBUG = False
ADD_HIDDEN_RACKS = False
SEND_ROW_POS_FOR_RACK = False
HARDWARE_MODELS_ARE_ALL_RACKABLE = True
APPEND_DOMAIN_NAME_TO_DEVICE_NAME = False
ADD_ROLE_AS_CUSTOM_KEY = True
CSV_ERRORS_FILE_NAME = 'rmerrors.csv'
db=MySQLdb.connect(host,user,passwd,dbname)
def post(params, what, API_METHOD='post'):
if what == 'device': THE_URL = D42_API_URL + '/api/device/'
elif what == 'ip': THE_URL = D42_API_URL + '/api/ip/'
else: THE_URL = D42_API_URL + '/api/1.0/' + what + '/'
data= urllib.urlencode(params)
headers = {
'Authorization' : 'Basic '+ b64encode(D42_USERNAME + ':' + D42_PASSWORD),
'Content-Type' : 'application/x-www-form-urlencoded'
}
req = urllib2.Request(THE_URL, data, headers)
if API_METHOD == 'put': req.get_method = lambda: 'PUT'
if DEBUG: print '---REQUEST---',req.get_full_url()
if DEBUG: print req.headers
if DEBUG: print req.data
try:
r = urllib2.urlopen(req)
if r.getcode() == 200:
obj = r.read()
msg = json.loads(obj)
return True, msg
else:
return False, r.getcode()
except urllib2.HTTPError, e:
error_response = e.read()
if DEBUG: print e.code, error_response
return False, error_response
except Exception,e:
return False, str(e)
def get_table_data(table):
the_table = []
the_cols = []
colunas=db.cursor()
colunas.execute('DESCRIBE %s' % table)
for col in colunas.fetchall():
the_cols.append(col[0])
dados=db.cursor()
sql = 'SELECT %s FROM %s'
dados.execute(sql % (','.join(the_cols), table))
item = {}
for row in dados.fetchall():
item = {}
for i in range(0,len(row)):
item[the_cols[i]] = str(row[i]) #todo add try/except around it?
the_table.append(item)
return the_table
def export():
fname = open(CSV_ERRORS_FILE_NAME, 'w')
f=csv.writer(fname)
the_building_object = {}
the_room_object = {}
the_row_object = {}
the_rack_object = {}
the_org_object = {}
the_customer_object = {}
the_service_object = {}
the_os_object = {}
the_hardware_object = {}
the_domain_object = {}
the_role_object = {}
the_device_object = {}
for table in ['building', 'room', 'row', 'rack', 'org', 'service', 'os', 'hardware','domain','role', 'device' ]:
rm_data = get_table_data(table)
if table == 'building':
for row in rm_data:
added, msg = post(row, 'buildings')
if added:
the_building_object[row['id']] = msg['msg'][1]
else: f.writerow(['add building failed', row, msg])
elif table == 'room':
for row in rm_data:
row['building_id'] = the_building_object[row['building']]
row.__delitem__('building')
added, msg = post(row, 'rooms')
if added:
the_room_object[row['id']] = msg['msg'][1]
else: f.writerow(['add room failed', row, msg])
elif table == 'row': #Need to use this to get rack to room mappaing from rackmonkey
for row in rm_data:
the_row_object[row['id']] = the_room_object[row['room']]
elif table == 'rack':
for row in rm_data:
if not ADD_HIDDEN_RACKS and row['hidden_rack'] == '1': continue
row['room_id'] = the_row_object[row['row']]
row.__delitem__('row')
if SEND_ROW_POS_FOR_RACK: row['row'] = row['row_pos']
if row['numbering_direction'] == '1': row['numbering_start_from_bottom'] = 'no'
row['first_number'] = 1 #TODO add link to KB article here
row['number_middle'] = 'yes'
added, msg = post(row, 'racks')
if added:
the_rack_object[row['id']] = msg['msg'][1]
else: f.writerow(['add rack failed', row, msg])
elif table == 'org': #RM org will be split into customer and vendor objects in D42 depending on customer & HW/SW manfufacturer flag in RM
for row in rm_data:
if row['customer'] == '1':
added, msg = post(row, 'customers')
if added:
the_customer_object[row['id']] = msg['msg'][1]
else: f.writerow(['add customer failed', row, msg])
if row['software'] == '1' or row['hardware'] == '1': #not use elif because an org in RM can be all 3 things
added, msg = post(row, 'vendors')
if added:
the_org_object[row['id']] = msg['msg'][2] #store name
else: f.writerow(['add vendor failed', row, msg])
elif table == 'service':
for row in rm_data:
added, msg = post(row, 'service_level')
if added:
the_service_object[row['id']] = msg['msg'][2] #store name
else: f.writerow(['add service failed', row, msg])
elif table == 'os':
for row in rm_data:
added, msg = post(row, 'operatingsystems')
if added:
the_os_object[row['id']] = msg['msg'][2] #store name
else: f.writerow(['add os failed', row, msg])
elif table == 'hardware':
for row in rm_data:
row['manufacturer'] = the_org_object[row['manufacturer']]
if HARDWARE_MODELS_ARE_ALL_RACKABLE: row['type'] = 1
added, msg = post(row, 'hardwares')
if added:
the_hardware_object[row['id']] = msg['msg'][2] #store name
else: f.writerow(['add hardware failed', row, msg])
elif table == 'domain' and APPEND_DOMAIN_NAME_TO_DEVICE_NAME:
for row in rm_data:
the_domain_object[row['id']] = row['name']
elif table == 'role' and ADD_ROLE_AS_CUSTOM_KEY:
for row in rm_data:
the_role_object[row['id']] = {'name': row['name'], 'notes': row['notes']} #store the dict for notes
elif table == 'device':
for row in rm_data:
if APPEND_DOMAIN_NAME_TO_DEVICE_NAME:
row['name'] += '.' + the_domain_object[row['domain']]
if row['in_service'] == '1': row['in_service'] = 'yes'
elif row['in_service'] == '0': row['in_service'] = 'no'
row['customer_id'] = the_customer_object[row['customer']]
row.__delitem__('customer')
if row['custom_info'] and row['custom_info'] != ''and row['custom_info'] != 'None': row['notes'] = row['notes'] + '\n' + row['custom_info']
row['service_level'] = the_service_object[row['service']]
row['os'] = the_os_object[row['os']]
row['osver'] = row['os_version']
row['hardware'] = the_hardware_object[row['hardware']]
added, msg = post(row, 'device')
if added:
the_device_object[row['id']] = msg['msg'][2] #store name
if row['rack_pos'] and row['rack_pos'] != 'None' and row['rack_pos'] != '':
if row['rack_pos'] != '0': #Starting position 0 in rackmonkey means the device is no longer racked.
devrackargs = {'device': msg['msg'][2], 'rack_id': the_rack_object[row['rack']]}
#if row['rack_pos'] == '0': devrackargs.update({'start_at': 'auto'})
devrackargs.update({'start_at': int(row['rack_pos'])}) #taking out -1
devrackadded, devrackmsg = post(devrackargs, 'device/rack')
if not devrackadded:
f.writerow(['add device to rack failed', devrackargs, devrackmsg])
if ADD_ROLE_AS_CUSTOM_KEY:
roleargs = {'name': msg['msg'][2],'key': 'role', 'value': the_role_object[row['role']]['name'], 'notes':the_role_object[row['role']]['notes'] }
roladded, rolemsg = post(roleargs, 'device/custom_field', 'put')
if not roladded: f.writerow(['add custom key role failed', roleargs, rolemsg])
else: f.writerow(['add device failed', row, msg])
if __name__=="__main__":
export()