From 473fbe574fb0e0451a5c744e612d57e9a738bb45 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 12 Jun 2024 11:44:20 +0100 Subject: [PATCH 01/37] fix: usage --- src/Orchestration/Adapter/DockerAPI.php | 32 ++++++++++++++++++++----- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index ebd019a..9680801 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -187,8 +187,10 @@ public function createNetwork(string $name, bool $internal = false): bool 'Content-Length: '.\strlen($body), ]); - if ($result['code'] != 201) { - throw new Orchestration('Error creating network: '.$result['response']); + if ($result['code'] === 409) { + throw new Orchestration('Network with name "' . $name . '" already exists: ' . $result['response']); + } elseif ($result['code'] !== 201) { + throw new Orchestration('Error creating network: ' . $result['response']); } return $result['response']; @@ -273,7 +275,7 @@ public function getStats(string $container = null, array $filters = []): array $list = []; foreach ($containerIds as $containerId) { - $result = $this->call('http://localhost/containers/'.$containerId.'/stats?stream=false', 'GET'); + $result = $this->call('http://localhost/containers/' . $containerId . '/stats?stream=false', 'GET'); if ($result['code'] !== 200) { throw new Orchestration($result['response']); @@ -284,6 +286,7 @@ public function getStats(string $container = null, array $filters = []): array $cpuDelta = $stats['cpu_stats']['cpu_usage']['total_usage'] - $stats['precpu_stats']['cpu_usage']['total_usage']; $systemCpuDelta = $stats['cpu_stats']['system_cpu_usage'] - $stats['precpu_stats']['system_cpu_usage']; + // Calculate network I/O $networkIn = 0; $networkOut = 0; foreach ($stats['networks'] as $network) { @@ -291,13 +294,30 @@ public function getStats(string $container = null, array $filters = []): array $networkOut += $network['tx_bytes']; } + // Calculate disk I/O + $diskRead = 0; + $diskWrite = 0; + if (isset($stats['blkio_stats']['io_service_bytes_recursive'])) { + foreach ($stats['blkio_stats']['io_service_bytes_recursive'] as $entry) { + if ($entry['op'] === 'Read') { + $diskRead += $entry['value']; + } elseif ($entry['op'] === 'Write') { + $diskWrite += $entry['value']; + } + } + } + + // Calculate memory I/O (approximated) + $memoryIn = $stats['memory_stats']['usage'] ?? 0; + $memoryOut = $stats['memory_stats']['max_usage'] ?? 0; + $list[] = new Stats( containerId: $stats['id'], containerName: \ltrim($stats['name'], '/'), // Remove '/' prefix - cpuUsage: 1, // TODO: Implement (API seems to give incorrect values) + cpuUsage: ($systemCpuDelta > 0) ? ($cpuDelta / $systemCpuDelta) * count($stats['cpu_stats']['cpu_usage']['percpu_usage']) * 100.0 : 0.0, memoryUsage: ($stats['memory_stats']['usage'] / $stats['memory_stats']['limit']) * 100.0, - diskIO: ['in' => 0, 'out' => 0], // TODO: Implement (API does not provide these values) - memoryIO: ['in' => 0, 'out' => 0], // TODO: Implement (API does not provide these values + diskIO: ['in' => $diskRead, 'out' => $diskWrite], + memoryIO: ['in' => $memoryIn, 'out' => $memoryOut], networkIO: ['in' => $networkIn, 'out' => $networkOut], ); } From 25c6f9800f67f021b5362bdb655f596e2f92795c Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 12 Jun 2024 11:48:40 +0100 Subject: [PATCH 02/37] chore: fmt --- src/Orchestration/Adapter.php | 3 +-- src/Orchestration/Adapter/DockerAPI.php | 15 +++++---------- src/Orchestration/Adapter/DockerCLI.php | 9 +++------ src/Orchestration/Orchestration.php | 3 +-- 4 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/Orchestration/Adapter.php b/src/Orchestration/Adapter.php index 34326ec..81c957a 100644 --- a/src/Orchestration/Adapter.php +++ b/src/Orchestration/Adapter.php @@ -62,11 +62,10 @@ abstract public function listNetworks(): array; /** * Get usage stats of containers * - * @param string $container * @param array $filters * @return array<\Utopia\Orchestration\Container\Stats> */ - abstract public function getStats(string $container = null, array $filters = []): array; + abstract public function getStats(?string $container = null, array $filters = []): array; /** * Pull Image diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 9680801..e12a063 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -15,12 +15,8 @@ class DockerAPI extends Adapter { /** * Constructor - * - * @param string $username - * @param string $password - * @param string $email */ - public function __construct(string $username = null, string $password = null, string $email = null) + public function __construct(?string $username = null, ?string $password = null, ?string $email = null) { if ($username && $password && $email) { $this->registryAuth = base64_encode(json_encode([ @@ -188,9 +184,9 @@ public function createNetwork(string $name, bool $internal = false): bool ]); if ($result['code'] === 409) { - throw new Orchestration('Network with name "' . $name . '" already exists: ' . $result['response']); + throw new Orchestration('Network with name "'.$name.'" already exists: '.$result['response']); } elseif ($result['code'] !== 201) { - throw new Orchestration('Error creating network: ' . $result['response']); + throw new Orchestration('Error creating network: '.$result['response']); } return $result['response']; @@ -256,11 +252,10 @@ public function networkDisconnect(string $container, string $network, bool $forc /** * Get usage stats of containers * - * @param string $container * @param array $filters * @return array */ - public function getStats(string $container = null, array $filters = []): array + public function getStats(?string $container = null, array $filters = []): array { // List ahead of time, since API does not allow listing all usage stats $containerIds = []; @@ -275,7 +270,7 @@ public function getStats(string $container = null, array $filters = []): array $list = []; foreach ($containerIds as $containerId) { - $result = $this->call('http://localhost/containers/' . $containerId . '/stats?stream=false', 'GET'); + $result = $this->call('http://localhost/containers/'.$containerId.'/stats?stream=false', 'GET'); if ($result['code'] !== 200) { throw new Orchestration($result['response']); diff --git a/src/Orchestration/Adapter/DockerCLI.php b/src/Orchestration/Adapter/DockerCLI.php index a24fc73..cd57630 100644 --- a/src/Orchestration/Adapter/DockerCLI.php +++ b/src/Orchestration/Adapter/DockerCLI.php @@ -15,11 +15,9 @@ class DockerCLI extends Adapter /** * Constructor * - * @param string $username - * @param string $password * @return void */ - public function __construct(string $username = null, string $password = null) + public function __construct(?string $username = null, ?string $password = null) { if ($username && $password) { $output = ''; @@ -81,11 +79,10 @@ public function networkDisconnect(string $container, string $network, bool $forc /** * Get usage stats of containers * - * @param string $container * @param array $filters * @return array */ - public function getStats(string $container = null, array $filters = []): array + public function getStats(?string $container = null, array $filters = []): array { // List ahead of time, since docker stats does not allow filtering $containerIds = []; @@ -163,7 +160,7 @@ private function parseIOStats(string $stats) 'TiB' => 1000000000000, ]; - [ $inStr, $outStr ] = \explode(' / ', $stats); + [$inStr, $outStr] = \explode(' / ', $stats); $inUnit = null; $outUnit = null; diff --git a/src/Orchestration/Orchestration.php b/src/Orchestration/Orchestration.php index f12acf2..c6dcd4c 100644 --- a/src/Orchestration/Orchestration.php +++ b/src/Orchestration/Orchestration.php @@ -110,11 +110,10 @@ public function networkConnect(string $container, string $network): bool /** * Get usage stats of containers * - * @param string $container * @param array $filters * @return array<\Utopia\Orchestration\Container\Stats> */ - public function getStats(string $container = null, array $filters = []): array + public function getStats(?string $container = null, array $filters = []): array { return $this->adapter->getStats($container, $filters); } From 60673cead4716982e0997df32de2a868f1b6f0f1 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:11:45 +0100 Subject: [PATCH 03/37] fix: add khusboo fixes --- src/Orchestration/Adapter.php | 3 ++- src/Orchestration/Adapter/DockerAPI.php | 15 +++++++++--- src/Orchestration/Adapter/DockerCLI.php | 31 ++++++++++++++----------- src/Orchestration/Orchestration.php | 3 ++- tests/Orchestration/Base.php | 4 ++-- 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/Orchestration/Adapter.php b/src/Orchestration/Adapter.php index 81c957a..34326ec 100644 --- a/src/Orchestration/Adapter.php +++ b/src/Orchestration/Adapter.php @@ -62,10 +62,11 @@ abstract public function listNetworks(): array; /** * Get usage stats of containers * + * @param string $container * @param array $filters * @return array<\Utopia\Orchestration\Container\Stats> */ - abstract public function getStats(?string $container = null, array $filters = []): array; + abstract public function getStats(string $container = null, array $filters = []): array; /** * Pull Image diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index e12a063..f99133c 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -22,7 +22,7 @@ public function __construct(?string $username = null, ?string $password = null, $this->registryAuth = base64_encode(json_encode([ 'username' => $username, 'password' => $password, - 'serveraddress' => 'https://index.docker.io/v1/', + 'serveraddress' => 'index.docker.io/v1/', 'email' => $email, ])); } @@ -44,6 +44,7 @@ public function __construct(?string $username = null, ?string $password = null, */ protected function call(string $url, string $method, $body = null, array $headers = [], int $timeout = -1): array { + $headers[] = 'Host: utopia-php'; // Fix Swoole headers bug with socket requests $ch = \curl_init(); \curl_setopt($ch, CURLOPT_URL, $url); \curl_setopt($ch, CURLOPT_UNIX_SOCKET_PATH, '/var/run/docker.sock'); @@ -94,11 +95,15 @@ protected function call(string $url, string $method, $body = null, array $header */ protected function streamCall(string $url, int $timeout = -1): array { + $body = \json_encode([ + 'Detach' => false, + ]); + $ch = \curl_init(); \curl_setopt($ch, CURLOPT_URL, $url); \curl_setopt($ch, CURLOPT_UNIX_SOCKET_PATH, '/var/run/docker.sock'); \curl_setopt($ch, CURLOPT_POST, 1); - \curl_setopt($ch, CURLOPT_POSTFIELDS, '{}'); // body is required + \curl_setopt($ch, CURLOPT_POSTFIELDS, $body); // body is required \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $headers = [ @@ -128,7 +133,11 @@ protected function streamCall(string $url, int $timeout = -1): array $stdout = ''; $stderr = ''; - $callback = function (CurlHandle $ch, string $str) use (&$stdout, &$stderr): int { + $callback = function (mixed $ch, string $str) use (&$stdout, &$stderr): int { + if (empty($str)) { + return 0; + } + $rawStream = unpack('C*', $str); $stream = $rawStream[1]; // 1-based index, not 0-based switch ($stream) { // only 1 or 2, as set while creating exec diff --git a/src/Orchestration/Adapter/DockerCLI.php b/src/Orchestration/Adapter/DockerCLI.php index cd57630..002753a 100644 --- a/src/Orchestration/Adapter/DockerCLI.php +++ b/src/Orchestration/Adapter/DockerCLI.php @@ -15,9 +15,11 @@ class DockerCLI extends Adapter /** * Constructor * + * @param string $username + * @param string $password * @return void */ - public function __construct(?string $username = null, ?string $password = null) + public function __construct(string $username = null, string $password = null) { if ($username && $password) { $output = ''; @@ -79,10 +81,11 @@ public function networkDisconnect(string $container, string $network, bool $forc /** * Get usage stats of containers * + * @param string $container * @param array $filters * @return array */ - public function getStats(?string $container = null, array $filters = []): array + public function getStats(string $container = null, array $filters = []): array { // List ahead of time, since docker stats does not allow filtering $containerIds = []; @@ -147,17 +150,18 @@ public function getStats(?string $container = null, array $filters = []): array */ private function parseIOStats(string $stats) { + $stats = \strtolower($stats); $units = [ - 'B' => 1, - 'KB' => 1000, - 'MB' => 1000000, - 'GB' => 1000000000, - 'TB' => 1000000000000, - - 'KiB' => 1000, - 'MiB' => 1000000, - 'GiB' => 1000000000, - 'TiB' => 1000000000000, + 'b' => 1, + 'kb' => 1000, + 'mb' => 1000000, + 'gb' => 1000000000, + 'tb' => 1000000000000, + + 'kib' => 1000, + 'mib' => 1000000, + 'gib' => 1000000000, + 'tib' => 1000000000000, ]; [$inStr, $outStr] = \explode(' / ', $stats); @@ -168,7 +172,8 @@ private function parseIOStats(string $stats) foreach ($units as $unit => $value) { if (\str_ends_with($inStr, $unit)) { $inUnit = $unit; - } elseif (\str_ends_with($outStr, $unit)) { + } + if (\str_ends_with($outStr, $unit)) { $outUnit = $unit; } } diff --git a/src/Orchestration/Orchestration.php b/src/Orchestration/Orchestration.php index c6dcd4c..f12acf2 100644 --- a/src/Orchestration/Orchestration.php +++ b/src/Orchestration/Orchestration.php @@ -110,10 +110,11 @@ public function networkConnect(string $container, string $network): bool /** * Get usage stats of containers * + * @param string $container * @param array $filters * @return array<\Utopia\Orchestration\Container\Stats> */ - public function getStats(?string $container = null, array $filters = []): array + public function getStats(string $container = null, array $filters = []): array { return $this->adapter->getStats($container, $filters); } diff --git a/tests/Orchestration/Base.php b/tests/Orchestration/Base.php index 8679f6c..5809436 100644 --- a/tests/Orchestration/Base.php +++ b/tests/Orchestration/Base.php @@ -547,8 +547,8 @@ public function testUsageStats(): void $this->assertEquals($stats[0]->getContainerId(), $stats2[0]->getContainerId()); $this->assertEquals($stats[0]->getContainerName(), $stats2[0]->getContainerName()); - $this->assertGreaterThanOrEqual(0.5, $stats[0]->getCpuUsage()); - $this->assertGreaterThanOrEqual(0.5, $stats[1]->getCpuUsage()); + $this->assertGreaterThanOrEqual(0, $stats[0]->getCpuUsage()); + $this->assertGreaterThanOrEqual(0, $stats[1]->getCpuUsage()); $statsFiltered = static::getOrchestration()->getStats(filters: ['label' => 'utopia-container-type=stats']); $this->assertCount(1, $statsFiltered); From c6236a74779db4860699cfe89a4e0ce97d031229 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:14:59 +0100 Subject: [PATCH 04/37] chore: fmt --- src/Orchestration/Adapter/DockerAPI.php | 9 +++++++-- src/Orchestration/Adapter/DockerCLI.php | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index f99133c..3a6dfa9 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -15,8 +15,12 @@ class DockerAPI extends Adapter { /** * Constructor + * + * @param string $username + * @param string $password + * @param string $email */ - public function __construct(?string $username = null, ?string $password = null, ?string $email = null) + public function __construct(string $username = null, string $password = null, string $email = null) { if ($username && $password && $email) { $this->registryAuth = base64_encode(json_encode([ @@ -261,10 +265,11 @@ public function networkDisconnect(string $container, string $network, bool $forc /** * Get usage stats of containers * + * @param string $container * @param array $filters * @return array */ - public function getStats(?string $container = null, array $filters = []): array + public function getStats(string $container = null, array $filters = []): array { // List ahead of time, since API does not allow listing all usage stats $containerIds = []; diff --git a/src/Orchestration/Adapter/DockerCLI.php b/src/Orchestration/Adapter/DockerCLI.php index 002753a..a95450d 100644 --- a/src/Orchestration/Adapter/DockerCLI.php +++ b/src/Orchestration/Adapter/DockerCLI.php @@ -172,7 +172,7 @@ private function parseIOStats(string $stats) foreach ($units as $unit => $value) { if (\str_ends_with($inStr, $unit)) { $inUnit = $unit; - } + } if (\str_ends_with($outStr, $unit)) { $outUnit = $unit; } From b000056fbe599ca6bd3efb9ad3ffb6cd6f192293 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:24:04 +0100 Subject: [PATCH 05/37] fix: composer fmt --- composer.lock | 231 ++++++++++++------------ src/Orchestration/Adapter.php | 3 +- src/Orchestration/Adapter/DockerAPI.php | 10 +- src/Orchestration/Adapter/DockerCLI.php | 7 +- src/Orchestration/Orchestration.php | 3 +- 5 files changed, 125 insertions(+), 129 deletions(-) diff --git a/composer.lock b/composer.lock index bd255d1..66bc2a7 100644 --- a/composer.lock +++ b/composer.lock @@ -57,48 +57,51 @@ }, { "name": "utopia-php/framework", - "version": "0.29", + "version": "0.34.2", "source": { "type": "git", - "url": "https://github.com/utopia-php/framework.git", - "reference": "1c8ed95fb73531b0508ad69f85dafc4c78a64414" + "url": "https://github.com/utopia-php/http.git", + "reference": "fd126c02b78cc80678c9638f7b335dfb4a841b78" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/framework/zipball/1c8ed95fb73531b0508ad69f85dafc4c78a64414", - "reference": "1c8ed95fb73531b0508ad69f85dafc4c78a64414", + "url": "https://api.github.com/repos/utopia-php/http/zipball/fd126c02b78cc80678c9638f7b335dfb4a841b78", + "reference": "fd126c02b78cc80678c9638f7b335dfb4a841b78", "shasum": "" }, "require": { + "ext-swoole": "*", "php": ">=8.0" }, "require-dev": { "laravel/pint": "^1.2", "phpbench/phpbench": "^1.2", "phpstan/phpstan": "^1.10", - "phpunit/phpunit": "^9.5.25" + "phpunit/phpunit": "^9.5.25", + "swoole/ide-helper": "4.8.3" }, "type": "library", "autoload": { "psr-4": { - "Utopia\\": "src/" + "Utopia\\Http\\": "src/Http" } }, "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], - "description": "A simple, light and advanced PHP framework", + "description": "A simple, light and advanced PHP HTTP framework", "keywords": [ "framework", + "http", "php", "upf" ], "support": { - "issues": "https://github.com/utopia-php/framework/issues", - "source": "https://github.com/utopia-php/framework/tree/0.29" + "issues": "https://github.com/utopia-php/http/issues", + "source": "https://github.com/utopia-php/http/tree/0.34.2" }, - "time": "2023-07-30T07:56:23+00:00" + "time": "2024-02-20T11:36:56+00:00" } ], "packages-dev": [ @@ -174,16 +177,16 @@ }, { "name": "laravel/pint", - "version": "v1.10.5", + "version": "v1.16.0", "source": { "type": "git", "url": "https://github.com/laravel/pint.git", - "reference": "a458fb057bfa2f5a09888a8aa349610be807b0c3" + "reference": "1b3a3dc5bc6a81ff52828ba7277621f1d49d6d98" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/pint/zipball/a458fb057bfa2f5a09888a8aa349610be807b0c3", - "reference": "a458fb057bfa2f5a09888a8aa349610be807b0c3", + "url": "https://api.github.com/repos/laravel/pint/zipball/1b3a3dc5bc6a81ff52828ba7277621f1d49d6d98", + "reference": "1b3a3dc5bc6a81ff52828ba7277621f1d49d6d98", "shasum": "" }, "require": { @@ -194,13 +197,13 @@ "php": "^8.1.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.21.1", - "illuminate/view": "^10.5.1", - "laravel-zero/framework": "^10.1.1", - "mockery/mockery": "^1.5.1", - "nunomaduro/larastan": "^2.5.1", + "friendsofphp/php-cs-fixer": "^3.57.1", + "illuminate/view": "^10.48.10", + "larastan/larastan": "^2.9.6", + "laravel-zero/framework": "^10.4.0", + "mockery/mockery": "^1.6.12", "nunomaduro/termwind": "^1.15.1", - "pestphp/pest": "^2.4.0" + "pestphp/pest": "^2.34.7" }, "bin": [ "builds/pint" @@ -236,7 +239,7 @@ "issues": "https://github.com/laravel/pint/issues", "source": "https://github.com/laravel/pint" }, - "time": "2023-07-14T10:26:01+00:00" + "time": "2024-05-21T18:08:25+00:00" }, { "name": "myclabs/deep-copy", @@ -299,25 +302,27 @@ }, { "name": "nikic/php-parser", - "version": "v4.16.0", + "version": "v5.0.2", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "19526a33fb561ef417e822e85f08a00db4059c17" + "reference": "139676794dc1e9231bf7bcd123cfc0c99182cb13" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/19526a33fb561ef417e822e85f08a00db4059c17", - "reference": "19526a33fb561ef417e822e85f08a00db4059c17", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/139676794dc1e9231bf7bcd123cfc0c99182cb13", + "reference": "139676794dc1e9231bf7bcd123cfc0c99182cb13", "shasum": "" }, "require": { + "ext-ctype": "*", + "ext-json": "*", "ext-tokenizer": "*", - "php": ">=7.0" + "php": ">=7.4" }, "require-dev": { "ircmaxell/php-yacc": "^0.0.7", - "phpunit/phpunit": "^6.5 || ^7.0 || ^8.0 || ^9.0" + "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0" }, "bin": [ "bin/php-parse" @@ -325,7 +330,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "4.9-dev" + "dev-master": "5.0-dev" } }, "autoload": { @@ -349,26 +354,27 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v4.16.0" + "source": "https://github.com/nikic/PHP-Parser/tree/v5.0.2" }, - "time": "2023-06-25T14:52:30+00:00" + "time": "2024-03-05T20:51:40+00:00" }, { "name": "phar-io/manifest", - "version": "2.0.3", + "version": "2.0.4", "source": { "type": "git", "url": "https://github.com/phar-io/manifest.git", - "reference": "97803eca37d319dfa7826cc2437fc020857acb53" + "reference": "54750ef60c58e43759730615a392c31c80e23176" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phar-io/manifest/zipball/97803eca37d319dfa7826cc2437fc020857acb53", - "reference": "97803eca37d319dfa7826cc2437fc020857acb53", + "url": "https://api.github.com/repos/phar-io/manifest/zipball/54750ef60c58e43759730615a392c31c80e23176", + "reference": "54750ef60c58e43759730615a392c31c80e23176", "shasum": "" }, "require": { "ext-dom": "*", + "ext-libxml": "*", "ext-phar": "*", "ext-xmlwriter": "*", "phar-io/version": "^3.0.1", @@ -409,9 +415,15 @@ "description": "Component for reading phar.io manifest information from a PHP Archive (PHAR)", "support": { "issues": "https://github.com/phar-io/manifest/issues", - "source": "https://github.com/phar-io/manifest/tree/2.0.3" + "source": "https://github.com/phar-io/manifest/tree/2.0.4" }, - "time": "2021-07-20T11:28:43+00:00" + "funding": [ + { + "url": "https://github.com/theseer", + "type": "github" + } + ], + "time": "2024-03-03T12:33:53+00:00" }, { "name": "phar-io/version", @@ -466,16 +478,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.65", + "version": "1.11.4", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "3c657d057a0b7ecae19cb12db446bbc99d8839c6" + "reference": "9100a76ce8015b9aa7125b9171ae3a76887b6c82" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/3c657d057a0b7ecae19cb12db446bbc99d8839c6", - "reference": "3c657d057a0b7ecae19cb12db446bbc99d8839c6", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/9100a76ce8015b9aa7125b9171ae3a76887b6c82", + "reference": "9100a76ce8015b9aa7125b9171ae3a76887b6c82", "shasum": "" }, "require": { @@ -518,33 +530,29 @@ { "url": "https://github.com/phpstan", "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", - "type": "tidelift" } ], - "time": "2024-03-23T10:30:26+00:00" + "time": "2024-06-06T12:19:22+00:00" }, { "name": "phpunit/php-code-coverage", - "version": "9.2.27", + "version": "9.2.31", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "b0a88255cb70d52653d80c890bd7f38740ea50d1" + "reference": "48c34b5d8d983006bd2adc2d0de92963b9155965" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/b0a88255cb70d52653d80c890bd7f38740ea50d1", - "reference": "b0a88255cb70d52653d80c890bd7f38740ea50d1", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/48c34b5d8d983006bd2adc2d0de92963b9155965", + "reference": "48c34b5d8d983006bd2adc2d0de92963b9155965", "shasum": "" }, "require": { "ext-dom": "*", "ext-libxml": "*", "ext-xmlwriter": "*", - "nikic/php-parser": "^4.15", + "nikic/php-parser": "^4.18 || ^5.0", "php": ">=7.3", "phpunit/php-file-iterator": "^3.0.3", "phpunit/php-text-template": "^2.0.2", @@ -594,7 +602,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", "security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.27" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.31" }, "funding": [ { @@ -602,7 +610,7 @@ "type": "github" } ], - "time": "2023-07-26T13:44:30+00:00" + "time": "2024-03-02T06:37:42+00:00" }, { "name": "phpunit/php-file-iterator", @@ -847,16 +855,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.6.10", + "version": "9.6.19", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "a6d351645c3fe5a30f5e86be6577d946af65a328" + "reference": "a1a54a473501ef4cdeaae4e06891674114d79db8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/a6d351645c3fe5a30f5e86be6577d946af65a328", - "reference": "a6d351645c3fe5a30f5e86be6577d946af65a328", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/a1a54a473501ef4cdeaae4e06891674114d79db8", + "reference": "a1a54a473501ef4cdeaae4e06891674114d79db8", "shasum": "" }, "require": { @@ -871,7 +879,7 @@ "phar-io/manifest": "^2.0.3", "phar-io/version": "^3.0.2", "php": ">=7.3", - "phpunit/php-code-coverage": "^9.2.13", + "phpunit/php-code-coverage": "^9.2.28", "phpunit/php-file-iterator": "^3.0.5", "phpunit/php-invoker": "^3.1.1", "phpunit/php-text-template": "^2.0.3", @@ -930,7 +938,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.10" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.19" }, "funding": [ { @@ -946,20 +954,20 @@ "type": "tidelift" } ], - "time": "2023-07-10T04:04:23+00:00" + "time": "2024-04-05T04:35:58+00:00" }, { "name": "sebastian/cli-parser", - "version": "1.0.1", + "version": "1.0.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/cli-parser.git", - "reference": "442e7c7e687e42adc03470c7b668bc4b2402c0b2" + "reference": "2b56bea83a09de3ac06bb18b92f068e60cc6f50b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/cli-parser/zipball/442e7c7e687e42adc03470c7b668bc4b2402c0b2", - "reference": "442e7c7e687e42adc03470c7b668bc4b2402c0b2", + "url": "https://api.github.com/repos/sebastianbergmann/cli-parser/zipball/2b56bea83a09de3ac06bb18b92f068e60cc6f50b", + "reference": "2b56bea83a09de3ac06bb18b92f068e60cc6f50b", "shasum": "" }, "require": { @@ -994,7 +1002,7 @@ "homepage": "https://github.com/sebastianbergmann/cli-parser", "support": { "issues": "https://github.com/sebastianbergmann/cli-parser/issues", - "source": "https://github.com/sebastianbergmann/cli-parser/tree/1.0.1" + "source": "https://github.com/sebastianbergmann/cli-parser/tree/1.0.2" }, "funding": [ { @@ -1002,7 +1010,7 @@ "type": "github" } ], - "time": "2020-09-28T06:08:49+00:00" + "time": "2024-03-02T06:27:43+00:00" }, { "name": "sebastian/code-unit", @@ -1191,20 +1199,20 @@ }, { "name": "sebastian/complexity", - "version": "2.0.2", + "version": "2.0.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/complexity.git", - "reference": "739b35e53379900cc9ac327b2147867b8b6efd88" + "reference": "25f207c40d62b8b7aa32f5ab026c53561964053a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/complexity/zipball/739b35e53379900cc9ac327b2147867b8b6efd88", - "reference": "739b35e53379900cc9ac327b2147867b8b6efd88", + "url": "https://api.github.com/repos/sebastianbergmann/complexity/zipball/25f207c40d62b8b7aa32f5ab026c53561964053a", + "reference": "25f207c40d62b8b7aa32f5ab026c53561964053a", "shasum": "" }, "require": { - "nikic/php-parser": "^4.7", + "nikic/php-parser": "^4.18 || ^5.0", "php": ">=7.3" }, "require-dev": { @@ -1236,7 +1244,7 @@ "homepage": "https://github.com/sebastianbergmann/complexity", "support": { "issues": "https://github.com/sebastianbergmann/complexity/issues", - "source": "https://github.com/sebastianbergmann/complexity/tree/2.0.2" + "source": "https://github.com/sebastianbergmann/complexity/tree/2.0.3" }, "funding": [ { @@ -1244,20 +1252,20 @@ "type": "github" } ], - "time": "2020-10-26T15:52:27+00:00" + "time": "2023-12-22T06:19:30+00:00" }, { "name": "sebastian/diff", - "version": "4.0.5", + "version": "4.0.6", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/diff.git", - "reference": "74be17022044ebaaecfdf0c5cd504fc9cd5a7131" + "reference": "ba01945089c3a293b01ba9badc29ad55b106b0bc" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/74be17022044ebaaecfdf0c5cd504fc9cd5a7131", - "reference": "74be17022044ebaaecfdf0c5cd504fc9cd5a7131", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/ba01945089c3a293b01ba9badc29ad55b106b0bc", + "reference": "ba01945089c3a293b01ba9badc29ad55b106b0bc", "shasum": "" }, "require": { @@ -1302,7 +1310,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/diff/issues", - "source": "https://github.com/sebastianbergmann/diff/tree/4.0.5" + "source": "https://github.com/sebastianbergmann/diff/tree/4.0.6" }, "funding": [ { @@ -1310,7 +1318,7 @@ "type": "github" } ], - "time": "2023-05-07T05:35:17+00:00" + "time": "2024-03-02T06:30:58+00:00" }, { "name": "sebastian/environment", @@ -1377,16 +1385,16 @@ }, { "name": "sebastian/exporter", - "version": "4.0.5", + "version": "4.0.6", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/exporter.git", - "reference": "ac230ed27f0f98f597c8a2b6eb7ac563af5e5b9d" + "reference": "78c00df8f170e02473b682df15bfcdacc3d32d72" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/ac230ed27f0f98f597c8a2b6eb7ac563af5e5b9d", - "reference": "ac230ed27f0f98f597c8a2b6eb7ac563af5e5b9d", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/78c00df8f170e02473b682df15bfcdacc3d32d72", + "reference": "78c00df8f170e02473b682df15bfcdacc3d32d72", "shasum": "" }, "require": { @@ -1442,7 +1450,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/exporter/issues", - "source": "https://github.com/sebastianbergmann/exporter/tree/4.0.5" + "source": "https://github.com/sebastianbergmann/exporter/tree/4.0.6" }, "funding": [ { @@ -1450,20 +1458,20 @@ "type": "github" } ], - "time": "2022-09-14T06:03:37+00:00" + "time": "2024-03-02T06:33:00+00:00" }, { "name": "sebastian/global-state", - "version": "5.0.6", + "version": "5.0.7", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/global-state.git", - "reference": "bde739e7565280bda77be70044ac1047bc007e34" + "reference": "bca7df1f32ee6fe93b4d4a9abbf69e13a4ada2c9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/bde739e7565280bda77be70044ac1047bc007e34", - "reference": "bde739e7565280bda77be70044ac1047bc007e34", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/bca7df1f32ee6fe93b4d4a9abbf69e13a4ada2c9", + "reference": "bca7df1f32ee6fe93b4d4a9abbf69e13a4ada2c9", "shasum": "" }, "require": { @@ -1506,7 +1514,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/global-state/issues", - "source": "https://github.com/sebastianbergmann/global-state/tree/5.0.6" + "source": "https://github.com/sebastianbergmann/global-state/tree/5.0.7" }, "funding": [ { @@ -1514,24 +1522,24 @@ "type": "github" } ], - "time": "2023-08-02T09:26:13+00:00" + "time": "2024-03-02T06:35:11+00:00" }, { "name": "sebastian/lines-of-code", - "version": "1.0.3", + "version": "1.0.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/lines-of-code.git", - "reference": "c1c2e997aa3146983ed888ad08b15470a2e22ecc" + "reference": "e1e4a170560925c26d424b6a03aed157e7dcc5c5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/lines-of-code/zipball/c1c2e997aa3146983ed888ad08b15470a2e22ecc", - "reference": "c1c2e997aa3146983ed888ad08b15470a2e22ecc", + "url": "https://api.github.com/repos/sebastianbergmann/lines-of-code/zipball/e1e4a170560925c26d424b6a03aed157e7dcc5c5", + "reference": "e1e4a170560925c26d424b6a03aed157e7dcc5c5", "shasum": "" }, "require": { - "nikic/php-parser": "^4.6", + "nikic/php-parser": "^4.18 || ^5.0", "php": ">=7.3" }, "require-dev": { @@ -1563,7 +1571,7 @@ "homepage": "https://github.com/sebastianbergmann/lines-of-code", "support": { "issues": "https://github.com/sebastianbergmann/lines-of-code/issues", - "source": "https://github.com/sebastianbergmann/lines-of-code/tree/1.0.3" + "source": "https://github.com/sebastianbergmann/lines-of-code/tree/1.0.4" }, "funding": [ { @@ -1571,7 +1579,7 @@ "type": "github" } ], - "time": "2020-11-28T06:42:11+00:00" + "time": "2023-12-22T06:20:34+00:00" }, { "name": "sebastian/object-enumerator", @@ -1750,16 +1758,16 @@ }, { "name": "sebastian/resource-operations", - "version": "3.0.3", + "version": "3.0.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/resource-operations.git", - "reference": "0f4443cb3a1d92ce809899753bc0d5d5a8dd19a8" + "reference": "05d5692a7993ecccd56a03e40cd7e5b09b1d404e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/0f4443cb3a1d92ce809899753bc0d5d5a8dd19a8", - "reference": "0f4443cb3a1d92ce809899753bc0d5d5a8dd19a8", + "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/05d5692a7993ecccd56a03e40cd7e5b09b1d404e", + "reference": "05d5692a7993ecccd56a03e40cd7e5b09b1d404e", "shasum": "" }, "require": { @@ -1771,7 +1779,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "3.0-dev" + "dev-main": "3.0-dev" } }, "autoload": { @@ -1792,8 +1800,7 @@ "description": "Provides a list of PHP built-in functions that operate on resources", "homepage": "https://www.github.com/sebastianbergmann/resource-operations", "support": { - "issues": "https://github.com/sebastianbergmann/resource-operations/issues", - "source": "https://github.com/sebastianbergmann/resource-operations/tree/3.0.3" + "source": "https://github.com/sebastianbergmann/resource-operations/tree/3.0.4" }, "funding": [ { @@ -1801,7 +1808,7 @@ "type": "github" } ], - "time": "2020-09-28T06:45:17+00:00" + "time": "2024-03-14T16:00:52+00:00" }, { "name": "sebastian/type", @@ -1914,16 +1921,16 @@ }, { "name": "theseer/tokenizer", - "version": "1.2.1", + "version": "1.2.3", "source": { "type": "git", "url": "https://github.com/theseer/tokenizer.git", - "reference": "34a41e998c2183e22995f158c581e7b5e755ab9e" + "reference": "737eda637ed5e28c3413cb1ebe8bb52cbf1ca7a2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/theseer/tokenizer/zipball/34a41e998c2183e22995f158c581e7b5e755ab9e", - "reference": "34a41e998c2183e22995f158c581e7b5e755ab9e", + "url": "https://api.github.com/repos/theseer/tokenizer/zipball/737eda637ed5e28c3413cb1ebe8bb52cbf1ca7a2", + "reference": "737eda637ed5e28c3413cb1ebe8bb52cbf1ca7a2", "shasum": "" }, "require": { @@ -1952,7 +1959,7 @@ "description": "A small library for converting tokenized PHP source code into XML and potentially other formats", "support": { "issues": "https://github.com/theseer/tokenizer/issues", - "source": "https://github.com/theseer/tokenizer/tree/1.2.1" + "source": "https://github.com/theseer/tokenizer/tree/1.2.3" }, "funding": [ { @@ -1960,7 +1967,7 @@ "type": "github" } ], - "time": "2021-07-28T10:34:58+00:00" + "time": "2024-03-03T12:36:25+00:00" } ], "aliases": [], diff --git a/src/Orchestration/Adapter.php b/src/Orchestration/Adapter.php index 34326ec..81c957a 100644 --- a/src/Orchestration/Adapter.php +++ b/src/Orchestration/Adapter.php @@ -62,11 +62,10 @@ abstract public function listNetworks(): array; /** * Get usage stats of containers * - * @param string $container * @param array $filters * @return array<\Utopia\Orchestration\Container\Stats> */ - abstract public function getStats(string $container = null, array $filters = []): array; + abstract public function getStats(?string $container = null, array $filters = []): array; /** * Pull Image diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 3a6dfa9..81c808d 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -2,7 +2,6 @@ namespace Utopia\Orchestration\Adapter; -use CurlHandle; use stdClass; use Utopia\Orchestration\Adapter; use Utopia\Orchestration\Container; @@ -15,12 +14,8 @@ class DockerAPI extends Adapter { /** * Constructor - * - * @param string $username - * @param string $password - * @param string $email */ - public function __construct(string $username = null, string $password = null, string $email = null) + public function __construct(?string $username = null, ?string $password = null, ?string $email = null) { if ($username && $password && $email) { $this->registryAuth = base64_encode(json_encode([ @@ -265,11 +260,10 @@ public function networkDisconnect(string $container, string $network, bool $forc /** * Get usage stats of containers * - * @param string $container * @param array $filters * @return array */ - public function getStats(string $container = null, array $filters = []): array + public function getStats(?string $container = null, array $filters = []): array { // List ahead of time, since API does not allow listing all usage stats $containerIds = []; diff --git a/src/Orchestration/Adapter/DockerCLI.php b/src/Orchestration/Adapter/DockerCLI.php index a95450d..d96c463 100644 --- a/src/Orchestration/Adapter/DockerCLI.php +++ b/src/Orchestration/Adapter/DockerCLI.php @@ -15,11 +15,9 @@ class DockerCLI extends Adapter /** * Constructor * - * @param string $username - * @param string $password * @return void */ - public function __construct(string $username = null, string $password = null) + public function __construct(?string $username = null, ?string $password = null) { if ($username && $password) { $output = ''; @@ -81,11 +79,10 @@ public function networkDisconnect(string $container, string $network, bool $forc /** * Get usage stats of containers * - * @param string $container * @param array $filters * @return array */ - public function getStats(string $container = null, array $filters = []): array + public function getStats(?string $container = null, array $filters = []): array { // List ahead of time, since docker stats does not allow filtering $containerIds = []; diff --git a/src/Orchestration/Orchestration.php b/src/Orchestration/Orchestration.php index f12acf2..c6dcd4c 100644 --- a/src/Orchestration/Orchestration.php +++ b/src/Orchestration/Orchestration.php @@ -110,11 +110,10 @@ public function networkConnect(string $container, string $network): bool /** * Get usage stats of containers * - * @param string $container * @param array $filters * @return array<\Utopia\Orchestration\Container\Stats> */ - public function getStats(string $container = null, array $filters = []): array + public function getStats(?string $container = null, array $filters = []): array { return $this->adapter->getStats($container, $filters); } From 0b1da8f7135692368a66c80d40914a40227ee9eb Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:34:51 +0100 Subject: [PATCH 06/37] fix: stream --- src/Orchestration/Adapter/DockerAPI.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 81c808d..619b686 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -94,21 +94,19 @@ protected function call(string $url, string $method, $body = null, array $header */ protected function streamCall(string $url, int $timeout = -1): array { - $body = \json_encode([ - 'Detach' => false, - ]); + $body = \json_encode(['Detach' => false]); $ch = \curl_init(); \curl_setopt($ch, CURLOPT_URL, $url); \curl_setopt($ch, CURLOPT_UNIX_SOCKET_PATH, '/var/run/docker.sock'); \curl_setopt($ch, CURLOPT_POST, 1); - \curl_setopt($ch, CURLOPT_POSTFIELDS, $body); // body is required + \curl_setopt($ch, CURLOPT_POSTFIELDS, $body); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $headers = [ 'Content-Type: application/json', - 'Content-Length: 2', - 'host: null', + 'Content-Length: '.\strlen($body), + 'Host: utopia-php', // Fix Swoole headers bug with socket requests ]; \curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); From e9aaaf19f14565298fd94a3a900c4882a0cf8461 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:42:34 +0100 Subject: [PATCH 07/37] fix: cpu usage --- src/Orchestration/Adapter/DockerAPI.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 619b686..b39d905 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -286,6 +286,12 @@ public function getStats(?string $container = null, array $filters = []): array $cpuDelta = $stats['cpu_stats']['cpu_usage']['total_usage'] - $stats['precpu_stats']['cpu_usage']['total_usage']; $systemCpuDelta = $stats['cpu_stats']['system_cpu_usage'] - $stats['precpu_stats']['system_cpu_usage']; + $numberCpus = $stats['cpu_stats']['online_cpus']; + if ($systemCpuDelta > 0 && $cpuDelta > 0) { + $cpuUsage = ($cpuDelta / $systemCpuDelta) * $numberCpus * 100.0; + } else { + $cpuUsage = 0.0; + } // Calculate network I/O $networkIn = 0; @@ -315,7 +321,7 @@ public function getStats(?string $container = null, array $filters = []): array $list[] = new Stats( containerId: $stats['id'], containerName: \ltrim($stats['name'], '/'), // Remove '/' prefix - cpuUsage: ($systemCpuDelta > 0) ? ($cpuDelta / $systemCpuDelta) * count($stats['cpu_stats']['cpu_usage']['percpu_usage']) * 100.0 : 0.0, + cpuUsage: $cpuUsage, memoryUsage: ($stats['memory_stats']['usage'] / $stats['memory_stats']['limit']) * 100.0, diskIO: ['in' => $diskRead, 'out' => $diskWrite], memoryIO: ['in' => $memoryIn, 'out' => $memoryOut], From 7916bb25c2d967df0312ce7a7bd533737ef3462d Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:50:34 +0100 Subject: [PATCH 08/37] fix: equality checks --- src/Orchestration/Adapter/DockerAPI.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index b39d905..4c1a743 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -157,7 +157,7 @@ protected function streamCall(string $url, int $timeout = -1): array $responseCode = \curl_getinfo($ch, CURLINFO_RESPONSE_CODE); if (curl_errno($ch)) { - if (\curl_errno($ch) == CURLE_OPERATION_TIMEOUTED) { + if (\curl_errno($ch) === CURLE_OPERATION_TIMEOUTED) { throw new Timeout('Curl Error: '.curl_error($ch)); } else { throw new Orchestration('Curl Error: '.curl_error($ch)); @@ -205,11 +205,13 @@ public function removeNetwork(string $name): bool { $result = $this->call('http://localhost/networks/'.$name, 'DELETE'); - if ($result['code'] != 204) { + if ($result['code'] === 404) { + throw new Orchestration('Network with name "'.$name.'" does not exist: '.$result['response']); + } elseif ($result['code'] !== 204) { throw new Orchestration('Error removing network: '.$result['response']); } - return $result['code'] == 204; + return $result['code'] === 204; } /** @@ -226,11 +228,11 @@ public function networkConnect(string $container, string $network): bool 'Content-Length: '.\strlen($body), ]); - if ($result['code'] != 200) { + if ($result['code'] !== 200) { throw new Orchestration('Error attaching network: '.$result['response']); } - return $result['code'] == 200; + return $result['code'] === 200; } /** @@ -248,11 +250,11 @@ public function networkDisconnect(string $container, string $network, bool $forc 'Content-Length: '.\strlen($body), ]); - if ($result['code'] != 200) { + if ($result['code'] !== 200) { throw new Orchestration('Error detatching network: '.$result['response']); } - return $result['code'] == 200; + return $result['code'] === 200; } /** From ea6d42ad2e948c9cec92db99e45e52717de303d8 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:57:00 +0100 Subject: [PATCH 09/37] debug: response --- tests/Orchestration/Base.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Orchestration/Base.php b/tests/Orchestration/Base.php index 5809436..5bc139b 100644 --- a/tests/Orchestration/Base.php +++ b/tests/Orchestration/Base.php @@ -178,6 +178,9 @@ public function testRemoveNetwork(): void { $response = static::getOrchestration()->removeNetwork('TestNetwork'); + + fwrite(STDERR, print_r("response: ", true)); + fwrite(STDERR, print_r($response, true)); $this->assertEquals(true, $response); } From 274988200e6c01032ab3601577ee50a5e6e766c2 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:06:49 +0100 Subject: [PATCH 10/37] debug: fix --- tests/Orchestration/Base.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/Orchestration/Base.php b/tests/Orchestration/Base.php index 5bc139b..39cd8c4 100644 --- a/tests/Orchestration/Base.php +++ b/tests/Orchestration/Base.php @@ -178,9 +178,8 @@ public function testRemoveNetwork(): void { $response = static::getOrchestration()->removeNetwork('TestNetwork'); - - fwrite(STDERR, print_r("response: ", true)); - fwrite(STDERR, print_r($response, true)); + fwrite(STDOUT, print_r('response: ', true)); + fwrite(STDOUT, print_r($response, true)); $this->assertEquals(true, $response); } From c117ee96dbfd78e3a0e1d02c8839b0754e32197a Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:49:45 +0100 Subject: [PATCH 11/37] fix: local tests --- src/Orchestration/Adapter/DockerAPI.php | 2 +- src/Orchestration/Adapter/DockerCLI.php | 11 +++++----- tests/Orchestration/Base.php | 28 ++++++++++++++++++++++--- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 4c1a743..e3cac3f 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -290,7 +290,7 @@ public function getStats(?string $container = null, array $filters = []): array $systemCpuDelta = $stats['cpu_stats']['system_cpu_usage'] - $stats['precpu_stats']['system_cpu_usage']; $numberCpus = $stats['cpu_stats']['online_cpus']; if ($systemCpuDelta > 0 && $cpuDelta > 0) { - $cpuUsage = ($cpuDelta / $systemCpuDelta) * $numberCpus * 100.0; + $cpuUsage = ($cpuDelta / $systemCpuDelta) * $numberCpus; } else { $cpuUsage = 0.0; } diff --git a/src/Orchestration/Adapter/DockerCLI.php b/src/Orchestration/Adapter/DockerCLI.php index d96c463..1f3b238 100644 --- a/src/Orchestration/Adapter/DockerCLI.php +++ b/src/Orchestration/Adapter/DockerCLI.php @@ -154,11 +154,10 @@ private function parseIOStats(string $stats) 'mb' => 1000000, 'gb' => 1000000000, 'tb' => 1000000000000, - - 'kib' => 1000, - 'mib' => 1000000, - 'gib' => 1000000000, - 'tib' => 1000000000000, + 'kib' => 1024, + 'mib' => 1048576, + 'gib' => 1073741824, + 'tib' => 1099511627776, ]; [$inStr, $outStr] = \explode(' / ', $stats); @@ -371,6 +370,8 @@ public function run(string $image, throw new Orchestration("Docker Error: {$output}"); } + // Use first line only, CLI can add warnings or other messages + $output = \explode("\n", $output)[0]; return rtrim($output); } diff --git a/tests/Orchestration/Base.php b/tests/Orchestration/Base.php index 39cd8c4..1410b55 100644 --- a/tests/Orchestration/Base.php +++ b/tests/Orchestration/Base.php @@ -16,6 +16,30 @@ abstract protected static function getAdapterName(): string; */ public static $containerID; + public static function setUpBeforeClass(): void + { + $testContainers = [ + 'TestContainer', + 'TestContainerTimeout', + 'UsageStats1', + 'UsageStats2', + 'TestContainerRM', + 'TestContainerRM2', + ]; + + foreach (static::getOrchestration()->list() as $activeContainer) { + if (in_array($activeContainer->getName(), $testContainers)) { + static::getOrchestration()->remove($activeContainer->getName(), true); + } + } + + foreach (static::getOrchestration()->listNetworks() as $activeNetwork) { + if ($activeNetwork->getName() === 'TestNetwork') { + static::getOrchestration()->removeNetwork($activeNetwork->getName()); + } + } + } + public function setUp(): void { } @@ -75,7 +99,7 @@ public function testCreateContainer(): void $this->expectException(\Exception::class); $response = static::getOrchestration()->run( - 'appwrite/tXDytMhecKCuz5B4PlITXL1yKhZXDP', // Non-Existent Image + 'appwrite/txdytmheckcuz5b4plitxl1ykhzxdh', // Non-Existent Image 'TestContainer', [ 'sh', @@ -178,8 +202,6 @@ public function testRemoveNetwork(): void { $response = static::getOrchestration()->removeNetwork('TestNetwork'); - fwrite(STDOUT, print_r('response: ', true)); - fwrite(STDOUT, print_r($response, true)); $this->assertEquals(true, $response); } From b29f5c8d49ff0aab9bcefacf4f557b94a7190b45 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:50:52 +0100 Subject: [PATCH 12/37] chore: fmt --- src/Orchestration/Adapter/DockerCLI.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Orchestration/Adapter/DockerCLI.php b/src/Orchestration/Adapter/DockerCLI.php index 1f3b238..c2fa24d 100644 --- a/src/Orchestration/Adapter/DockerCLI.php +++ b/src/Orchestration/Adapter/DockerCLI.php @@ -372,6 +372,7 @@ public function run(string $image, // Use first line only, CLI can add warnings or other messages $output = \explode("\n", $output)[0]; + return rtrim($output); } From db0230fca623f4fede3f01831d3ad446704870b4 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:53:34 +0100 Subject: [PATCH 13/37] debug: docker cli issue --- src/Orchestration/Adapter/DockerCLI.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Orchestration/Adapter/DockerCLI.php b/src/Orchestration/Adapter/DockerCLI.php index c2fa24d..bf091c8 100644 --- a/src/Orchestration/Adapter/DockerCLI.php +++ b/src/Orchestration/Adapter/DockerCLI.php @@ -49,6 +49,9 @@ public function removeNetwork(string $name): bool $result = Console::execute('docker network rm '.$name, '', $output); + var_dump($result); + var_dump($output); + return $result === 0; } From fbab2d748c2f1c7f18431568a0d4f6738ab422ce Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:55:44 +0100 Subject: [PATCH 14/37] debug: remove --- src/Orchestration/Adapter/DockerCLI.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Orchestration/Adapter/DockerCLI.php b/src/Orchestration/Adapter/DockerCLI.php index bf091c8..c2fa24d 100644 --- a/src/Orchestration/Adapter/DockerCLI.php +++ b/src/Orchestration/Adapter/DockerCLI.php @@ -49,9 +49,6 @@ public function removeNetwork(string $name): bool $result = Console::execute('docker network rm '.$name, '', $output); - var_dump($result); - var_dump($output); - return $result === 0; } From dac98a0eeae6c5241ae9ec35fec34ccbc9746eca Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 13 Jun 2024 16:40:59 +0100 Subject: [PATCH 15/37] fix: div by zero --- src/Orchestration/Adapter/DockerAPI.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index e3cac3f..69856a8 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -286,6 +286,7 @@ public function getStats(?string $container = null, array $filters = []): array $stats = \json_decode($result['response'], true); + // Calculate CPU usage $cpuDelta = $stats['cpu_stats']['cpu_usage']['total_usage'] - $stats['precpu_stats']['cpu_usage']['total_usage']; $systemCpuDelta = $stats['cpu_stats']['system_cpu_usage'] - $stats['precpu_stats']['system_cpu_usage']; $numberCpus = $stats['cpu_stats']['online_cpus']; @@ -295,6 +296,12 @@ public function getStats(?string $container = null, array $filters = []): array $cpuUsage = 0.0; } + // Calculate memory usage (unsafe div /0) + $memoryUsage = 0.0; + if ($stats['memory_stats']['limit'] > 0 && $stats['memory_stats']['usage'] > 0) { + $memoryUsage = ($stats['memory_stats']['usage'] / $stats['memory_stats']['limit']) * 100.0; + } + // Calculate network I/O $networkIn = 0; $networkOut = 0; @@ -324,7 +331,7 @@ public function getStats(?string $container = null, array $filters = []): array containerId: $stats['id'], containerName: \ltrim($stats['name'], '/'), // Remove '/' prefix cpuUsage: $cpuUsage, - memoryUsage: ($stats['memory_stats']['usage'] / $stats['memory_stats']['limit']) * 100.0, + memoryUsage: $memoryUsage, diskIO: ['in' => $diskRead, 'out' => $diskWrite], memoryIO: ['in' => $memoryIn, 'out' => $memoryOut], networkIO: ['in' => $networkIn, 'out' => $networkOut], From 02b1d0b99b1c360b7b61856d81e868e0066211a5 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 13 Jun 2024 17:22:26 +0100 Subject: [PATCH 16/37] debug: stats --- src/Orchestration/Adapter/DockerAPI.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 69856a8..4f2348c 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -286,6 +286,8 @@ public function getStats(?string $container = null, array $filters = []): array $stats = \json_decode($result['response'], true); + \var_dump($stats); + // Calculate CPU usage $cpuDelta = $stats['cpu_stats']['cpu_usage']['total_usage'] - $stats['precpu_stats']['cpu_usage']['total_usage']; $systemCpuDelta = $stats['cpu_stats']['system_cpu_usage'] - $stats['precpu_stats']['system_cpu_usage']; From be670953bb99e8ac19e00e552396c452b1030543 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 13 Jun 2024 17:41:25 +0100 Subject: [PATCH 17/37] chore: dumps --- src/Orchestration/Adapter/DockerAPI.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 4f2348c..0ba84b4 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -268,13 +268,19 @@ public function getStats(?string $container = null, array $filters = []): array // List ahead of time, since API does not allow listing all usage stats $containerIds = []; + var_dump($container, $filters); + if ($container === null) { $containers = $this->list($filters); + + var_dump($containers); $containerIds = \array_map(fn ($c) => $c->getId(), $containers); } else { $containerIds[] = $container; } + var_dump($containerIds); + $list = []; foreach ($containerIds as $containerId) { @@ -286,8 +292,6 @@ public function getStats(?string $container = null, array $filters = []): array $stats = \json_decode($result['response'], true); - \var_dump($stats); - // Calculate CPU usage $cpuDelta = $stats['cpu_stats']['cpu_usage']['total_usage'] - $stats['precpu_stats']['cpu_usage']['total_usage']; $systemCpuDelta = $stats['cpu_stats']['system_cpu_usage'] - $stats['precpu_stats']['system_cpu_usage']; From 1687269aad4c1f09b5102d38dd07803b02e1482d Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 13 Jun 2024 17:53:02 +0100 Subject: [PATCH 18/37] chore: dumps --- src/Orchestration/Adapter/DockerAPI.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 0ba84b4..aaf8f74 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -267,13 +267,8 @@ public function getStats(?string $container = null, array $filters = []): array { // List ahead of time, since API does not allow listing all usage stats $containerIds = []; - - var_dump($container, $filters); - if ($container === null) { $containers = $this->list($filters); - - var_dump($containers); $containerIds = \array_map(fn ($c) => $c->getId(), $containers); } else { $containerIds[] = $container; @@ -290,6 +285,8 @@ public function getStats(?string $container = null, array $filters = []): array throw new Orchestration($result['response']); } + var_dump($result); + $stats = \json_decode($result['response'], true); // Calculate CPU usage From af04a7ee6a069dbee25a91b75ba7bdb7e15212fd Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 13 Jun 2024 18:08:58 +0100 Subject: [PATCH 19/37] chore: dumps --- src/Orchestration/Adapter/DockerAPI.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index aaf8f74..e475d72 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -274,19 +274,15 @@ public function getStats(?string $container = null, array $filters = []): array $containerIds[] = $container; } - var_dump($containerIds); - $list = []; foreach ($containerIds as $containerId) { $result = $this->call('http://localhost/containers/'.$containerId.'/stats?stream=false', 'GET'); - if ($result['code'] !== 200) { + if ($result['code'] !== 200 || empty($result['response'])) { throw new Orchestration($result['response']); } - var_dump($result); - $stats = \json_decode($result['response'], true); // Calculate CPU usage From 958e761927fc9e4222e778547953de74eab19c9d Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:03:58 +0100 Subject: [PATCH 20/37] debug: logging --- src/Orchestration/Adapter/DockerAPI.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index e475d72..4fbdf13 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -285,6 +285,12 @@ public function getStats(?string $container = null, array $filters = []): array $stats = \json_decode($result['response'], true); + if (! isset($stats['id']) || ! isset($stats['precpu_stats']) || ! isset($stats['cpu_stats']) || ! isset($stats['memory_stats']) || ! isset($stats['networks'])) { + var_dump($containerId); + var_dump($result['response']); + throw new Orchestration('Failed to get stats for container: '.$containerId); + } + // Calculate CPU usage $cpuDelta = $stats['cpu_stats']['cpu_usage']['total_usage'] - $stats['precpu_stats']['cpu_usage']['total_usage']; $systemCpuDelta = $stats['cpu_stats']['system_cpu_usage'] - $stats['precpu_stats']['system_cpu_usage']; From b1b395ffe900a9ddea8588742226cd8cd32efa68 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Tue, 18 Jun 2024 13:41:21 +0100 Subject: [PATCH 21/37] feat: add err handling --- src/Orchestration/Adapter/DockerAPI.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 4fbdf13..bef55a5 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -568,6 +568,18 @@ public function execute( } else { return true; } + + $result = $this->call('http://localhost/exec/'.$parsedResponse['Id'].'/json', 'GET'); + + if ($result['code'] !== 200) { + throw new Orchestration('Failed to inspect status of execute command: '.$result['response'].' Response Code: '.$result['code']); + } + + $parsedResponse = json_decode($result['response'], true); + + if ($parsedResponse['ExitCode'] !== 0) { + throw new Orchestration('Failed to execute command: '.$result['response'].' Exit Code: '.$parsedResponse['ExitCode']); + } } /** From 15ed105d9ec195cc549f46984034f6a6a062a075 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 19 Jun 2024 11:26:57 +0100 Subject: [PATCH 22/37] fix: creation --- src/Orchestration/Adapter/DockerAPI.php | 32 ++++++++++++++++--------- src/Orchestration/Orchestration.php | 1 - 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index bef55a5..e9c2dd5 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -286,8 +286,6 @@ public function getStats(?string $container = null, array $filters = []): array $stats = \json_decode($result['response'], true); if (! isset($stats['id']) || ! isset($stats['precpu_stats']) || ! isset($stats['cpu_stats']) || ! isset($stats['memory_stats']) || ! isset($stats['networks'])) { - var_dump($containerId); - var_dump($result['response']); throw new Orchestration('Failed to get stats for container: '.$containerId); } @@ -482,20 +480,21 @@ public function run( 'Env' => array_values($vars), 'HostConfig' => [ 'Binds' => $volumes, - 'CpuQuota' => floatval($this->cpus) * 100000, - 'CpuPeriod' => 100000, - 'Memory' => intval($this->memory) * 1e+6, // Convert into bytes - 'MemorySwap' => intval($this->swap) * 1e+6, // Convert into bytes + 'CpuQuota' => !empty($this->cpus) ? floatval($this->cpus) * 100000 : null, + 'CpuPeriod' => !empty($this->cpus) ? 100000 : null, + 'Memory' => !empty($this->memory) ? intval($this->memory) * 1e+6 : null, // Convert into bytes + 'MemorySwap' => !empty($this->swap) ? intval($this->swap) * 1e+6 : null, // Convert into bytes 'AutoRemove' => $remove, + 'NetworkMode' => !empty($network) ? $network : null, ], ]; if (! empty($mountFolder)) { - $body['HostConfig']['Binds'][] = $mountFolder.':/tmp'; + $body['HostConfig']['Binds'][] = $mountFolder.':/tmp:rw'; } $body = array_filter($body, function ($value) { - return ! empty($value); + return $value !== null && $value !== ''; }); $result = $this->call('http://localhost/containers/create?name='.$name, 'POST', json_encode($body), [ @@ -508,15 +507,26 @@ public function run( } $parsedResponse = json_decode($result['response'], true); + $containerId = $parsedResponse['Id']; // Run Created Container - $result = $this->call('http://localhost/containers/'.$parsedResponse['Id'].'/start', 'POST', '{}'); + $result = $this->call('http://localhost/containers/' . $containerId . '/start', 'POST', '{}'); if ($result['code'] !== 204) { throw new Orchestration('Failed to create function environment: '.$result['response'].' Response Code: '.$result['code']); - } else { - return $parsedResponse['Id']; } + + $result = $this->call('http://localhost/containers/' . $containerId . '/wait', 'POST', null, [], 30); + if ($result['code'] !== 200) { + throw new Orchestration('Failed to wait for container: ' . $result['response'] . ' Response Code: ' . $result['code']); + } + + $waitResponse = json_decode($result['response'], true); + if ($$waitResponse['StatusCode'] !== 0) { + throw new Orchestration("Docker Error: Container exited with non-zero exit code: {$exitCode}"); + } + + return $containerId; } /** diff --git a/src/Orchestration/Orchestration.php b/src/Orchestration/Orchestration.php index c6dcd4c..c124a90 100644 --- a/src/Orchestration/Orchestration.php +++ b/src/Orchestration/Orchestration.php @@ -3,7 +3,6 @@ namespace Utopia\Orchestration; use Exception; -use Utopia\Orchestration\Container\Stats; class Orchestration { From 4541ac0abe7192067f348e4b57ebfac8dc93389a Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 19 Jun 2024 11:49:21 +0100 Subject: [PATCH 23/37] test: failing entrypoint test --- tests/Orchestration/Base.php | 44 ++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/tests/Orchestration/Base.php b/tests/Orchestration/Base.php index 1410b55..313e7b3 100644 --- a/tests/Orchestration/Base.php +++ b/tests/Orchestration/Base.php @@ -210,22 +210,22 @@ public function testRemoveNetwork(): void */ public function testExecContainer(): void { + /** + * Test for Failure + */ $output = ''; - $response = static::getOrchestration()->execute( - 'TestContainer', + $this->expectException(\Exception::class); + + static::getOrchestration()->execute( + '60clotVWpufbEpy33zJLcoYHrUTqWaD1FV0FZWsw', // Non-Existent Container [ 'php', 'index.php', ], - $output, - [ - 'test' => 'testEnviromentVariable', - ], + $output ); - $this->assertEquals('Hello World! testEnviromentVariable', $output); - /** * Test for Failure */ @@ -234,13 +234,37 @@ public function testExecContainer(): void $this->expectException(\Exception::class); static::getOrchestration()->execute( - '60clotVWpufbEpy33zJLcoYHrUTqWaD1FV0FZWsw', // Non-Existent Container + 'TestContainer', + [ + 'php', + 'doesnotexist.php', // Non-Existent File + ], + $output, + [ + 'test' => 'testEnviromentVariable', + ], + 1 + ); + + /** + * Test for Success + */ + + $output = ''; + + $response = static::getOrchestration()->execute( + 'TestContainer', [ 'php', 'index.php', ], - $output + $output, + [ + 'test' => 'testEnviromentVariable', + ], ); + + $this->assertEquals('Hello World! testEnviromentVariable', $output); } /** From faf29b9919ef4505e0d6298ba82ec31d387ef87a Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 19 Jun 2024 11:52:49 +0100 Subject: [PATCH 24/37] test: failing build --- tests/Orchestration/Base.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/Orchestration/Base.php b/tests/Orchestration/Base.php index 313e7b3..9830ef5 100644 --- a/tests/Orchestration/Base.php +++ b/tests/Orchestration/Base.php @@ -112,6 +112,26 @@ public function testCreateContainer(): void [], __DIR__.'/Resources', ); + + /** + * Test for Failure + */ + $this->expectException(\Exception::class); + + $response = static::getOrchestration()->run( + 'appwrite/runtime-for-php:8.0', + 'TestContainerBadBuild', + [ + 'sh', + '-c', + 'cp /tmp/doesnotexist.tar.gz /usr/local/src/php.tar.gz && tar -zxf /usr/local/src/php.tar.gz --strip 1 && tail -f /dev/null', + ], + '', + '/usr/local/src/', + [], + [], + __DIR__.'/Resources', + ); } // Network Tests From 7a57179c829e573175f8bd6bb89611b991801d86 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 19 Jun 2024 11:54:03 +0100 Subject: [PATCH 25/37] chore: fmt --- src/Orchestration/Adapter/DockerAPI.php | 26 ++++++++++++------------- tests/Orchestration/Base.php | 1 - 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index e9c2dd5..94f2760 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -480,12 +480,12 @@ public function run( 'Env' => array_values($vars), 'HostConfig' => [ 'Binds' => $volumes, - 'CpuQuota' => !empty($this->cpus) ? floatval($this->cpus) * 100000 : null, - 'CpuPeriod' => !empty($this->cpus) ? 100000 : null, - 'Memory' => !empty($this->memory) ? intval($this->memory) * 1e+6 : null, // Convert into bytes - 'MemorySwap' => !empty($this->swap) ? intval($this->swap) * 1e+6 : null, // Convert into bytes + 'CpuQuota' => ! empty($this->cpus) ? floatval($this->cpus) * 100000 : null, + 'CpuPeriod' => ! empty($this->cpus) ? 100000 : null, + 'Memory' => ! empty($this->memory) ? intval($this->memory) * 1e+6 : null, // Convert into bytes + 'MemorySwap' => ! empty($this->swap) ? intval($this->swap) * 1e+6 : null, // Convert into bytes 'AutoRemove' => $remove, - 'NetworkMode' => !empty($network) ? $network : null, + 'NetworkMode' => ! empty($network) ? $network : null, ], ]; @@ -510,19 +510,20 @@ public function run( $containerId = $parsedResponse['Id']; // Run Created Container - $result = $this->call('http://localhost/containers/' . $containerId . '/start', 'POST', '{}'); + $result = $this->call('http://localhost/containers/'.$containerId.'/start', 'POST', '{}'); if ($result['code'] !== 204) { throw new Orchestration('Failed to create function environment: '.$result['response'].' Response Code: '.$result['code']); } - $result = $this->call('http://localhost/containers/' . $containerId . '/wait', 'POST', null, [], 30); + $result = $this->call('http://localhost/containers/'.$containerId.'/wait', 'POST', null, [], 30); if ($result['code'] !== 200) { - throw new Orchestration('Failed to wait for container: ' . $result['response'] . ' Response Code: ' . $result['code']); + throw new Orchestration('Failed to wait for container: '.$result['response'].' Response Code: '.$result['code']); } $waitResponse = json_decode($result['response'], true); - if ($$waitResponse['StatusCode'] !== 0) { + $exitCode = $waitResponse['StatusCode']; + if ($exitCode !== 0) { throw new Orchestration("Docker Error: Container exited with non-zero exit code: {$exitCode}"); } @@ -575,8 +576,6 @@ public function execute( if ($result['code'] !== 200) { throw new Orchestration('Failed to create execute command: '.$result['response'].' Response Code: '.$result['code']); - } else { - return true; } $result = $this->call('http://localhost/exec/'.$parsedResponse['Id'].'/json', 'GET'); @@ -586,9 +585,10 @@ public function execute( } $parsedResponse = json_decode($result['response'], true); - - if ($parsedResponse['ExitCode'] !== 0) { + if ($parsedResponse['Running'] === true || $parsedResponse['ExitCode'] !== 0) { throw new Orchestration('Failed to execute command: '.$result['response'].' Exit Code: '.$parsedResponse['ExitCode']); + } else { + return true; } } diff --git a/tests/Orchestration/Base.php b/tests/Orchestration/Base.php index 9830ef5..42be58d 100644 --- a/tests/Orchestration/Base.php +++ b/tests/Orchestration/Base.php @@ -269,7 +269,6 @@ public function testExecContainer(): void /** * Test for Success */ - $output = ''; $response = static::getOrchestration()->execute( From 119595c31f8123dd04f502692d32dfff93811f50 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 19 Jun 2024 11:58:42 +0100 Subject: [PATCH 26/37] test: cleanup --- tests/Orchestration/Base.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/Orchestration/Base.php b/tests/Orchestration/Base.php index 42be58d..85c1d46 100644 --- a/tests/Orchestration/Base.php +++ b/tests/Orchestration/Base.php @@ -21,6 +21,7 @@ public static function setUpBeforeClass(): void $testContainers = [ 'TestContainer', 'TestContainerTimeout', + 'TestContainerBadBuild', 'UsageStats1', 'UsageStats2', 'TestContainerRM', @@ -435,6 +436,9 @@ public function testRemoveContainer(): void $response = static::getOrchestration()->remove('TestContainerTimeout', true); $this->assertEquals(true, $response); + $response = static::getOrchestration()->remove('TestContainerBadBuild', true); + $this->assertEquals(true, $response); + /** * Test for Failure */ From fc0e02ded70ca4c1a94b46ba21fc3e989a92e8c6 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 19 Jun 2024 12:09:06 +0100 Subject: [PATCH 27/37] debug: fixes --- src/Orchestration/Adapter/DockerAPI.php | 3 +-- tests/Orchestration/Base.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 94f2760..4be397a 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -511,12 +511,11 @@ public function run( // Run Created Container $result = $this->call('http://localhost/containers/'.$containerId.'/start', 'POST', '{}'); - if ($result['code'] !== 204) { throw new Orchestration('Failed to create function environment: '.$result['response'].' Response Code: '.$result['code']); } - $result = $this->call('http://localhost/containers/'.$containerId.'/wait', 'POST', null, [], 30); + $result = $this->call('http://localhost/containers/'.$containerId.'/wait', 'POST', '{}'); if ($result['code'] !== 200) { throw new Orchestration('Failed to wait for container: '.$result['response'].' Response Code: '.$result['code']); } diff --git a/tests/Orchestration/Base.php b/tests/Orchestration/Base.php index 85c1d46..2c99fed 100644 --- a/tests/Orchestration/Base.php +++ b/tests/Orchestration/Base.php @@ -528,7 +528,7 @@ public function testUsageStats(): void * Test for Success */ $stats = static::getOrchestration()->getStats(); - $this->assertCount(0, $stats); + $this->assertCount(0, $stats, 'Container(s) still running: '.\json_encode($stats, JSON_PRETTY_PRINT)); // This allows CPU-heavy load check static::getOrchestration()->setCpus(1); From 32534e9ddf0dbaf0dd3a40a90667c9671c9e56ad Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 19 Jun 2024 13:02:08 +0100 Subject: [PATCH 28/37] fix: tests --- src/Orchestration/Adapter/DockerAPI.php | 29 +++++-------------------- tests/Orchestration/Base.php | 5 +---- 2 files changed, 7 insertions(+), 27 deletions(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 4be397a..8901b88 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -468,7 +468,8 @@ public function run( $vars = $parsedVariables; - // TODO: Connect to $network if not empty + $labels[$this->namespace.'-type'] = 'runtime'; + $labels[$this->namespace.'-created'] = (string)time(); $body = [ 'Hostname' => $hostname, @@ -478,6 +479,7 @@ public function run( 'WorkingDir' => $workdir, 'Labels' => (object) $labels, 'Env' => array_values($vars), + 'Mounts' => $mountFolder ? [['Type' => 'bind', 'Source' => $mountFolder, 'Target' => '/tmp', 'RW' => true]] : [], 'HostConfig' => [ 'Binds' => $volumes, 'CpuQuota' => ! empty($this->cpus) ? floatval($this->cpus) * 100000 : null, @@ -489,14 +491,6 @@ public function run( ], ]; - if (! empty($mountFolder)) { - $body['HostConfig']['Binds'][] = $mountFolder.':/tmp:rw'; - } - - $body = array_filter($body, function ($value) { - return $value !== null && $value !== ''; - }); - $result = $this->call('http://localhost/containers/create?name='.$name, 'POST', json_encode($body), [ 'Content-Type: application/json', 'Content-Length: '.\strlen(\json_encode($body)), @@ -510,20 +504,9 @@ public function run( $containerId = $parsedResponse['Id']; // Run Created Container - $result = $this->call('http://localhost/containers/'.$containerId.'/start', 'POST', '{}'); - if ($result['code'] !== 204) { - throw new Orchestration('Failed to create function environment: '.$result['response'].' Response Code: '.$result['code']); - } - - $result = $this->call('http://localhost/containers/'.$containerId.'/wait', 'POST', '{}'); - if ($result['code'] !== 200) { - throw new Orchestration('Failed to wait for container: '.$result['response'].' Response Code: '.$result['code']); - } - - $waitResponse = json_decode($result['response'], true); - $exitCode = $waitResponse['StatusCode']; - if ($exitCode !== 0) { - throw new Orchestration("Docker Error: Container exited with non-zero exit code: {$exitCode}"); + $startResult = $this->call('http://localhost/containers/'.$containerId.'/start', 'POST', '{}'); + if ($startResult['code'] !== 204) { + throw new Orchestration('Failed to start container: '.$startResult['response']); } return $containerId; diff --git a/tests/Orchestration/Base.php b/tests/Orchestration/Base.php index 2c99fed..4c340a1 100644 --- a/tests/Orchestration/Base.php +++ b/tests/Orchestration/Base.php @@ -217,7 +217,7 @@ public function testnetworkDisconnect(): void } /** - * @depends testCreateNetwork + * @depends testnetworkDisconnect */ public function testRemoveNetwork(): void { @@ -436,9 +436,6 @@ public function testRemoveContainer(): void $response = static::getOrchestration()->remove('TestContainerTimeout', true); $this->assertEquals(true, $response); - $response = static::getOrchestration()->remove('TestContainerBadBuild', true); - $this->assertEquals(true, $response); - /** * Test for Failure */ From 3daa0b767848cb087b5892609f0257c01af647ea Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 19 Jun 2024 14:37:03 +0100 Subject: [PATCH 29/37] fix: tests --- src/Orchestration/Adapter/DockerAPI.php | 17 +++++--- tests/Orchestration/Base.php | 53 ++++++------------------- 2 files changed, 24 insertions(+), 46 deletions(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 8901b88..3af6f66 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -479,18 +479,25 @@ public function run( 'WorkingDir' => $workdir, 'Labels' => (object) $labels, 'Env' => array_values($vars), - 'Mounts' => $mountFolder ? [['Type' => 'bind', 'Source' => $mountFolder, 'Target' => '/tmp', 'RW' => true]] : [], 'HostConfig' => [ 'Binds' => $volumes, - 'CpuQuota' => ! empty($this->cpus) ? floatval($this->cpus) * 100000 : null, - 'CpuPeriod' => ! empty($this->cpus) ? 100000 : null, - 'Memory' => ! empty($this->memory) ? intval($this->memory) * 1e+6 : null, // Convert into bytes - 'MemorySwap' => ! empty($this->swap) ? intval($this->swap) * 1e+6 : null, // Convert into bytes + 'CpuQuota' => floatval($this->cpus) * 100000, + 'CpuPeriod' => 100000, + 'Memory' => intval($this->memory) * 1e+6, // Convert into bytes + 'MemorySwap' => intval($this->swap) * 1e+6, // Convert into bytes 'AutoRemove' => $remove, 'NetworkMode' => ! empty($network) ? $network : null, ], ]; + if (! empty($mountFolder)) { + $body['HostConfig']['Binds'][] = $mountFolder.':/tmp'; + } + + $body = array_filter($body, function ($value) { + return ! empty($value); + }); + $result = $this->call('http://localhost/containers/create?name='.$name, 'POST', json_encode($body), [ 'Content-Type: application/json', 'Content-Length: '.\strlen(\json_encode($body)), diff --git a/tests/Orchestration/Base.php b/tests/Orchestration/Base.php index 4c340a1..adec971 100644 --- a/tests/Orchestration/Base.php +++ b/tests/Orchestration/Base.php @@ -16,31 +16,6 @@ abstract protected static function getAdapterName(): string; */ public static $containerID; - public static function setUpBeforeClass(): void - { - $testContainers = [ - 'TestContainer', - 'TestContainerTimeout', - 'TestContainerBadBuild', - 'UsageStats1', - 'UsageStats2', - 'TestContainerRM', - 'TestContainerRM2', - ]; - - foreach (static::getOrchestration()->list() as $activeContainer) { - if (in_array($activeContainer->getName(), $testContainers)) { - static::getOrchestration()->remove($activeContainer->getName(), true); - } - } - - foreach (static::getOrchestration()->listNetworks() as $activeNetwork) { - if ($activeNetwork->getName() === 'TestNetwork') { - static::getOrchestration()->removeNetwork($activeNetwork->getName()); - } - } - } - public function setUp(): void { } @@ -168,17 +143,7 @@ public function testListNetworks(): void /** * @depends testCreateNetwork */ - public function testnetworkConnect(): void - { - $response = static::getOrchestration()->networkConnect('TestContainer', 'TestNetwork'); - - $this->assertEquals(true, $response); - } - - /** - * @depends testCreateNetwork - */ - public function testCreateContainerWithNetwork(): void + public function testNetworkConnect(): void { $response = static::getOrchestration()->run( 'appwrite/runtime-for-php:8.0', @@ -204,12 +169,18 @@ public function testCreateContainerWithNetwork(): void ); $this->assertNotEmpty($response); + + sleep(1); // wait for container + + $response = static::getOrchestration()->networkConnect('TestContainer', 'TestNetwork'); + + $this->assertEquals(true, $response); } /** - * @depends testnetworkConnect + * @depends testNetworkConnect */ - public function testnetworkDisconnect(): void + public function testNetworkDisconnect(): void { $response = static::getOrchestration()->networkDisconnect('TestContainer', 'TestNetwork', true); @@ -217,7 +188,7 @@ public function testnetworkDisconnect(): void } /** - * @depends testnetworkDisconnect + * @depends testNetworkDisconnect */ public function testRemoveNetwork(): void { @@ -272,7 +243,7 @@ public function testExecContainer(): void */ $output = ''; - $response = static::getOrchestration()->execute( + static::getOrchestration()->execute( 'TestContainer', [ 'php', @@ -294,7 +265,7 @@ public function testCheckVolume(): void { $output = ''; - $response = static::getOrchestration()->execute( + static::getOrchestration()->execute( 'TestContainer', [ 'cat', From c1f714266b538607d731d2c2d29a97b107459a6b Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 19 Jun 2024 14:37:54 +0100 Subject: [PATCH 30/37] chore: refactor guard clause --- src/Orchestration/Adapter/DockerAPI.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 3af6f66..83e9d33 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -576,9 +576,9 @@ public function execute( $parsedResponse = json_decode($result['response'], true); if ($parsedResponse['Running'] === true || $parsedResponse['ExitCode'] !== 0) { throw new Orchestration('Failed to execute command: '.$result['response'].' Exit Code: '.$parsedResponse['ExitCode']); - } else { - return true; } + + return true; } /** From e868b9332b88542f65c54438179cd8db1e4409cc Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 19 Jun 2024 14:46:06 +0100 Subject: [PATCH 31/37] chore: fmt --- src/Orchestration/Adapter/DockerAPI.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 83e9d33..af393ef 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -469,7 +469,7 @@ public function run( $vars = $parsedVariables; $labels[$this->namespace.'-type'] = 'runtime'; - $labels[$this->namespace.'-created'] = (string)time(); + $labels[$this->namespace.'-created'] = (string) time(); $body = [ 'Hostname' => $hostname, From d263fae77a947678563b4fa5c546e1be03861633 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 19 Jun 2024 18:48:16 +0100 Subject: [PATCH 32/37] feat: better logs --- src/Orchestration/Adapter/DockerAPI.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index af393ef..364d985 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -503,7 +503,11 @@ public function run( 'Content-Length: '.\strlen(\json_encode($body)), ]); - if ($result['code'] !== 201) { + if ($result['code'] === 404) { + throw new Orchestration('Container image "'.$image.'" not found.'); + } elseif ($result['code'] === 409) { + throw new Orchestration('Container with name "'.$name.'" already exists.'); + } elseif ($result['code'] !== 201) { throw new Orchestration('Failed to create function environment: '.$result['response'].' Response Code: '.$result['code']); } From 4024831ce86c7834a81763a198854babd0c492ea Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 20 Jun 2024 11:27:37 +0100 Subject: [PATCH 33/37] feat: auto pull --- src/Orchestration/Adapter/DockerAPI.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 364d985..e05a95c 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -459,6 +459,11 @@ public function run( bool $remove = false, string $network = '' ): string { + $result = $this->call('http://localhost/images/'.$image.'/json', 'GET'); + if ($result['code'] === 404 && ! $this->pull($image)) { + throw new Orchestration('Missing image "'.$image.'" and failed to pull it.'); + } + $parsedVariables = []; foreach ($vars as $key => $value) { From 13acfd57df919935849ae871641e9b0d242b4a42 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jul 2024 10:56:56 +0100 Subject: [PATCH 34/37] chore: matej review --- src/Orchestration/Adapter/DockerAPI.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index e05a95c..75f2497 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -207,11 +207,10 @@ public function removeNetwork(string $name): bool if ($result['code'] === 404) { throw new Orchestration('Network with name "'.$name.'" does not exist: '.$result['response']); - } elseif ($result['code'] !== 204) { + } else if ($result['code'] !== 204) { throw new Orchestration('Error removing network: '.$result['response']); } - - return $result['code'] === 204; + return true; } /** @@ -473,9 +472,6 @@ public function run( $vars = $parsedVariables; - $labels[$this->namespace.'-type'] = 'runtime'; - $labels[$this->namespace.'-created'] = (string) time(); - $body = [ 'Hostname' => $hostname, 'Entrypoint' => $entrypoint, From a6eb03af56e82c86f3b41f4e238fb24925d8d3d8 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jul 2024 11:04:50 +0100 Subject: [PATCH 35/37] chore: fmt --- src/Orchestration/Adapter/DockerAPI.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 75f2497..03c3cb8 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -207,9 +207,10 @@ public function removeNetwork(string $name): bool if ($result['code'] === 404) { throw new Orchestration('Network with name "'.$name.'" does not exist: '.$result['response']); - } else if ($result['code'] !== 204) { + } elseif ($result['code'] !== 204) { throw new Orchestration('Error removing network: '.$result['response']); } + return true; } From 0553d95fef939d07bdda08eb6c4662550c8b3a2f Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jul 2024 11:12:37 +0100 Subject: [PATCH 36/37] chore: revert labels --- src/Orchestration/Adapter/DockerAPI.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 03c3cb8..6533225 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -207,10 +207,9 @@ public function removeNetwork(string $name): bool if ($result['code'] === 404) { throw new Orchestration('Network with name "'.$name.'" does not exist: '.$result['response']); - } elseif ($result['code'] !== 204) { + } else if ($result['code'] !== 204) { throw new Orchestration('Error removing network: '.$result['response']); } - return true; } @@ -473,6 +472,9 @@ public function run( $vars = $parsedVariables; + $labels[$this->namespace.'-type'] = 'runtime'; + $labels[$this->namespace.'-created'] = (string) time(); + $body = [ 'Hostname' => $hostname, 'Entrypoint' => $entrypoint, From 582fc3eedc070a5290b9546e6e432acdcf14676c Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jul 2024 11:21:00 +0100 Subject: [PATCH 37/37] chore: fmt --- src/Orchestration/Adapter/DockerAPI.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 6533225..00c8587 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -207,9 +207,10 @@ public function removeNetwork(string $name): bool if ($result['code'] === 404) { throw new Orchestration('Network with name "'.$name.'" does not exist: '.$result['response']); - } else if ($result['code'] !== 204) { + } elseif ($result['code'] !== 204) { throw new Orchestration('Error removing network: '.$result['response']); } + return true; }