mirror of
				https://github.com/kevinveenbirkenbach/computer-playbook.git
				synced 2025-10-31 10:19:09 +00:00 
			
		
		
		
	refactor: improve get_service_name suffix handling and handler usage
- Updated filter_plugins/get_service_name.py: * Default suffix handling: auto-select .service (no '@') or .timer (with '@') * Explicit False disables suffix entirely * Explicit string suffix still supported - Updated sys-systemctl handler to use new filter instead of SYS_SERVICE_SUFFIX - Extended unit tests to cover new suffix behavior Ref: https://chat.openai.com/share/8c2de9e6-daa0-44dd-ae13-d7a7d8d8b6d9
This commit is contained in:
		| @@ -1,23 +1,36 @@ | ||||
| # filter_plugins/get_service_name.py | ||||
| """ | ||||
| Custom Ansible filter to build a systemctl unit name (always lowercase). | ||||
|  | ||||
| Rules: | ||||
| - If `systemctl_id` ends with '@': drop the '@' and return | ||||
|   "{systemctl_id_without_at}.{software_name}@.{suffix}". | ||||
| - Else: return "{systemctl_id}{software_name}.{suffix}". | ||||
|   "{systemctl_id_without_at}.{software_name}@{suffix_handling}". | ||||
| - Else: return "{systemctl_id}.{software_name}{suffix_handling}". | ||||
|  | ||||
| Suffix handling: | ||||
| - Default "" → automatically pick: | ||||
|     - ".service" if no '@' in systemctl_id | ||||
|     - ".timer"  if '@' in systemctl_id | ||||
| - Explicit False → no suffix at all | ||||
| - Any string → ".{suffix}" (lowercased) | ||||
| """ | ||||
|  | ||||
| def get_service_name(systemctl_id, software_name, suffix="service"): | ||||
| def get_service_name(systemctl_id, software_name, suffix=""): | ||||
|     sid = str(systemctl_id).strip().lower() | ||||
|     sw  = str(software_name).strip().lower() | ||||
|     sfx = str(suffix).strip().lower() | ||||
|  | ||||
|     if sid.endswith('@'): | ||||
|         base = sid[:-1]  # drop the trailing '@' | ||||
|         return f"{base}.{sw}@.{sfx}" | ||||
|     # Determine suffix | ||||
|     if suffix is False: | ||||
|         sfx = ""  # no suffix at all | ||||
|     elif suffix == "" or suffix is None: | ||||
|         sfx = ".timer" if sid.endswith("@") else ".service" | ||||
|     else: | ||||
|         return f"{sid}.{sw}.{sfx}" | ||||
|         sfx = "." + str(suffix).strip().lower() | ||||
|  | ||||
|     if sid.endswith("@"): | ||||
|         base = sid[:-1]  # drop the trailing '@' | ||||
|         return f"{base}.{sw}@{sfx}" | ||||
|     else: | ||||
|         return f"{sid}.{sw}{sfx}" | ||||
|  | ||||
|  | ||||
| class FilterModule(object): | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| - name: "refresh systemctl service" | ||||
|   systemd: | ||||
|     name: "{{ systemctl_id }}{{ SYS_SERVICE_SUFFIX }}" | ||||
|     name: "{{ systemctl_id | get_service_name(SOFTWARE_NAME) }}" | ||||
|     daemon_reload: yes | ||||
|     enabled: yes | ||||
|     state: "{{ systemctl_state }}" | ||||
|   | ||||
| @@ -2,30 +2,32 @@ import unittest | ||||
| from filter_plugins import get_service_name | ||||
|  | ||||
| class TestGetServiceName(unittest.TestCase): | ||||
|     def test_normal_service(self): | ||||
|         # Expect a dot between id and software name | ||||
|     def test_default_suffix_service(self): | ||||
|         self.assertEqual( | ||||
|             get_service_name.get_service_name("sys-ctl-cln-backups", "nginx"), | ||||
|             "sys-ctl-cln-backups.nginx.service" | ||||
|         ) | ||||
|  | ||||
|     def test_normal_service_custom_suffix(self): | ||||
|         self.assertEqual( | ||||
|             get_service_name.get_service_name("sys-ctl-cln-backups", "nginx", "timer"), | ||||
|             "sys-ctl-cln-backups.nginx.timer" | ||||
|         ) | ||||
|  | ||||
|     def test_with_at_suffix(self): | ||||
|         # If systemctl_id ends with '@', @ is moved behind software name | ||||
|     def test_default_suffix_timer(self): | ||||
|         self.assertEqual( | ||||
|             get_service_name.get_service_name("sys-ctl-bkp@", "postgres"), | ||||
|             "sys-ctl-bkp.postgres@.service" | ||||
|             "sys-ctl-bkp.postgres@.timer" | ||||
|         ) | ||||
|  | ||||
|     def test_with_at_and_custom_suffix(self): | ||||
|     def test_explicit_custom_suffix(self): | ||||
|         self.assertEqual( | ||||
|             get_service_name.get_service_name("sys-ctl-bkp@", "postgres", "timer"), | ||||
|             "sys-ctl-bkp.postgres@.timer" | ||||
|             get_service_name.get_service_name("sys-ctl-bkp@", "postgres", "special"), | ||||
|             "sys-ctl-bkp.postgres@.special" | ||||
|         ) | ||||
|  | ||||
|     def test_explicit_false_suffix(self): | ||||
|         self.assertEqual( | ||||
|             get_service_name.get_service_name("sys-ctl-bkp@", "postgres", False), | ||||
|             "sys-ctl-bkp.postgres@" | ||||
|         ) | ||||
|         self.assertEqual( | ||||
|             get_service_name.get_service_name("sys-ctl-cln-backups", "nginx", False), | ||||
|             "sys-ctl-cln-backups.nginx" | ||||
|         ) | ||||
|  | ||||
|     def test_case_is_lowered(self): | ||||
|   | ||||
		Reference in New Issue
	
	Block a user