diff --git a/.env.example b/.env.example index 0043ee4..5025ea1 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,6 @@ NETBOX_URL= NETBOX_TOKEN= REPO_URL=https://github.com/netbox-community/devicetype-library.git +REPO_BRANCH=master IGNORE_SSL_ERRORS=False #SLUGS=c9300-48u isr4431 isr4331 diff --git a/README.md b/README.md index 58315fd..390c1c8 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ docker build -t netbox-devicetype-import-library . The container supports the following env var as configuration : - `REPO_URL`, the repo to look for device types (defaults to _https://github.com/netbox-community/devicetype-library.git_) +- `REPO_BRANCH`, the branch to check out if appropriate, defaults to master. - `NETBOX_URL`, used to access netbox - `NETBOX_TOKEN`, token for accessing netbox - `VENDORS`, a space-separated list of vendors to import (defaults to None) diff --git a/nb-dt-import.py b/nb-dt-import.py index 6a51b86..1ffca5c 100755 --- a/nb-dt-import.py +++ b/nb-dt-import.py @@ -14,11 +14,12 @@ import sys counter = Counter(added=0, updated=0, manufacturer=0) -def update_package(path: str): +def update_package(path: str, branch: str): try: repo = Repo(path) if repo.remotes.origin.url.endswith('.git'): repo.remotes.origin.pull() + repo.git.checkout(branch) print(f"Pulled Repo {repo.remotes.origin.url}") except exc.InvalidGitRepositoryError: pass @@ -394,7 +395,9 @@ def main(): VENDORS = settings.VENDORS REPO_URL = settings.REPO_URL + SLUGS = settings.SLUGS + REPO_BRANCH = settings.REPO_BRANCH parser = argparse.ArgumentParser(description='Import Netbox Device Types') parser.add_argument('--vendors', nargs='+', default=VENDORS, @@ -403,7 +406,8 @@ def main(): help="Git URL with valid Device Type YAML files") parser.add_argument('--slugs', nargs='+', default=SLUGS, help="List of device-type slugs to import eg. ap4431 ws-c3850-24t-l") - + parser.add_argument('--branch', default=REPO_BRANCH, + help="Git branch to use from repo") args = parser.parse_args() try: @@ -412,7 +416,7 @@ def main(): + f"updating {os.path.join(cwd, 'repo')}") update_package('./repo') else: - repo = Repo.clone_from(args.url, os.path.join(cwd, 'repo')) + repo = Repo.clone_from(args.url, os.path.join(cwd, 'repo'), branch=args.branch) print(f"Package Installed {repo.remotes.origin.url}") except exc.GitCommandError as error: print("Couldn't clone {} ({})".format(args.url, error)) diff --git a/settings.py b/settings.py index 34a9092..58a747c 100644 --- a/settings.py +++ b/settings.py @@ -3,6 +3,7 @@ from dotenv import load_dotenv load_dotenv() REPO_URL = os.getenv("REPO_URL") +REPO_BRANCH = os.getenv("REPO_BRANCH", "master") NETBOX_URL = os.getenv("NETBOX_URL") NETBOX_TOKEN = os.getenv("NETBOX_TOKEN") IGNORE_SSL_ERRORS = (os.getenv("IGNORE_SSL_ERRORS", "False") == "True")