From c9eecb11179561fc574b788e79baebda2cafcf8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Gierwia=C5=82o?= Date: Fri, 16 Aug 2024 14:20:00 +0200 Subject: [PATCH] Add Docker service management - Stop service before backup operation - Start service after backup completion - Proper error handling for docker-compose commands - Dry-run support for service operations --- docker2pbs.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/docker2pbs.py b/docker2pbs.py index 902bd6e..488a3f8 100644 --- a/docker2pbs.py +++ b/docker2pbs.py @@ -115,6 +115,40 @@ class Docker2PBS: network_config = self.compose_data['networks'].get(network_name, {}) return network_config.get('external', False) return False + + def stop_service(self) -> None: + """Stop the Docker service""" + print(f"Stopping service: {self.service_name}") + cmd = ['docker-compose', '-f', str(self.compose_file), 'stop', self.service_name] + + if self.dry_run: + print(f"[DRY-RUN] Would run: {' '.join(cmd)}") + print(f"[DRY-RUN] Service {self.service_name} would be stopped") + return + + result = subprocess.run(cmd, capture_output=True, text=True) + + if result.returncode != 0: + raise RuntimeError(f"Failed to stop service: {result.stderr}") + + print(f"Service {self.service_name} stopped successfully") + + def start_service(self) -> None: + """Start the Docker service""" + print(f"Starting service: {self.service_name}") + cmd = ['docker-compose', '-f', str(self.compose_file), 'up', '-d', self.service_name] + + if self.dry_run: + print(f"[DRY-RUN] Would run: {' '.join(cmd)}") + print(f"[DRY-RUN] Service {self.service_name} would be started") + return + + result = subprocess.run(cmd, capture_output=True, text=True) + + if result.returncode != 0: + raise RuntimeError(f"Failed to start service: {result.stderr}") + + print(f"Service {self.service_name} started successfully") def main():