Utility class for Docker operations.
Source code in nebula/utils.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 | class DockerUtils:
"""
Utility class for Docker operations.
"""
@classmethod
def create_docker_network(cls, network_name, subnet=None, prefix=24):
try:
# Connect to Docker
client = docker.from_env()
base_subnet = "192.168"
# Obtain existing docker subnets
existing_subnets = []
networks = client.networks.list()
existing_network = next((n for n in networks if n.name == network_name), None)
if existing_network:
ipam_config = existing_network.attrs.get("IPAM", {}).get("Config", [])
if ipam_config:
# Assume there's only one subnet per network for simplicity
existing_subnet = ipam_config[0].get("Subnet", "")
potential_base = ".".join(existing_subnet.split(".")[:3]) # Extract base from subnet
logging.info(f"Network '{network_name}' already exists with base {potential_base}")
return potential_base
for network in networks:
ipam_config = network.attrs.get("IPAM", {}).get("Config", [])
if ipam_config:
for config in ipam_config:
if "Subnet" in config:
existing_subnets.append(config["Subnet"])
# If no subnet is provided or it exists, find the next available one
if not subnet or subnet in existing_subnets:
for i in range(50, 255): # Iterate over 192.168.50.0 to 192.168.254.0
subnet = f"{base_subnet}.{i}.0/{prefix}"
potential_base = f"{base_subnet}.{i}"
if subnet not in existing_subnets:
break
else:
raise ValueError("No available subnets found.")
# Create the Docker network
gateway = f"{subnet.split('/')[0].rsplit('.', 1)[0]}.1"
ipam_pool = docker.types.IPAMPool(subnet=subnet, gateway=gateway)
ipam_config = docker.types.IPAMConfig(pool_configs=[ipam_pool])
network = client.networks.create(name=network_name, driver="bridge", ipam=ipam_config)
logging.info(f"Network created: {network.name} with subnet {subnet}")
return potential_base
except docker.errors.APIError:
logging.exception("Error interacting with Docker")
return None
except Exception:
logging.exception("Unexpected error")
return None
finally:
client.close() # Ensure the Docker client is closed
@classmethod
def remove_docker_network(cls, network_name):
try:
# Connect to Docker
client = docker.from_env()
# Get the network by name
network = client.networks.get(network_name)
# Remove the network
network.remove()
logging.info(f"Network {network_name} removed successfully.")
except docker.errors.NotFound:
logging.exception(f"Network {network_name} not found.")
except docker.errors.APIError:
logging.exception("Error interacting with Docker")
except Exception:
logging.exception("Unexpected error")
@classmethod
def remove_docker_networks_by_prefix(cls, prefix):
try:
# Connect to Docker
client = docker.from_env()
# List all networks
networks = client.networks.list()
# Filter and remove networks with names starting with the prefix
for network in networks:
if network.name.startswith(prefix):
network.remove()
logging.info(f"Network {network.name} removed successfully.")
except docker.errors.NotFound:
logging.info(f"One or more networks with prefix {prefix} not found.")
except docker.errors.APIError:
logging.info("Error interacting with Docker")
except Exception:
logging.info("Unexpected error")
@classmethod
def remove_containers_by_prefix(cls, prefix):
try:
# Connect to Docker client
client = docker.from_env()
containers = client.containers.list(all=True) # `all=True` to include stopped containers
# Iterate through containers and remove those with the matching prefix
for container in containers:
if container.name.startswith(prefix):
logging.info(f"Removing container: {container.name}")
container.remove(force=True) # force=True to stop and remove if running
logging.info(f"Container {container.name} removed successfully.")
except docker.errors.APIError:
logging.exception("Error interacting with Docker")
except Exception:
logging.exception("Unexpected error")
|