Skip to content

Commit

Permalink
chore: move sync-srv-redfish-intfs-to-nb into python package
Browse files Browse the repository at this point in the history
Moved the sync-srv-redfish-intfs-to-nb code into the our workflows python
package. This required fixing up some places to craft the necessary
entry points as well as setting dependencies. Removed the building of
the container for the old directory and add it into the common one.
  • Loading branch information
cardoe committed Jul 29, 2024
1 parent 378689b commit 916148b
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 91 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/build-container-images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,6 @@ jobs:
labels: |
org.opencontainers.image.version=${{ env.VERSION_OBM_UTILS }}
- name: Build and deploy Python 3.11 with Nautobot Int Sync
uses: docker/build-push-action@v5
with:
context: argo-workflows/sync-srv-redfish-intfs-to-nb
file: argo-workflows/sync-srv-redfish-intfs-to-nb/containers/Dockerfile.nautobot_int_sync
# push for all main branch commits
push: ${{ github.event_name != 'pull_request' }}
tags: ghcr.io/rackerlabs/understack/nautobot-interfaces-sync:latest,ghcr.io/rackerlabs/understack/nautobot-interfaces-sync:${{ env.VERSION_PYTHON_NAUTOBOT_INT_SYNC }}
labels: |
org.opencontainers.image.version=${{ env.VERSION_PYTHON_NAUTOBOT_INT_SYNC }}
- name: Build and deploy Python 3.11 with PyNautobot
uses: docker/build-push-action@v5
with:
Expand Down
Empty file.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ spec:
templates:
- name: synchronize-interfaces
container:
image: ghcr.io/rackerlabs/understack/nautobot-interfaces-sync:0.0.1
image: ghcr.io/rackerlabs/understack/ironic-nautobot-client:latest
command:
- python
- /app/sync_nautobot_interfaces.py
- sync-nautobot-interfaces
args: ["--hostname", "{{workflow.parameters.device_hostname}}"]
volumeMounts:
- mountPath: /etc/nb-token/
Expand Down
3 changes: 3 additions & 0 deletions python/understack-workflows/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ dependencies = [
"pydantic==2.8.0",
"pynautobot==2.2.0",
"python-ironicclient==5.5.0",
"requests==2.32.3",
"sushy==5.1.1",
]

[project.scripts]
synchronize-interfaces = "understack_workflows.main.synchronize_interfaces:main"
synchronize-obm-creds = "understack_workflows.main.synchronize_obm_creds:main"
synchronize-server = "understack_workflows.main.synchronize_server:main"
sync-nautobot-interfaces = "understack_workflows.main.sync_nautobot_interfaces:main"

[build-system]
requires = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import argparse
import logging
import os
import sushy
import sys

import sushy

logger = logging.getLogger(__name__)


Expand All @@ -23,8 +24,7 @@ def arg_parser(name):
parser = argparse.ArgumentParser(
prog=os.path.basename(name), description="Nautobot Interface sync"
)
parser.add_argument("--hostname", required=True,
help="Nautobot device name")
parser.add_argument("--hostname", required=True, help="Nautobot device name")
parser.add_argument("--oob_username", required=False, help="OOB username")
parser.add_argument("--oob_password", required=False, help="OOB password")
parser.add_argument("--nautobot_url", required=False)
Expand All @@ -39,7 +39,7 @@ def exit_with_error(error):

def credential(subpath, item):
try:
return open(f"/etc/{subpath}/{item}", "r").read().strip()
return open(f"/etc/{subpath}/{item}").read().strip()
except FileNotFoundError:
exit_with_error(f"{subpath} {item} not found in mounted files")

Expand All @@ -57,7 +57,4 @@ def oob_sushy_session(oob_ip, oob_username, oob_password):


def is_off_board(interface):
return (
"Embedded ALOM" in interface.location
or "Embedded" not in interface.location
)
return "Embedded ALOM" in interface.location or "Embedded" not in interface.location
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import sushy
from models import Chassis
from helpers import (
setup_logger,
arg_parser,
credential,
oob_sushy_session,
exit_with_error,
is_off_board,
)
from nautobot import Nautobot

from understack_workflows.helpers import arg_parser
from understack_workflows.helpers import credential
from understack_workflows.helpers import exit_with_error
from understack_workflows.helpers import is_off_board
from understack_workflows.helpers import oob_sushy_session
from understack_workflows.helpers import setup_logger
from understack_workflows.models import Chassis

