Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: socket request headers #26

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions src/Orchestration/Adapter/DockerAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -98,18 +100,25 @@ 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',
];

$headers[] = 'Host: utopia-php'; // Fix Swoole headers bug with socket requests

\curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

/*
Expand All @@ -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
Expand Down Expand Up @@ -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 ],
);
}

Expand Down
Loading