2022-03-30 10:42:47 +02:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-03-30 10:42:47 +02:00
|
|
|
|
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
packages_model "code.gitea.io/gitea/models/packages"
|
|
|
|
container_model "code.gitea.io/gitea/models/packages/container"
|
2024-03-02 16:42:31 +01:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2022-07-28 05:59:39 +02:00
|
|
|
container_module "code.gitea.io/gitea/modules/packages/container"
|
2023-12-12 14:51:33 +01:00
|
|
|
packages_service "code.gitea.io/gitea/services/packages"
|
2023-02-06 11:07:09 +01:00
|
|
|
|
|
|
|
digest "github.com/opencontainers/go-digest"
|
2022-03-30 10:42:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Cleanup removes expired container data
|
|
|
|
func Cleanup(ctx context.Context, olderThan time.Duration) error {
|
|
|
|
if err := cleanupExpiredBlobUploads(ctx, olderThan); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-07-26 19:39:19 +02:00
|
|
|
if err := CleanupSHA256(ctx, olderThan); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-30 10:42:47 +02:00
|
|
|
return cleanupExpiredUploadedBlobs(ctx, olderThan)
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanupExpiredBlobUploads removes expired blob uploads
|
|
|
|
func cleanupExpiredBlobUploads(ctx context.Context, olderThan time.Duration) error {
|
|
|
|
pbus, err := packages_model.FindExpiredBlobUploads(ctx, olderThan)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pbu := range pbus {
|
|
|
|
if err := RemoveBlobUploadByID(ctx, pbu.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanupExpiredUploadedBlobs removes expired uploaded blobs not referenced by a manifest
|
|
|
|
func cleanupExpiredUploadedBlobs(ctx context.Context, olderThan time.Duration) error {
|
|
|
|
pfs, err := container_model.SearchExpiredUploadedBlobs(ctx, olderThan)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pf := range pfs {
|
2023-12-12 14:51:33 +01:00
|
|
|
if err := packages_service.DeletePackageFile(ctx, pf); err != nil {
|
2022-03-30 10:42:47 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 03:32:09 +02:00
|
|
|
pvs, _, err := packages_model.SearchVersions(ctx, &packages_model.PackageSearchOptions{
|
|
|
|
Type: packages_model.TypeContainer,
|
|
|
|
Version: packages_model.SearchValue{
|
|
|
|
ExactMatch: true,
|
|
|
|
Value: container_model.UploadVersion,
|
|
|
|
},
|
2024-03-02 16:42:31 +01:00
|
|
|
IsInternal: optional.Some(true),
|
|
|
|
HasFiles: optional.Some(false),
|
2022-04-06 03:32:09 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pv := range pvs {
|
|
|
|
if err := packages_model.DeleteAllProperties(ctx, packages_model.PropertyTypeVersion, pv.ID); err != nil {
|
2022-03-30 10:42:47 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-06 03:32:09 +02:00
|
|
|
if err := packages_model.DeleteVersionByID(ctx, pv.ID); err != nil {
|
|
|
|
return err
|
2022-03-30 10:42:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-28 05:59:39 +02:00
|
|
|
|
2022-11-20 15:08:38 +01:00
|
|
|
func ShouldBeSkipped(ctx context.Context, pcr *packages_model.PackageCleanupRule, p *packages_model.Package, pv *packages_model.PackageVersion) (bool, error) {
|
|
|
|
// Always skip the "latest" tag
|
|
|
|
if pv.LowerVersion == "latest" {
|
|
|
|
return true, nil
|
2022-07-28 05:59:39 +02:00
|
|
|
}
|
|
|
|
|
2022-11-20 15:08:38 +01:00
|
|
|
// Check if the version is a digest (or untagged)
|
2023-02-06 11:07:09 +01:00
|
|
|
if digest.Digest(pv.LowerVersion).Validate() == nil {
|
2022-11-20 15:08:38 +01:00
|
|
|
// Check if there is another manifest referencing this version
|
|
|
|
has, err := packages_model.ExistVersion(ctx, &packages_model.PackageSearchOptions{
|
|
|
|
PackageID: p.ID,
|
|
|
|
Properties: map[string]string{
|
|
|
|
container_module.PropertyManifestReference: pv.LowerVersion,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2022-07-28 05:59:39 +02:00
|
|
|
}
|
|
|
|
|
2022-11-20 15:08:38 +01:00
|
|
|
// Skip it if the version is referenced
|
|
|
|
if has {
|
|
|
|
return true, nil
|
2022-07-28 05:59:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 15:08:38 +01:00
|
|
|
return false, nil
|
2022-07-28 05:59:39 +02:00
|
|
|
}
|