Skip to content

Commit

Permalink
attemp to resolve service aliases issue if used inconsistently, env back
Browse files Browse the repository at this point in the history
  • Loading branch information
dimov-cz committed Nov 23, 2023
1 parent b49d38b commit 8d4a2c1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
24 changes: 23 additions & 1 deletion files/docker/systemctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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,... ]
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"]:
Expand Down
24 changes: 23 additions & 1 deletion files/docker/systemctl3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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,... ]
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"]:
Expand Down
1 change: 1 addition & 0 deletions types/systemctl3.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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] = ...
Expand Down

0 comments on commit 8d4a2c1

Please sign in to comment.