mirror of
https://github.com/Quiq/docker-registry-ui.git
synced 2025-07-17 15:51:27 +00:00
Implement purge_tags_keep_from_file option. Refactoring.
This commit is contained in:
parent
6d0fc3d913
commit
b7c5c52fa2
15
config.go
15
config.go
@ -7,7 +7,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/quiq/docker-registry-ui/registry"
|
"github.com/quiq/docker-registry-ui/registry"
|
||||||
"github.com/tidwall/gjson"
|
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -31,7 +30,7 @@ type configData struct {
|
|||||||
PurgeTagsKeepDays int `yaml:"purge_tags_keep_days"`
|
PurgeTagsKeepDays int `yaml:"purge_tags_keep_days"`
|
||||||
PurgeTagsKeepCount int `yaml:"purge_tags_keep_count"`
|
PurgeTagsKeepCount int `yaml:"purge_tags_keep_count"`
|
||||||
PurgeTagsKeepRegexp string `yaml:"purge_tags_keep_regexp"`
|
PurgeTagsKeepRegexp string `yaml:"purge_tags_keep_regexp"`
|
||||||
PurgeTagsKeepFile string `yaml:"purge_tags_keep_from_file"`
|
PurgeTagsKeepFromFile string `yaml:"purge_tags_keep_from_file"`
|
||||||
PurgeTagsSchedule string `yaml:"purge_tags_schedule"`
|
PurgeTagsSchedule string `yaml:"purge_tags_schedule"`
|
||||||
|
|
||||||
PurgeConfig *registry.PurgeTagsConfig
|
PurgeConfig *registry.PurgeTagsConfig
|
||||||
@ -77,17 +76,7 @@ func readConfig(configFile string) *configData {
|
|||||||
KeepDays: config.PurgeTagsKeepDays,
|
KeepDays: config.PurgeTagsKeepDays,
|
||||||
KeepMinCount: config.PurgeTagsKeepCount,
|
KeepMinCount: config.PurgeTagsKeepCount,
|
||||||
KeepTagRegexp: config.PurgeTagsKeepRegexp,
|
KeepTagRegexp: config.PurgeTagsKeepRegexp,
|
||||||
|
KeepFromFile: config.PurgeTagsKeepFromFile,
|
||||||
}
|
}
|
||||||
if config.PurgeTagsKeepFile != "" {
|
|
||||||
if _, err := os.Stat(config.PurgeTagsKeepFile); os.IsNotExist(err) {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
data, err := ioutil.ReadFile(config.PurgeTagsKeepFile)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
config.PurgeConfig.KeepTagsFromFile = gjson.ParseBytes(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &config
|
return &config
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,9 @@ package registry
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"math"
|
"math"
|
||||||
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
@ -15,7 +17,7 @@ type PurgeTagsConfig struct {
|
|||||||
KeepDays int
|
KeepDays int
|
||||||
KeepMinCount int
|
KeepMinCount int
|
||||||
KeepTagRegexp string
|
KeepTagRegexp string
|
||||||
KeepTagsFromFile gjson.Result
|
KeepFromFile string
|
||||||
}
|
}
|
||||||
|
|
||||||
type tagData struct {
|
type tagData struct {
|
||||||
@ -48,6 +50,23 @@ func (p timeSlice) Swap(i, j int) {
|
|||||||
// PurgeOldTags purge old tags.
|
// PurgeOldTags purge old tags.
|
||||||
func PurgeOldTags(client *Client, config *PurgeTagsConfig) {
|
func PurgeOldTags(client *Client, config *PurgeTagsConfig) {
|
||||||
logger := SetupLogging("registry.tasks.PurgeOldTags")
|
logger := SetupLogging("registry.tasks.PurgeOldTags")
|
||||||
|
|
||||||
|
var keepTagsFromFile gjson.Result
|
||||||
|
if config.KeepFromFile != "" {
|
||||||
|
if _, err := os.Stat(config.KeepFromFile); os.IsNotExist(err) {
|
||||||
|
logger.Warnf("Cannot open %s: %s", config.KeepFromFile, err)
|
||||||
|
logger.Error("Not purging anything!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data, err := ioutil.ReadFile(config.KeepFromFile)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warnf("Cannot read %s: %s", config.KeepFromFile, err)
|
||||||
|
logger.Error("Not purging anything!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
keepTagsFromFile = gjson.ParseBytes(data)
|
||||||
|
}
|
||||||
|
|
||||||
dryRunText := ""
|
dryRunText := ""
|
||||||
if config.DryRun {
|
if config.DryRun {
|
||||||
logger.Warn("Dry-run mode enabled.")
|
logger.Warn("Dry-run mode enabled.")
|
||||||
@ -88,8 +107,8 @@ func PurgeOldTags(client *Client, config *PurgeTagsConfig) {
|
|||||||
if config.KeepTagRegexp != "" {
|
if config.KeepTagRegexp != "" {
|
||||||
logger.Infof("Keeping tags matching regexp: %s", config.KeepTagRegexp)
|
logger.Infof("Keeping tags matching regexp: %s", config.KeepTagRegexp)
|
||||||
}
|
}
|
||||||
if config.KeepTagsFromFile.IsObject() {
|
if config.KeepFromFile != "" {
|
||||||
logger.Infof("Keeping tags for repos from the file: %+v", config.KeepTagsFromFile)
|
logger.Infof("Keeping tags for repos from the file: %+v", keepTagsFromFile)
|
||||||
}
|
}
|
||||||
purgeTags := map[string][]string{}
|
purgeTags := map[string][]string{}
|
||||||
keepTags := map[string][]string{}
|
keepTags := map[string][]string{}
|
||||||
@ -100,7 +119,7 @@ func PurgeOldTags(client *Client, config *PurgeTagsConfig) {
|
|||||||
|
|
||||||
// Prep the list of tags to preserve if defined in the file
|
// Prep the list of tags to preserve if defined in the file
|
||||||
tagsFromFile := []string{}
|
tagsFromFile := []string{}
|
||||||
for _, i := range config.KeepTagsFromFile.Get(repo).Array() {
|
for _, i := range keepTagsFromFile.Get(repo).Array() {
|
||||||
tagsFromFile = append(tagsFromFile, i.String())
|
tagsFromFile = append(tagsFromFile, i.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user