Skip to content

Commit 0604f3e

Browse files
feat: Read Minion ID from Client's salt conf
1 parent 3bbd6b4 commit 0604f3e

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

examples/general/read_minion_id.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""Example to demonstrate reading the minion ID from the Salt configuration."""
2+
3+
from nisystemlink.clients.core.helpers import read_minion_id
4+
5+
# Read the minion ID from the Salt configuration file
6+
minion_id = read_minion_id()
7+
8+
if minion_id:
9+
print(f"Minion ID: {minion_id}")
10+
else:
11+
print("Minion ID not found. Please ensure the SystemLink client is connected to the Server.")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from ._iterator_file_like import IteratorFileLike
2+
from ._minion_id import read_minion_id
23

34
# flake8: noqa
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""Helper function to get minion ID from Salt configuration."""
4+
5+
from typing import Optional
6+
7+
from nisystemlink.clients.core._internal._path_constants import PathConstants
8+
9+
10+
def read_minion_id() -> Optional[str]:
11+
"""Read the minion ID from the Salt configuration.
12+
13+
Returns:
14+
str | None: The minion ID content if the file exists, None otherwise.
15+
"""
16+
minion_id_path = PathConstants.salt_data_directory / "conf" / "minion_id"
17+
18+
if not minion_id_path.exists():
19+
return None
20+
21+
try:
22+
with open(minion_id_path, "r", encoding="utf-8") as fp:
23+
return fp.read().strip()
24+
except (OSError, PermissionError):
25+
return None

0 commit comments

Comments
 (0)