/hudson-core/src/main/java/hudson/cli/BuildCommand.java

http://github.com/hudson/hudson · Java · 132 lines · 86 code · 17 blank · 29 comment · 8 complexity · 46a80ab8f40149a109f33327671c0e17 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2010, Sun Microsystems, Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.cli;
  25. import hudson.model.AbstractBuild;
  26. import hudson.model.AbstractProject;
  27. import hudson.model.Cause;
  28. import hudson.model.ParametersAction;
  29. import hudson.model.ParameterValue;
  30. import hudson.model.ParametersDefinitionProperty;
  31. import hudson.model.ParameterDefinition;
  32. import hudson.Extension;
  33. import hudson.AbortException;
  34. import hudson.model.Item;
  35. import hudson.util.EditDistance;
  36. import org.kohsuke.args4j.Argument;
  37. import org.kohsuke.args4j.Option;
  38. import java.util.concurrent.Future;
  39. import java.util.Map;
  40. import java.util.HashMap;
  41. import java.util.List;
  42. import java.util.ArrayList;
  43. import java.util.Map.Entry;
  44. import java.io.PrintStream;
  45. /**
  46. * Builds a job, and optionally waits until its completion.
  47. *
  48. * @author Kohsuke Kawaguchi
  49. */
  50. @Extension
  51. public class BuildCommand extends CLICommand {
  52. @Override
  53. public String getShortDescription() {
  54. return "Builds a job, and optionally waits until its completion.";
  55. }
  56. @Argument(metaVar="JOB",usage="Name of the job to build",required=true)
  57. public AbstractProject<?,?> job;
  58. @Option(name="-s",usage="Wait until the completion/abortion of the command")
  59. public boolean sync = false;
  60. @Option(name="-p",usage="Specify the build parameters in the key=value format.")
  61. public Map<String,String> parameters = new HashMap<String, String>();
  62. protected int run() throws Exception {
  63. job.checkPermission(Item.BUILD);
  64. ParametersAction a = null;
  65. if (!parameters.isEmpty()) {
  66. ParametersDefinitionProperty pdp = job.getProperty(ParametersDefinitionProperty.class);
  67. if (pdp==null)
  68. throw new AbortException(job.getFullDisplayName()+" is not parameterized but the -p option was specified");
  69. List<ParameterValue> values = new ArrayList<ParameterValue>();
  70. for (Entry<String, String> e : parameters.entrySet()) {
  71. String name = e.getKey();
  72. ParameterDefinition pd = pdp.getParameterDefinition(name);
  73. if (pd==null)
  74. throw new AbortException(String.format("\'%s\' is not a valid parameter. Did you mean %s?",
  75. name, EditDistance.findNearest(name, pdp.getParameterDefinitionNames())));
  76. values.add(pd.createValue(this,e.getValue()));
  77. }
  78. for (ParameterDefinition pd : pdp.getParameterDefinitions()) {
  79. if (parameters.get(pd.getName()) == null) {
  80. values.add(pd.getDefaultParameterValue());
  81. }
  82. }
  83. a = new ParametersAction(values);
  84. }
  85. Future<? extends AbstractBuild> f = job.scheduleBuild2(0, new CLICause(), a);
  86. if (!sync) return 0;
  87. AbstractBuild b = f.get(); // wait for the completion
  88. stdout.println("Completed "+b.getFullDisplayName()+" : "+b.getResult());
  89. return b.getResult().ordinal;
  90. }
  91. @Override
  92. protected void printUsageSummary(PrintStream stderr) {
  93. stderr.println(
  94. "Starts a build, and optionally waits for a completion.\n" +
  95. "Aside from general scripting use, this command can be\n" +
  96. "used to invoke another job from within a build of one job.\n" +
  97. "With the -s option, this command changes the exit code based on\n" +
  98. "the outcome of the build (exit code 0 indicates a success.)\n"
  99. );
  100. }
  101. // TODO: CLI can authenticate as different users, so should record which user here..
  102. public static class CLICause extends Cause {
  103. public String getShortDescription() {
  104. return "Started by command line";
  105. }
  106. @Override
  107. public boolean equals(Object o) {
  108. return o instanceof CLICause;
  109. }
  110. @Override
  111. public int hashCode() {
  112. return 7;
  113. }
  114. }
  115. }