|
27 | 27 | class Docker extends Adapter |
28 | 28 | { |
29 | 29 | private Stats $stats; |
30 | | - /** |
31 | | - * @var string[] |
32 | | - */ |
33 | | - private array $networks; |
34 | 30 |
|
35 | 31 | /** |
36 | 32 | * @param Orchestration $orchestration |
37 | 33 | * @param Runtimes $runtimes |
38 | | - * @param string[] $networks |
| 34 | + * @param NetworkManager $networkManager |
39 | 35 | */ |
40 | 36 | public function __construct( |
41 | 37 | private readonly Orchestration $orchestration, |
42 | 38 | private readonly Runtimes $runtimes, |
43 | | - array $networks |
| 39 | + private readonly NetworkManager $networkManager |
44 | 40 | ) { |
45 | 41 | $this->stats = new Stats(); |
46 | | - $this->init($networks); |
| 42 | + $this->init(); |
47 | 43 | } |
48 | 44 |
|
49 | 45 | /** |
50 | | - * @param string[] $networks |
51 | 46 | * @return void |
52 | 47 | * @throws \Utopia\Http\Exception |
53 | 48 | */ |
54 | | - private function init(array $networks): void |
| 49 | + private function init(): void |
55 | 50 | { |
56 | | - /* |
57 | | - * Remove residual runtimes and networks |
58 | | - */ |
59 | | - Console::info('Removing orphan runtimes and networks...'); |
60 | | - $this->cleanUp(); |
61 | | - Console::success("Orphan runtimes and networks removal finished."); |
62 | | - |
63 | | - /** |
64 | | - * Create and store Docker Bridge networks used for communication between executor and runtimes |
65 | | - */ |
66 | | - Console::info('Creating networks...'); |
67 | | - $createdNetworks = $this->createNetworks($networks); |
68 | | - $this->networks = $createdNetworks; |
69 | | - |
70 | 51 | /** |
71 | 52 | * Warmup: make sure images are ready to run fast 🚀 |
72 | 53 | */ |
@@ -170,10 +151,10 @@ private function init(array $networks): void |
170 | 151 |
|
171 | 152 | Console::success('Stats interval started.'); |
172 | 153 |
|
173 | | - Process::signal(SIGINT, fn () => $this->cleanUp($this->networks)); |
174 | | - Process::signal(SIGQUIT, fn () => $this->cleanUp($this->networks)); |
175 | | - Process::signal(SIGKILL, fn () => $this->cleanUp($this->networks)); |
176 | | - Process::signal(SIGTERM, fn () => $this->cleanUp($this->networks)); |
| 154 | + Process::signal(SIGINT, fn () => $this->cleanUp()); |
| 155 | + Process::signal(SIGQUIT, fn () => $this->cleanUp()); |
| 156 | + Process::signal(SIGKILL, fn () => $this->cleanUp()); |
| 157 | + Process::signal(SIGTERM, fn () => $this->cleanUp()); |
177 | 158 | } |
178 | 159 |
|
179 | 160 | /** |
@@ -485,7 +466,7 @@ public function createRuntime( |
485 | 466 | $codeMountPath = $version === 'v2' ? '/usr/code' : '/mnt/code'; |
486 | 467 | $workdir = $version === 'v2' ? '/usr/code' : ''; |
487 | 468 |
|
488 | | - $network = $this->networks[array_rand($this->networks)]; |
| 469 | + $network = $this->networkManager->getAvailable()[array_rand($this->networkManager->getAvailable())]; |
489 | 470 |
|
490 | 471 | $volumes = [ |
491 | 472 | \dirname($tmpSource) . ':/tmp:rw', |
@@ -1199,10 +1180,9 @@ public function createExecution( |
1199 | 1180 | } |
1200 | 1181 |
|
1201 | 1182 | /** |
1202 | | - * @param string[] $networks |
1203 | 1183 | * @return void |
1204 | 1184 | */ |
1205 | | - private function cleanUp(array $networks = []): void |
| 1185 | + private function cleanUp(): void |
1206 | 1186 | { |
1207 | 1187 | Console::log('Cleaning up containers and networks...'); |
1208 | 1188 |
|
@@ -1233,73 +1213,11 @@ private function cleanUp(array $networks = []): void |
1233 | 1213 | } |
1234 | 1214 | batch($jobsRuntimes); |
1235 | 1215 |
|
1236 | | - $jobsNetworks = []; |
1237 | | - foreach ($networks as $network) { |
1238 | | - $jobsNetworks[] = function () use ($network) { |
1239 | | - try { |
1240 | | - $this->orchestration->removeNetwork($network); |
1241 | | - Console::success("Removed network: $network"); |
1242 | | - } catch (Exception $e) { |
1243 | | - Console::error("Failed to remove network $network: " . $e->getMessage()); |
1244 | | - } |
1245 | | - }; |
1246 | | - } |
1247 | | - batch($jobsNetworks); |
| 1216 | + $this->networkManager->removeAll(); |
1248 | 1217 |
|
1249 | 1218 | Console::success('Cleanup finished.'); |
1250 | 1219 | } |
1251 | 1220 |
|
1252 | | - /** |
1253 | | - * @param string[] $networks |
1254 | | - * @return string[] |
1255 | | - */ |
1256 | | - private function createNetworks(array $networks): array |
1257 | | - { |
1258 | | - $jobs = []; |
1259 | | - $createdNetworks = []; |
1260 | | - foreach ($networks as $network) { |
1261 | | - $jobs[] = function () use ($network, &$createdNetworks) { |
1262 | | - if (!$this->orchestration->networkExists($network)) { |
1263 | | - try { |
1264 | | - $this->orchestration->createNetwork($network, false); |
1265 | | - Console::success("Created network: $network"); |
1266 | | - $createdNetworks[] = $network; |
1267 | | - } catch (\Throwable $e) { |
1268 | | - Console::error("Failed to create network $network: " . $e->getMessage()); |
1269 | | - } |
1270 | | - } else { |
1271 | | - Console::info("Network $network already exists"); |
1272 | | - $createdNetworks[] = $network; |
1273 | | - } |
1274 | | - }; |
1275 | | - } |
1276 | | - batch($jobs); |
1277 | | - |
1278 | | - $image = System::getEnv('OPR_EXECUTOR_IMAGE', ''); |
1279 | | - $containers = $this->orchestration->list(['label' => "com.openruntimes.executor.image=$image"]); |
1280 | | - |
1281 | | - if (count($containers) < 1) { |
1282 | | - $containerName = ''; |
1283 | | - Console::warning('No matching executor found. Please check the value of OPR_EXECUTOR_IMAGE. Executor will need to be connected to the runtime network manually.'); |
1284 | | - } else { |
1285 | | - $containerName = $containers[0]->getName(); |
1286 | | - Console::success('Found matching executor. Executor will be connected to runtime network automatically.'); |
1287 | | - } |
1288 | | - |
1289 | | - if (!empty($containerName)) { |
1290 | | - foreach ($createdNetworks as $network) { |
1291 | | - try { |
1292 | | - $this->orchestration->networkConnect($containerName, $network); |
1293 | | - Console::success("Successfully connected executor '$containerName' to network '$network'"); |
1294 | | - } catch (\Throwable $e) { |
1295 | | - Console::error("Failed to connect executor '$containerName' to network '$network': " . $e->getMessage()); |
1296 | | - } |
1297 | | - } |
1298 | | - } |
1299 | | - |
1300 | | - return $createdNetworks; |
1301 | | - } |
1302 | | - |
1303 | 1221 | public function getRuntimes(): mixed |
1304 | 1222 | { |
1305 | 1223 | $runtimes = []; |
|
0 commit comments