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 ;
}
2022-03-14 22:40:13 +01:00
// 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 {
2023-02-01 19:55:54 +01:00
$request = $this -> guzzleClient -> request (
2021-11-30 11:20:42 +01:00
'GET' ,
2023-02-01 19:55:54 +01:00
'https://hub.docker.com/v2/repositories/' . $name . '/tags?page_size=128'
2021-11-30 11:20:42 +01:00
);
2023-02-01 19:55:54 +01:00
$body = $request -> getBody () -> getContents ();
2021-11-30 11:20:42 +01:00
$decodedBody = json_decode ( $body , true );
2023-02-01 19:55:54 +01:00
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
}
}
2022-04-09 14:17:53 +02:00
error_log ( 'Could not get digest of container ' . $name . ':' . $tag );
2021-11-30 11:20:42 +01:00
return null ;
} catch ( \Exception $e ) {
2022-04-09 14:17:53 +02:00
error_log ( 'Could not get digest of container ' . $name . ':' . $tag . ' ' . $e -> getMessage ());
2021-11-30 11:20:42 +01:00
return null ;
}
}
}