Search code examples
apiamazon-s3groovytasknexus3

Nexus 3 Repository Manager Create (Or Run Pre-generated) Task Without Using User Interface


This question arose when I was trying to reboot my Nexus3 container on a weekly schedule and connect to an S3 bucket I have. I have my container set up to connect to the S3 bucket just fine (it creates a new [A-Z,0-9]-metrics.properties file each time) but the previous artifacts are not found when looking though the UI.

I used the Repair - Reconcile component database from blob store task from the UI settings and it works great!

But... all the previous steps are done automatically through scripts and I would like the same for the final step of Reconciling the blob store.

Connecting to the S3 blob store is done with reference to examples from nexus-book-examples. As below:

Map<String, String> config = new HashMap<>()
config.put("bucket", "nexus-artifact-storage")
blobStore.createS3BlobStore('nexus-artifact-storage', config)

AWS credentials are provided during the docker run step so the above is all that is needed for the blob store set up. It is called by a modified version of provision.sh, which is a script from the nexus-book-examples git page.

Is there a way to either:

  1. Create a task with a groovy script? or,
  2. Reference one of the task types and run the task that way with a POST?

Solution

  • depending on the specific version of repository manager that you are using, there may be REST endpoints for listing and running scheduled tasks. This was introduced in 3.6.0 according to this ticket: https://issues.sonatype.org/browse/NEXUS-11935. For more information about the REST integration in 3.x, check out the following: https://help.sonatype.com/display/NXRM3/Tasks+API

    For creating a scheduled task, you will have to add some groovy code. Perhaps the following would be a good start:

    import org.sonatype.nexus.scheduling.TaskConfiguration
    import org.sonatype.nexus.scheduling.TaskInfo
    import org.sonatype.nexus.scheduling.TaskScheduler
    
    import groovy.json.JsonOutput
    import groovy.json.JsonSlurper
    
    class TaskXO
    {
      String typeId
      Boolean enabled
      String name
      String alertEmail
      Map<String, String> properties
    }
    
    TaskXO task = new JsonSlurper().parseText(args)
    
    TaskScheduler scheduler = container.lookup(TaskScheduler.class.name)
    
    TaskConfiguration config = scheduler.createTaskConfigurationInstance(task.typeId)
    config.enabled = task.enabled
    config.name = task.name
    config.alertEmail = task.alertEmail
    task.properties?.each { key, value -> config.setString(key, value) }
    TaskInfo taskInfo = scheduler.scheduleTask(config, scheduler.scheduleFactory.manual())
    JsonOutput.toJson(taskInfo)