Skip to content

Commit

Permalink
Merge pull request #513 from nextcloud/backport/510/stable31
Browse files Browse the repository at this point in the history
[stable31] Improve error logging
  • Loading branch information
oleksandr-nc authored Feb 14, 2025
2 parents 99e67ef + 715cd8c commit 6ca2336
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions lib/DeployActions/DockerActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class DockerActions implements IDeployActions {

private Client $guzzleClient;
private bool $useSocket = false; # for `pullImage` function, to detect can be stream used or not.
private string $socketAddress;

public function __construct(
private readonly LoggerInterface $logger,
Expand Down Expand Up @@ -465,7 +466,8 @@ public function pullImage(
$this->logger->info(sprintf('Image(%s) pulled successfully.', $imageId));
}
} catch (GuzzleException $e) {
$r = sprintf('Failed to pull image, GuzzleException occur: %s', $e->getMessage());
$urlToLog = $this->useSocket ? $this->socketAddress : $dockerUrl;
$r = sprintf('Failed to pull image via "%s", GuzzleException occur: %s', $urlToLog, $e->getMessage());
}
return $r;
}
Expand Down Expand Up @@ -661,7 +663,8 @@ public function ping(string $dockerUrl): bool {
return true;
}
} catch (Exception $e) {
$this->logger->error('Could not connect to Docker daemon', ['exception' => $e]);
$urlToLog = $this->useSocket ? $this->socketAddress : $url;
$this->logger->error('Could not connect to Docker daemon via {url}', ['exception' => $e, 'url' => $urlToLog]);
error_log($e->getMessage());
}
return false;
Expand Down Expand Up @@ -823,21 +826,20 @@ public function healthcheckContainer(string $containerId, DaemonConfig $daemonCo
}

public function buildDockerUrl(DaemonConfig $daemonConfig): string {
if (file_exists($daemonConfig->getHost())) {
return 'http://localhost';
}
return $daemonConfig->getProtocol() . '://' . $daemonConfig->getHost();
// When using local socket, we the curl URL needs to be set to http://localhost
return $this->isLocalSocket($daemonConfig->getHost()) ? 'http://localhost' : $daemonConfig->getProtocol() . '://' . $daemonConfig->getHost();
}

public function initGuzzleClient(DaemonConfig $daemonConfig): void {
$guzzleParams = [];
if (file_exists($daemonConfig->getHost())) {
if ($this->isLocalSocket($daemonConfig->getHost())) {
$guzzleParams = [
'curl' => [
CURLOPT_UNIX_SOCKET_PATH => $daemonConfig->getHost(),
],
];
$this->useSocket = true;
$this->socketAddress = $daemonConfig->getHost();
} elseif ($daemonConfig->getProtocol() === 'https') {
$guzzleParams = $this->setupCerts($guzzleParams);
}
Expand Down Expand Up @@ -900,4 +902,16 @@ private function buildDefaultGPUDeviceRequests(): array {
],
];
}

private function isLocalSocket(string $host): bool {
$isLocalPath = strpos($host, '/') === 0;
if ($isLocalPath) {
if (!file_exists($host)) {
$this->logger->error('Local docker socket path {path} does not exist', ['path' => $host]);
} elseif (!is_writable($host)) {
$this->logger->error('Local docker socket path {path} is not writable', ['path' => $host]);
}
}
return $isLocalPath;
}
}

0 comments on commit 6ca2336

Please sign in to comment.