mirror of
				https://github.com/kevinveenbirkenbach/computer-playbook.git
				synced 2025-10-31 10:19:09 +00:00 
			
		
		
		
	sys-ctl-rpr-docker-hard: Refactor restart script with argparse & update systemd ExecStart
- Removed unused soft restart function and switched to argparse-based CLI. - Added --only argument to selectively restart subdirectories. - Updated systemctl service template to pass PATH_DOCKER_COMPOSE_INSTANCES as argument. - Ensures service unit correctly invokes the Python script with target path. See conversation: https://chatgpt.com/share/68a771d9-5fd8-800f-a410-08132699cc3a
This commit is contained in:
		| @@ -1,17 +1,7 @@ | ||||
| import os | ||||
| import sys | ||||
| import subprocess | ||||
|  | ||||
| def restart_docker_services(dir_path): | ||||
|     """ | ||||
|     Restart docker-compose services in the given directory. | ||||
|     """ | ||||
|     try: | ||||
|         print(f"Restarting docker-compose services in: {dir_path}") | ||||
|         subprocess.run(["docker-compose", "restart"], cwd=dir_path, check=True) | ||||
|         print(f"Services restarted successfully in: {dir_path}") | ||||
|     except subprocess.CalledProcessError as e: | ||||
|         print(f"Error restarting services in {dir_path}: {e}") | ||||
| import argparse | ||||
|  | ||||
| def hard_restart_docker_services(dir_path): | ||||
|     """ | ||||
| @@ -26,28 +16,45 @@ def hard_restart_docker_services(dir_path): | ||||
|     except subprocess.CalledProcessError as e: | ||||
|         print(f"Error during hard restart in {dir_path}: {e}") | ||||
|  | ||||
| if __name__ == "__main__": | ||||
|     if len(sys.argv) < 2: | ||||
|         print("Please provide the path to the parent directory as a parameter.") | ||||
|         sys.exit(1) | ||||
|  | ||||
|     parent_directory = sys.argv[1] | ||||
| def main(): | ||||
|     parser = argparse.ArgumentParser( | ||||
|         description="Restart docker-compose services in subdirectories." | ||||
|     ) | ||||
|     parser.add_argument( | ||||
|         "parent_directory", | ||||
|         help="Path to the parent directory containing docker-compose projects" | ||||
|     ) | ||||
|     parser.add_argument( | ||||
|         "--only", | ||||
|         nargs="+", | ||||
|         help="Restart only the specified subdirectories (by name)" | ||||
|     ) | ||||
|     args = parser.parse_args() | ||||
|  | ||||
|     parent_directory = args.parent_directory | ||||
|  | ||||
|     if not os.path.isdir(parent_directory): | ||||
|         print(f"Error: {parent_directory} is not a valid directory.") | ||||
|         sys.exit(1) | ||||
|  | ||||
|     for dir_entry in os.scandir(parent_directory): | ||||
|         if dir_entry.is_dir(): | ||||
|             dir_path = dir_entry.path | ||||
|             dir_name = os.path.basename(dir_path) | ||||
|             print(f"Checking directory: {dir_path}") | ||||
|              | ||||
|  | ||||
|             docker_compose_file = os.path.join(dir_path, "docker-compose.yml") | ||||
|              | ||||
|  | ||||
|             if os.path.isfile(docker_compose_file): | ||||
|                 print(f"Found docker-compose.yml in {dir_path}.") | ||||
|                 if dir_name == "web-app-mailu": | ||||
|                     print(f"Directory {dir_name} detected. Performing hard restart...") | ||||
|                     hard_restart_docker_services(dir_path) | ||||
|                 else: | ||||
|                     print(f"Restarting services in {dir_path}...") | ||||
|                     restart_docker_services(dir_path) | ||||
|                 if args.only and dir_name not in args.only: | ||||
|                     print(f"Skipping {dir_name} (not in --only list).") | ||||
|                     continue | ||||
|                 print(f"Performing normal restart in {dir_name}...") | ||||
|                 hard_restart_docker_services(dir_path) | ||||
|             else: | ||||
|                 print(f"No docker-compose.yml found in {dir_path}. Skipping.") | ||||
|  | ||||
|  | ||||
| if __name__ == "__main__": | ||||
|     main() | ||||
|   | ||||
| @@ -5,4 +5,4 @@ OnFailure={{ SYS_SERVICE_ON_FAILURE_COMPOSE }} | ||||
| [Service] | ||||
| Type=oneshot | ||||
| ExecStartPre=/usr/bin/python {{ PATH_SYSTEM_LOCK_SCRIPT }} {{ SYS_SERVICE_GROUP_MANIPULATION | join(' ')  }} --ignore {{ SYS_SERVICE_REPAIR_DOCKER_HARD }} --timeout "{{ SYS_TIMEOUT_RESTART_DOCKER }}" | ||||
| ExecStart={{ system_service_script_exec }} | ||||
| ExecStart={{ system_service_script_exec }} {{ PATH_DOCKER_COMPOSE_INSTANCES }} | ||||
		Reference in New Issue
	
	Block a user