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
This commit is contained in:
Radosław Gierwiało
2024-08-16 14:20:00 +02:00
parent 6576568c58
commit c9eecb1117

View File

@@ -115,6 +115,40 @@ class Docker2PBS:
network_config = self.compose_data['networks'].get(network_name, {}) network_config = self.compose_data['networks'].get(network_name, {})
return network_config.get('external', False) return network_config.get('external', False)
return 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(): def main():