all-in-one/php/src/Docker/DockerHubManager.php

64 lines
2.3 KiB
PHP
Raw Normal View History

2021-11-30 11:20:42 +01:00
<?php
namespace AIO\Docker;
use AIO\ContainerDefinitionFetcher;
use AIO\Data\ConfigurationManager;
use GuzzleHttp\Client;
class DockerHubManager
{
private Client $guzzleClient;
public function __construct()
{
$this->guzzleClient = new Client();
}
public function GetLatestDigestOfTag(string $name, string $tag) : ?string {
$cacheKey = 'dockerhub-manifest-' . $name . $tag;
$cachedVersion = apcu_fetch($cacheKey);
if($cachedVersion !== false && is_string($cachedVersion)) {
return $cachedVersion;
}
// If one of the links below should ever become outdated, we can still upgrade the mastercontainer via the webinterface manually by opening '/api/docker/getwatchtower'
2021-11-30 11:20:42 +01:00
try {
$request = $this->guzzleClient->request(
2021-11-30 11:20:42 +01:00
'GET',
'https://hub.docker.com/v2/repositories/' . $name . '/tags?page_size=128'
2021-11-30 11:20:42 +01:00
);
$body = $request->getBody()->getContents();
2021-11-30 11:20:42 +01:00
$decodedBody = json_decode($body, true);
if (isset($decodedBody['results'])) {
$arch = php_uname('m') === 'x86_64' ? 'amd64' : 'arm64';
foreach($decodedBody['results'] as $values) {
if (isset($values['name'])
&& $values['name'] === $tag
&& isset($values['images'])
&& is_array($values['images'])) {
foreach ($values['images'] as $images) {
if (isset($images['architecture'])
&& $images['architecture'] === $arch
&& isset($images['digest'])
&& is_string($images['digest'])) {
$latestVersion = $images['digest'];
apcu_add($cacheKey, $latestVersion, 600);
return $latestVersion;
}
}
}
2021-11-30 11:20:42 +01:00
}
}
error_log('Could not get digest of container ' . $name . ':' . $tag);
2021-11-30 11:20:42 +01:00
return null;
} catch (\Exception $e) {
error_log('Could not get digest of container ' . $name . ':' . $tag . ' ' . $e->getMessage());
2021-11-30 11:20:42 +01:00
return null;
}
}
}