-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcleanup.groovy
149 lines (129 loc) · 6.21 KB
/
cleanup.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//import org.sonatype.nexus.common.app.GlobalComponentLookupHelper
import org.sonatype.nexus.repository.maintenance.MaintenanceService
//import org.sonatype.nexus.repository.storage.ComponentMaintenance
import org.sonatype.nexus.repository.storage.Query
import org.sonatype.nexus.repository.storage.StorageFacet
import org.sonatype.nexus.repository.Repository
//import org.sonatype.nexus.script.plugin.RepositoryApi
//import org.sonatype.nexus.script.plugin.internal.provisioning.RepositoryApiImpl
import com.google.common.collect.ImmutableList
import org.joda.time.DateTime
import java.util.regex.Pattern
//import org.slf4j.Logger
// ----------------------------------------------------
// delete these rows when this script is added to nexus
/* RepositoryApiImpl repository = null;
Logger log = null;
GlobalComponentLookupHelper container = null; */
// ----------------------------------------------------
log.info(":::Cleanup script started!")
repository.repositoryManager.browse().each { Repository myRepo ->
def retentionDays = 5
def retentionCount = 3
def pattern = ~/^([a-zA-Z]+)_/
def patternSemver = ~/^\d+\.\d+\.\d+-([a-zA-Z0-9-]+)/
def whitelist = ["org.javaee7.sample/javaee7-simple-sample", "org.javaee7.next/javaee7-another-sample"].toArray()
//log.info("Repository: $myRepo");
def repositoryName = myRepo.name
if (myRepo.getFormat().toString() != 'docker') return
log.info("*** Proceeding with repository: $repositoryName")
def alterRetention = [:]
alterRetention['etr_s7middleware_docker'] = [retentionCount: 30]
//alterRetention['etr_ncenter_docker'] = [pattern: ~/^\d+\.\d+\.\d+-([a-zA-Z0-9-]+)/]
//alterRetention['etr_s7common_docker'] = [retentionCount: 30]
if(alterRetention.containsKey(repositoryName)){
def alter = alterRetention[repositoryName] as Map
log.info("Altering retention params: ${alter}")
if(alter.containsKey('retentionDays')){
retentionDays = alter['retentionDays'] as Integer
}
if(alter.containsKey('retentionCount')){
retentionCount = alter['retentionCount'] as Integer
}
if(alter.containsKey('pattern')){
pattern = alter['pattern'] as Pattern
}
}
MaintenanceService service = container.lookup("org.sonatype.nexus.repository.maintenance.MaintenanceService") as MaintenanceService
def repo = repository.repositoryManager.get(repositoryName)
def tx = repo.facet(StorageFacet.class).txSupplier().get()
def components = null
try {
tx.begin()
//components = ImmutableList.copyOf(tx.browseComponents(tx.findBucket(repo)))
components = ImmutableList.copyOf(tx.findComponents(Query.builder().suffix(' ORDER BY name ASC, last_updated ASC').build(), [repo]))
}catch(Exception e){
log.info("Error: "+e)
}finally{
if(tx!=null)
tx.close()
}
if(components != null && components.size() > 0) {
//log.info("${components}");
//noinspection GrDeprecatedAPIUsage
def retentionDate = DateTime.now().minusDays(retentionDays).dayOfMonth().roundFloorCopy()
int deletedComponentCount = 0
int compCount = 0
def listOfComponents = components
def tagCount = [:]
def previousComp = listOfComponents.head().group() + listOfComponents.head().name()
listOfComponents.reverseEach{comp ->
//log.info("Processing Component - group: ${comp.group()}, ${comp.name()}, version: ${comp.version()}")
if(!whitelist.contains(comp.group()+"/"+comp.name())){
//log.info("group: ${comp.group()}, ${comp.name()}, version: ${comp.version()}")
//log.info("previous: ${previousComp}");
if(previousComp != (comp.group() + comp.name())) {
compCount = 0
tagCount = [:]
previousComp = comp.group() + comp.name()
log.info("group: ${comp.group()}, ${comp.name()}")
}
if(comp.version() == 'latest'){
log.info(" version: ${comp.version()}, skipping")
return
}
def prefix = null
def matcher = comp.version() =~ pattern
if(matcher) {
prefix = matcher.group(1)
} else{
matcher = comp.version() =~ patternSemver
if(matcher) {
prefix = matcher.group(1)
}
}
def actualCount
if(prefix != null) {
if(tagCount[prefix] == null) {
tagCount[prefix] = 0
}
tagCount[prefix]++
actualCount = tagCount[prefix]
log.info(" version: ${comp.version()}, prefix: ${prefix}")
} else {
compCount++
actualCount = compCount
log.info(" version: ${comp.version()}")
}
log.info(" CompCount: ${actualCount}, ReteCount: ${retentionCount}")
if(actualCount > retentionCount) {
//log.info(" CompDate: ${comp.lastUpdated()} RetDate: ${retentionDate}")
if (comp.lastUpdated().isBefore(retentionDate)) {
//log.info("compDate after retentionDate: ${comp.lastUpdated()} isAfter ${retentionDate}")
//log.info("deleting ${comp.group()}, ${comp.name()}, version: ${comp.version()}")
log.info(" !!!Deleting")
// ------------------------------------------------
// uncomment to delete components and their assets
//service.deleteComponent(repo, comp)
// ------------------------------------------------
//log.info("component deleted");
deletedComponentCount++
}
}
}else{
log.info("Component skipped: ${comp.group()} ${comp.name()}")
}
}
log.info("Deleted Component count: ${deletedComponentCount}")
}
}