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-02 18:26:44 +01:00
$authTokenRequest = $this -> guzzleClient -> request (
2021-11-30 11:20:42 +01:00
'GET' ,
2023-02-02 18:26:44 +01:00
'https://auth.docker.io/token?service=registry.docker.io&scope=repository:' . $name . ':pull'
2021-11-30 11:20:42 +01:00
);
2023-02-02 18:26:44 +01:00
$body = $authTokenRequest -> getBody () -> getContents ();
2021-11-30 11:20:42 +01:00
$decodedBody = json_decode ( $body , true );
2023-02-02 18:26:44 +01:00
if ( isset ( $decodedBody [ 'token' ])) {
$authToken = $decodedBody [ 'token' ];
$manifestRequest = $this -> guzzleClient -> request (
'GET' ,
'https://registry-1.docker.io/v2/' . $name . '/manifests/' . $tag ,
[
'headers' => [
2023-02-03 11:38:42 +01:00
'Accept' => 'application/vnd.oci.image.index.v1+json,application/vnd.docker.distribution.manifest.list.v2+json,application/vnd.docker.distribution.manifest.v2+json' ,
2023-02-02 18:26:44 +01:00
'Authorization' => 'Bearer ' . $authToken ,
],
]
);
$responseHeaders = $manifestRequest -> getHeader ( 'docker-content-digest' );
if ( count ( $responseHeaders ) === 1 ) {
$latestVersion = $responseHeaders [ 0 ];
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 ;
}
}
}