Skip to content

Commit

Permalink
Added role vars + groovy script to manage scheduled tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
samherve committed Oct 28, 2016
1 parent 08b43c3 commit e425a43
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ Delete the default blobstore from the nexus install initial default configuratio

[Blobstores](https://books.sonatype.com/nexus-book/3.0/reference/admin.html#admin-repository-blobstores) to create. A blobstore path and a repository blobstore cannot be updated after initial creation (any update here will be ignored on re-provisionning).

nexus_scheduled_tasks: []
# example task to compact blobstore :
# - name: compact-blobstore
# cron: '0 0 22 * * ?'
# typeId: blobstore.compact
# taskProperties:
# blobstoreName: 'default' # all task attributes are stored as strings by nexus internally

[Scheduled tasks](https://books.sonatype.com/nexus-book/reference3/admin.html#admin-system-tasks) to setup. `typeId` and task-specific `taskProperties` can be guessed either from the java type hierarchy of `org.sonatype.nexus.scheduling.TaskDescriptorSupport` or from peeking at the browser AJAX requests while manually configuring a task.

nexus_repos_maven_proxy:
- name: central
remote_url: 'https://repo1.maven.org/maven2/'
Expand Down Expand Up @@ -257,6 +267,12 @@ The java and httpd requirements /can/ be fulfilled with the following galaxy rol
nexus_blobstores:
- name: company-artifacts
path: /var/nexus/blobs/company-artifacts
nexus_scheduled_tasks:
- name: compact-blobstore
cron: '0 0 22 * * ?'
typeId: blobstore.compact
taskProperties:
blobstoreName: 'company-artifacts'
nexus_repos_maven_proxy:
- name: central
remote_url: 'https://repo1.maven.org/maven2/'
Expand Down
8 changes: 8 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ nexus_repos_maven_group:
- central
- jboss

nexus_scheduled_tasks: []
# example task to compact blobstore :
# - name: compact-blobstore
# cron: '0 0 22 * * ?'
# typeId: blobstore.compact
# taskProperties:
# blobstoreName: 'default' # all task attributes are stored as strings by nexus internally

_nexus_privilege_defaults:
type: repository-view
format: maven2
Expand Down
5 changes: 5 additions & 0 deletions tasks/create_task_each.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- include: call_script.yml
vars:
script_name: create_task
args: "{{ item }}"
3 changes: 3 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@
headerHtml: "{{ nexus_branding_header }}"
footerEnabled: "{{ nexus_branding_footer != '' }}"
headerEnabled: "{{ nexus_branding_header != '' }}"

- include: create_task_each.yml
with_items: "{{ nexus_scheduled_tasks }}"
3 changes: 2 additions & 1 deletion tasks/nexus_install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,5 @@
- create_blobstore
- create_repo_maven_proxy
- create_repo_maven_group
- create_repo_maven_hosted
- create_repo_maven_hosted
- create_task
26 changes: 26 additions & 0 deletions templates/groovy/create_task.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import groovy.json.JsonSlurper
import org.sonatype.nexus.scheduling.TaskConfiguration
import org.sonatype.nexus.scheduling.TaskInfo
import org.sonatype.nexus.scheduling.TaskScheduler
import org.sonatype.nexus.scheduling.schedule.Schedule

parsed_args = new JsonSlurper().parseText(args)

TaskScheduler taskScheduler = container.lookup(TaskScheduler.class.getName())

TaskInfo existingTask = taskScheduler.listsTasks().find { TaskInfo taskInfo ->
taskInfo.name == parsed_args.name
}

if (existingTask && !existingTask.remove()) {
throw new RuntimeException("Could not remove currently running task : " + parsed_args.name)
}

TaskConfiguration taskConfiguration = taskScheduler.createTaskConfigurationInstance(parsed_args.typeId)
taskConfiguration.setName(parsed_args.name)

parsed_args.taskProperties.each { key, value -> taskConfiguration.setString(key, value) }

Schedule schedule = taskScheduler.scheduleFactory.cron(new Date(), parsed_args.cron)

taskScheduler.scheduleTask(taskConfiguration, schedule)

0 comments on commit e425a43

Please sign in to comment.