PageRenderTime 24ms CodeModel.GetById 12ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/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 */
 24package hudson.cli;
 25
 26import hudson.model.AbstractBuild;
 27import hudson.model.AbstractProject;
 28import hudson.model.Cause;
 29import hudson.model.ParametersAction;
 30import hudson.model.ParameterValue;
 31import hudson.model.ParametersDefinitionProperty;
 32import hudson.model.ParameterDefinition;
 33import hudson.Extension;
 34import hudson.AbortException;
 35import hudson.model.Item;
 36import hudson.util.EditDistance;
 37import org.kohsuke.args4j.Argument;
 38import org.kohsuke.args4j.Option;
 39
 40import java.util.concurrent.Future;
 41import java.util.Map;
 42import java.util.HashMap;
 43import java.util.List;
 44import java.util.ArrayList;
 45import java.util.Map.Entry;
 46import java.io.PrintStream;
 47
 48/**
 49 * Builds a job, and optionally waits until its completion.
 50 *
 51 * @author Kohsuke Kawaguchi
 52 */
 53@Extension
 54public class BuildCommand extends CLICommand {
 55    @Override
 56    public String getShortDescription() {
 57        return "Builds a job, and optionally waits until its completion.";
 58    }
 59
 60    @Argument(metaVar="JOB",usage="Name of the job to build",required=true)
 61    public AbstractProject<?,?> job;
 62
 63    @Option(name="-s",usage="Wait until the completion/abortion of the command")
 64    public boolean sync = false;
 65
 66    @Option(name="-p",usage="Specify the build parameters in the key=value format.")
 67    public Map<String,String> parameters = new HashMap<String, String>();
 68
 69    protected int run() throws Exception {
 70        job.checkPermission(Item.BUILD);
 71
 72        ParametersAction a = null;
 73        if (!parameters.isEmpty()) {
 74            ParametersDefinitionProperty pdp = job.getProperty(ParametersDefinitionProperty.class);
 75            if (pdp==null)
 76                throw new AbortException(job.getFullDisplayName()+" is not parameterized but the -p option was specified");
 77
 78            List<ParameterValue> values = new ArrayList<ParameterValue>(); 
 79
 80            for (Entry<String, String> e : parameters.entrySet()) {
 81                String name = e.getKey();
 82                ParameterDefinition pd = pdp.getParameterDefinition(name);
 83                if (pd==null)
 84                    throw new AbortException(String.format("\'%s\' is not a valid parameter. Did you mean %s?",
 85                            name, EditDistance.findNearest(name, pdp.getParameterDefinitionNames())));
 86                values.add(pd.createValue(this,e.getValue()));
 87            }
 88            for (ParameterDefinition pd : pdp.getParameterDefinitions()) {
 89                if (parameters.get(pd.getName()) == null) {
 90                    values.add(pd.getDefaultParameterValue());
 91                }
 92            }
 93            a = new ParametersAction(values);
 94        }
 95
 96        Future<? extends AbstractBuild> f = job.scheduleBuild2(0, new CLICause(), a);
 97        if (!sync)  return 0;
 98
 99        AbstractBuild b = f.get();    // wait for the completion
100        stdout.println("Completed "+b.getFullDisplayName()+" : "+b.getResult());
101        return b.getResult().ordinal;
102    }
103
104    @Override
105    protected void printUsageSummary(PrintStream stderr) {
106        stderr.println(
107            "Starts a build, and optionally waits for a completion.\n" +
108            "Aside from general scripting use, this command can be\n" +
109            "used to invoke another job from within a build of one job.\n" +
110            "With the -s option, this command changes the exit code based on\n" +
111            "the outcome of the build (exit code 0 indicates a success.)\n"
112        );
113    }
114
115    // TODO: CLI can authenticate as different users, so should record which user here..
116    public static class CLICause extends Cause {
117        public String getShortDescription() {
118            return "Started by command line";
119        }
120
121        @Override
122        public boolean equals(Object o) {
123            return o instanceof CLICause;
124        }
125
126        @Override
127        public int hashCode() {
128            return 7;
129        }
130    }
131}
132