Merge pull request #17 from ndom91/update-readme-env

Update readme env
This commit is contained in:
minitriga 2021-03-02 21:34:07 +00:00 committed by GitHub
commit b499a9770b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 112 additions and 72 deletions

3
.env.example Normal file
View File

@ -0,0 +1,3 @@
NETBOX_URL=
NETBOX_TOKEN=
REPO_URL=https://github.com/netbox-community/devicetype-library.git

4
.gitignore vendored
View File

@ -129,4 +129,6 @@ dmypy.json
.pyre/
# Editor
.vscode
.vscode
repo

21
LICENSE Normal file
View File

@ -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.

73
README.md Normal file
View File

@ -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.9.4, 2.10.4
## 🪄 Description
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
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

View File

@ -1,5 +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
@ -7,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")
@ -19,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):
@ -357,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']))

View File

@ -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
```

View File

@ -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

View File

@ -1,10 +1,13 @@
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:
raise EnvironmentError("Failed because {} is not set.".format(var))
raise EnvironmentError("Failed because {} is not set.".format(var))