Skip to content

Instantly share code, notes, and snippets.

@ahonor
Created August 6, 2013 22:43
Show Gist options
  • Select an option

  • Save ahonor/6169482 to your computer and use it in GitHub Desktop.

Select an option

Save ahonor/6169482 to your computer and use it in GitHub Desktop.
import com.dtolabs.rundeck.plugins.notification.NotificationPlugin;
import com.dtolabs.rundeck.core.plugins.configuration.StringRenderingConstants;
import com.dtolabs.rundeck.core.plugins.configuration.ValidationException;
import com.fasterxml.jackson.databind.ObjectMapper;
// curl -H "Content-type: application/json" -X POST \
// -d '{
// "service_key": "ee59049e89dd45f28ce35467a08577cb",
// "event_type": "trigger",
// "description": "FAILURE for production/HTTP on machine srv01.acme.com",
// "details": {
// "ping time": "1500ms",
// "load avg": 0.75
// }
// }' \
// "https://events.pagerduty.com/generic/2010-04-15/create_event.json"
/**
* define the default subject line configuration
*/
def defaultSubjectLine='$STATUS [$PROJECT] $JOB run by $USER (#$ID)'
/**
* Expands the Subject string using a predefined set of tokens
*/
def subjectString={text,binding->
//defines the set of tokens usable in the subject configuration property
def tokens=[
'$STATUS': binding.execution.status.toUpperCase(),
'$status': binding.execution.status.toLowerCase(),
'$PROJECT': binding.execution.project,
'$JOB': binding.execution.job.name,
'$GROUP': binding.execution.job.group,
'$JOB_FULL': (binding.execution.job.group?binding.execution.job.group+'/':'')+binding.execution.job.name,
'$USER': binding.execution.user,
'$ID': binding.execution.id.toString()
]
text.replaceAll(/(\$\w+)/){
if(tokens[it[1]]){
tokens[it[1]]
}else{
it[0]
}
}
}
rundeckPlugin(NotificationPlugin){
title="PagerDuty Trigger"
description="Create a Trigger event."
configuration{
service_key title:"Service Key", description:"The service key", required:true
description title:"Description", description:"Notification description",defaultValue:defaultSubjectLine,required:true
}
onstart { Map executionData,Map config ->
true
}
onfailure { Map executionData ->
def json = new ObjectMapper()
def expandedSubject = subjectString(config.description, [execution:executionData])
System.err.println("DEBUG: expandedSubject="+expandedSubject)
def data =[event_type:'trigger',service_key:config.service_key,
description:expandedSubject,
details:[job: executionData.job.name,
group: executionData.job.group,
project: executionData.project,
user: executionData.user]
]
//config values are available as variables
System.err.println json.writeValueAsString(data)
true
}
onsuccess {
true
}
}
2013-08-06 15:36:41.563::INFO: Started [email protected]:4440
2013-08-06 15:38:21.623:/:INFO: Initializing Spring FrameworkServlet 'gsp'
2013-08-06 15:38:21.624:/:INFO: GSP servlet initialized
2013-08-06 15:38:23,813 ERROR ExecutionService - Execution failed: 121: [Workflow step failures: {2=Dispatch failed on 1 nodes: [Targa.local]}, Node failures: {Targa.local=[NonZeroResultCode: Result code was 1]}]
2013-08-06 15:38:23,996 ERROR NotificationService - Error sending notification: Notification{eventTrigger='onfailure', type='PagerDutyNotification', content='{"service_key":"ee59049e89dd45f28ce35467a08577cb","description":"$STATUS [$PROJECT] $JOB run by $USER (#$ID)"}'}: class java.lang.NullPointerException: Cannot get property 'description' on null object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment