Containerlab Worker
ContainerlabWorker(inventory: str, broker: str, worker_name: str, exit_event=None, init_done_event=None, log_level: str = None, log_queue: object = None)
ยค
Bases: NFPWorker
FastAPContainerlabWorker IWorker is a worker class that integrates with containerlab to run network topologies.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inventory
|
str
|
Inventory configuration for the worker. |
required |
broker
|
str
|
Broker URL to connect to. |
required |
worker_name
|
str
|
Name of this worker. |
required |
exit_event
|
Event
|
Event to signal worker to stop/exit. |
None
|
init_done_event
|
Event
|
Event to signal when worker is done initializing. |
None
|
log_level
|
str
|
Logging level for this worker. |
None
|
log_queue
|
object
|
Queue for logging. |
None
|
Source code in norfab\workers\containerlab_worker.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
|
worker_exit()
ยค
Terminates the current process by sending a SIGTERM signal to itself.
This method retrieves the current process ID using os.getpid()
and then
sends a SIGTERM signal to terminate the process using os.kill()
.
Source code in norfab\workers\containerlab_worker.py
64 65 66 67 68 69 70 71 |
|
get_version()
ยค
Produce a report of the versions of various Python packages.
This method collects the versions of several specified Python packages and returns them in a dictionary.
Returns:
Name | Type | Description |
---|---|---|
Result |
An object containing the task name and a dictionary with the package names as keys and their respective versions as values. |
Source code in norfab\workers\containerlab_worker.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
|
get_inventory() -> Dict
ยค
Retrieve the inventory of the Containerlab worker.
Returns:
Name | Type | Description |
---|---|---|
Dict |
Dict
|
A dictionary containing the combined inventory of Containerlab. |
Source code in norfab\workers\containerlab_worker.py
113 114 115 116 117 118 119 120 121 122 123 |
|
get_containerlab_status() -> Result
ยค
Retrieve the status of the Containerlab worker.
Returns:
Name | Type | Description |
---|---|---|
Result |
Result
|
A result object containing the status of the Containerlab worker. |
Source code in norfab\workers\containerlab_worker.py
125 126 127 128 129 130 131 132 133 134 135 136 |
|
get_running_labs(timeout: int = None) -> Result
ยค
Retrieve a list of running containerlab lab names.
This method inspects the current state of containerlab and returns the names of labs that are currently running. The names are sorted and duplicates are removed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout
|
int
|
The timeout value in seconds for the inspection operation. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Result |
Result
|
A Result object containing the task name and a list of running |
Result
|
lab names. |
Source code in norfab\workers\containerlab_worker.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
run_containerlab_command(args: list, cwd: str = None, timeout: int = 600, ret: Result = None, env: dict = None) -> Tuple
ยค
Executes a containerlab command using subprocess and processes its output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args
|
list
|
The list of command-line arguments to execute. |
required |
cwd
|
str
|
The working directory to execute the command in. Defaults to None. |
None
|
timeout
|
int
|
The timeout for the command execution in seconds. Defaults to None. |
600
|
ret
|
Result
|
An optional Norfab result object to populate with the command's output. Defaults to None. |
None
|
env
|
(dict, Optional)
|
OS Environment variables ti use when running the process |
None
|
Returns:
Name | Type | Description |
---|---|---|
Tuple |
Tuple
|
If |
Result |
Tuple
|
If |
Raises:
Type | Description |
---|---|
Exception
|
If the output cannot be parsed as JSON when |
Notes
- The method reads the command's standard error line by line and processes messages containing "msg=".
- If the command fails (non-zero return code), the
ret.failed
attribute is set to True, and errors are populated. - If the command succeeds, the
ret.messages
attribute is populated with log messages.
Source code in norfab\workers\containerlab_worker.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
|
deploy(topology: str, reconfigure: bool = False, timeout: int = None, node_filter: str = None) -> Result
ยค
Deploys a containerlab topology.
This method handles the deployment of a containerlab topology by downloading
the topology file, organizing it into a specific folder structure, and executing
the containerlab deploy
command with the appropriate arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
topology
|
str
|
The path to the topology file to be deployed. |
required |
reconfigure
|
bool
|
If True, reconfigures an already deployed lab. Defaults to False. |
False
|
timeout
|
int
|
The timeout in seconds for the deployment process. Defaults to None (no timeout). |
None
|
node_filter
|
str
|
A filter to specify which nodes to deploy. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Result |
Result
|
deployment results with a list of nodes deployed |
Raises:
Type | Description |
---|---|
Exception
|
If the topology file cannot be fetched. |
Source code in norfab\workers\containerlab_worker.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
|
destroy_lab(lab_name: str, timeout: int = None) -> Result
ยค
Destroys a specified lab.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lab_name
|
str
|
The name of the lab to be destroyed. |
required |
timeout
|
int
|
The timeout value in seconds for the operation. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Result |
Result
|
An object containing the status of the operation, errors (if any), and the result indicating whether the lab was successfully destroyed. |
Behavior
- Retrieves the lab details using the
inspect
method. - If the lab is not found, marks the operation as failed and returns an error.
- If the lab is found, retrieves the topology file and its folder.
- Executes the
containerlab destroy
command using the topology file. - Updates the result to indicate success or failure of the destruction process.
Source code in norfab\workers\containerlab_worker.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
|
inspect(lab_name: str = None, timeout: int = None, details: bool = False) -> Result
ยค
Inspect the container lab containers configuration and status.
This method retrieves information about a specific container lab or all container labs, optionally including detailed information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lab_name
|
str
|
The name of the container lab to inspect. If not provided, all container labs will be inspected. |
None
|
timeout
|
int
|
The maximum time in seconds to wait for the inspection command to complete. Defaults to None. |
None
|
details
|
bool
|
Whether to include detailed information in the inspection output. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
Result |
Result
|
An object containing the result of the inspection task. |
Source code in norfab\workers\containerlab_worker.py
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
|
save(lab_name: str, timeout: int = None) -> Result
ยค
Saves the config of a specified lab devices by invoking the containerlab save
command.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lab_name
|
str
|
The name of the lab to save. |
required |
timeout
|
int
|
The maximum time in seconds to wait for the operation to complete. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Result |
Result
|
An object containing the outcome of the save operation. If successful,
|
Source code in norfab\workers\containerlab_worker.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
|
restart_lab(lab_name: str, timeout: int = None) -> Result
ยค
Restart a specified Containerlab lab.
This method retrieves the lab details, destroys the existing lab, and redeploys it using the provided topology file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lab_name
|
str
|
The name of the lab to restart. |
required |
timeout
|
int
|
The timeout value for the operation in seconds. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Result |
Result
|
An object containing the status of the operation, any errors encountered, and the result indicating whether the lab was successfully restarted. |
Source code in norfab\workers\containerlab_worker.py
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
|
get_nornir_inventory(lab_name: str = None, timeout: int = None, groups: list = None, use_default_credentials: bool = True) -> Result
ยค
Retrieves the Nornir inventory for a specified lab.
This method inspects the container lab environment and generates a Nornir-compatible inventory of hosts based on the lab's configuration. It maps containerlab node kinds to Netmiko SSH platform types and extracts relevant connection details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lab_name
|
str
|
The name of the container lab to inspect. If not given loads inventory for all labs. |
None
|
timeout
|
int
|
The timeout value for the inspection operation. Defaults to None. |
None
|
groups
|
list
|
A list of group names to assign to the hosts in the inventory. Defaults to None. |
None
|
use_default_credentials
|
bool
|
Use Containerlab default credentials for hosts ot not. |
True
|
Returns:
Name | Type | Description |
---|---|---|
Result |
Result
|
A |
Result
|
includes a dictionary with host details. If the lab is not found or an error occurs, |
|
Result
|
the |
Notes
- The method uses a predefined mapping (
norfab.utils.platform_map
) to translate containerlab node kinds to Netmiko platform types. - If a container's SSH port cannot be determined, it is skipped, and an error is logged.
- The primary host IP address is determined dynamically using a UDP socket connection or by checking the host IP address in the container's port configuration.
Example of returned inventory structure
{ "hosts": { "host_name": { "hostname": "host_ip", "platform": "netmiko_platform", "groups": ["group1", "group2"], }, ...
Source code in norfab\workers\containerlab_worker.py
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
|
deploy_netbox(lab_name: str = None, tenant: str = None, filters: list = None, devices: list = None, instance: str = None, image: str = None, ipv4_subnet: str = '172.100.100.0/24', ports: tuple = (12000, 15000), progress: bool = False, reconfigure: bool = False, timeout: int = 600, node_filter: str = None, dry_run: bool = False) -> Result
ยค
Deploys a containerlab topology using device data from the Netbox database.
This method orchestrates the deployment of a containerlab topology by:
- Inspecting existing containers to determine subnets and ports in use.
- Allocating a management IPv4 subnet for the new lab, avoiding conflicts.
- Downloading inventory data from Netbox for the specified lab and filters.
- Saving the generated topology file to a dedicated folder.
- Executing the
containerlab deploy
command with appropriate arguments.
To retrieve topology data from Netbox at least one of these arguments must be provided to identify a set of devices to include into Containerlab topology:
tenant
- deploys lab using all devices and links that belong to this tenantdevices
- lab deployed only using devices in the listsfilters
- list of device filters to retrieve from Netbox
If multiple of above arguments provided, resulting lab topology is a sum of all devices matched.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lab_name
|
str
|
The name to use for the lab to deploy. |
None
|
tenant
|
str
|
Deploy lab for given tenant, lab name if not set becomes equal to tenant name. |
None
|
filters
|
list
|
List of filters to apply when fetching devices from Netbox. |
None
|
devices
|
list
|
List of specific devices to include in the topology. |
None
|
instance
|
str
|
Netbox instance identifier. |
None
|
image
|
str
|
Container image to use for devices. |
None
|
ipv4_subnet
|
str
|
Management IPv4 subnet for the lab. |
'172.100.100.0/24'
|
ports
|
tuple
|
Tuple specifying the range of ports to allocate. |
(12000, 15000)
|
progress
|
bool
|
If True, emits progress events. |
False
|
reconfigure
|
bool
|
If True, reconfigures an already deployed lab. |
False
|
timeout
|
int
|
Timeout in seconds for the deployment process. |
600
|
node_filter
|
str
|
Comma-separated string of nodes to deploy. |
None
|
dry_run
|
bool
|
If True, only generates and returns the topology inventory without deploying. |
False
|
Returns:
Name | Type | Description |
---|---|---|
Result |
Result
|
deployment results with a list of nodes deployed |
Raises:
Type | Description |
---|---|
Exception
|
If the topology file cannot be fetched. |
Source code in norfab\workers\containerlab_worker.py
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 |
|