-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from criteo-cookbooks/task-management
Task management resource
- Loading branch information
Showing
10 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import org.sonatype.nexus.scheduling.TaskConfiguration; | ||
import org.sonatype.nexus.scheduling.TaskInfo; | ||
import org.sonatype.nexus.scheduling.TaskScheduler; | ||
|
||
TaskScheduler taskScheduler = container.lookup(TaskScheduler.class.getName()); | ||
TaskInfo existingTask = taskScheduler.listsTasks().find { TaskInfo taskInfo -> | ||
taskInfo.getName() == args; | ||
} | ||
|
||
if (existingTask && !existingTask.remove()) { | ||
throw new RuntimeException("Could not remove currently running task: " + args); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import org.sonatype.nexus.scheduling.TaskConfiguration; | ||
import org.sonatype.nexus.scheduling.TaskInfo; | ||
import org.sonatype.nexus.scheduling.TaskScheduler; | ||
|
||
import groovy.json.JsonOutput; | ||
|
||
TaskScheduler taskScheduler = container.lookup(TaskScheduler.class.getName()); | ||
|
||
TaskInfo existingTask = taskScheduler.listsTasks().find { TaskInfo taskInfo -> | ||
taskInfo.getName() == args; | ||
} | ||
|
||
if (existingTask) { | ||
return JsonOutput.toJson(existingTask.getConfiguration().asMap()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Freely adapted from | ||
// https://github.com/savoirfairelinux/ansible-nexus3-oss/blob/master/files/groovy/create_task.groovy | ||
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; | ||
|
||
import groovy.json.JsonSlurper; | ||
|
||
def params = new JsonSlurper().parseText(args); | ||
|
||
TaskScheduler taskScheduler = container.lookup(TaskScheduler.class.getName()); | ||
TaskInfo existingTask = taskScheduler.listsTasks().find { TaskInfo taskInfo -> | ||
taskInfo.getName() == params.name; | ||
} | ||
if (existingTask && !existingTask.remove()) { | ||
throw new RuntimeException("Could not remove currently running task: " + params.name); | ||
} | ||
|
||
TaskConfiguration taskConfiguration = taskScheduler.createTaskConfigurationInstance('script'); | ||
taskConfiguration.setName(params.name); | ||
taskConfiguration.setString('source', params.source); | ||
Schedule schedule = taskScheduler.scheduleFactory.cron(new Date(), params.crontab); | ||
|
||
taskScheduler.scheduleTask(taskConfiguration, schedule); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
property :task_name, String, name_property: true | ||
property :task_source, String, default: ''.freeze | ||
property :task_crontab, String, default: '0 1 * * * ?'.freeze | ||
property :api_endpoint, String, identity: true, default: lazy { node['nexus3']['api']['endpoint'] } | ||
property :api_username, String, identity: true, default: lazy { node['nexus3']['api']['username'] } | ||
property :api_password, String, identity: true, sensitive: true, default: lazy { node['nexus3']['api']['password'] } | ||
|
||
load_current_value do |desired| | ||
begin | ||
res = ::Nexus3::Api.new(api_endpoint, api_username, api_password).run_script('get_task', desired.task_name) | ||
config = JSON.parse(res) | ||
current_value_does_not_exist! if config.nil? | ||
::Chef::Log.debug "Config is: #{config}" | ||
task_source config['source'] | ||
# We rescue here because during the first run, the task will not exist yet, so we let Chef know that | ||
# the resource has to be created. | ||
rescue LoadError, ::Nexus3::ApiError => e | ||
::Chef::Log.warn "A '#{e.class}' occured: #{e.message}" | ||
current_value_does_not_exist! | ||
end | ||
end | ||
|
||
action :create do | ||
init | ||
|
||
converge_if_changed do | ||
nexus3_api "upsert_task #{new_resource.task_name}" do | ||
script_name 'upsert_task' | ||
args name: new_resource.task_name, | ||
source: new_resource.task_source, | ||
crontab: new_resource.task_crontab | ||
|
||
action %i(create run) | ||
endpoint new_resource.api_endpoint | ||
username new_resource.api_username | ||
password new_resource.api_password | ||
|
||
content ::Nexus3::Scripts.groovy_content('upsert_task', node) | ||
end | ||
end | ||
end | ||
|
||
action :delete do | ||
init | ||
|
||
converge_if_changed do | ||
nexus3_api "delete_task #{new_resource.task_name}" do | ||
action %i(create run) | ||
script_name 'delete_task' | ||
args new_resource.task_name | ||
|
||
content ::Nexus3::Scripts.groovy_content('delete_task', node) | ||
|
||
endpoint new_resource.api_endpoint | ||
username new_resource.api_username | ||
password new_resource.api_password | ||
|
||
not_if { current_resource.nil? } | ||
end | ||
end | ||
end | ||
|
||
action_class do | ||
def init | ||
chef_gem 'httpclient' do | ||
compile_time true | ||
end | ||
|
||
nexus3_api "get_task #{new_resource.task_name}" do | ||
action :create | ||
script_name 'get_task' | ||
args new_resource.task_name | ||
endpoint new_resource.api_endpoint | ||
username new_resource.api_username | ||
password new_resource.api_password | ||
|
||
content ::Nexus3::Scripts.groovy_content('get_task', node) | ||
end | ||
end | ||
|
||
def whyrun_supported? | ||
true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'spec_helper' | ||
|
||
describe 'nexus3_test::tasks' do | ||
context 'linux' do | ||
let(:chef_run) do | ||
ChefSpec::SoloRunner.new(platform: 'centos', version: CENTOS_VERSION, | ||
file_cache_path: CACHE).converge(described_recipe) | ||
end | ||
|
||
it 'creates a task' do | ||
expect(chef_run).to create_nexus3_task('foo') | ||
end | ||
|
||
it 'deletes a task' do | ||
expect(chef_run).to create_nexus3_task('bar') | ||
expect(chef_run).to delete_nexus3_task('bar') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
nexus3_task 'foo' | ||
|
||
nexus3_task 'foo again' do | ||
task_name 'foo' | ||
notifies :run, 'ruby_block[fail if foo is created again]', :immediately | ||
end | ||
|
||
ruby_block 'fail if foo is created again' do | ||
action :nothing | ||
block { raise 'nexus3_task is not idempotent!' } | ||
end | ||
|
||
nexus3_task 'bar' do | ||
action %i(create delete) | ||
end | ||
|
||
nexus3_task 'bar again' do | ||
task_name 'bar' | ||
action :delete | ||
notifies :run, 'ruby_block[fail if bar is deleted again]', :immediately | ||
end | ||
|
||
ruby_block 'fail if bar is deleted again' do | ||
action :nothing | ||
block { raise 'nexus3_task is not idempotent!' } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require 'serverspec_helper' | ||
|
||
describe 'nexus::tasks' do | ||
describe command('curl -uadmin:admin123 http://localhost:8081/service/siesta/rest/v1/script/get_task/run -X POST ' \ | ||
'-H "Content-Type: text/plain" -d foo') do | ||
its(:exit_status) { should eq 0 } | ||
its(:stdout) { should match(/result.*name.*foo/) } | ||
end | ||
end |