Skip to content

Instantly share code, notes, and snippets.

@gschueler
Created April 19, 2013 17:46
Show Gist options
  • Save gschueler/5421936 to your computer and use it in GitHub Desktop.
Save gschueler/5421936 to your computer and use it in GitHub Desktop.
import com.dtolabs.rundeck.plugins.notification.NotificationPlugin;
import org.springframework.mail.javamail.*;
import javax.mail.internet.*;
import groovy.xml.MarkupBuilder
import groovy.text.SimpleTemplateEngine
//mail sender configuration
def mailProperties=[
host: "localhost",
port: 25,
defaultEncoding:"utf-8"
]
def engine = new SimpleTemplateEngine()
def evalString={text,binding->
engine.createTemplate(text).make(binding).toString()
}
//sends mail by building the message from the closure argument
def sendMail={Closure callable->
def sender = new JavaMailSenderImpl()
mailProperties.each{k,v->
sender[k]=v
}
def mbuilder = new org.grails.mail.MailMessageBuilder(null,sender)
callable.delegate = mbuilder
callable.resolveStrategy = Closure.DELEGATE_FIRST
callable.call()
def message = mbuilder.createMessage()
message.sentDate = new Date()
if(message instanceof MimeMailMessage) {
MimeMailMessage msg = message
MimeMessage mimeMsg = msg.getMimeMessage()
sender.send(mimeMsg)
}
}
//generates html string using a markup builder
def buildHtml={ Closure clos->
def sw=new StringWriter()
def mk=new MarkupBuilder(sw)
clos.delegate=mk
clos.resolveStrategy=Closure.DELEGATE_FIRST
clos.call()
sw.toString()
}
//generate the mail content for the given data
def generateMail={subject, Map execution, Map config->
buildHtml{
//customize this content with your own HTML
html{
body{
h1(subject)
ul{
li{
p{
if(execution.status=='running'){
em("Started")
yield(" by: ${execution.user} at ${execution.dateStarted}")
}else if(execution.status=='succeeded'){
em("Finished")
yield(" at: ${execution.dateEnded}")
}else if(execution.abortedBy){
em("KILLED")
yield(" by ${execution.abortedBy} at: ${execution.dateEnded}")
}else{
//failed
em("Failed")
yield(" at: ${execution.dateEnded}")
div{
yield("Failed node list:")
ul{
execution.failedNodeList.each{
li(it)
}
}
}
}
}
}
li{
a(href:execution.href,"Output for ${execution.id}")
}
// div{
// yield("execution data: ${execution}")
// }
}
}
}
}
}
//define the NotificationPlugin
rundeckPlugin(NotificationPlugin){
title="Mail Test"
description="Sends Mail"
configuration{
recipients(title:"Email recipients",required:true, description: "Enter comma-separated email addresses"){
it.split(",").every { obj ->
org.apache.commons.validator.EmailValidator.getInstance().isValid(obj)
// obj==~/^.+@.+\.$/
}
}
subject(title:"Subject line",defaultValue:'${execution.status.toUpperCase()} [${execution.project}] ${execution.job.name}', required:true)
}
def handleTrigger= { String trigger, Map execution, Map config->
def subjectStr=evalString(config.subject,[execution:execution,trigger:trigger])
try{
sendMail{
to( config.recipients.split(",") as List)
subject subjectStr
html( generateMail(subjectStr,execution,config))
}
return true
}catch(Exception e){
System.err.println("Error sending notification email: "+e.getMessage());
}
false
}
//define triggers
onsuccess(handleTrigger.curry('success'))
onfailure(handleTrigger.curry('failure'))
onstart(handleTrigger.curry('start'))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment