diff --git a/core/src/main/java/hudson/cli/CreateJobCommand.java b/core/src/main/java/hudson/cli/CreateJobCommand.java new file mode 100644 index 0000000000..5638ddadd7 --- /dev/null +++ b/core/src/main/java/hudson/cli/CreateJobCommand.java @@ -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; + } +} + + diff --git a/core/src/main/java/hudson/model/Hudson.java b/core/src/main/java/hudson/model/Hudson.java index ae725d0d28..f3b575bfae 100644 --- a/core/src/main/java/hudson/model/Hudson.java +++ b/core/src/main/java/hudson/model/Hudson.java @@ -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, 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, 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. *