Skip to content

Commit 28e4593

Browse files
committed
refactor: support headless env configuration
Signed-off-by: thxCode <[email protected]>
1 parent 2138a38 commit 28e4593

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

gpustack_runtime/envs.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import contextlib
44
import os
55
from functools import lru_cache
6-
from os import getenv
6+
from os import getenv as sys_getenv
77
from pathlib import Path
88
from typing import TYPE_CHECKING, Any
99

@@ -670,6 +670,55 @@ def ternary(
670670
return false_callback()
671671

672672

673+
_ENV_PREFIX = "GPUSTACK_RUNTIME_"
674+
675+
676+
def getenv(key: str, default=None) -> any | None:
677+
"""
678+
Get the value of an environment variable.
679+
Try headless module variable if the key starts with "GPUSTACK_RUNTIME_".
680+
681+
Args:
682+
key:
683+
The environment variable key.
684+
default:
685+
The default value if the key is not found.
686+
687+
Returns:
688+
The value of the environment variable if it exists, otherwise None.
689+
690+
"""
691+
value = sys_getenv(key)
692+
if value is not None:
693+
return value
694+
if key.startswith(_ENV_PREFIX):
695+
headless_key = key.removeprefix(_ENV_PREFIX)
696+
return sys_getenv(headless_key, default)
697+
return default
698+
699+
700+
def getenvs(keys: list[str], default=None) -> any | None:
701+
"""
702+
Get the value of an environment variable.
703+
Return the first found value among the provided keys.
704+
705+
Args:
706+
keys:
707+
The environment variable key(s).
708+
default:
709+
The default value if none of the keys are found.
710+
711+
Returns:
712+
The value of the environment variable if it exists, otherwise None.
713+
714+
"""
715+
for key in keys:
716+
value = getenv(key)
717+
if value is not None:
718+
return value
719+
return default
720+
721+
673722
def get_os_release() -> str:
674723
"""
675724
Get the operating system release information.

0 commit comments

Comments
 (0)