1 package hudson; 2 3 import hudson.model.ExternalJob; 4 import hudson.model.ExternalRun; 5 import hudson.model.Hudson; 6 import hudson.model.Result; 7 import hudson.model.TopLevelItem; 8 import hudson.util.DualOutputStream; 9 import hudson.util.EncodingStream; 10 11 import java.io.File ; 12 import java.io.OutputStream ; 13 import java.io.OutputStreamWriter ; 14 import java.io.Writer ; 15 import java.net.HttpURLConnection ; 16 import java.net.URL ; 17 import java.net.URLEncoder ; 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 29 public class Main { 30 public static void main(String [] args) { 31 try { 32 System.exit(run(args)); 33 } catch (Exception e) { 34 e.printStackTrace(); 35 System.exit(-1); 36 } 37 } 38 39 public static int run(String [] args) throws Exception { 40 String home = getHudsonHome(); 41 if(home==null) { 42 System.err.println("HUDSON_HOME is not set."); 43 return -1; 44 } 45 46 if(home.startsWith("http")) { 47 return remotePost(args); 48 } else { 49 return localPost(args); 50 } 51 } 52 53 private static String getHudsonHome() { 54 return EnvVars.masterEnvVars.get("HUDSON_HOME"); 55 } 56 57 60 public static int localPost(String [] args) throws Exception { 61 Hudson app = new Hudson(new File (getHudsonHome()),null); 62 63 TopLevelItem item = app.getItem(args[0]); 64 if(!(item instanceof ExternalJob)) { 65 System.err.println(args[0]+" is not a valid external job name in Hudson"); 66 return -1; 67 } 68 ExternalJob ejob = (ExternalJob) item; 69 70 ExternalRun run = ejob.newBuild(); 71 72 List <String > cmd = new ArrayList <String >(); 74 for( int i=1; i<args.length; i++ ) 75 cmd.add(args[i]); 76 run.run(cmd.toArray(new String [cmd.size()])); 77 78 return run.getResult()==Result.SUCCESS?0:1; 79 } 80 81 84 public static int remotePost(String [] args) throws Exception { 85 String projectName = args[0]; 86 87 String home = getHudsonHome(); 88 if(!home.endsWith("/")) home = home + '/'; 90 { HttpURLConnection con = (HttpURLConnection )new URL (home).openConnection(); 92 con.connect(); 93 if(con.getResponseCode()!=200 94 || con.getHeaderField("X-Hudson")==null) { 95 System.err.println(home+" is not Hudson ("+con.getResponseMessage()+")"); 96 return -1; 97 } 98 } 99 100 String projectNameEnc = URLEncoder.encode(projectName,"UTF-8").replaceAll("\\+","%20"); 101 102 { HttpURLConnection con = (HttpURLConnection )new URL (home+"job/"+projectNameEnc+"/acceptBuildResult").openConnection(); 104 con.connect(); 105 if(con.getResponseCode()!=200) { 106 System.err.println(projectName+" is not a valid job name on "+home+" ("+con.getResponseMessage()+")"); 107 return -1; 108 } 109 } 110 111 HttpURLConnection con = (HttpURLConnection ) new URL (home+"job/"+projectNameEnc+"/postBuildResult").openConnection(); 113 con.setDoOutput(true); 114 con.connect(); 115 OutputStream os = con.getOutputStream(); 116 Writer w = new OutputStreamWriter (os,"UTF-8"); 117 w.write("<?xml version='1.0' encoding='UTF-8'?>"); 118 w.write("<run><log encoding='hexBinary'>"); 119 w.flush(); 120 121 long start = System.currentTimeMillis(); 123 124 List <String > cmd = new ArrayList <String >(); 125 for( int i=1; i<args.length; i++ ) 126 cmd.add(args[i]); 127 Proc proc = new Proc.LocalProc(cmd.toArray(new String [0]),(String [])null,System.in, 128 new DualOutputStream(System.out,new EncodingStream(os))); 129 130 int ret = proc.join(); 131 132 w.write("</log><result>"+ret+"</result><duration>"+(System.currentTimeMillis()-start)+"</duration></run>"); 133 w.close(); 134 135 if(con.getResponseCode()!=200) { 136 Util.copyStream(con.getErrorStream(),System.err); 137 } 138 139 return ret; 140 } 141 } 142 | Popular Tags |