2023-02-05 10:12:31 +00:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
packages_model "code.gitea.io/gitea/models/packages"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-03-02 15:42:31 +00:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2023-02-05 10:12:31 +00:00
|
|
|
packages_module "code.gitea.io/gitea/modules/packages"
|
|
|
|
packages_service "code.gitea.io/gitea/services/packages"
|
2023-12-31 04:08:46 +00:00
|
|
|
alpine_service "code.gitea.io/gitea/services/packages/alpine"
|
2024-08-04 06:16:29 +00:00
|
|
|
arch_service "code.gitea.io/gitea/services/packages/arch"
|
2023-02-05 10:12:31 +00:00
|
|
|
cargo_service "code.gitea.io/gitea/services/packages/cargo"
|
|
|
|
container_service "code.gitea.io/gitea/services/packages/container"
|
2023-05-02 16:31:35 +00:00
|
|
|
debian_service "code.gitea.io/gitea/services/packages/debian"
|
2024-01-19 11:37:10 +00:00
|
|
|
rpm_service "code.gitea.io/gitea/services/packages/rpm"
|
2023-02-05 10:12:31 +00:00
|
|
|
)
|
|
|
|
|
2023-08-08 00:46:10 +00:00
|
|
|
// Task method to execute cleanup rules and cleanup expired package data
|
|
|
|
func CleanupTask(ctx context.Context, olderThan time.Duration) error {
|
|
|
|
if err := ExecuteCleanupRules(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return CleanupExpiredData(ctx, olderThan)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExecuteCleanupRules(outerCtx context.Context) error {
|
|
|
|
ctx, committer, err := db.TxContext(outerCtx)
|
2023-02-05 10:12:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer committer.Close()
|
|
|
|
|
|
|
|
err = packages_model.IterateEnabledCleanupRules(ctx, func(ctx context.Context, pcr *packages_model.PackageCleanupRule) error {
|
|
|
|
select {
|
2023-08-08 00:46:10 +00:00
|
|
|
case <-outerCtx.Done():
|
2023-02-05 10:12:31 +00:00
|
|
|
return db.ErrCancelledf("While processing package cleanup rules")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pcr.CompiledPattern(); err != nil {
|
|
|
|
return fmt.Errorf("CleanupRule [%d]: CompilePattern failed: %w", pcr.ID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
olderThan := time.Now().AddDate(0, 0, -pcr.RemoveDays)
|
|
|
|
|
|
|
|
packages, err := packages_model.GetPackagesByType(ctx, pcr.OwnerID, pcr.Type)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("CleanupRule [%d]: GetPackagesByType failed: %w", pcr.ID, err)
|
|
|
|
}
|
|
|
|
|
2023-05-02 16:31:35 +00:00
|
|
|
anyVersionDeleted := false
|
2023-02-05 10:12:31 +00:00
|
|
|
for _, p := range packages {
|
|
|
|
pvs, _, err := packages_model.SearchVersions(ctx, &packages_model.PackageSearchOptions{
|
|
|
|
PackageID: p.ID,
|
2024-03-02 15:42:31 +00:00
|
|
|
IsInternal: optional.Some(false),
|
2023-02-05 10:12:31 +00:00
|
|
|
Sort: packages_model.SortCreatedDesc,
|
|
|
|
Paginator: db.NewAbsoluteListOptions(pcr.KeepCount, 200),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("CleanupRule [%d]: SearchVersions failed: %w", pcr.ID, err)
|
|
|
|
}
|
|
|
|
versionDeleted := false
|
|
|
|
for _, pv := range pvs {
|
|
|
|
if pcr.Type == packages_model.TypeContainer {
|
|
|
|
if skip, err := container_service.ShouldBeSkipped(ctx, pcr, p, pv); err != nil {
|
|
|
|
return fmt.Errorf("CleanupRule [%d]: container.ShouldBeSkipped failed: %w", pcr.ID, err)
|
|
|
|
} else if skip {
|
|
|
|
log.Debug("Rule[%d]: keep '%s/%s' (container)", pcr.ID, p.Name, pv.Version)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toMatch := pv.LowerVersion
|
|
|
|
if pcr.MatchFullName {
|
|
|
|
toMatch = p.LowerName + "/" + pv.LowerVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
if pcr.KeepPatternMatcher != nil && pcr.KeepPatternMatcher.MatchString(toMatch) {
|
|
|
|
log.Debug("Rule[%d]: keep '%s/%s' (keep pattern)", pcr.ID, p.Name, pv.Version)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if pv.CreatedUnix.AsLocalTime().After(olderThan) {
|
|
|
|
log.Debug("Rule[%d]: keep '%s/%s' (remove days)", pcr.ID, p.Name, pv.Version)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if pcr.RemovePatternMatcher != nil && !pcr.RemovePatternMatcher.MatchString(toMatch) {
|
|
|
|
log.Debug("Rule[%d]: keep '%s/%s' (remove pattern)", pcr.ID, p.Name, pv.Version)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("Rule[%d]: remove '%s/%s'", pcr.ID, p.Name, pv.Version)
|
|
|
|
|
|
|
|
if err := packages_service.DeletePackageVersionAndReferences(ctx, pv); err != nil {
|
|
|
|
return fmt.Errorf("CleanupRule [%d]: DeletePackageVersionAndReferences failed: %w", pcr.ID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
versionDeleted = true
|
2023-05-02 16:31:35 +00:00
|
|
|
anyVersionDeleted = true
|
2023-02-05 10:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if versionDeleted {
|
|
|
|
if pcr.Type == packages_model.TypeCargo {
|
|
|
|
owner, err := user_model.GetUserByID(ctx, pcr.OwnerID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetUserByID failed: %w", err)
|
|
|
|
}
|
2023-10-24 03:26:38 +00:00
|
|
|
if err := cargo_service.UpdatePackageIndexIfExists(ctx, owner, owner, p.ID); err != nil {
|
|
|
|
return fmt.Errorf("CleanupRule [%d]: cargo.UpdatePackageIndexIfExists failed: %w", pcr.ID, err)
|
2023-02-05 10:12:31 +00:00
|
|
|
}
|
2023-05-02 16:31:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if anyVersionDeleted {
|
|
|
|
if pcr.Type == packages_model.TypeDebian {
|
|
|
|
if err := debian_service.BuildAllRepositoryFiles(ctx, pcr.OwnerID); err != nil {
|
|
|
|
return fmt.Errorf("CleanupRule [%d]: debian.BuildAllRepositoryFiles failed: %w", pcr.ID, err)
|
2023-02-05 10:12:31 +00:00
|
|
|
}
|
2023-12-31 04:08:46 +00:00
|
|
|
} else if pcr.Type == packages_model.TypeAlpine {
|
|
|
|
if err := alpine_service.BuildAllRepositoryFiles(ctx, pcr.OwnerID); err != nil {
|
|
|
|
return fmt.Errorf("CleanupRule [%d]: alpine.BuildAllRepositoryFiles failed: %w", pcr.ID, err)
|
|
|
|
}
|
2024-01-19 11:37:10 +00:00
|
|
|
} else if pcr.Type == packages_model.TypeRpm {
|
|
|
|
if err := rpm_service.BuildAllRepositoryFiles(ctx, pcr.OwnerID); err != nil {
|
|
|
|
return fmt.Errorf("CleanupRule [%d]: rpm.BuildAllRepositoryFiles failed: %w", pcr.ID, err)
|
|
|
|
}
|
2024-08-04 06:16:29 +00:00
|
|
|
} else if pcr.Type == packages_model.TypeArch {
|
|
|
|
if err := arch_service.BuildAllRepositoryFiles(ctx, pcr.OwnerID); err != nil {
|
|
|
|
return fmt.Errorf("CleanupRule [%d]: arch.BuildAllRepositoryFiles failed: %w", pcr.ID, err)
|
|
|
|
}
|
2023-02-05 10:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-08 00:46:10 +00:00
|
|
|
return committer.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func CleanupExpiredData(outerCtx context.Context, olderThan time.Duration) error {
|
|
|
|
ctx, committer, err := db.TxContext(outerCtx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer committer.Close()
|
|
|
|
|
2023-02-05 10:12:31 +00:00
|
|
|
if err := container_service.Cleanup(ctx, olderThan); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-27 13:29:53 +00:00
|
|
|
pIDs, err := packages_model.FindUnreferencedPackages(ctx)
|
2023-02-05 10:12:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-07-27 13:29:53 +00:00
|
|
|
for _, pID := range pIDs {
|
|
|
|
if err := packages_model.DeleteAllProperties(ctx, packages_model.PropertyTypePackage, pID); err != nil {
|
2023-02-05 10:12:31 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-07-27 13:29:53 +00:00
|
|
|
if err := packages_model.DeletePackageByID(ctx, pID); err != nil {
|
2023-02-05 10:12:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pbs, err := packages_model.FindExpiredUnreferencedBlobs(ctx, olderThan)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pb := range pbs {
|
|
|
|
if err := packages_model.DeleteBlobByID(ctx, pb.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := committer.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
contentStore := packages_module.NewContentStore()
|
|
|
|
for _, pb := range pbs {
|
|
|
|
if err := contentStore.Delete(packages_module.BlobHash256Key(pb.HashSHA256)); err != nil {
|
|
|
|
log.Error("Error deleting package blob [%v]: %v", pb.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|