-
-
Save emexelem/bcf6b504d81ea9019ad4ab2369006e66 to your computer and use it in GitHub Desktop.
import org.sonatype.nexus.repository.storage.StorageFacet; | |
import org.sonatype.nexus.repository.storage.Query; | |
import org.joda.time.DateTime; | |
import org.joda.time.format.DateTimeFormat; | |
def fmt = DateTimeFormat.forPattern('yyyy-MM-dd HH:mm:ss'); | |
// Get a repository | |
def repo = repository.repositoryManager.get('nuget-releases'); | |
// Get a database transaction | |
def tx = repo.facet(StorageFacet).txSupplier().get(); | |
// Begin the transaction | |
tx.begin(); | |
// Search assets that havn't been downloaded for more than three months | |
tx.findAssets(Query.builder().where('last_downloaded <').param(DateTime.now().minusMonths(3).toString(fmt)).build(), [repo]).each { asset -> | |
def component = tx.findComponent(asset.componentId()); | |
// Check if there is newer components of the same name | |
def count = tx.countComponents(Query.builder().where('name').eq(component.name()).and('version >').param(component.version()).build(), [repo]); | |
if (count > 0) { | |
log.info("Delete asset ${asset.name()} as it has not been downloaded since 3 months and has a newer version") | |
tx.deleteAsset(asset); | |
tx.deleteComponent(component); | |
} | |
} | |
// End the transaction | |
tx.commit(); |
For a format like NuGet, you might be able to just delete the component (IIRC the Asset will get deleted when you delete the Component). If you want more live feedback, pop in to: https://gitter.im/sonatype/nexus-developers
@DarthHater sorry for the late reply, no problem to share this
It doesn't work on Nexus 3.5.1 or I did something wrong:
Stack-trace:
javax.script.ScriptException: javax.script.ScriptException: java.lang.IllegalArgumentException: Nested DB TX!
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:158)
at org.sonatype.nexus.internal.script.ScriptServiceImpl.eval(ScriptServiceImpl.java:153)
at org.sonatype.nexus.internal.script.ScriptServiceImpl.eval(ScriptServiceImpl.java:162)
at org.sonatype.nexus.internal.script.ScriptTask.execute(ScriptTask.java:78)
at org.sonatype.nexus.scheduling.TaskSupport.call(TaskSupport.java:92)
at org.sonatype.nexus.quartz.internal.task.QuartzTaskJob.doExecute(QuartzTaskJob.java:145)
at org.sonatype.nexus.quartz.internal.task.QuartzTaskJob.execute(QuartzTaskJob.java:108)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.sonatype.nexus.thread.internal.MDCAwareRunnable.run(MDCAwareRunnable.java:40)
at org.apache.shiro.subject.support.SubjectRunnable.doRun(SubjectRunnable.java:120)
at org.apache.shiro.subject.support.SubjectRunnable.run(SubjectRunnable.java:108)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: javax.script.ScriptException: java.lang.IllegalArgumentException: Nested DB TX!
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:155)
... 15 more
Caused by: java.lang.IllegalArgumentException: Nested DB TX!
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:122)
at org.sonatype.nexus.repository.storage.StorageTxImpl.<init>(StorageTxImpl.java:146)
at org.sonatype.nexus.repository.storage.StorageFacetImpl.openStorageTx(StorageFacetImpl.java:247)
at org.sonatype.nexus.repository.storage.StorageFacetImpl.lambda$0(StorageFacetImpl.java:145)
at com.google.common.base.Supplier$get.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at Script3.run(Script3.groovy:10)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317)
... 16 more```
The tx.findAssets block needs to be enclosed in a try / catch block and use tx.rollback() in the catch to release the DB lock, otherwise you end up in situations where you cannot run any script.
Thanks olive42 for tip, I had to do a lot more.
It might be that my repo was broken or corrupted because I had components without assets and similar bugs. This caused a lot of errors.
I changed this script a little - my version is here: https://gist.github.com/tgagor/f904221d888c8daaad679cf99c407aef
Interestingly, I ran a version of this script on my Nexus 3 instance. It worked in that all the old assets seem to be gone. It deleted about 650 artifacts. They no longer show up in the UI. However, the disk usage remains exactly the same on the host where nexus 3 is running. So it seems like the artifacts are gone from the database, but they don't seem to be gone from the disk. Wondering if anyone has had that issue? Does nexus lazily go garbage collect or something?
For those wondering, you need to run a Compact blob store task to actually free up the local storage.
Cool! Mind if I share this on the Nexus Exchange?