From f8a078220c8936846e8171dcc12c48e8663884d3 Mon Sep 17 00:00:00 2001 From: ndom91 Date: Fri, 26 Feb 2021 23:26:32 +0100 Subject: [PATCH 1/5] update: README and added dotenv other small stuff --- .env.example | 2 ++ .gitignore | 4 ++- LICENSE | 21 ++++++++++++++ README.md | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ nb-dt-import.py | 1 + readme.md | 68 -------------------------------------------- requirements.txt | 1 + settings.py | 4 ++- 8 files changed, 104 insertions(+), 70 deletions(-) create mode 100644 .env.example create mode 100644 LICENSE create mode 100644 README.md delete mode 100644 readme.md diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..83cd7be --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +NETBOX_URL= +NETBOX_TOKEN= diff --git a/.gitignore b/.gitignore index 3481434..6d362a2 100644 --- a/.gitignore +++ b/.gitignore @@ -129,4 +129,6 @@ dmypy.json .pyre/ # Editor -.vscode \ No newline at end of file +.vscode + +repo diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4db3295 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Alexander Gittings + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..cf4179d --- /dev/null +++ b/README.md @@ -0,0 +1,73 @@ +# Netbox Device Type Import + +This library is intended to be your friend and help you import all the device-types defined within the the [NetBox Device Type Library Repository](https://github.com/netbox-community/devicetype-library). + +> Tested working with 2.7.8, 2.8.8, 2.9.4, 2.10.4 + +## 🪄 Description + +These instructions will clone a copy of the `netbox-community/devicetype-library` repository to your machine to allow you to import the device types you would like without copy and pasting them into the NetBox UI. + +### 🚀 Getting Started + +1. This script is written in Python, so lets setup a virtual environment. + +``` +git clone https://github.com/minitriga/Netbox-Device-Type-Library-Import +cd Netbox-Device-Type-Library-Import +python3 -m venv venv +source venv/bin/activate +``` + +2. Now that we have the basics setup, we'll need to install the requirements. + +``` +pip install -r requirements.txt +``` + +3. There are two variables that are required when using this script to import device types into your Netbox installation. (1) Your Netbox instance URL and (2) a token with **write rights**. + +Copy the existing `.env.example` to your own `.env` file, and fill in the variables. + +``` +cp .env.example .env +vim .env +``` + +Finally, we are able to execute the script and import some device templates! + +### 🔌 Usage + +To use the script, simply execute the script as follows. Make sure you're still in the activated virtual environment we created before. + +``` +./nb-dt-import.py +``` + +This will clone the latest master branch from the `netbox-community/devicetype-library` from Github and install it into the `repo` subdirectory. If this directory already exists, it will perform a `git pull` to update the reposity instead. + +Next, it will loop over every manufacturer and every device of every manufacturer and begin checking if your Netbox install already has them, and if not, creates them. It will skip preexisting manufacturers, devices, interfaces, etc. so as to not end up with duplicate entries in your Netbox instance. + +#### 🧰 Arguments + +This script currently accepts a list of vendors as an arugment, so that you can selectively import devices. + +To import only device by APC, for example: + +``` +./nb-dt-import.py --vendors apc +``` + +`--vendors` can also accept a space separated list of vendors if you want to import multiple. + +``` +./nb-dt-import.py --vendors apc juniper +``` + +### 🧑‍💻 Contributing + +We're happy about any pull requests! + +### 📜 License + +MIT diff --git a/nb-dt-import.py b/nb-dt-import.py index f944a18..f0cde9b 100755 --- a/nb-dt-import.py +++ b/nb-dt-import.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 from git import Repo, exc, RemoteProgress from collections import Counter import yaml diff --git a/readme.md b/readme.md deleted file mode 100644 index b869301..0000000 --- a/readme.md +++ /dev/null @@ -1,68 +0,0 @@ -# Netbox Device Type Import - -This library is intended to be your friend and help you import all the device-types defined within the the [NetBox Device Type Library Repository](https://github.com/netbox-community/devicetype-library). - -> Tested working with 2.7.8, 2.8.8, 2.9.4 - -## Getting Started - -These instructions will get you a copy of the project on your machine to allow you to import the device types you would like without copy and pasting them into the NetBox UI. - -### Prerequisites - -This script is written in python so this must be installed. - -``` -Python3 -Python PIP -``` - -## Using the Repo - -Cloning the repo - -``` -git clone https://github.com/minitriga/Netbox-Device-Type-Library-Import.git -``` - -Installing the requirements - -``` -cd Netbox-Device-Type-Library-Import -pip install -r requirements.txt -``` - -### Setting your variables - -There are a number of variables that are required when using this script to import device types into your netbox environment. - -``` -export NETBOX_URL=http://netbox.company.com -export NETBOX_TOKEN=0123456789abcdef0123456789abcdef01234567 -``` - -### Using the script - -To use the script simply run the following. - -``` -python nb-dt-import.py -``` - -This will pull the device-type library from Gitlab and install it into the `.repo` directory. if this directory is already there it will perform a git pull to update the reposity. - -#### Arguments - -This script currently accepts vendors so that only a few vendors are imported into your NetBox Environment. - -This can be done the following. - -``` -python nb-dt-import.py --vendors apc -``` - -`--vendors` also accepts a list of vendors so that multiple vendors could be imported. - -``` -python nb-dt-import.py --vendors apc juniper -``` diff --git a/requirements.txt b/requirements.txt index ec39f10..1f6a5bc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,3 +11,4 @@ requests==2.22.0 six==1.14.0 smmap2==2.0.5 urllib3==1.25.8 +python-dotenv==0.15.0 diff --git a/settings.py b/settings.py index 2713c09..7012e62 100644 --- a/settings.py +++ b/settings.py @@ -1,4 +1,6 @@ import os +from dotenv import load_dotenv +load_dotenv() NETBOX_URL = str(os.getenv("NETBOX_URL")) NETBOX_TOKEN = str(os.getenv("NETBOX_TOKEN")) @@ -7,4 +9,4 @@ MANDATORY_ENV_VARS = ["NETBOX_URL", "NETBOX_TOKEN"] for var in MANDATORY_ENV_VARS: if var not in os.environ: - raise EnvironmentError("Failed because {} is not set.".format(var)) \ No newline at end of file + raise EnvironmentError("Failed because {} is not set.".format(var)) From ef4c0fa5155041ad7af15d333054d4ecd45cba3c Mon Sep 17 00:00:00 2001 From: ndom91 Date: Sat, 27 Feb 2021 01:02:03 +0100 Subject: [PATCH 2/5] add: script timer + devicetype library repo url to .env --- .env.example | 1 + nb-dt-import.py | 6 +++++- settings.py | 3 ++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 83cd7be..70e1d3e 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,3 @@ NETBOX_URL= NETBOX_TOKEN= +REPO_URL=https://github.com/netbox-community/devicetype-library.git diff --git a/nb-dt-import.py b/nb-dt-import.py index f0cde9b..08f3427 100755 --- a/nb-dt-import.py +++ b/nb-dt-import.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 from git import Repo, exc, RemoteProgress from collections import Counter +from datetime import datetime import yaml import pynetbox import glob @@ -8,7 +9,7 @@ import argparse import os import settings -REPO_URL = 'https://github.com/netbox-community/devicetype-library.git' +REPO_URL = settings.REPO_URL parser = argparse.ArgumentParser(description='Import Netbox Device Types') parser.add_argument('--vendor', nargs='+', help="List of vendors to import eg. apc cisco") @@ -20,6 +21,7 @@ cwd = os.getcwd() counter = Counter(added=0, updated=0, manufacturer=0) nbUrl = settings.NETBOX_URL nbToken = settings.NETBOX_TOKEN +startTime = datetime.now() def update_package(path: str): @@ -358,6 +360,8 @@ else: createDeviceTypes(deviceTypes, nb) print('---') +print('Script took {} to run'.format(datetime.now() - startTime)) print('{} devices created'.format(counter['added'])) print('{} interfaces/ports updated'.format(counter['updated'])) print('{} manufacturers created'.format(counter['manufacturer'])) + diff --git a/settings.py b/settings.py index 7012e62..5785129 100644 --- a/settings.py +++ b/settings.py @@ -2,10 +2,11 @@ import os from dotenv import load_dotenv load_dotenv() +REPO_URL = str(os.getenv("REPO_URL")) NETBOX_URL = str(os.getenv("NETBOX_URL")) NETBOX_TOKEN = str(os.getenv("NETBOX_TOKEN")) -MANDATORY_ENV_VARS = ["NETBOX_URL", "NETBOX_TOKEN"] +MANDATORY_ENV_VARS = ["REPO_URL", "NETBOX_URL", "NETBOX_TOKEN"] for var in MANDATORY_ENV_VARS: if var not in os.environ: From c8279ff878e0bdf6a8b714fca0548c88d8295491 Mon Sep 17 00:00:00 2001 From: ndom91 Date: Sat, 27 Feb 2021 10:48:05 +0100 Subject: [PATCH 3/5] update: README --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index cf4179d..532af4c 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This library is intended to be your friend and help you import all the device-ty These instructions will clone a copy of the `netbox-community/devicetype-library` repository to your machine to allow you to import the device types you would like without copy and pasting them into the NetBox UI. -### 🚀 Getting Started +## 🚀 Getting Started 1. This script is written in Python, so lets setup a virtual environment. @@ -36,7 +36,7 @@ vim .env Finally, we are able to execute the script and import some device templates! -### 🔌 Usage +## 🔌 Usage To use the script, simply execute the script as follows. Make sure you're still in the activated virtual environment we created before. @@ -48,7 +48,7 @@ This will clone the latest master branch from the `netbox-community/devicetype-l Next, it will loop over every manufacturer and every device of every manufacturer and begin checking if your Netbox install already has them, and if not, creates them. It will skip preexisting manufacturers, devices, interfaces, etc. so as to not end up with duplicate entries in your Netbox instance. -#### 🧰 Arguments +### 🧰 Arguments This script currently accepts a list of vendors as an arugment, so that you can selectively import devices. @@ -64,10 +64,10 @@ To import only device by APC, for example: ./nb-dt-import.py --vendors apc juniper ``` -### 🧑‍💻 Contributing +## 🧑‍💻 Contributing We're happy about any pull requests! -### 📜 License +## 📜 License MIT From 602f9d43895b0cb435f5b9eecdad792ac72ac159 Mon Sep 17 00:00:00 2001 From: ndom91 Date: Sat, 27 Feb 2021 11:28:15 +0100 Subject: [PATCH 4/5] fix: remove old versions that dont work anymore --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 532af4c..93fe755 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ This library is intended to be your friend and help you import all the device-types defined within the the [NetBox Device Type Library Repository](https://github.com/netbox-community/devicetype-library). -> Tested working with 2.7.8, 2.8.8, 2.9.4, 2.10.4 +> Tested working with 2.9.4, 2.10.4 ## 🪄 Description From 965ad5cc6544440c1ec563e85f76fd590062a048 Mon Sep 17 00:00:00 2001 From: ndom91 Date: Sat, 27 Feb 2021 15:18:42 +0100 Subject: [PATCH 5/5] fix: wording --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 93fe755..49f4eac 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This library is intended to be your friend and help you import all the device-ty ## 🪄 Description -These instructions will clone a copy of the `netbox-community/devicetype-library` repository to your machine to allow you to import the device types you would like without copy and pasting them into the NetBox UI. +This script will clone a copy of the `netbox-community/devicetype-library` repository to your machine to allow it to import the device types you would like without copy and pasting them into the Netbox UI. ## 🚀 Getting Started