2021-02-26 23:26:32 +01:00
|
|
|
#!/usr/bin/env python3
|
2020-02-20 14:52:01 +01:00
|
|
|
from git import Repo, exc, RemoteProgress
|
2020-10-08 22:28:12 +02:00
|
|
|
from collections import Counter
|
2021-02-27 01:02:03 +01:00
|
|
|
from datetime import datetime
|
2020-12-11 19:37:00 +01:00
|
|
|
import yaml
|
|
|
|
import pynetbox
|
|
|
|
import glob
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import settings
|
2020-02-20 14:52:01 +01:00
|
|
|
|
2021-02-27 01:02:03 +01:00
|
|
|
REPO_URL = settings.REPO_URL
|
2020-02-20 14:52:01 +01:00
|
|
|
parser = argparse.ArgumentParser(description='Import Netbox Device Types')
|
2021-02-09 02:26:59 +01:00
|
|
|
parser.add_argument('--vendors', nargs='+',
|
2020-12-11 19:37:00 +01:00
|
|
|
help="List of vendors to import eg. apc cisco")
|
|
|
|
parser.add_argument('--url', '--git', default=REPO_URL,
|
|
|
|
help="Git URL with valid Device Type YAML files")
|
2020-02-20 14:52:01 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
cwd = os.getcwd()
|
2020-12-11 19:37:00 +01:00
|
|
|
counter = Counter(added=0, updated=0, manufacturer=0)
|
2020-02-26 15:49:12 +01:00
|
|
|
nbUrl = settings.NETBOX_URL
|
|
|
|
nbToken = settings.NETBOX_TOKEN
|
2021-02-27 01:02:03 +01:00
|
|
|
startTime = datetime.now()
|
2020-02-20 14:52:01 +01:00
|
|
|
|
2020-12-11 19:37:00 +01:00
|
|
|
|
2020-02-20 14:52:01 +01:00
|
|
|
def update_package(path: str):
|
|
|
|
try:
|
|
|
|
repo = Repo(path)
|
|
|
|
if repo.remotes.origin.url.endswith('.git'):
|
|
|
|
repo.remotes.origin.pull()
|
2020-11-12 20:38:48 +01:00
|
|
|
print(f"Pulled Repo {repo.remotes.origin.url}")
|
2020-02-20 14:52:01 +01:00
|
|
|
except exc.InvalidGitRepositoryError:
|
|
|
|
pass
|
|
|
|
|
2020-12-11 19:37:00 +01:00
|
|
|
|
|
|
|
def slugFormat(name):
|
|
|
|
return name.lower().replace(' ', '_')
|
|
|
|
|
|
|
|
|
2020-02-20 14:52:01 +01:00
|
|
|
def getFiles(vendors=None):
|
|
|
|
files = []
|
|
|
|
discoveredVendors = []
|
2020-12-11 19:37:00 +01:00
|
|
|
base_path = './repo/device-types/'
|
2020-02-20 14:52:01 +01:00
|
|
|
if vendors:
|
2020-12-11 19:37:00 +01:00
|
|
|
for r, d, f in os.walk(base_path):
|
2020-02-20 14:52:01 +01:00
|
|
|
for folder in d:
|
|
|
|
for vendor in vendors:
|
|
|
|
if vendor.lower() == folder.lower():
|
2020-12-11 19:37:00 +01:00
|
|
|
discoveredVendors.append({'name': folder,
|
|
|
|
'slug': slugFormat(folder)})
|
|
|
|
files.extend(glob.glob(base_path + folder + '/*.yaml'))
|
2020-02-20 14:52:01 +01:00
|
|
|
else:
|
2020-12-11 19:37:00 +01:00
|
|
|
for r, d, f in os.walk(base_path):
|
2020-02-20 14:52:01 +01:00
|
|
|
for folder in d:
|
|
|
|
if folder.lower() != "Testing":
|
2020-12-11 19:37:00 +01:00
|
|
|
discoveredVendors.append({'name': folder,
|
|
|
|
'slug': slugFormat(folder)})
|
|
|
|
files.extend(glob.glob(base_path + '[!Testing]*/*.yaml'))
|
2020-02-20 14:52:01 +01:00
|
|
|
return files, discoveredVendors
|
|
|
|
|
2020-12-11 19:37:00 +01:00
|
|
|
|
2020-02-20 14:52:01 +01:00
|
|
|
def readYAMl(files):
|
|
|
|
deviceTypes = []
|
|
|
|
manufacturers = []
|
|
|
|
for file in files:
|
|
|
|
with open(file, 'r') as stream:
|
|
|
|
try:
|
|
|
|
data = yaml.safe_load(stream)
|
|
|
|
except yaml.YAMLError as exc:
|
|
|
|
print(exc)
|
2020-11-12 20:44:11 +01:00
|
|
|
continue
|
2020-02-20 14:52:01 +01:00
|
|
|
manufacturer = data['manufacturer']
|
|
|
|
data['manufacturer'] = {}
|
|
|
|
data['manufacturer']['name'] = manufacturer
|
2020-12-11 19:37:00 +01:00
|
|
|
data['manufacturer']['slug'] = slugFormat(manufacturer)
|
2020-02-20 14:52:01 +01:00
|
|
|
deviceTypes.append(data)
|
|
|
|
manufacturers.append(manufacturer)
|
|
|
|
return deviceTypes
|
|
|
|
|
2020-12-11 19:37:00 +01:00
|
|
|
|
2020-02-20 14:52:01 +01:00
|
|
|
def createManufacturers(vendors, nb):
|
|
|
|
for vendor in vendors:
|
|
|
|
try:
|
|
|
|
manGet = nb.dcim.manufacturers.get(name=vendor["name"])
|
|
|
|
if manGet:
|
|
|
|
print(f'Manufacturer Exists: {manGet.name} - {manGet.id}')
|
|
|
|
else:
|
|
|
|
manSuccess = nb.dcim.manufacturers.create(vendor)
|
2020-12-11 19:37:00 +01:00
|
|
|
print(f'Manufacturer Created: {manSuccess.name} - '
|
|
|
|
+ f'{manSuccess.id}')
|
|
|
|
counter.update({'manufacturer': 1})
|
2020-02-20 14:52:01 +01:00
|
|
|
except pynetbox.RequestError as e:
|
2020-12-11 19:37:00 +01:00
|
|
|
print(e.error)
|
|
|
|
|
2020-02-20 14:52:01 +01:00
|
|
|
|
|
|
|
def createInterfaces(interfaces, deviceType, nb):
|
|
|
|
for interface in interfaces:
|
|
|
|
interface['device_type'] = deviceType
|
|
|
|
try:
|
2020-12-11 19:37:00 +01:00
|
|
|
ifGet = nb.dcim.interface_templates.get(devicetype_id=deviceType,
|
|
|
|
name=interface["name"])
|
2020-02-20 14:52:01 +01:00
|
|
|
if ifGet:
|
2020-12-11 19:37:00 +01:00
|
|
|
print(f'Interface Template Exists: {ifGet.name} - {ifGet.type}'
|
|
|
|
+ f' - {ifGet.device_type.id} - {ifGet.id}')
|
2020-02-20 14:52:01 +01:00
|
|
|
else:
|
|
|
|
ifSuccess = nb.dcim.interface_templates.create(interface)
|
2020-12-11 19:37:00 +01:00
|
|
|
print(f'Interface Template Created: {ifSuccess.name} - '
|
|
|
|
+ f'{ifSuccess.type} - {ifSuccess.device_type.id} - '
|
|
|
|
+ f'{ifSuccess.id}')
|
|
|
|
counter.update({'updated': 1})
|
2020-02-20 14:52:01 +01:00
|
|
|
except pynetbox.RequestError as e:
|
|
|
|
print(e.error)
|
|
|
|
|
2020-12-11 19:37:00 +01:00
|
|
|
|
2020-02-20 14:52:01 +01:00
|
|
|
def createConsolePorts(consoleports, deviceType, nb):
|
|
|
|
for consoleport in consoleports:
|
|
|
|
consoleport['device_type'] = deviceType
|
|
|
|
try:
|
2020-12-11 19:37:00 +01:00
|
|
|
cpGet = nb.dcim.console_port_templates.get(
|
|
|
|
devicetype_id=deviceType, name=consoleport["name"])
|
2020-02-20 14:52:01 +01:00
|
|
|
if cpGet:
|
2020-12-11 19:37:00 +01:00
|
|
|
print(f'Console Port Template Exists: {cpGet.name} - '
|
|
|
|
+ f'{cpGet.type} - {cpGet.device_type.id} - {cpGet.id}')
|
2020-02-20 14:52:01 +01:00
|
|
|
else:
|
|
|
|
cpSuccess = nb.dcim.console_port_templates.create(consoleport)
|
2020-12-11 19:37:00 +01:00
|
|
|
print(f'Console Port Created: {cpSuccess.name} - '
|
|
|
|
+ f'{cpSuccess.type} - {cpSuccess.device_type.id} - '
|
|
|
|
+ f'{cpSuccess.id}')
|
|
|
|
counter.update({'updated': 1})
|
2020-02-20 14:52:01 +01:00
|
|
|
except pynetbox.RequestError as e:
|
|
|
|
print(e.error)
|
|
|
|
|
2020-12-11 19:37:00 +01:00
|
|
|
|
2020-02-20 14:52:01 +01:00
|
|
|
def createPowerPorts(powerports, deviceType, nb):
|
|
|
|
for powerport in powerports:
|
|
|
|
powerport['device_type'] = deviceType
|
|
|
|
try:
|
2020-12-11 19:37:00 +01:00
|
|
|
ppGet = nb.dcim.power_port_templates.get(devicetype_id=deviceType,
|
|
|
|
name=powerport["name"])
|
2020-02-20 14:52:01 +01:00
|
|
|
if ppGet:
|
2020-12-11 19:37:00 +01:00
|
|
|
print(f'Power Port Template Exists: {ppGet.name} - '
|
|
|
|
+ f'{ppGet.type} - {ppGet.device_type.id} - {ppGet.id}')
|
2020-02-20 14:52:01 +01:00
|
|
|
else:
|
|
|
|
ppSuccess = nb.dcim.power_port_templates.create(powerport)
|
2020-12-11 19:37:00 +01:00
|
|
|
print(f'Power Port Created: {ppSuccess.name} - '
|
|
|
|
+ f'{ppSuccess.type} - {ppSuccess.device_type.id} - '
|
|
|
|
+ f'{ppSuccess.id}')
|
|
|
|
counter.update({'updated': 1})
|
2020-02-20 14:52:01 +01:00
|
|
|
except pynetbox.RequestError as e:
|
|
|
|
print(e.error)
|
|
|
|
|
2020-12-11 19:37:00 +01:00
|
|
|
|
2020-02-20 14:52:01 +01:00
|
|
|
def createConsoleServerPorts(consoleserverports, deviceType, nb):
|
|
|
|
for csport in consoleserverports:
|
|
|
|
csport['device_type'] = deviceType
|
|
|
|
try:
|
2020-12-11 19:37:00 +01:00
|
|
|
cspGet = nb.dcim.console_server_port_templates.get(
|
|
|
|
devicetype_id=deviceType, name=csport["name"])
|
2020-02-20 14:52:01 +01:00
|
|
|
if cspGet:
|
2020-12-11 19:37:00 +01:00
|
|
|
print(f'Console Server Port Template Exists: {cspGet.name} - '
|
|
|
|
+ f'{cspGet.type} - {cspGet.device_type.id} - '
|
|
|
|
+ f'{cspGet.id}')
|
2020-02-20 14:52:01 +01:00
|
|
|
else:
|
2020-12-11 19:37:00 +01:00
|
|
|
cspSuccess = nb.dcim.console_server_port_templates.create(
|
|
|
|
csport)
|
|
|
|
print(f'Console Server Port Created: {cspSuccess.name} - '
|
|
|
|
+ f'{cspSuccess.type} - {cspSuccess.device_type.id} - '
|
|
|
|
+ f'{cspSuccess.id}')
|
|
|
|
counter.update({'updated': 1})
|
2020-02-20 14:52:01 +01:00
|
|
|
except pynetbox.RequestError as e:
|
2020-12-11 19:37:00 +01:00
|
|
|
print(e.error)
|
|
|
|
|
2020-02-20 14:52:01 +01:00
|
|
|
|
|
|
|
def createFrontPorts(frontports, deviceType, nb):
|
|
|
|
for frontport in frontports:
|
|
|
|
frontport['device_type'] = deviceType
|
|
|
|
try:
|
2020-12-11 19:37:00 +01:00
|
|
|
fpGet = nb.dcim.front_port_templates.get(devicetype_id=deviceType,
|
|
|
|
name=frontport["name"])
|
2020-02-20 14:52:01 +01:00
|
|
|
if fpGet:
|
2020-12-11 19:37:00 +01:00
|
|
|
print(f'Front Port Template Exists: {fpGet.name} - '
|
|
|
|
+ f'{fpGet.type} - {fpGet.device_type.id} - {fpGet.id}')
|
2020-02-20 14:52:01 +01:00
|
|
|
else:
|
2020-12-11 19:37:00 +01:00
|
|
|
rpGet = nb.dcim.rear_port_templates.get(
|
|
|
|
devicetype_id=deviceType, name=frontport["rear_port"])
|
2020-02-20 14:52:01 +01:00
|
|
|
if rpGet:
|
|
|
|
frontport['rear_port'] = rpGet.id
|
|
|
|
fpSuccess = nb.dcim.front_port_templates.create(frontport)
|
2020-12-11 19:37:00 +01:00
|
|
|
print(f'Front Port Created: {fpSuccess.name} - '
|
|
|
|
+ f'{fpSuccess.type} - {fpSuccess.device_type.id} - '
|
|
|
|
+ f'{fpSuccess.id}')
|
|
|
|
counter.update({'updated': 1})
|
2020-02-20 14:52:01 +01:00
|
|
|
except pynetbox.RequestError as e:
|
2020-12-11 19:37:00 +01:00
|
|
|
print(e.error)
|
|
|
|
|
2020-02-20 14:52:01 +01:00
|
|
|
|
|
|
|
def createRearPorts(rearports, deviceType, nb):
|
|
|
|
for rearport in rearports:
|
|
|
|
rearport['device_type'] = deviceType
|
|
|
|
try:
|
2020-12-11 19:37:00 +01:00
|
|
|
rpGet = nb.dcim.rear_port_templates.get(devicetype_id=deviceType,
|
|
|
|
name=rearport["name"])
|
2020-02-20 14:52:01 +01:00
|
|
|
if rpGet:
|
2020-12-11 19:37:00 +01:00
|
|
|
print(f'Rear Port Template Exists: {rpGet.name} - {rpGet.type}'
|
|
|
|
+ f' - {rpGet.device_type.id} - {rpGet.id}')
|
2020-02-20 14:52:01 +01:00
|
|
|
else:
|
|
|
|
rpSuccess = nb.dcim.rear_port_templates.create(rearport)
|
2020-12-11 19:37:00 +01:00
|
|
|
print(f'Rear Port Created: {rpSuccess.name} - {rpSuccess.type}'
|
|
|
|
+ f' - {rpSuccess.device_type.id} - {rpSuccess.id}')
|
|
|
|
counter.update({'updated': 1})
|
2020-02-20 14:52:01 +01:00
|
|
|
except pynetbox.RequestError as e:
|
|
|
|
print(e.error)
|
|
|
|
|
2020-12-11 19:37:00 +01:00
|
|
|
|
2020-02-20 14:52:01 +01:00
|
|
|
def createDeviceBays(devicebays, deviceType, nb):
|
|
|
|
for devicebay in devicebays:
|
|
|
|
devicebay['device_type'] = deviceType
|
|
|
|
try:
|
2020-12-11 19:37:00 +01:00
|
|
|
dbGet = nb.dcim.device_bay_templates.get(devicetype_id=deviceType,
|
|
|
|
name=devicebay["name"])
|
2020-02-20 14:52:01 +01:00
|
|
|
if dbGet:
|
2020-12-11 19:37:00 +01:00
|
|
|
print(f'Device Bay Template Exists: {dbGet.name} - '
|
|
|
|
+ f'{dbGet.device_type.id} - {dbGet.id}')
|
2020-02-20 14:52:01 +01:00
|
|
|
else:
|
|
|
|
dbSuccess = nb.dcim.device_bay_templates.create(devicebay)
|
2020-12-11 19:37:00 +01:00
|
|
|
print(f'Device Bay Created: {dbSuccess.name} - '
|
|
|
|
+ f'{dbSuccess.device_type.id} - {dbSuccess.id}')
|
|
|
|
counter.update({'updated': 1})
|
2020-02-20 14:52:01 +01:00
|
|
|
except pynetbox.RequestError as e:
|
|
|
|
print(e.error)
|
|
|
|
|
2020-12-11 19:37:00 +01:00
|
|
|
|
2020-02-20 14:52:01 +01:00
|
|
|
def createPowerOutlets(poweroutlets, deviceType, nb):
|
|
|
|
for poweroutlet in poweroutlets:
|
|
|
|
try:
|
2020-12-11 19:37:00 +01:00
|
|
|
poGet = nb.dcim.power_outlet_templates.get(
|
|
|
|
devicetype_id=deviceType, name=poweroutlet["name"])
|
2020-02-20 14:52:01 +01:00
|
|
|
if poGet:
|
2020-12-11 19:37:00 +01:00
|
|
|
print(f'Power Outlet Template Exists: {poGet.name} - '
|
|
|
|
+ f'{poGet.type} - {poGet.device_type.id} - {poGet.id}')
|
2020-02-20 14:52:01 +01:00
|
|
|
else:
|
2020-10-10 21:02:56 +02:00
|
|
|
try:
|
2020-12-11 19:37:00 +01:00
|
|
|
ppGet = nb.dcim.power_port_templates.get(
|
|
|
|
devicetype_id=deviceType)
|
2020-10-10 21:02:56 +02:00
|
|
|
if ppGet:
|
|
|
|
poweroutlet["power_port"] = ppGet.id
|
|
|
|
poweroutlet["device_type"] = deviceType
|
2020-12-11 19:37:00 +01:00
|
|
|
poSuccess = nb.dcim.power_outlet_templates.create(
|
|
|
|
poweroutlet)
|
|
|
|
print(f'Power Outlet Created: {poSuccess.name} - '
|
|
|
|
+ f'{poSuccess.type} - '
|
|
|
|
+ f'{poSuccess.device_type.id} - {poSuccess.id}')
|
|
|
|
counter.update({'updated': 1})
|
2020-10-10 21:02:56 +02:00
|
|
|
except:
|
2020-10-10 21:12:53 +02:00
|
|
|
poweroutlet["device_type"] = deviceType
|
2020-12-11 19:37:00 +01:00
|
|
|
poSuccess = nb.dcim.power_outlet_templates.create(
|
|
|
|
poweroutlet)
|
|
|
|
print(f'Power Outlet Created: {poSuccess.name} - '
|
|
|
|
+ f'{poSuccess.type} - {poSuccess.device_type.id} - '
|
|
|
|
+ f'{poSuccess.id}')
|
|
|
|
counter.update({'updated': 1})
|
2020-02-20 14:52:01 +01:00
|
|
|
except pynetbox.RequestError as e:
|
2020-12-11 19:37:00 +01:00
|
|
|
print(e.error)
|
|
|
|
|
2020-02-20 14:52:01 +01:00
|
|
|
|
|
|
|
def createDeviceTypes(deviceTypes, nb):
|
|
|
|
for deviceType in deviceTypes:
|
|
|
|
try:
|
|
|
|
dtGet = nb.dcim.device_types.get(model=deviceType["model"])
|
|
|
|
if dtGet:
|
2020-12-11 19:37:00 +01:00
|
|
|
print(f'Device Type Exists: {dtGet.manufacturer.name} - '
|
|
|
|
+ f'{dtGet.model} - {dtGet.id}')
|
2020-02-20 14:52:01 +01:00
|
|
|
if "interfaces" in deviceType:
|
|
|
|
createInterfaces(deviceType["interfaces"], dtGet.id, nb)
|
|
|
|
if "power-ports" in deviceType:
|
|
|
|
createPowerPorts(deviceType["power-ports"], dtGet.id, nb)
|
2020-02-26 15:07:07 +01:00
|
|
|
if "power-port" in deviceType:
|
|
|
|
createPowerPorts(deviceType["power-port"], dtGet.id, nb)
|
2020-02-20 14:52:01 +01:00
|
|
|
if "console-ports" in deviceType:
|
2020-12-11 19:37:00 +01:00
|
|
|
createConsolePorts(deviceType["console-ports"],
|
|
|
|
dtGet.id, nb)
|
2020-02-20 14:52:01 +01:00
|
|
|
if "power-outlets" in deviceType:
|
2020-12-11 19:37:00 +01:00
|
|
|
createPowerOutlets(deviceType["power-outlets"],
|
|
|
|
dtGet.id, nb)
|
2020-02-20 14:52:01 +01:00
|
|
|
if "console-server-ports" in deviceType:
|
2020-12-11 19:37:00 +01:00
|
|
|
createConsoleServerPorts(
|
|
|
|
deviceType["console-server-ports"], dtGet.id, nb)
|
2020-02-20 14:52:01 +01:00
|
|
|
if "rear-ports" in deviceType:
|
|
|
|
createRearPorts(deviceType["rear-ports"], dtGet.id, nb)
|
|
|
|
if "front-ports" in deviceType:
|
|
|
|
createFrontPorts(deviceType["front-ports"], dtGet.id, nb)
|
|
|
|
if "device-bays" in deviceType:
|
|
|
|
createDeviceBays(deviceType["device-bays"], dtGet.id, nb)
|
|
|
|
else:
|
|
|
|
dtSuccess = nb.dcim.device_types.create(deviceType)
|
2020-12-11 19:37:00 +01:00
|
|
|
counter.update({'added': 1})
|
|
|
|
print(f'Device Type Created: {dtSuccess.manufacturer.name} - '
|
|
|
|
+ f'{dtSuccess.model} - {dtSuccess.id}')
|
2020-02-20 14:52:01 +01:00
|
|
|
if "interfaces" in deviceType:
|
2020-12-11 19:37:00 +01:00
|
|
|
createInterfaces(deviceType["interfaces"],
|
|
|
|
dtSuccess.id, nb)
|
2020-07-28 15:21:58 +02:00
|
|
|
if "power-ports" in deviceType:
|
2020-12-11 19:37:00 +01:00
|
|
|
createPowerPorts(deviceType["power-ports"],
|
|
|
|
dtSuccess.id, nb)
|
2020-07-28 15:21:58 +02:00
|
|
|
if "power-port" in deviceType:
|
2020-12-11 19:37:00 +01:00
|
|
|
createPowerPorts(deviceType["power-port"],
|
|
|
|
dtSuccess.id, nb)
|
2020-02-20 14:52:01 +01:00
|
|
|
if "console-ports" in deviceType:
|
2020-12-11 19:37:00 +01:00
|
|
|
createConsolePorts(deviceType["console-ports"],
|
|
|
|
dtSuccess.id, nb)
|
2020-02-20 14:52:01 +01:00
|
|
|
if "power-outlets" in deviceType:
|
2020-12-11 19:37:00 +01:00
|
|
|
createPowerOutlets(deviceType["power-outlets"],
|
|
|
|
dtSuccess.id, nb)
|
2020-02-20 14:52:01 +01:00
|
|
|
if "console-server-ports" in deviceType:
|
2020-12-11 19:37:00 +01:00
|
|
|
createConsoleServerPorts(
|
|
|
|
deviceType["console-server-ports"], dtSuccess.id, nb)
|
2020-02-20 14:52:01 +01:00
|
|
|
if "rear-ports" in deviceType:
|
2020-12-11 19:37:00 +01:00
|
|
|
createRearPorts(deviceType["rear-ports"],
|
|
|
|
dtSuccess.id, nb)
|
2020-02-20 14:52:01 +01:00
|
|
|
if "front-ports" in deviceType:
|
2020-12-11 19:37:00 +01:00
|
|
|
createFrontPorts(deviceType["front-ports"],
|
|
|
|
dtSuccess.id, nb)
|
2020-02-20 14:52:01 +01:00
|
|
|
if "device-bays" in deviceType:
|
2020-12-11 19:37:00 +01:00
|
|
|
createDeviceBays(deviceType["device-bays"],
|
|
|
|
dtSuccess.id, nb)
|
2020-02-20 14:52:01 +01:00
|
|
|
except pynetbox.RequestError as e:
|
2020-10-08 22:28:12 +02:00
|
|
|
print(e.error)
|
2020-02-20 14:52:01 +01:00
|
|
|
|
2020-12-11 19:37:00 +01:00
|
|
|
|
2020-02-20 14:52:01 +01:00
|
|
|
try:
|
|
|
|
if os.path.isdir('./repo'):
|
2020-12-11 19:37:00 +01:00
|
|
|
print(f"Package devicetype-library is already installed, "
|
|
|
|
+ f"updating {os.path.join(cwd, 'repo')}")
|
2020-02-20 14:52:01 +01:00
|
|
|
update_package('./repo')
|
|
|
|
else:
|
2020-11-12 20:20:21 +01:00
|
|
|
repo = Repo.clone_from(args.url, os.path.join(cwd, 'repo'))
|
2020-11-12 20:38:48 +01:00
|
|
|
print(f"Package Installed {repo.remotes.origin.url}")
|
2020-02-20 14:52:01 +01:00
|
|
|
except exc.GitCommandError as error:
|
2020-11-12 20:20:21 +01:00
|
|
|
print("Couldn't clone {} ({})".format(args.url, error))
|
2020-02-20 14:52:01 +01:00
|
|
|
|
|
|
|
nb = pynetbox.api(nbUrl, token=nbToken)
|
|
|
|
|
2021-02-09 02:26:59 +01:00
|
|
|
if args.vendors is None:
|
|
|
|
print("No Vendors Specified, Gathering All Device-Types")
|
2020-02-20 14:52:01 +01:00
|
|
|
files, vendors = getFiles()
|
2020-02-26 15:49:12 +01:00
|
|
|
print(str(len(vendors)) + " Vendors Found")
|
2020-10-08 22:28:12 +02:00
|
|
|
print(str(len(files)) + " Device-Types Found")
|
2020-02-20 14:52:01 +01:00
|
|
|
deviceTypes = readYAMl(files)
|
|
|
|
createManufacturers(vendors, nb)
|
|
|
|
createDeviceTypes(deviceTypes, nb)
|
2020-02-26 15:49:12 +01:00
|
|
|
|
2020-02-20 14:52:01 +01:00
|
|
|
else:
|
|
|
|
print("Vendor Specified, Gathering All Matching Device-Types")
|
|
|
|
files, vendors = getFiles(args.vendor)
|
|
|
|
print(str(len(vendors)) + " Vendors Found")
|
|
|
|
print(str(len(files)) + " Device-Types Found")
|
|
|
|
deviceTypes = readYAMl(files)
|
|
|
|
createManufacturers(vendors, nb)
|
|
|
|
createDeviceTypes(deviceTypes, nb)
|
|
|
|
|
2020-10-08 22:28:12 +02:00
|
|
|
print('---')
|
2021-02-27 01:02:03 +01:00
|
|
|
print('Script took {} to run'.format(datetime.now() - startTime))
|
2020-10-08 22:28:12 +02:00
|
|
|
print('{} devices created'.format(counter['added']))
|
|
|
|
print('{} interfaces/ports updated'.format(counter['updated']))
|
|
|
|
print('{} manufacturers created'.format(counter['manufacturer']))
|
2021-02-27 01:02:03 +01:00
|
|
|
|