Praqma / artifactory-retention

Clone & Own solution for Artifactory Retention Policies
MIT License
8 stars 4 forks source link

trying to write my own retention policies #3

Closed niharkarra closed 5 years ago

niharkarra commented 5 years ago

Hi, I am trying to define my own retention policies.

My use case is: we have this 3 repos dev-releases, test-releases, and stage-releases for dev-releases, I want to delete everything older than 7 days but want to keep min 5 counts for test-releases, I want to delete everything older than 15 days but want to keep min 5 counts for test-releases, I want to delete everything older than 30 days but want to keep min 5 counts

Can you please help me in writing aql for this?

Appreciate your help in advance!

praqma-thi commented 5 years ago

Selecting items on age is pretty simple by filtering on updated or created:

items.find({
    "repo": { "$eq":"dev-releases" },
    "$and": [
        {"updated" : {"$before" : "7d"}}
    ]
})

You can also use offset(n) to skip the first n results, so if you want to keep 5 items, you'd end up with:

items.find({
    "repo": { "$eq":"dev-releases" },
    "$and": [
        {"updated" : {"$before" : "7d"}}
    ]
}).offset(5)

I'm not sure which 5 items you'll end up keeping, either the oldest or the most recent. Whichever the case, you can probably achieve that using sort with either asc or desc, depending on what you want to clean up:

items.find({
    "repo": { "$eq":"dev-releases" },
    "$and": [
        {"updated" : {"$before" : "7d"}}
    ]
}).sort({"$desc" : ["updated"]}).offset(5)

The docs are pretty helpful and offer some examples as well: https://www.jfrog.com/confluence/display/RTF/Artifactory+Query+Language

Considering the three repositories use very similar policies, you might want to consider setting up a template as mentioned in the Readme, so you only have to maintain a single AQL script.

Let me know if that helped out.

(Note that I just threw these examples together without running them, might contain a syntax error and might need a little tweak.)