1 package hudson; 2 3 import hudson.model.Hudson; 4 import hudson.model.TaskListener; 5 import hudson.model.Computer; 6 import hudson.remoting.VirtualChannel; 7 import hudson.remoting.Channel; 8 import hudson.Proc.LocalProc; 9 import hudson.util.StreamCopyThread; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import java.io.OutputStream ; 15 import java.util.HashMap ; 16 import java.util.Map ; 17 18 37 public abstract class Launcher { 38 39 protected final TaskListener listener; 40 41 protected final VirtualChannel channel; 42 43 public Launcher(TaskListener listener, VirtualChannel channel) { 44 this.listener = listener; 45 this.channel = channel; 46 } 47 48 56 public VirtualChannel getChannel() { 57 return channel; 58 } 59 60 public final Proc launch(String cmd, Map <String ,String > env, OutputStream out, FilePath workDir) throws IOException { 61 return launch(cmd,Util.mapToEnv(env),out,workDir); 62 } 63 64 public final Proc launch(String [] cmd, Map <String ,String > env, OutputStream out, FilePath workDir) throws IOException { 65 return launch(cmd,Util.mapToEnv(env),out,workDir); 66 } 67 68 public final Proc launch(String [] cmd, Map <String ,String > env, InputStream in, OutputStream out) throws IOException { 69 return launch(cmd,Util.mapToEnv(env),in,out); 70 } 71 72 public final Proc launch(String cmd,String [] env,OutputStream out, FilePath workDir) throws IOException { 73 return launch(Util.tokenize(cmd),env,out,workDir); 74 } 75 76 public final Proc launch(String [] cmd,String [] env,OutputStream out, FilePath workDir) throws IOException { 77 return launch(cmd,env,null,out,workDir); 78 } 79 80 public final Proc launch(String [] cmd,String [] env,InputStream in,OutputStream out) throws IOException { 81 return launch(cmd,env,in,out,null); 82 } 83 84 93 public abstract Proc launch(String [] cmd,String [] env,InputStream in,OutputStream out, FilePath workDir) throws IOException ; 94 95 102 public abstract Channel launchChannel(String [] cmd, OutputStream out, FilePath workDir) throws IOException , InterruptedException ; 103 104 107 public boolean isUnix() { 108 return File.pathSeparatorChar==':'; 109 } 110 111 114 protected final void printCommandLine(String [] cmd, FilePath workDir) { 115 StringBuffer buf = new StringBuffer (); 116 if (workDir != null) { 117 buf.append('['); 118 if(showFullPath) 119 buf.append(workDir.getRemote()); 120 else 121 buf.append(workDir.getRemote().replaceFirst("^.+[/\\\\]", "")); 122 buf.append("] "); 123 } 124 buf.append('$'); 125 for (String c : cmd) { 126 buf.append(' '); 127 if(c.indexOf(' ')>=0) { 128 if(c.indexOf('"')>=0) 129 buf.append('\'').append(c).append('\''); 130 else 131 buf.append('"').append(c).append('"'); 132 } else 133 buf.append(c); 134 } 135 listener.getLogger().println(buf.toString()); 136 } 137 138 public static class LocalLauncher extends Launcher { 139 public LocalLauncher(TaskListener listener) { 140 this(listener,Hudson.MasterComputer.localChannel); 141 } 142 143 public LocalLauncher(TaskListener listener, VirtualChannel channel) { 144 super(listener, channel); 145 } 146 147 public Proc launch(String [] cmd,String [] env,InputStream in,OutputStream out, FilePath workDir) throws IOException { 148 printCommandLine(cmd, workDir); 149 return new LocalProc(cmd,Util.mapToEnv(inherit(env)),in,out, toFile(workDir)); 150 } 151 152 private File toFile(FilePath f) { 153 return f==null ? null : new File (f.getRemote()); 154 } 155 156 public Channel launchChannel(String [] cmd, OutputStream out, FilePath workDir) throws IOException { 157 printCommandLine(cmd, workDir); 158 159 Process proc = Runtime.getRuntime().exec(cmd, null, toFile(workDir)); 160 161 163 Thread t2 = new StreamCopyThread(cmd+": stderr copier", proc.getErrorStream(), out); 164 t2.start(); 165 166 return new Channel("locally launched channel on "+cmd, 167 Computer.threadPoolForRemoting, proc.getInputStream(), proc.getOutputStream(), out); 168 } 169 170 173 private Map <String ,String > inherit(String [] env) { 174 Map <String ,String > m = new HashMap <String ,String >(EnvVars.masterEnvVars); 175 for (String e : env) { 176 int index = e.indexOf('='); 177 String key = e.substring(0,index); 178 String value = e.substring(index+1); 179 if(value.length()==0) 180 m.remove(key); 181 else 182 m.put(key,value); 183 } 184 return m; 185 } 186 } 187 188 191 public static boolean showFullPath = false; 192 } 193 | Popular Tags |