halfer / gitlab-ls-registry
一个简单的HTTP库,用于查询GitLab的非官方Docker仓库API
Requires
- ext-curl: *
This package is not auto-updated.
Last update: 2024-09-30 00:09:23 UTC
README
这是一个简单的PHP控制台命令,用于从GitLab获取有用的Docker仓库数据。我写这个命令是因为我不知道如何(a)使用Docker方式,或(b)使用官方GitLab API。可能有更好的方法来做这件事,如果有的话,请告诉我!
这是一个正在进行中的项目,但已存在的类相当直观。使用IDE在GitLab
类上获取自动补全。
API状态不确定
更新:截至2018年11月,似乎public gitlab.com实例上的内部仓库API不再与令牌一起工作。欢迎为此项目提交修复的拉取请求。我可能找到时间自己来做这件事,但目前我对GitLab不会再次破坏向下兼容性没有太多希望。
任何希望讨论此问题的读者都欢迎在GitHub上提交一个工单。
私有仓库
如果您有一个私有仓库,您需要创建一个个人访问令牌才能访问它。您需要给令牌命名并设置过期日期,并选择合适的权限。api
权限是可以的,但如果您只使用此令牌来获取仓库信息,请使用read_registry
。
出于安全原因,此页面只会显示令牌的值,直到它可见 - 一旦您转到GitLab上的另一个页面,您将无法再查看它。因此,请复制并保存在安全的地方。
安装
安装说明可以在这里找到。
初始化
要创建一个注册列表的新实例,请运行以下命令
// Set up the autoloaders
require_once $root . '/vendor/autoload.php';
use DockerLs\Registry\GitLab;
$username = '<your-username>';
$project = '<your-project>';
$token = '<your-token>';
$gitLab = new GitLab($username, $project, $token);
我没有尝试过,但我认为如果您想与公共仓库一起工作,您只需省略令牌即可
$username = '<your-username>';
$project = '<your-project>';
$gitLab = new GitLab($username, $project);
获取仓库信息
您可以通过以下方式获取一些有趣的仓库元数据
$registry = $gitLab
->fetchRegistryInfo()
->getRegistryInfo();
print_r($registry);
这将返回类似以下的数据
Array
(
[id] => 12345
[path] => jonuser/my-wordpress
[location] => registry.gitlab.com/jonuser/my-wordpress
[tags_path] => /jonuser/my-wordpress/registry/repository/12345/tags?format=json
[destroy_path] => /jonuser/my-wordpress/container_registry/12345.json
)
请注意,要获取镜像数据,必须首先获取仓库信息。这是必要的,以便获取tags_path
中的URL,用于查询仓库数据。
获取镜像信息
以下是如何设置页面大小,获取第一页数据,然后按关键字排序的方法
$images = $gitLab
->fetchRegistryInfo()
->setResultsPerPage(5)
->setPageNo(1)
->fetchImageList()
->sortImageList('total_size')
->getImageList();
print_r($images);
这是一个跳过几页结果的快捷方法。在这种情况下,您不需要设置页面号,因为它会为您设置,从第1页开始
$images = $gitLab
->fetchRegistryInfo()
->setResultsPerPage(20)
->fetchAllImages()
->sortImageList('total_size')
->getImageList();
print_r($images);
如果成功,两种方法都会得到类似以下格式的数组数组
[name] => v1.01
[location] => registry.gitlab.com/jonuser/my-wordpress:v1.01
[revision] => 8b6e7c5564c3256eefa894ed8303dd0bdc441fce998bd1853f857226ed929d8f
[short_revision] => 8b6e7c556
[total_size] => 32049316
[created_at] => 2018-04-22T12:23:57.707+00:00
[destroy_path] => /jonuser/my-wordpress/registry/repository/84654/tags/v1.01
过滤镜像信息
可以使用相等过滤器过滤图像元数据。例如,要获取匹配特定哈希的图像
$images = $gitLab
->fetchRegistryInfo()
->setResultsPerPage(20)
->fetchAllImages()
->matchFilter('revision', '8b6edd0bdc441fce997c5564c3d8b6eefa894e72261853f85258303ded929d8f')
->getImageList();
计算图像数量
一个有用的检查是查看某些内容是否存在,例如一个标签。为此,我们可以使用计数方法
echo $gitLab
->fetchRegistryInfo()
->setResultsPerPage(20)
->fetchAllImages()
->matchFilter('name', 'v1.02')
->getImageCount();
这将输出一个整数,可以使用grep搜索0(即不存在)。
元数据块中的任何键都可以用作过滤字段。
获取图像年龄
您可能需要根据图像的年龄采取行动。例如,如果您的图像超过一周,您可能希望执行无缓存的干净Docker构建。您可以这样做:
$images = $gitLab
->fetchRegistryInfo()
->fetchAllImages()
->sortImageList('created_at')
->matchFilter('name', 'v1.02')
->calculateImageAges()
->getImageList();
if ($images && isset($images[0]))
{
echo $images[0]['created_at_age'];
}
这将返回通常的图像列表格式,以及一个新的键,包含图像的完整天数。
[created_at_age] => 2
默认情况下,这是相对于现在计算年龄,但您可以提供自定义的DateTime
,从该日期时间计算年龄。
年龄计算器的默认单位是天。但是,您可以提供自定义的DateTimeInterval格式字符串,例如,如果您想要以小时为单位。
提示
如果您想调试curl所发出的调用,有一个调试模式。
$images = $gitLab
->setDebugMode(true)
->fetchRegistryInfo()
->setResultsPerPage(20)
->fetchAllImages();
输出将看起来像这样
Debug: calling https://gitlab.com/jonuser/my-wordpress/container_registry.json
Debug: call took 35.351953 sec
Debug: calling https://gitlab.com/jonuser/my-wordpress/registry/repository/12345/tags?format=json&page=1&per_page=20
Debug: call took 10.322790 sec
在我的经验中,GitLab的云版本可能有点慢。如果您有很多图像,尝试增加您的setResultsPerPage
,它默认为仅10。这将减少所需的调用次数。我不知道每页的最大结果数是多少,但可以通过简单的实验找到。
测试
此代码已在托管GitLab的免费版本上进行了测试。我预计它也会在云的付费版本上工作。我尚未在自托管的实例上测试此代码。
此代码依赖于内部API而不是公共API。我询问了此API的稳定性即是否可以依赖。
如果GitLab将注册表端点添加到其官方API,则此库将变得不再必要。如果发生这种情况,将存在经过充分测试的库,例如这个。