logger = setup_logger(__name__)


Expand All @@ -34,9 +33,7 @@ def main():
exit_with_error(e)

interfaces = [
interface
for interface in chassis.network_interfaces
if is_off_board(interface)
interface for interface in chassis.network_interfaces if is_off_board(interface)
]

nautobot.bulk_create_interfaces(device_name, interfaces)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

from dataclasses import dataclass

from sushy import Sushy
from sushy.exceptions import ResourceNotFoundError
from sushy.resources.chassis.chassis import Chassis as SushyChassis
from sushy.resources.system.network.adapter import NetworkAdapter
from sushy.resources.system.network.port import NetworkPort
from sushy.resources.chassis.chassis import Chassis as SushyChassis


class ManufacturerNotSupported(Exception):
Expand All @@ -22,10 +24,7 @@ class NIC:
def from_redfish(cls, data: NetworkAdapter) -> NIC:
location = cls.nic_location(data)
nic = cls(data.identity, location, [], data.model)
nic.interfaces = [
Interface.from_redfish(i, nic)
for i in cls.nic_ports(data)
]
nic.interfaces = [Interface.from_redfish(i, nic) for i in cls.nic_ports(data)]
return nic

@classmethod
Expand Down Expand Up @@ -89,9 +88,11 @@ def from_hp_json(cls, data: dict, nic: NIC, ports: list) -> Interface:
def fetch_macaddr_from_sys_resource(cls, data: NetworkPort) -> str:
try:
path = f"{data.root.get_system().ethernet_interfaces.path}/{data.identity}"
macaddr = data.root.get_system().ethernet_interfaces.get_member(path).mac_address
macaddr = (
data.root.get_system().ethernet_interfaces.get_member(path).mac_address
)
except ResourceNotFoundError:
macaddr = ''
macaddr = ""
return macaddr


Expand Down Expand Up @@ -130,8 +131,7 @@ def from_redfish(cls, oob_obj: Sushy) -> Chassis:

chassis = cls(chassis_data.name, [], [])
chassis.nics = [
NIC.from_redfish(i)
for i in chassis_data.network_adapters.get_members()
NIC.from_redfish(i) for i in chassis_data.network_adapters.get_members()
]
chassis.network_interfaces = cls.interfaces_from_nics(chassis.nics)
return chassis
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import logging
import pynautobot
import requests
import sys
from typing import Protocol

import pynautobot
import requests
from pynautobot.core.api import Api as NautobotApi
from pynautobot.models.dcim import Devices as NautobotDevice
from pynautobot.models.dcim import Interfaces as NautobotInterface
Expand Down Expand Up @@ -43,7 +44,6 @@ def device_oob_interface(
self,
device: NautobotDevice,
) -> NautobotInterface:

oob_intf = self.session.dcim.interfaces.get(
device_id=device.id, name=["iDRAC", "iLO"]
)
Expand Down Expand Up @@ -73,31 +73,24 @@ def construct_interfaces_payload(
device_id: str,
device_name: str,
) -> list[dict]:

payload = []
for interface in interfaces:
nautobot_intf = self.session.dcim.interfaces.get(
device_id=device_id, name=interface.name
)
if nautobot_intf is None:
self.logger.info(
f"{interface.name} was NOT found for "
f"{device_name}, creating..."
)
payload.append(
self.interface_payload_data(device_id, interface)
f"{interface.name} was NOT found for " f"{device_name}, creating..."
)
payload.append(self.interface_payload_data(device_id, interface))
else:
self.logger.info(
f"{nautobot_intf.name} found in Nautobot for "
f"{device_name}, no action will be taken."
)
return payload

def interface_payload_data(
self, device_id: str, interface: Interface
) -> dict:

def interface_payload_data(self, device_id: str, interface: Interface) -> dict:
return {
"device": device_id,
"name": interface.name,
Expand All @@ -111,9 +104,7 @@ def bulk_create_interfaces(
self, device_name: str, interfaces: list[Interface]
) -> list[NautobotInterface] | None:
device = self.device(device_name)
payload = self.construct_interfaces_payload(
interfaces, device.id, device.name
)
payload = self.construct_interfaces_payload(interfaces, device.id, device.name)
if payload:
try:
req = self.session.dcim.interfaces.create(payload)
Expand Down

0 comments on commit 916148b

Please sign in to comment.