Skip to content

Integrate NetBox VM management#340

Merged
Bobzemob merged 6 commits into
mainfrom
cmyers_netbox
Jun 12, 2026
Merged

Integrate NetBox VM management#340
Bobzemob merged 6 commits into
mainfrom
cmyers_netbox

Conversation

@cmyers-mieweb

@cmyers-mieweb cmyers-mieweb commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

Add NetBox integration to register and remove LXC records that mirror managed containers.

  • Add utils/netbox.js implementing NetBox API helpers: nbFetch, findClusterId, createVirtualMachine, deleteVirtualMachine, and withNetbox (loads netbox_url/netbox_token from Settings).
  • Use withNetbox/createVirtualMachine in bin/create-container.js to register a VM after container creation (non-fatal; errors are warned).
  • Use withNetbox/deleteVirtualMachine in routers/api/v1/containers.js to remove the VM when a container is deleted.
  • Expose new settings keys (netbox_url, netbox_token) in routers/api/v1/settings.js for GET/PUT so NetBox credentials can be configured.

The NetBox deletion helper is resilient to errors (logs but does not throw) to avoid blocking core workflows.

NetBox Integration — How It Works

All NetBox logic lives in utils/netbox.js. It's invoked from two places: bin/create-container.js (on create) and routers/api/v1/containers.js (on delete).


Prerequisites (manual setup in NetBox)

The integration is opt-in — it silently does nothing if these two settings are absent from the Settings table:

Setting key Value
netbox_url Base URL, e.g. https://netbox.example.com
netbox_token API token with write access to IPAM + Virtualization

The integration also currently expects a cluster to already exist in NetBox whose name matches your Site.name. The cluster is required; site and device are looked up opportunistically.


On container creation (create-container.js)

Triggered after the container is running and has an IP address. Steps:

flowchart TD
    A[withNetbox: check settings] -->|missing| Z[skip silently]
    A -->|present| B[Parallel lookups]
    B --> C[findClusterId — required\nmatches Site.name]
    B --> D[findSiteId — optional\nmatches Site.name in /dcim/sites/]
    B --> E[findDeviceId — optional\nmatches Node.name in /dcim/devices/]
    C --> F[POST /virtualization/virtual-machines/\nname=hostname, cluster, site?, device?\ncomments = created-by username]
    F --> G[POST /virtualization/interfaces/\neth0 on the VM]
    G --> H[POST /ipam/ip-addresses/\nassigned to eth0, CIDR notation]
    H --> I[PATCH virtual-machine\nset primary_ip4]
Loading
  • If findClusterId fails (cluster not found), the whole call throws — caught by the try/catch in create-container.js and logged as a non-fatal warning. The container is still marked running.
  • site and device are only included in the VM body if the lookups return a result. Missing ones are silently omitted.

On container deletion (containers.js DELETE handler)

flowchart TD
    A[withNetbox: check settings] -->|missing| Z[skip silently]
    A -->|present| B[GET /virtualization/virtual-machines/?name=hostname]
    B -->|not found| C[log and return]
    B -->|found| D[GET /virtualization/interfaces/?virtual_machine_id=id]
    D --> E[For each interface:\nGET assigned IP addresses]
    E --> F[DELETE each IP address]
    F --> G[DELETE each interface]
    G --> H[DELETE virtual-machine]
Loading

Deletion is fully non-throwing — any error is caught, logged to stderr, and the primary container deletion proceeds regardless.


What ends up in NetBox per container

NetBox object Value
virtualization.virtual-machine name = hostname, cluster = site name, site = site (if found), device = Proxmox node (if found), comments = "This container was built using opensource-server\nCreated by: <username>"
virtualization.interface name = eth0, attached to the VM
ipam.ip-address Container's IPv4 in /32 CIDR, assigned to eth0, set as primary_ip4

Add NetBox integration to register and remove LXC records that mirror managed containers.

- Add utils/netbox.js implementing NetBox API helpers: nbFetch, findClusterId, createVirtualMachine, deleteVirtualMachine, and withNetbox (loads netbox_url/netbox_token from Settings).
- Use withNetbox/createVirtualMachine in bin/create-container.js to register a VM after container creation (non-fatal; errors are warned).
- Use withNetbox/deleteVirtualMachine in routers/api/v1/containers.js to remove the VM when a container is deleted.
- Expose new settings keys (netbox_url, netbox_token) in routers/api/v1/settings.js for GET/PUT so NetBox credentials can be configured.

The NetBox deletion helper is resilient to errors (logs but does not throw) to avoid blocking core workflows.
Add helpers to resolve NetBox site and device IDs (findSiteId, findDeviceId) and extend createVirtualMachine to accept nodeName. The code now concurrently resolves cluster, site, and device IDs and includes site/device fields in the VM payload when available. Also pass nodeName from the container script when creating the NetBox VM record so the Proxmox node can be mapped to the NetBox device.
Expose NetBox URL/token in settings and types, add UI inputs and defaults; include vcpus/memory/disk when creating VMs and export a new updateVirtualMachine helper to patch resources by hostname. Also tighten hostname validation on the container form and improve API error toast details. Minor: include **/node_modules in .dockerignore.
@cmyers-mieweb

Copy link
Copy Markdown
Collaborator Author
image image image

Comment thread create-a-container/bin/create-container.js Outdated
Comment thread create-a-container/utils/netbox.js
Replace hardcoded vCPU/memory/disk values with actual provisioned values read from the LXC config. Add parseRootfsSizeGb helper that parses Proxmox LXC `rootfs` size strings (supports T/G/M/K, defaults to GB) and returns rounded gigabytes or null if unparseable. Update create-container flow to set vcpus, memoryMb and diskGb from the container config. Add a design note in netbox utils documenting why standalone functions (stateless Token auth) are used instead of a NetBoxApi class.
@cmyers-mieweb

Copy link
Copy Markdown
Collaborator Author

@copilot resolve the merge conflicts in this pull request

Copilot AI commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Resolved. I merged main into this branch and fixed the conflict in .dockerignore in commit fb18491.

@cmyers-mieweb cmyers-mieweb marked this pull request as ready for review June 12, 2026 20:21
@cmyers-mieweb

Copy link
Copy Markdown
Collaborator Author

@copilot resolve the merge conflicts in this pull request

@Bobzemob Bobzemob enabled auto-merge June 12, 2026 20:29

Copilot AI commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Resolved. I merged the latest main into this branch and fixed the conflict in create-a-container/client/src/pages/settings/SettingsPage.tsx in commit 9a4a8dc.

auto-merge was automatically disabled June 12, 2026 20:29

Head branch was pushed to by a user without write access

@Bobzemob Bobzemob enabled auto-merge June 12, 2026 20:29
@Bobzemob Bobzemob merged commit 4e25507 into main Jun 12, 2026
7 of 8 checks passed
@Bobzemob Bobzemob deleted the cmyers_netbox branch June 12, 2026 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants