Skip to content

Commit

Permalink
extract generic nautobot-update-cf image
Browse files Browse the repository at this point in the history
  • Loading branch information
skrobul committed Jul 29, 2024
1 parent 8543f5e commit 4a23c10
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 44 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build-container-images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ jobs:
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
- name: Build and deploy Python 3.11 with PyNautobot nautobot-update-cf
uses: docker/build-push-action@v5
with:
context: argo-workflows/ironic-to-nautobot-sync
file: argo-workflows/ironic-to-nautobot-sync/containers/Dockerfile
context: argo-workflows/nautobot-update-cf
file: argo-workflows/nautobot-update-cf/containers/Dockerfile
# push for all main branch commits
push: ${{ github.event_name != 'pull_request' }}
tags: ghcr.io/rackerlabs/understack/ironic-to-nautobot-sync:latest,ghcr.io/rackerlabs/understack/ironic-to-nautobot-sync:${{ env.VERSION_PYTHON_NAUTOBOT }}
tags: ghcr.io/rackerlabs/understack/nautobot-update-cf:latest,ghcr.io/rackerlabs/understack/nautobot-update-cf:${{ env.VERSION_PYTHON_NAUTOBOT }}
labels: |
org.opencontainers.image.version=${{ env.VERSION_PYTHON_NAUTOBOT }}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ spec:
templates:
- name: synchronize-state
container:
image: ghcr.io/rackerlabs/understack/ironic-to-nautobot-sync:latest
image: ghcr.io/rackerlabs/understack/nautobot-update-cf:latest
command:
- python
- /app/main.py
args:
- --device_uuid
- "{{workflow.parameters.device_uuid}}"
- --provisioning-state
- --field-name
- "ironic_provisioning_status"
- --field-value
- "{{workflow.parameters.provision_state}}"
volumeMounts:
- mountPath: /etc/nb-token/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def arg_parser(name):
)
parser.add_argument("--device_uuid", required=True,
help="Nautobot device UUID")
parser.add_argument("--provisioning-state", required=True)
parser.add_argument("--field-name", required=True)
parser.add_argument("--field-value", required=True)
parser.add_argument("--nautobot_url", required=False)
parser.add_argument("--nautobot_token", required=False)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ def main():

default_nb_url = "http://nautobot-default.nautobot.svc.cluster.local"
device_uuid = args.device_uuid
provision_state = args.provisioning_state
field_name = args.field_name
field_value = args.field_value
nb_url = args.nautobot_url or default_nb_url
nb_token = args.nautobot_token or credential("nb-token", "token")

nautobot = Nautobot(nb_url, nb_token, logger=logger)
nautobot.update_status(device_uuid, provision_state)
nautobot.update_cf(device_uuid, field_name, field_value)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,11 @@
import pynautobot
import requests
import sys
from typing import Protocol
from pynautobot.core.api import Api as NautobotApi
from pynautobot.models.dcim import Devices as NautobotDevice


class Nautobot:
ALLOWED_STATUSES = [
"enroll",
"verifying",
"manageable",
"inspecting",
"inspect wait",
"inspect failed",
"cleaning",
"clean wait",
"available",
"deploying",
"wait call-back",
"deploy failed",
"active",
"deleting",
"error",
"adopting",
"rescuing",
"rescue wait",
"rescue failed",
"rescue",
"unrescuing",
"unrescue failed",
"servicing",
"service wait",
"service failed",
]
def __init__(self, url, token, logger=None, session=None):
self.url = url
self.token = token
Expand All @@ -58,13 +31,9 @@ def device_by_id(self, device_id: str) -> NautobotDevice:
self.exit_with_error(f"Device {device_id} not found in Nautobot")
return device

def update_status(self, device_id, new_status: str):
def update_cf(self, device_id, field_name: str, field_value: str):
device = self.device_by_id(device_id)

if new_status not in self.ALLOWED_STATUSES:
raise Exception(f"Status {new_status} for device {device_id} is not in ALLOWED_STATUSES.")

device.custom_fields['ironic_provisioning_status'] = new_status
device.custom_fields[field_name] = field_value
response = device.save()
print(f"save result: {response}")
return response
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ RUN --mount=type=cache,target=/root/.cache/.pip pip install --no-cache-dir -r /a
FROM ${BASE} as prod

LABEL org.opencontainers.image.title="Python 3.11 image with pynautobot"
LABEL org.opencontainers.image.base.name="ghcr.io/rackerlabs/understack/ironic-to-nautobot-sync-python3.11.8"
LABEL org.opencontainers.image.base.name="ghcr.io/rackerlabs/understack/nautobot-update-cf"
LABEL org.opencontainers.image.source=https://github.com/rackerlabs/understack


Expand Down

0 comments on commit 4a23c10

Please sign in to comment.