Skip to content

Commit fc66088

Browse files
authored
Merge pull request #29 from srl-labs/env_vars
Add environment variable expansion to topology loader and new utility…
2 parents 1a40bb8 + 3e02796 commit fc66088

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

core/data/topology_loader.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import yaml
22
import logging
3+
from core.utils.env_expander import (
4+
expand_env_vars,
5+
) # if you put the function above in env_expander.py
36

47
logger = logging.getLogger(__name__)
58

@@ -10,29 +13,36 @@ class TopologyLoaderError(Exception):
1013

1114
class TopologyLoader:
1215
"""
13-
Loads containerlab topology data from a YAML file.
16+
Loads containerlab topology data from a YAML file, expanding environment variables.
1417
"""
1518

1619
def load(self, input_file: str) -> dict:
1720
"""
18-
Load the containerlab YAML topology file.
21+
Load the containerlab YAML topology file, including environment-variable expansion.
1922
2023
:param input_file: Path to the containerlab YAML file.
21-
:return: Parsed containerlab topology data.
24+
:return: Parsed containerlab topology data with env variables expanded.
2225
:raises TopologyLoaderError: If file not found or parse error occurs.
2326
"""
2427
logger.debug(f"Loading topology from file: {input_file}")
2528
try:
2629
with open(input_file, "r") as file:
27-
containerlab_data = yaml.safe_load(file)
30+
raw_content = file.read()
31+
32+
# Expand ${VAR:=default} placeholders before parsing
33+
expanded_content = expand_env_vars(raw_content)
34+
35+
containerlab_data = yaml.safe_load(expanded_content)
2836
logger.debug("Topology successfully loaded.")
2937
return containerlab_data
38+
3039
except FileNotFoundError:
3140
error_message = (
3241
f"Error: The specified clab file '{input_file}' does not exist."
3342
)
3443
logger.error(error_message)
3544
raise TopologyLoaderError(error_message)
45+
3646
except Exception as e:
3747
error_message = f"An error occurred while loading the config: {e}"
3848
logger.error(error_message)

core/utils/env_expander.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
import re
3+
4+
5+
def expand_env_vars(content: str) -> str:
6+
"""
7+
Expand placeholders of the form ${VAR:=default} or ${VAR:default} in the given string.
8+
9+
Examples:
10+
- ${FOO} will be replaced by os.environ.get("FOO", "")
11+
- ${FOO:=bar} will be replaced by os.environ.get("FOO", "bar")
12+
- ${FOO:bar} means the same as above (Containerlab also accepts : or :=)
13+
"""
14+
pattern = re.compile(r"\$\{([^:}]+):?=?([^}]*)\}")
15+
16+
def replacer(match):
17+
var_name = match.group(1)
18+
default_val = match.group(2)
19+
# If the environment variable exists, use it; otherwise, use the default.
20+
return os.environ.get(var_name, default_val)
21+
22+
return pattern.sub(replacer, content)

lab-examples/lab_with_vars.clab.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: srl2
2+
# node names will be srl1 and srl2 unless overridden by CLAB_PREFIX
3+
prefix: ${CLAB_PREFIX:=""}
4+
5+
topology:
6+
nodes:
7+
srl1:
8+
kind: nokia_srlinux
9+
image: ghcr.io/nokia/srlinux:${SRL_VERSION:=latest}
10+
type: ${SRL_TYPE:=ixrd3l}
11+
12+
srl2:
13+
kind: nokia_srlinux
14+
image: ghcr.io/nokia/srlinux:${SRL_VERSION:=latest}
15+
type: ${SRL_TYPE:=ixrd3l}
16+
17+
links:
18+
- endpoints: [srl1:e1-1, srl2:e1-1]
19+
- endpoints: [srl1:e1-2, srl2:e1-2]

0 commit comments

Comments
 (0)