Skip to content

Commit

Permalink
Refactor of jobs.xml code
Browse files Browse the repository at this point in the history
  • Loading branch information
gschueler committed Jan 29, 2011
2 parents 43351f0 + 03e3b54 commit ff9eca4
Show file tree
Hide file tree
Showing 15 changed files with 1,661 additions and 2,392 deletions.
Expand Up @@ -92,10 +92,12 @@ private static void addScriptDispatch(final IDispatchedScript dispatchdef, final
final Element ctx = job.addElement("context");
ctx.addElement("project").addText(dispatchdef.getFrameworkProject());
final InputStream stream = dispatchdef.getScriptAsStream();
final Element seq = job.addElement("sequence");
final Element cmd = seq.addElement("command");
if (null != dispatchdef.getScript() || null != stream) {

//full script
final Element script = job.addElement("script");
final Element script = cmd.addElement("script");
if(null!= dispatchdef.getScript()){
script.addCDATA(dispatchdef.getScript());
}else{
Expand All @@ -106,20 +108,20 @@ private static void addScriptDispatch(final IDispatchedScript dispatchdef, final
script.addCDATA(sw.toString());
}
if (null != dispatchdef.getArgs() && dispatchdef.getArgs().length > 0) {
final Element argstring = job.addElement("scriptargs");
final Element argstring = cmd.addElement("scriptargs");
argstring.addText(CLIUtils.generateArgline(null, dispatchdef.getArgs()));
}
} else if (null != dispatchdef.getServerScriptFilePath()) {
//server-local script filepath
final Element filepath = job.addElement("scriptfile");
final Element filepath = cmd.addElement("scriptfile");
filepath.addText(dispatchdef.getServerScriptFilePath());
if (null != dispatchdef.getArgs() && dispatchdef.getArgs().length>0) {
final Element argstring = job.addElement("scriptargs");
final Element argstring = cmd.addElement("scriptargs");
argstring.addText(CLIUtils.generateArgline(null, dispatchdef.getArgs()));
}
} else if (null != dispatchdef.getArgs() && dispatchdef.getArgs().length > 0) {
//shell command
final Element exec = job.addElement("exec");
final Element exec = cmd.addElement("exec");
exec.addText(CLIUtils.generateArgline(null, dispatchdef.getArgs()));
} else {
throw new IllegalArgumentException("Dispatched script did not specify a command, script or filepath");
Expand Down
Expand Up @@ -242,11 +242,11 @@ public String[] getArgs() {
assertNotNull("missing /joblist/job element", jobs);
assertEquals("wrong size for /joblist/job", 1, jobs.size());

assertNotNull(document.selectSingleNode("/joblist/job/exec"));
assertEquals("some shell command", document.selectSingleNode("/joblist/job/exec").getText());
assertNull(document.selectSingleNode("/joblist/job/script"));
assertNull(document.selectSingleNode("/joblist/job/scriptfile"));
assertNull(document.selectSingleNode("/joblist/job/scriptargs"));
assertNotNull(document.selectSingleNode("/joblist/job/sequence/command/exec"));
assertEquals("some shell command", document.selectSingleNode("/joblist/job/sequence/command/exec").getText());
assertNull(document.selectSingleNode("/joblist/job/sequence/command/script"));
assertNull(document.selectSingleNode("/joblist/job/sequence/command/scriptfile"));
assertNull(document.selectSingleNode("/joblist/job/sequence/command/scriptargs"));
}

{
Expand Down Expand Up @@ -279,14 +279,14 @@ public String[] getArgs() {
assertNotNull("missing /joblist/job element", jobs);
assertEquals("wrong size for /joblist/job", 1, jobs.size());

assertNull(document.selectSingleNode("/joblist/job/exec"));
assertNotNull(document.selectSingleNode("/joblist/job/script"));
assertNull(document.selectSingleNode("/joblist/job/sequence/command/exec"));
assertNotNull(document.selectSingleNode("/joblist/job/sequence/command/script"));
assertEquals("#!/bin/bash\n"
+ "\n"
+ "echo this is a test\n"
+ "uptime\n", document.selectSingleNode("/joblist/job/script").getText());
assertNull(document.selectSingleNode("/joblist/job/scriptfile"));
assertNull(document.selectSingleNode("/joblist/job/scriptargs"));
+ "uptime\n", document.selectSingleNode("/joblist/job/sequence/command/script").getText());
assertNull(document.selectSingleNode("/joblist/job/sequence/command/scriptfile"));
assertNull(document.selectSingleNode("/joblist/job/sequence/command/scriptargs"));
}
{
IDispatchedScript script = new testScript() {
Expand Down Expand Up @@ -317,14 +317,14 @@ public String[] getArgs() {
assertNotNull("missing /joblist/job element", jobs);
assertEquals("wrong size for /joblist/job", 1, jobs.size());

assertNull(document.selectSingleNode("/joblist/job/exec"));
assertNotNull(document.selectSingleNode("/joblist/job/script"));
assertNull(document.selectSingleNode("/joblist/job/sequence/command/exec"));
assertNotNull(document.selectSingleNode("/joblist/job/sequence/command/script"));
assertEquals("#!/bin/bash\n"
+ "\n"
+ "echo this is a test\n"
+ "uptime\n", document.selectSingleNode("/joblist/job/script").getText());
assertNull(document.selectSingleNode("/joblist/job/scriptfile"));
assertEquals("this is args", document.selectSingleNode("/joblist/job/scriptargs").getText());
+ "uptime\n", document.selectSingleNode("/joblist/job/sequence/command/script").getText());
assertNull(document.selectSingleNode("/joblist/job/sequence/command/scriptfile"));
assertEquals("this is args", document.selectSingleNode("/joblist/job/sequence/command/scriptargs").getText());
}
{
IDispatchedScript script = new testScript() {
Expand Down Expand Up @@ -352,11 +352,11 @@ public String[] getArgs() {
assertNotNull("missing /joblist/job element", jobs);
assertEquals("wrong size for /joblist/job", 1, jobs.size());

assertNull(document.selectSingleNode("/joblist/job/exec"));
assertNull(document.selectSingleNode("/joblist/job/script"));
assertNotNull(document.selectSingleNode("/joblist/job/scriptfile"));
assertEquals("/usr/local/scripts/test1.sh", document.selectSingleNode("/joblist/job/scriptfile").getText());
assertNull(document.selectSingleNode("/joblist/job/scriptargs"));
assertNull(document.selectSingleNode("/joblist/job/sequence/command/exec"));
assertNull(document.selectSingleNode("/joblist/job/sequence/command/script"));
assertNotNull(document.selectSingleNode("/joblist/job/sequence/command/scriptfile"));
assertEquals("/usr/local/scripts/test1.sh", document.selectSingleNode("/joblist/job/sequence/command/scriptfile").getText());
assertNull(document.selectSingleNode("/joblist/job/sequence/command/scriptargs"));
}
{
IDispatchedScript script = new testScript() {
Expand Down Expand Up @@ -385,12 +385,12 @@ public String[] getArgs() {
assertNotNull("missing /joblist/job element", jobs);
assertEquals("wrong size for /joblist/job", 1, jobs.size());

assertNull(document.selectSingleNode("/joblist/job/exec"));
assertNull(document.selectSingleNode("/joblist/job/script"));
assertNotNull(document.selectSingleNode("/joblist/job/scriptfile"));
assertEquals("/usr/local/scripts/test1.sh", document.selectSingleNode("/joblist/job/scriptfile").getText());
assertNotNull("expected not null scriptargs",document.selectSingleNode("/joblist/job/scriptargs"));
assertEquals("-this arg -is here", document.selectSingleNode("/joblist/job/scriptargs").getText());
assertNull(document.selectSingleNode("/joblist/job/sequence/command/exec"));
assertNull(document.selectSingleNode("/joblist/job/sequence/command/script"));
assertNotNull(document.selectSingleNode("/joblist/job/sequence/command/scriptfile"));
assertEquals("/usr/local/scripts/test1.sh", document.selectSingleNode("/joblist/job/sequence/command/scriptfile").getText());
assertNotNull("expected not null scriptargs",document.selectSingleNode("/joblist/job/sequence/command/scriptargs"));
assertEquals("-this arg -is here", document.selectSingleNode("/joblist/job/sequence/command/scriptargs").getText());
}
}

Expand Down
Expand Up @@ -1180,8 +1180,9 @@ class ScheduledExecutionController {
}
def results
try{
results= _parseJobsXMLFile(file);
results= file.getInputStream().decodeJobsXML()
}catch(Exception e){
flash.error=e.toString()
if(!params.xmlreq){
render(view:'upload')
return;
Expand Down Expand Up @@ -1214,7 +1215,7 @@ class ScheduledExecutionController {
def errmsg
def failed
try{
def result= _dovalidate(jobdata)
def result= _dovalidate(jobdata instanceof ScheduledExecution?jobdata.properties:jobdata)
scheduledExecution=result.scheduledExecution
failed=result.failed
if(failed){
Expand Down Expand Up @@ -1296,47 +1297,7 @@ class ScheduledExecutionController {
}

}
/**
* Parse an uploaded file and return the Collection of jobs data as parsed.
* @throws Exception if an error occurs, and sets the flash.error message
*/
def _parseJobsXMLFile={file->

def XmlSlurper parser = new XmlSlurper()
def doc
try{
doc = parser.parse(file.getInputStream())
}catch(Exception e){
flash.error="Unable to parse file: ${e}"
throw e
}
if(!doc){
final String errmsg = "XML Document could not be parsed."
flash.error=errmsg
throw new Exception(errmsg)
}

if(!doc.job){
final String errmsg = "Jobs XML Document was not valid: 'job' element not found."
flash.error=errmsg
throw new Exception(errmsg)
}
def jobset
try{
jobset = doc.decodeJobsXML()
}catch (JobXMLException e){
final String errmsg = "Jobs XML Document was not valid: ${e}"
flash.error=errmsg
throw new Exception(errmsg)
}
if(null==jobset){
final String errmsg = "Jobs XML Document was not valid"
flash.error=errmsg
throw new Exception(errmsg)
}

return jobset
}

/**
* execute the job defined via input parameters, but do not store it.
*/
Expand Down Expand Up @@ -1937,8 +1898,10 @@ class ScheduledExecutionController {
}
if('xml'==fileformat){
try{
jobset= _parseJobsXMLFile(file);
jobset= file.getInputStream().decodeJobsXML()
}catch(Exception e){
flash.error="Jobs XML Document was not valid: ${e}"
flash.message="Jobs XML Document was not valid: ${e}"
if(!params.xmlreq){
render(view:'upload')
return;
Expand Down
4 changes: 2 additions & 2 deletions rundeckapp/grails-app/domain/CommandExec.groovy
Expand Up @@ -90,7 +90,7 @@ public class CommandExec extends ExecutionContext implements IWorkflowCmdItem {
}else {
map.scriptfile=adhocFilepath
}
if(argString){
if(argString && !adhocRemoteString){
map.args=argString
}
return map
Expand All @@ -108,7 +108,7 @@ public class CommandExec extends ExecutionContext implements IWorkflowCmdItem {
ce.adhocExecution = true
ce.adhocFilepath=data.scriptfile
}
if(data.args){
if(data.args && !ce.adhocRemoteString){
ce.argString=data.args
}
return ce
Expand Down
13 changes: 13 additions & 0 deletions rundeckapp/grails-app/domain/Notification.groovy
Expand Up @@ -47,6 +47,19 @@ public class Notification {
content(nullable:true,blank:true)
}

public static Notification fromMap(String key,Map data){
Notification n = new Notification(eventTrigger:key)
n.type='email'
n.content=data.recipients
return n;
}
public Map toMap(){
if(type=='email'){
return [recipients:content]
}else{
return null
}
}


public String toString ( ) {
Expand Down
10 changes: 3 additions & 7 deletions rundeckapp/grails-app/domain/Option.groovy
Expand Up @@ -93,12 +93,8 @@ public class Option implements Comparable{
public static Option fromMap(String name,Map data){
Option opt = new Option()
opt.name=name
if(data.enforced){
opt.enforced=true
}
if(data.required){
opt.required=true
}
opt.enforced=data.enforced?true:false
opt.required=data.required?true:false
if(data.description){
opt.description=data.description
}
Expand All @@ -112,7 +108,7 @@ public class Option implements Comparable{
opt.regex=data.regex
}
if(data.values){
opt.values=new TreeSet(data.values)
opt.values=data.values instanceof Collection?new TreeSet(data.values):new TreeSet([data.values])
}
return opt
}
Expand Down

0 comments on commit ff9eca4

Please sign in to comment.