Add volume and network parsing functionality

- Parse Docker compose volumes (bind mounts and named volumes)
- Handle external volume detection
- Parse network configuration with external network support
- Support both short and long syntax for volumes/networks
This commit is contained in:
Radosław Gierwiało
2024-08-14 11:15:00 +02:00
parent 422dfaec6f
commit 6576568c58

View File

@@ -43,6 +43,78 @@ class Docker2PBS:
raise ValueError(f"Service '{self.service_name}' not found. Available services: {available_services}")
self.service_config = self.compose_data['services'][self.service_name]
def parse_volumes(self) -> List[Dict[str, Any]]:
"""Parse volumes configuration for the service"""
volumes = []
# Get service volumes
if 'volumes' in self.service_config:
for volume in self.service_config['volumes']:
volume_info = {'type': 'bind', 'external': False}
if isinstance(volume, str):
# Short syntax: "host_path:container_path"
parts = volume.split(':')
if len(parts) >= 2:
volume_info.update({
'source': parts[0],
'target': parts[1],
'mode': parts[2] if len(parts) > 2 else 'rw'
})
elif isinstance(volume, dict):
# Long syntax
volume_info.update(volume)
if volume_info.get('type') == 'volume':
# Check if it's external volume
volume_name = volume_info.get('source')
if volume_name and self.is_external_volume(volume_name):
volume_info['external'] = True
volumes.append(volume_info)
return volumes
def is_external_volume(self, volume_name: str) -> bool:
"""Check if volume is external"""
if 'volumes' in self.compose_data:
volume_config = self.compose_data['volumes'].get(volume_name, {})
return volume_config.get('external', False)
return False
def parse_networks(self) -> List[Dict[str, Any]]:
"""Parse network configuration for the service"""
networks = []
if 'networks' in self.service_config:
service_networks = self.service_config['networks']
if isinstance(service_networks, list):
# Short syntax
for network in service_networks:
networks.append({
'name': network,
'external': self.is_external_network(network)
})
elif isinstance(service_networks, dict):
# Long syntax
for network_name, config in service_networks.items():
network_info = {
'name': network_name,
'external': self.is_external_network(network_name)
}
if config:
network_info.update(config)
networks.append(network_info)
return networks
def is_external_network(self, network_name: str) -> bool:
"""Check if network is external"""
if 'networks' in self.compose_data:
network_config = self.compose_data['networks'].get(network_name, {})
return network_config.get('external', False)
return False
def main():