Added create-job CLI command.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@20337 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
kohsuke committed 2009-07-31 17:22:52 +00:00
1 parent 8299a33dba
commit 7fffedbc99
2 files changed
+66 -29

No files matched your search

@@ -0,0 +1,32 @@
package hudson.cli;
import hudson.model.Hudson;
import org.kohsuke.args4j.Argument;
/**
* Creates a new job by reading stdin as a configuration XML file.
*
* @author Kohsuke Kawaguchi
*/
public class CreateJobCommand extends CLICommand {
@Override
public String getShortDescription() {
return "Creates a new job by reading stdin as a configuration XML file";
}
@Argument(metaVar="NAME",usage="Name of the job to create")
public String name;
protected int run() throws Exception {
Hudson h = Hudson.getInstance();
if (h.getItem(name)!=null) {
stderr.println("Job '"+name+"' already exists");
return -1;
}
h.createProjectFromXML(name,stdin);
return 0;
}
}
+34 -29
View File
@@ -194,6 +194,7 @@ import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.nio.charset.Charset;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.crypto.SecretKey;
import groovy.lang.GroovyShell;
@@ -2435,27 +2436,9 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl
result = copy(src,name);
} else {
if(isXmlSubmission) {
// config.xml submission
// first copy it as config.xml
File configXml = Items.getConfigFile(getRootDirFor(name)).getFile();
configXml.getParentFile().mkdirs();
try {
FileOutputStream fos = new FileOutputStream(configXml);
try {
Util.copyStream(req.getInputStream(),fos);
} finally {
fos.close();
}
// load it
result = (TopLevelItem)Items.load(this,configXml.getParentFile());
items.put(name,result);
} catch (IOException e) {
// if anything fails, delete the config file to avoid further confusion
Util.deleteRecursive(configXml.getParentFile());
throw e;
}
result = createProjectFromXML(name, req.getInputStream());
rsp.setStatus(HttpServletResponse.SC_OK);
return result;
} else {
// create empty job and redirect to the project config screen
if(mode==null) {
@@ -2469,17 +2452,39 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl
l.onCreated(result);
}
if(isXmlSubmission) {
// it worked
rsp.setStatus(HttpServletResponse.SC_OK);
} else {
// send the browser to the config page
rsp.sendRedirect2(result.getUrl()+"configure");
}
// send the browser to the config page
rsp.sendRedirect2(result.getUrl()+"configure");
return result;
}
/**
* Creates a new job from its configuration XML. The type of the job created will be determined by
* what's in this XML.
* @since 1.319
*/
public TopLevelItem createProjectFromXML(String name, InputStream xml) throws IOException {
// place it as config.xml
File configXml = Items.getConfigFile(getRootDirFor(name)).getFile();
configXml.getParentFile().mkdirs();
try {
FileOutputStream fos = new FileOutputStream(configXml);
try {
Util.copyStream(xml,fos);
} finally {
fos.close();
}
// load it
TopLevelItem result = (TopLevelItem)Items.load(this,configXml.getParentFile());
items.put(name,result);
return result;
} catch (IOException e) {
// if anything fails, delete the config file to avoid further confusion
Util.deleteRecursive(configXml.getParentFile());
throw e;
}
}
/**
* Copys a job.
*