From f3ba2ca609a5cf395c8edb900f067d7090e96440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 13 Dec 2022 12:35:46 +0000 Subject: [PATCH 1/2] Fix socket request headers --- 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 1e53f02..155f814 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -52,6 +52,8 @@ public function __construct(string $username = null, string $password = null, st */ 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'); From 6cfb78939b1d8c5f9f917d30c3eaa43c79e28c45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 13 Dec 2022 14:02:04 +0000 Subject: [PATCH 2/2] Bug fixing for Docker API --- src/Orchestration/Adapter/DockerAPI.php | 38 ++++++++++++++++++------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index 155f814..84b633d 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -107,18 +107,24 @@ 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); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $headers = [ 'Content-Type: application/json', - 'Content-Length: 2', - 'host: null' + 'Content-Length: ' . \strlen($body) ]; + + $headers[] = 'Host: utopia-php'; // Fix Swoole headers bug with socket requests + \curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); /* @@ -141,7 +147,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 @@ -158,7 +168,6 @@ protected function streamCall(string $url, int $timeout = -1): array }; \curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callback); - \curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); $result = \curl_exec($ch); $responseCode = \curl_getinfo($ch, CURLINFO_RESPONSE_CODE); @@ -312,21 +321,28 @@ public function getStats(string $container = null, array $filters = []): array $stats = \json_decode($result['response'], true); - $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']; - $networkIn = 0; $networkOut = 0; - foreach ($stats['networks'] as $network) { + foreach (($stats['networks'] ?? []) as $network) { $networkIn += $network['rx_bytes']; $networkOut += $network['tx_bytes']; } + $memoryUsage = 0; + + if(isset($stats['memory_stats']['usage']) && isset($stats['memory_stats']['limit'])) { + $memoryUsage = ($stats['memory_stats']['usage'] / $stats['memory_stats']['limit']) * 100.0; + } + + // $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']; + $cpuUsage = 1; // TODO: Implement (API seems to give incorrect values) + $list[] = new Stats( containerId: $stats['id'], containerName: \ltrim($stats['name'], '/'), // Remove '/' prefix - cpuUsage:1, // TODO: Implement (API seems to give incorrect values) - memoryUsage: ($stats['memory_stats']['usage'] / $stats['memory_stats']['limit']) * 100.0, + cpuUsage: $cpuUsage, + memoryUsage: $memoryUsage, 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 networkIO: [ 'in' => $networkIn, 'out' => $networkOut ],