diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index ebd019a..d6ecb71 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -48,6 +48,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'); @@ -98,11 +100,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); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $headers = [ @@ -110,6 +116,9 @@ protected function streamCall(string $url, int $timeout = -1): array 'Content-Length: 2', 'host: null', ]; + + $headers[] = 'Host: utopia-php'; // Fix Swoole headers bug with socket requests + \curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); /* @@ -132,7 +141,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 @@ -286,19 +299,29 @@ public function getStats(string $container = null, array $filters = []): array $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, - 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], + 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 ], ); }