From 8d4a2c10df3019450852df1aafe1b202659dd4a2 Mon Sep 17 00:00:00 2001 From: Martin Dimov Date: Thu, 23 Nov 2023 13:02:20 +0000 Subject: [PATCH] attemp to resolve service aliases issue if used inconsistently, env back --- files/docker/systemctl.py | 24 +++++++++++++++++++++++- files/docker/systemctl3.py | 24 +++++++++++++++++++++++- types/systemctl3.pyi | 1 + 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/files/docker/systemctl.py b/files/docker/systemctl.py index abd47bb..9be1a93 100755 --- a/files/docker/systemctl.py +++ b/files/docker/systemctl.py @@ -1329,6 +1329,7 @@ def __init__(self): self._loaded_file_sysd = {} # /etc/systemd/system/name.service => config data self._file_for_unit_sysv = None # name.service => /etc/init.d/name self._file_for_unit_sysd = None # name.service => /etc/systemd/system/name.service + self._sysd_alias = None #map od aliases of services (aliased => realservice) self._preset_file_list = None # /etc/systemd/system-preset/* => file content self._default_target = DefaultTarget self._sysinit_target = None # stores a UnitConf() @@ -1433,6 +1434,7 @@ def scan_unit_sysd_files(self, module = None): # -> [ unit-names,... ] """ reads all unit files, returns the first filename for the unit given """ if self._file_for_unit_sysd is None: self._file_for_unit_sysd = {} + self._sysd_alias = {} for folder in self.sysd_folders(): if not folder: continue @@ -1446,6 +1448,12 @@ def scan_unit_sysd_files(self, module = None): # -> [ unit-names,... ] service_name = name if service_name not in self._file_for_unit_sysd: self._file_for_unit_sysd[service_name] = path + if os.path.islink(path): + path_target = os.readlink(path) + if path_target.endswith(".service"): + service_name_target = os.path.basename(path_target) + self._sysd_alias[service_name] = service_name_target + logg.debug("alias found %s => %s", service_name, service_name_target) logg.debug("found %s sysd files", len(self._file_for_unit_sysd)) return list(self._file_for_unit_sysd.keys()) def scan_unit_sysv_files(self, module = None): # -> [ unit-names,... ] @@ -1467,10 +1475,15 @@ def scan_unit_sysv_files(self, module = None): # -> [ unit-names,... ] self._file_for_unit_sysv[service_name] = path logg.debug("found %s sysv files", len(self._file_for_unit_sysv)) return list(self._file_for_unit_sysv.keys()) + def _getRealModuleName(self, module): + if self._sysd_alias is None: + self.scan_unit_sysd_files() + return module if not module in self._sysd_alias else self._sysd_alias[module] def unit_sysd_file(self, module = None): # -> filename? """ file path for the given module (systemd) """ self.scan_unit_sysd_files() assert self._file_for_unit_sysd is not None + module = self._getRealModuleName(module) if module and module in self._file_for_unit_sysd: return self._file_for_unit_sysd[module] if module and unit_of(module) in self._file_for_unit_sysd: @@ -1573,7 +1586,8 @@ def load_sysd_unit_conf(self, module): # -> conf? for name in sorted(drop_in_files): path = drop_in_files[name] data.read_sysd(path) - conf = SystemctlConf(data, module) + module_real = module if not module in self._sysd_alias else self._sysd_alias[module] + conf = SystemctlConf(data, module_real) conf.masked = masked conf.nonloaded_path = path # if masked conf.drop_in_files = drop_in_files @@ -6575,6 +6589,14 @@ def run(command, *modules): exitcode = is_not_ok(systemctl.enable_modules(*modules)) elif command in ["environment"]: print_str_dict(systemctl.environment_of_unit(*modules)) + elif command in ["get-environment"]: + result = systemctl.get_environment_modules(*modules) + if result is int: exitcode = result + elif result is str: print_str(result) + elif command in ["set-environment"]: + exitcode = systemctl.set_environment_modules(*modules) + elif command in ["unset-environment"]: + exitcode = systemctl.unset_environment_modules(*modules) elif command in ["get-default"]: print_str(systemctl.get_default_target()) elif command in ["get-preset"]: diff --git a/files/docker/systemctl3.py b/files/docker/systemctl3.py index 62e9259..76c8d55 100755 --- a/files/docker/systemctl3.py +++ b/files/docker/systemctl3.py @@ -1329,6 +1329,7 @@ def __init__(self): self._loaded_file_sysd = {} # /etc/systemd/system/name.service => config data self._file_for_unit_sysv = None # name.service => /etc/init.d/name self._file_for_unit_sysd = None # name.service => /etc/systemd/system/name.service + self._sysd_alias = None #map od aliases of services (aliased => realservice) self._preset_file_list = None # /etc/systemd/system-preset/* => file content self._default_target = DefaultTarget self._sysinit_target = None # stores a UnitConf() @@ -1433,6 +1434,7 @@ def scan_unit_sysd_files(self, module = None): # -> [ unit-names,... ] """ reads all unit files, returns the first filename for the unit given """ if self._file_for_unit_sysd is None: self._file_for_unit_sysd = {} + self._sysd_alias = {} for folder in self.sysd_folders(): if not folder: continue @@ -1446,6 +1448,12 @@ def scan_unit_sysd_files(self, module = None): # -> [ unit-names,... ] service_name = name if service_name not in self._file_for_unit_sysd: self._file_for_unit_sysd[service_name] = path + if os.path.islink(path): + path_target = os.readlink(path) + if path_target.endswith(".service"): + service_name_target = os.path.basename(path_target) + self._sysd_alias[service_name] = service_name_target + logg.debug("alias found %s => %s", service_name, service_name_target) logg.debug("found %s sysd files", len(self._file_for_unit_sysd)) return list(self._file_for_unit_sysd.keys()) def scan_unit_sysv_files(self, module = None): # -> [ unit-names,... ] @@ -1467,10 +1475,15 @@ def scan_unit_sysv_files(self, module = None): # -> [ unit-names,... ] self._file_for_unit_sysv[service_name] = path logg.debug("found %s sysv files", len(self._file_for_unit_sysv)) return list(self._file_for_unit_sysv.keys()) + def _getRealModuleName(self, module): + if self._sysd_alias is None: + self.scan_unit_sysd_files() + return module if not module in self._sysd_alias else self._sysd_alias[module] def unit_sysd_file(self, module = None): # -> filename? """ file path for the given module (systemd) """ self.scan_unit_sysd_files() assert self._file_for_unit_sysd is not None + module = self._getRealModuleName(module) if module and module in self._file_for_unit_sysd: return self._file_for_unit_sysd[module] if module and unit_of(module) in self._file_for_unit_sysd: @@ -1573,7 +1586,8 @@ def load_sysd_unit_conf(self, module): # -> conf? for name in sorted(drop_in_files): path = drop_in_files[name] data.read_sysd(path) - conf = SystemctlConf(data, module) + module_real = module if not module in self._sysd_alias else self._sysd_alias[module] + conf = SystemctlConf(data, module_real) conf.masked = masked conf.nonloaded_path = path # if masked conf.drop_in_files = drop_in_files @@ -6575,6 +6589,14 @@ def run(command, *modules): exitcode = is_not_ok(systemctl.enable_modules(*modules)) elif command in ["environment"]: print_str_dict(systemctl.environment_of_unit(*modules)) + elif command in ["get-environment"]: + result = systemctl.get_environment_modules(*modules) + if result is int: exitcode = result + elif result is str: print_str(result) + elif command in ["set-environment"]: + exitcode = systemctl.set_environment_modules(*modules) + elif command in ["unset-environment"]: + exitcode = systemctl.unset_environment_modules(*modules) elif command in ["get-default"]: print_str(systemctl.get_default_target()) elif command in ["get-preset"]: diff --git a/types/systemctl3.pyi b/types/systemctl3.pyi index 421463c..46f2f86 100644 --- a/types/systemctl3.pyi +++ b/types/systemctl3.pyi @@ -270,6 +270,7 @@ class Systemctl: _loaded_file_sysd: Dict[str, SystemctlConf] = ... _file_for_unit_sysv: Optional[Dict[str, str]] = ... _file_for_unit_sysd: Optional[Dict[str, str]] = ... + _sysd_alias: Optional[Dict[str, str]] = ... _preset_file_list: Optional[Dict[str, PresetFile]] = ... _default_target: str = ... _sysinit_target: Optional[SystemctlConf] = ...