Skip to content

Commit

Permalink
Merge pull request #34 from netboxlabs/develop
Browse files Browse the repository at this point in the history
release 🚚
  • Loading branch information
leoparente authored Dec 12, 2024
2 parents 7dd325d + 82e3505 commit 75f44e5
Show file tree
Hide file tree
Showing 28 changed files with 430 additions and 628 deletions.
1 change: 1 addition & 0 deletions .github/workflows/device-discovery-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ env:

permissions:
contents: write
issues: write
pull-requests: write

jobs:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/network-discovery-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ env:

permissions:
contents: write
issues: write
pull-requests: write

jobs:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

Orb discovery backends collection

- [device-discovery](./device-discovery/README.md) - TBD
- [network-discovery](./network-discovery/README.md) - TBD
- [device-discovery](./device-discovery/README.md) - Device Discovery Backend that uses [NAPALM](https://github.com/napalm-automation/napalm) Drivers.
- [network-discovery](./network-discovery/README.md) - Network Discovery Backend which is a wrapper over [NMAP](https://nmap.org/) scanner.
75 changes: 42 additions & 33 deletions device-discovery/README.md
Original file line number Diff line number Diff line change
@@ -1,55 +1,64 @@
# device-discovery
Orb device discovery backend

### Config RFC
```yaml
discovery:
config:
target: grpc://localhost:8080/diode
api_key: ${DIODE_API_KEY}
host: 0.0.0.0
port: 8072
### Usage
```bash
usage: device-discovery [-h] [-V] [-s HOST] [-p PORT] -t DIODE_TARGET -k DIODE_API_KEY

Orb Discovery Backend

options:
-h, --help show this help message and exit
-V, --version Display Discovery, NAPALM and Diode SDK versions
-s HOST, --host HOST Server host
-p PORT, --port PORT Server port
-t DIODE_TARGET, --diode-target DIODE_TARGET
Diode target
-k DIODE_API_KEY, --diode-api-key DIODE_API_KEY
Diode API key. Environment variables can be used by wrapping them in ${} (e.g.
${MY_API_KEY})
```

### Policy RFC
```yaml
discovery:
policies:
discovery_1:
config:
schedule: "* * * * *" #Cron expression
defaults:
site: New York NY
scope:
- hostname: 192.168.0.32
username: ${USER}
password: admin
- driver: eos
hostname: 127.0.0.1
username: admin
password: ${ARISTA_PASSWORD}
optional_args:
enable_password: ${ARISTA_PASSWORD}
discover_once: # will run only once
scope:
- hostname: 192.168.0.34
username: ${USER}
password: ${PASSWORD}
policies:
discovery_1:
config:
schedule: "* * * * *" #Cron expression
defaults:
site: New York NY
scope:
- hostname: 192.168.0.32
username: ${USER}
password: admin
- driver: eos
hostname: 127.0.0.1
username: admin
password: ${ARISTA_PASSWORD}
optional_args:
enable_password: ${ARISTA_PASSWORD}
discover_once: # will run only once
scope:
- hostname: 192.168.0.34
username: ${USER}
password: ${PASSWORD}
```
## Run device-discovery
device-discovery can be run by installing it with pip
```sh
git clone https://github.com/netboxlabs/orb-discovery.git
cd orb-discovery/
pip install --no-cache-dir ./device-discovery/
device-discovery -c config.yaml
device-discovery -t 'grpc://192.168.0.10:8080/diode' -k '${DIODE_API_KEY}'
```

## Docker Image
device-discovery can be build and run using docker:
```sh
docker build --no-cache -t device-discovery:develop -f device-discovery/docker/Dockerfile .
docker run -v /local/orb:/usr/local/orb/ -p 8072:8072 device-discovery:develop device-discovery -c /usr/local/orb/config.yaml
cd device-discovery
docker build --no-cache -t device-discovery:develop -f docker/Dockerfile .
docker run -e DIODE_API_KEY={YOUR_API_KEY} -p 8072:8072 device-discovery:develop \
device-discovery -t 'grpc://192.168.0.10:8080/diode' -k '${DIODE_API_KEY}'
```

### Routes (v1)
Expand Down
14 changes: 6 additions & 8 deletions device-discovery/device_discovery/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ def napalm_driver_list() -> list[str]:
"""
List the available NAPALM drivers.
This function scans the installed Python packages to identify NAPALM drivers,
appending their names (with the 'napalm-' prefix removed and hyphens replaced
with underscores) to a list of known drivers.
This function scans the installed Python modules to identify NAPALM drivers,
appending their names (with the 'napalm_' prefix removed) to a list of known drivers.
Returns
-------
Expand All @@ -28,11 +27,10 @@ def napalm_driver_list() -> list[str]:
"""
napalm_packages = ["ios", "eos", "junos", "nxos"]
prefix = "napalm-"
for dist in importlib_metadata.distributions():
if dist.metadata["Name"].startswith(prefix):
package = dist.metadata["Name"][len(prefix) :].replace("-", "_")
napalm_packages.append(package)
prefix = "napalm_"
for dist in importlib_metadata.packages_distributions():
if dist.startswith(prefix):
napalm_packages.append(dist[len(prefix) :])
return napalm_packages


Expand Down
47 changes: 37 additions & 10 deletions device-discovery/device_discovery/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
"""Orb Discovery entry point."""

import argparse
import os
import sys
from importlib.metadata import version

import netboxlabs.diode.sdk.version as SdkVersion
import uvicorn

from device_discovery.client import Client
from device_discovery.parser import parse_config_file
from device_discovery.server import app
from device_discovery.version import version_semver

Expand All @@ -31,23 +31,50 @@ def main():
help="Display Discovery, NAPALM and Diode SDK versions",
)
parser.add_argument(
"-c",
"--config",
metavar="config.yaml",
help="Yaml configuration file",
"-s",
"--host",
default="0.0.0.0",
help="Server host",
type=str,
required=False,
)
parser.add_argument(
"-p",
"--port",
default=8072,
help="Server port",
type=str,
required=False,
)
parser.add_argument(
"-t",
"--diode-target",
help="Diode target",
type=str,
required=True,
)

parser.add_argument(
"-k",
"--diode-api-key",
help="Diode API key. Environment variables can be used by wrapping them in ${} (e.g. ${MY_API_KEY})",
type=str,
required=True,
)
args = parser.parse_args()

try:
cfg = parse_config_file(args.config)
args = parser.parse_args()
api_key = args.diode_api_key
if api_key.startswith("${") and api_key.endswith("}"):
env_var = api_key[2:-1]
api_key = os.getenv(env_var, api_key)

client = Client()
client.init_client(target=cfg.config.target, api_key=cfg.config.api_key)
client.init_client(target=args.diode_target, api_key=args.diode_api_key)
uvicorn.run(
app,
host=cfg.config.host,
port=cfg.config.port,
host=args.host,
port=args.port,
)
except (KeyboardInterrupt, RuntimeError):
pass
Expand Down
120 changes: 0 additions & 120 deletions device-discovery/device_discovery/parser.py

This file was deleted.

24 changes: 23 additions & 1 deletion device-discovery/device_discovery/policy/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"""Orb Discovery Policy Manager."""

import logging
import os

import yaml

from device_discovery.parser import resolve_env_vars
from device_discovery.policy.models import Policy, PolicyRequest
from device_discovery.policy.runner import PolicyRunner

Expand All @@ -15,6 +15,28 @@
logger = logging.getLogger(__name__)


def resolve_env_vars(config):
"""
Recursively resolve environment variables in the configuration.
Args:
----
config (dict): The configuration dictionary.
Returns:
-------
dict: The configuration dictionary with environment variables resolved.
"""
if isinstance(config, dict):
return {k: resolve_env_vars(v) for k, v in config.items()}
if isinstance(config, list):
return [resolve_env_vars(i) for i in config]
if isinstance(config, str) and config.startswith("${") and config.endswith("}"):
env_var = config[2:-1]
return os.getenv(env_var, config)
return config

class PolicyManager:
"""Policy Manager class."""

Expand Down
Loading

0 comments on commit 75f44e5

Please sign in to comment.