Repository navigation
Expand file tree
/
Copy pathopenstack-to-netbox.py
More file actions
216 lines (181 loc) · 9.21 KB
/
Copy pathopenstack-to-netbox.py
File metadata and controls
216 lines (181 loc) · 9.21 KB
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
# MIT License
#
# Copyright (c) 2025. Patrick Brammerloo, Mark Zijdemans, DirectVPS [https://directvps.nl/]
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# 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.
import sys
import time
from scripts.openstack.fetchinfo import get_keystone
from scripts.openstack.fetchinfo import get_nova
from scripts.openstack.fetchinfo import get_glance_images
from scripts.openstack.fetchinfo import get_cinder
from scripts.openstack.fetchinfo import get_neutron
from scripts.netbox.fetchinfo import nbfetchinterfaces
from scripts.netbox.fetchinfo import nbfetchvms
from scripts.netbox.fetchinfo import nbfetchvolumes
from scripts.netbox.fetchinfo import nbfetchvrfs
from scripts.netbox.fetchinfo import nbfetchsubnets
from scripts.netbox.fetchinfo import nbfetchaddresses
from scripts.parse_nova_vm import nova_to_netboxvms
from scripts import parse_glance_images
from scripts.parse_neutron_vm import neutronrouter_to_netboxvms
from scripts.parse_neutron_vm import neutrondhcp_to_netboxvms
from scripts.parse_cinder_volumes import cinder_to_netboxdisks
from scripts.parse_neutron_interfaces import netboxinterfaces
from scripts.parse_neutron_interfaces import netboxmacs
from scripts.parse_neutron_networks import netboxipamvrfs
from scripts.parse_neutron_networks import netboxipamsubnets
from scripts.parse_neutron_ipam import netboxipam
from scripts.parse_neutron_ipam import netboxipamfloat
import settings
nb = settings.nb
cluster_name = settings.cluster_name
cluster_type = settings.cluster_type
try:
print(f'\nFetching information from OpenStack \n')
keystone_tenant_dictionary = get_keystone()
nova_instances, nova_flavor_dictionary = get_nova()
glance_image_dictionary = get_glance_images(nova_instances)
cinder_volume_dictionary = get_cinder()
(neutron_interface_dictionary, neutron_network_private_dictionary, neutron_float_dictionary,
neutron_router_dictionary, neutron_dhcpagent_dictionary, neutron_subnet_dictionary) = get_neutron()
print(f'Finished fetching information from OpenStack. \n')
except Exception as e:
print(f"Unable to collect information from OpenStack \n{e}")
sys.exit(1)
try:
print(f'Fetching information from NetBox for cluster {cluster_name}\n')
netboxvmdic = nbfetchvms()
netboxinterfacedic = nbfetchinterfaces()
netboxvoldic = nbfetchvolumes()
netboxvrfdic = nbfetchvrfs()
netboxsubnetdic = nbfetchsubnets()
netboxlanaddressdic, netboxwanaddressdic = nbfetchaddresses()
print(f'\nFinished collecting information from NetBox for cluster {cluster_name}')
except Exception as e:
print(f"Unable to collect information from NetBox \n{e}")
sys.exit(1)
print(f'Creation and or updating of NetBox objects will start in 5 seconds. \n')
time.sleep(5)
try:
print(f"Attempting to create/update NetBox Platforms based on OpenStack Images")
parse_glance_images.glanceimages_to_netboxplatforms(glance_image_dictionary)
print('NetBox Platforms have been created or updated succesfully \n')
except Exception as e:
print(f"NetBox Platform creation or updating failed \n{e}")
sys.exit(1)
try:
print(f"Attempting to create/update NetBox Virtual Machines based on OpenStack Instances")
nova_to_netboxvms(nova_instances, nova_flavor_dictionary, keystone_tenant_dictionary, netboxvmdic)
print('NetBox Virtual Machines have been created or updated succesfully \n')
except Exception as e:
# We really only want to proceed to the next functions, when the previous step has been completed succesfully
print(f"NetBox Virtual Machine creation or updating failed \n{e}")
sys.exit(1)
try:
print(f"Attempting to create/update NetBox Virtual Machines based on OpenStack routers")
neutronrouter_to_netboxvms(neutron_router_dictionary, nova_flavor_dictionary,
keystone_tenant_dictionary, netboxvmdic)
print('NetBox routers have been created or updated succesfully \n')
except Exception as e:
print(f"NetBox Router creation or updating failed \n{e}")
sys.exit(1)
try:
print(f"Attempting to create/update NetBox Virtual Machines based on Neutron DHCP agents")
neutrondhcp_to_netboxvms(neutron_dhcpagent_dictionary, netboxvmdic)
print('NetBox DHCP agents have been created or updated succesfully \n')
except Exception as e:
print(f"NetBox DHCP agent creation or updating failed \n{e}")
sys.exit(1)
try:
print(f"Re-fetching Virtual Machines from NetBox as states may have been modified")
netboxvmdic = nbfetchvms()
except Exception as e:
print(f"Unable to re-fetch Virtual Machines from NetBox cluster {cluster_name} \n{e}")
sys.exit(1)
try:
print(f"\nAttempting to create/update Netbox Virtual Disks based on Volumes associated with OpenStack Instances")
cinder_to_netboxdisks(cinder_volume_dictionary, netboxvoldic, netboxvmdic)
print(f'NetBox disks have been created or updated succesfully \n')
except Exception as e:
print(f"NetBox disk creation or updating failed \n{e}")
sys.exit(1)
try:
print(f"Attempting to create/update Netbox Interfaces based on interfaces associated with OpenStack Instances")
netboxinterfaces(neutron_interface_dictionary, netboxinterfacedic, netboxvmdic)
print('NetBox Interfaces have been created or updated succesfully \n')
except Exception as e:
print(f"NetBox Interfaces creation or updating failed \n{e}")
sys.exit(1)
try:
print(f'Re-fetching NetBox Interfaces information')
netboxinterfacedic = nbfetchinterfaces()
print(f'Finished re-fetching NetBox Interfaces \n')
except Exception as e:
print(f"Unable to collect Interface information from NetBox \n{e}")
sys.exit(1)
try:
print(f"Attempting to create and or associate NetBox MAC-addresses based on Neutron interfaces.")
netboxmacs(neutron_interface_dictionary, netboxinterfacedic)
print('NetBox MAC-addresses have been created and or associated succesfully \n')
except Exception as e:
print(f"NetBox MAC-addresses creation or associating failed \n{e}")
sys.exit(1)
try:
print(f"Attempting to create/update Netbox VRFs based on OpenStack networks containing private IP-addresses")
netboxipamvrfs(neutron_network_private_dictionary, netboxvrfdic)
print(f"NetBox VRFs have been created or updated succesfully \n")
try:
print(f'Re-fetching NetBox VRF information')
netboxvrfdic = nbfetchvrfs()
except Exception as e:
print(f"Unable to collect VRF information from NetBox \n{e}")
sys.exit(1)
try:
print(f"\nAttempting to create/update NetBox subnets based on OpenStack subnets containing relevant addresses")
netboxipamsubnets(neutron_subnet_dictionary, neutron_interface_dictionary, netboxsubnetdic, netboxvrfdic)
print(f"NetBox subnets have been created or updated succesfully \n")
except Exception as e:
print(f"NetBox subnets based on OpenStack subnets creation or updating failed \n{e}")
sys.exit(1)
except Exception as e:
print(f"NetBox VRF creation or updating failed \n{e}")
sys.exit(1)
try:
print(f'Re-fetching NetBox subnet information')
netboxsubnetdic = nbfetchsubnets()
except Exception as e:
print(f"Unable to collect subnet information from NetBox \n{e}")
sys.exit(1)
try:
print(f"\nAttempting to create/update NetBox IP-addresses based on Interfaces bound to OpenStack Instances")
netboxipam(neutron_interface_dictionary, neutron_subnet_dictionary, netboxvmdic, netboxinterfacedic, netboxvrfdic,
netboxlanaddressdic, netboxwanaddressdic)
print(f'NetBox IP-addresses based on OpenStack Interfaces have been created or updated succesfully \n')
except Exception as e:
print(f"NetBox IP-addresses based on Instance Interfaces creation or updating failed \n{e}")
sys.exit(1)
try:
print(f"Attempting to create/update NetBox IP-addresses based on Floating-IPs bound to Instances")
netboxipamfloat(neutron_float_dictionary, neutron_subnet_dictionary, netboxvmdic, netboxinterfacedic, netboxvrfdic,
netboxlanaddressdic, netboxwanaddressdic)
print(f'NetBox IP-addresses based on Floating-IPs have been created or updated succesfully \n')
print(f"The script has finished succesfully!")
except Exception as e:
print(f"NetBox IP-addresses based on Floating-IPs creation or updating failed \n{e}")
sys.exit(1)