GroovyCommand.java 3.8 KB
Newer Older
K
kohsuke 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * The MIT License
 *
 * Copyright (c) 2004-2009, Sun Microsystems, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24 25 26 27
package hudson.cli;

import groovy.lang.GroovyShell;
import groovy.lang.Binding;
28
import hudson.cli.util.ScriptLoader;
29
import hudson.model.AbstractProject;
30
import jenkins.model.Jenkins;
31 32
import hudson.model.Item;
import hudson.model.Run;
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
import hudson.Extension;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Executes the specified groovy script.
 *
 * @author Kohsuke Kawaguchi
 */
@Extension
49
public class GroovyCommand extends CLICommand {
50 51
    @Override
    public String getShortDescription() {
52
        return Messages.GroovyCommand_ShortDescription();
53 54
    }

55
    @Argument(metaVar="SCRIPT",usage="Script to be executed. File, URL or '=' to represent stdin.")
56 57 58 59 60
    public String script;

    /**
     * Remaining arguments.
     */
61
    @Argument(metaVar="ARGUMENTS", index=1, usage="Command line arguments to pass into script.")
62 63 64
    public List<String> remaining = new ArrayList<String>();

    protected int run() throws Exception {
65
        // this allows the caller to manipulate the JVM state, so require the execute script privilege.
66
        Jenkins.getInstance().checkPermission(Jenkins.RUN_SCRIPTS);
67 68 69

        Binding binding = new Binding();
        binding.setProperty("out",new PrintWriter(stdout,true));
K
Kohsuke Kawaguchi 已提交
70 71 72 73
        binding.setProperty("stdin",stdin);
        binding.setProperty("stdout",stdout);
        binding.setProperty("stderr",stderr);
        binding.setProperty("channel",channel);
74 75
        String j = getClientEnvironmentVariable("JOB_NAME");
        if (j!=null) {
76
            Item job = Jenkins.getInstance().getItemByFullName(j);
77 78 79 80 81 82 83 84
            binding.setProperty("currentJob", job);
            String b = getClientEnvironmentVariable("BUILD_NUMBER");
            if (b!=null && job instanceof AbstractProject) {
                Run r = ((AbstractProject) job).getBuildByNumber(Integer.parseInt(b));
                binding.setProperty("currentBuild", r);
            }
        }

85
        GroovyShell groovy = new GroovyShell(Jenkins.getInstance().getPluginManager().uberClassLoader, binding);
86
        groovy.run(loadScript(),"RemoteClass",remaining.toArray(new String[remaining.size()]));
87 88 89 90 91 92 93 94
        return 0;
    }

    /**
     * Loads the script from the argument.
     */
    private String loadScript() throws CmdLineException, IOException, InterruptedException {
        if(script==null)
M
mindless 已提交
95
            throw new CmdLineException(null, "No script is specified");
96 97 98
        if (script.equals("="))
            return IOUtils.toString(stdin);

99
        return checkChannel().call(new ScriptLoader(script));
100 101 102
    }
}