5

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?

1 Answers1

3

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)
Wes W
  • 133
  • 6
  • Looks good but I need to figure out the `properties` of a Reconcile Components task to pass that as part of `args`. I've been pretty busy but if this works it is the closest (and only) answer I've gotten. – Taylor McRae Jan 15 '19 at 14:23
  • 1
    I finally had a chance to test this out and it worked! Thank you so much! My only issue for a bit was figuring out which properties had to be included for certain tasks. For my situtaion I just used `TaskInfo taskReconcile = scheduler.getTaskById("") log.info(taskInfo.toString())` And it gave me the properties needed for creating the Reconcile tag. – Taylor McRae Feb 21 '19 at 15:29
  • Is there any way to pass blob store for this task scheduler? – Jessica Jan 15 '23 at 15:06
  • I tried this code but it didn't set blobstore for my task scheduler – Jessica Jan 25 '23 at 14:09