Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions app/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use OpenRuntimes\Executor\Runner\ImagePuller;
use OpenRuntimes\Executor\Runner\Maintenance;
use OpenRuntimes\Executor\Runner\Repository\Runtimes;
use OpenRuntimes\Executor\Runner\NetworkManager;
use OpenRuntimes\Executor\Runner\Network;
use Swoole\Process;
use Swoole\Runtime;
use Utopia\Console;
Expand Down Expand Up @@ -50,14 +50,16 @@
));
$runtimes = new Runtimes();

/* Create desired networks if they don't exist */
$networks = explode(',', System::getEnv('OPR_EXECUTOR_NETWORK') ?: 'openruntimes-runtimes');
$networkManager = new NetworkManager($orchestration, $networks);

/* Add the current executor to the networks */
/* Fetch own container information */
$hostname = gethostname() ?: throw new \RuntimeException('Could not determine hostname');
$selfContainer = $orchestration->list(['name' => $hostname])[0] ?? throw new \RuntimeException('Own container not found');
$networkManager->connectAll($selfContainer);

/* Create desired networks if they don't exist */
$network = new Network($orchestration);
$network->setup(
explode(',', System::getEnv('OPR_EXECUTOR_NETWORK') ?: 'openruntimes-runtimes'),
$selfContainer->getName()
);

/* Pull images */
$imagePuller = new ImagePuller($orchestration);
Expand All @@ -71,16 +73,17 @@
);

/* Runner service, used to manage runtimes */
$runner = new Docker($orchestration, $runtimes, $networkManager);
$runner = new Docker($orchestration, $runtimes, $network->getAvailable());
Http::setResource('runner', fn () => $runner);

$server = new Server('0.0.0.0', '80', $settings);
$http = new Http($server, 'UTC');

Process::signal(SIGTERM, function () use ($maintenance, $runner) {
Process::signal(SIGTERM, function () use ($maintenance, $runner, $network) {
// This doesn't actually work. We need to fix utopia-php/http@0.34.x
$maintenance->stop();
$runner->cleanUp();
$network->cleanup();
$runner->cleanup();
});

Console::success('Executor is ready.');
Expand Down
10 changes: 4 additions & 6 deletions src/Executor/Runner/Docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ class Docker extends Adapter
/**
* @param Orchestration $orchestration
* @param Runtimes $runtimes
* @param NetworkManager $networkManager
* @param string[] $networks
*/
public function __construct(
private readonly Orchestration $orchestration,
private readonly Runtimes $runtimes,
private readonly NetworkManager $networkManager
private readonly array $networks
) {
}

Expand Down Expand Up @@ -340,7 +340,7 @@ public function createRuntime(
$codeMountPath = $version === 'v2' ? '/usr/code' : '/mnt/code';
$workdir = $version === 'v2' ? '/usr/code' : '';

$network = $this->networkManager->getAvailable()[array_rand($this->networkManager->getAvailable())];
$network = $this->networks[array_rand($this->networks)];

$volumes = [
\dirname($tmpSource) . ':/tmp:rw',
Expand Down Expand Up @@ -1053,7 +1053,7 @@ public function createExecution(
/**
* @return void
*/
public function cleanUp(): void
public function cleanup(): void
{
Console::log('Cleaning up containers and networks...');

Expand Down Expand Up @@ -1084,8 +1084,6 @@ public function cleanUp(): void
}
batch($jobsRuntimes);

$this->networkManager->removeAll();

Console::success('Cleanup finished.');
}

Expand Down
88 changes: 88 additions & 0 deletions src/Executor/Runner/Network.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace OpenRuntimes\Executor\Runner;

use Utopia\Console;
use Utopia\Orchestration\Orchestration;

class Network
{
/** @var string[] Networks that are available */
private array $available = [];

/** @var string|null Name of the container linked to the networks */
private ?string $container = null;

public function __construct(
private readonly Orchestration $orchestration,
) {
}

/**
* @param string[] $networks Networks to ensure exist
* @param string $container Name of the container to link to the networks
*/
public function setup(array $networks, string $container): void
{
foreach ($networks as $network) {
/* Ensure network exists */
if ($this->orchestration->networkExists($network)) {
Console::info("[Network] Network {$network} already exists");
} else {
try {
$this->orchestration->createNetwork($network, false);
Console::success("[Network] Created network: {$network}");
} catch (\Throwable $e) {
Console::error("[Network] Failed to create network {$network}: {$e->getMessage()}");
continue;
}
}
$this->available[] = $network;

/* Add the container */
try {
$this->orchestration->networkConnect($container, $network);
$this->container = $container;
} catch (\Throwable $e) {
Console::error("[Network] Failed to connect container {$container} to network {$network}: {$e->getMessage()}");
}
}
}

/**
* Remove a network and disconnect a container from it.
*/
public function cleanup(): void
{
foreach ($this->available as $network) {
if ($this->container !== null) {
/* Remove the container */
try {
$this->orchestration->networkDisconnect($this->container, $network);
} catch (\Throwable $e) {
Console::error("[Network] Failed to disconnect container {$this->container} from network {$network}: {$e->getMessage()}");
}
}

/* Ensure network exists */
if ($this->orchestration->networkExists($network)) {
Console::info("[Network] Network {$network} already exists");
} else {
try {
$this->orchestration->removeNetwork($network);
Console::success("[Network] Deleted network: {$network}");
} catch (\Throwable $e) {
Console::error("[Network] Failed to delete network {$network}: {$e->getMessage()}");
}
}
}
}

/**
* @return string[] Networks that are available
*/
public function getAvailable(): array
{
return $this->available;
}
}
98 changes: 0 additions & 98 deletions src/Executor/Runner/NetworkManager.php

This file was deleted.