mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-17 14:02:22 +00:00
- added stdin forwarding.
- added groovysh command for interactive manipulation of Hudson server git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@17597 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
1 parent
95d4f7b21f
commit
1f8dc570aa
5 files changed
+79
-11
No files matched your search
@@ -33,22 +33,42 @@ import org.kohsuke.args4j.CmdLineParser;
|
||||
import org.kohsuke.args4j.CmdLineException;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Base class for Hudson CLI.
|
||||
*
|
||||
* <h2>How to implement a CLI command</h2>
|
||||
* <h2>How does a CLI command work</h2>
|
||||
* <p>
|
||||
* CLI commands are defined on the server.
|
||||
* The users starts {@linkplain CLI the "CLI agent"} on a remote system, by specifying arguments, like
|
||||
* <tt>"java -jar hudson-cli.jar command arg1 arg2 arg3"</tt>. The CLI agent creates
|
||||
* a remoting channel with the server, and it sends the entire arguments to the server, along with
|
||||
* the remoted stdin/out/err.
|
||||
*
|
||||
* <p>
|
||||
* The Hudson master then picks the right {@link CLICommand} to execute, clone it, and
|
||||
* calls {@link #main(List, InputStream, PrintStream, PrintStream)} method.
|
||||
*
|
||||
* <h2>Note for CLI command implementor</h2>
|
||||
* <ul>
|
||||
* <li>
|
||||
* Put {@link Extension} on your implementation to have it discovered by Hudson.
|
||||
*
|
||||
* <li>
|
||||
* Use <a href="http://args4j.dev.java.net/">args4j</a> annotation on your implementation to define
|
||||
* options and arguments (however, if you don't like that, you could override the {@link #main(List, PrintStream, PrintStream)} method
|
||||
* directly.
|
||||
* options and arguments (however, if you don't like that, you could override
|
||||
* the {@link #main(List, InputStream, PrintStream, PrintStream)} method directly.
|
||||
*
|
||||
* <p>
|
||||
* Put {@link Extension} on your implementation to have it auto-registered.
|
||||
* <li>
|
||||
* stdin, stdout, stderr are remoted, so proper buffering is necessary for good user experience.
|
||||
*
|
||||
* <li>
|
||||
* Send {@link Callable} to a CLI agent by using {@link #channel} to get local interaction,
|
||||
* such as uploading a file, asking for a password, etc.
|
||||
*
|
||||
* </ul>
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
* @since 1.302
|
||||
@@ -59,11 +79,20 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable {
|
||||
* IOW, if you write to these streams, the person who launched the CLI command
|
||||
* will see the messages in his terminal.
|
||||
*
|
||||
* <p>
|
||||
* (In contrast, calling {@code System.out.println(...)} would print out
|
||||
* the message to the server log file, which is probably not what you want.
|
||||
*/
|
||||
protected PrintStream stdout,stderr;
|
||||
|
||||
/**
|
||||
* Connected to stdin of the CLI agent.
|
||||
*
|
||||
* <p>
|
||||
* This input stream is buffered to hide the latency in the remoting.
|
||||
*/
|
||||
protected InputStream stdin;
|
||||
|
||||
/**
|
||||
* {@link Channel} that represents the CLI JVM. You can use this to
|
||||
* execute {@link Callable} on the CLI JVM, among other things.
|
||||
@@ -92,7 +121,8 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable {
|
||||
return name.replaceAll("([a-z0-9])([A-Z])","$1-$2").toLowerCase();
|
||||
}
|
||||
|
||||
public int main(List<String> args, PrintStream stdout, PrintStream stderr) {
|
||||
public int main(List<String> args, InputStream stdin, PrintStream stdout, PrintStream stderr) {
|
||||
this.stdin = new BufferedInputStream(stdin);
|
||||
this.stdout = stdout;
|
||||
this.stderr = stderr;
|
||||
this.channel = Channel.current();
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package hudson.cli;
|
||||
|
||||
import hudson.Extension;
|
||||
import org.codehaus.groovy.tools.shell.Groovysh;
|
||||
import org.codehaus.groovy.tools.shell.IO;
|
||||
|
||||
import java.util.List;
|
||||
import java.io.PrintStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.BufferedInputStream;
|
||||
|
||||
import jline.UnsupportedTerminal;
|
||||
import jline.Terminal;
|
||||
|
||||
/**
|
||||
* Executes Groovy shell.
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
@Extension
|
||||
public class GroovyshCommand extends CLICommand {
|
||||
@Override
|
||||
public int main(List<String> args, InputStream stdin, PrintStream stdout, PrintStream stderr) {
|
||||
// this being remote means no jline capability is available
|
||||
System.setProperty("jline.terminal", UnsupportedTerminal.class.getName());
|
||||
Terminal.resetTerminal();
|
||||
|
||||
Groovysh shell = new Groovysh(new IO(new BufferedInputStream(stdin),stdout,stderr));
|
||||
return shell.run(args.toArray(new String[args.size()]));
|
||||
}
|
||||
|
||||
protected int run() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
@@ -2660,14 +2660,14 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl
|
||||
* {@link CliEntryPoint} implementation exposed to the remote CLI.
|
||||
*/
|
||||
private final class CliManager implements CliEntryPoint, Serializable {
|
||||
public int main(List<String> args, OutputStream stdout, OutputStream stderr) {
|
||||
public int main(List<String> args, InputStream stdin, OutputStream stdout, OutputStream stderr) {
|
||||
PrintStream err = new PrintStream(stderr);
|
||||
|
||||
String subCmd = args.get(0);
|
||||
CLICommand cmd = CLICommand.clone(subCmd);
|
||||
if(cmd!=null)
|
||||
return cmd.main(args.subList(1,args.size()),
|
||||
new PrintStream(stdout), err);
|
||||
stdin, new PrintStream(stdout), err);
|
||||
|
||||
err.println("No such command: "+subCmd);
|
||||
for (CLICommand c : CLICommand.all())
|
||||
|
||||
Reference in new issue
Block a user