| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | |
| 34 | | | package org.jcoderz.commons.taskdefs; |
| 35 | | | |
| 36 | | | import java.io.File; |
| 37 | | | import java.io.FileNotFoundException; |
| 38 | | | import java.io.FileOutputStream; |
| 39 | | | import java.io.IOException; |
| 40 | | | import java.io.InputStream; |
| 41 | | | import java.io.OutputStream; |
| 42 | | | import java.net.HttpURLConnection; |
| 43 | | | import java.net.MalformedURLException; |
| 44 | | | import java.net.URL; |
| 45 | | | import java.util.ArrayList; |
| 46 | | | import java.util.Iterator; |
| 47 | | | import java.util.List; |
| 48 | | | |
| 49 | | | import org.apache.tools.ant.BuildException; |
| 50 | | | import org.apache.tools.ant.Project; |
| 51 | | | import org.apache.tools.ant.Task; |
| 52 | | | import org.jcoderz.commons.util.IoUtil; |
| 53 | | | |
| 54 | | | import com.caucho.hessian.client.HessianProxyFactory; |
| 55 | | | import com.luntsys.luntbuild.facades.BuildParams; |
| 56 | | | import com.luntsys.luntbuild.facades.Constants; |
| 57 | | | import com.luntsys.luntbuild.facades.ILuntbuild; |
| 58 | | | import com.luntsys.luntbuild.facades.lb12.BuildFacade; |
| 59 | | | import com.luntsys.luntbuild.facades.lb12.ScheduleFacade; |
| 60 | | | |
| 61 | | | |
| 62 | | | |
| 63 | | | |
| 64 | | | |
| 65 | | | @author |
| 66 | | | |
| 67 | 0 | | public class LuntBuildTask |
| 68 | | | extends Task |
| 69 | | | { |
| 70 | | | |
| 71 | | | |
| 72 | | | |
| 73 | | | |
| 74 | | | public static final String START_MULTIPLE = "startMultiple"; |
| 75 | | | |
| 76 | | | |
| 77 | | | |
| 78 | | | public static final String SKIP_IF_RUNNING = "skipIfRunning"; |
| 79 | | | |
| 80 | | | |
| 81 | | | |
| 82 | | | public static final String FAIL_IF_RUNNING = "failIfRunning"; |
| 83 | | | |
| 84 | 0 | | private static final List POLICY_LIST = new ArrayList(); |
| 85 | | | static |
| 86 | | | { |
| 87 | 0 | | POLICY_LIST.add(START_MULTIPLE); |
| 88 | 0 | | POLICY_LIST.add(SKIP_IF_RUNNING); |
| 89 | 0 | | POLICY_LIST.add(FAIL_IF_RUNNING); |
| 90 | 0 | | } |
| 91 | | | |
| 92 | | | |
| 93 | | | |
| 94 | | | private static final int WAIT_PERIOD = 5000; |
| 95 | | | |
| 96 | | | private static final int POLL_INTERVAL = 2000; |
| 97 | | | |
| 98 | | | private String mLuntUrl; |
| 99 | | | private String mUserName; |
| 100 | | | private String mPassword; |
| 101 | | | private String mProjectName; |
| 102 | | | private String mScheduleName; |
| 103 | 0 | | private String mStartPolicy = FAIL_IF_RUNNING; |
| 104 | 0 | | private boolean mWaitForSchedule = true; |
| 105 | | | private String mToDir; |
| 106 | 0 | | private final List mArtifacts = new ArrayList(); |
| 107 | | | |
| 108 | | | private ILuntbuild mLuntServer; |
| 109 | | | |
| 110 | | | |
| 111 | | | @param |
| 112 | | | |
| 113 | | | public void setLuntUrl (String luntUrl) |
| 114 | | | { |
| 115 | 0 | | mLuntUrl = luntUrl; |
| 116 | 0 | | } |
| 117 | | | |
| 118 | | | |
| 119 | | | @param |
| 120 | | | |
| 121 | | | public void setUserName (String userName) |
| 122 | | | { |
| 123 | 0 | | mUserName = userName; |
| 124 | 0 | | } |
| 125 | | | |
| 126 | | | |
| 127 | | | @param |
| 128 | | | |
| 129 | | | public void setPassword (String password) |
| 130 | | | { |
| 131 | 0 | | mPassword = password; |
| 132 | 0 | | } |
| 133 | | | |
| 134 | | | |
| 135 | | | @param |
| 136 | | | |
| 137 | | | public void setProjectName (String projectName) |
| 138 | | | { |
| 139 | 0 | | mProjectName = projectName; |
| 140 | 0 | | } |
| 141 | | | |
| 142 | | | |
| 143 | | | @param |
| 144 | | | |
| 145 | | | public void setScheduleName (String scheduleName) |
| 146 | | | { |
| 147 | 0 | | mScheduleName = scheduleName; |
| 148 | 0 | | } |
| 149 | | | |
| 150 | | | |
| 151 | | | @param |
| 152 | | | |
| 153 | | | public void setStartPolicy (String startPolicy) |
| 154 | | | { |
| 155 | 0 | (1) | if (!POLICY_LIST.contains(startPolicy)) |
| 156 | | | { |
| 157 | 0 | | throw new BuildException("Invalid start policy " + startPolicy |
| 158 | | | + ", must be one of " + POLICY_LIST); |
| 159 | | | } |
| 160 | 0 | | mStartPolicy = startPolicy; |
| 161 | 0 | | } |
| 162 | | | |
| 163 | | | |
| 164 | | | @param |
| 165 | | | |
| 166 | | | public void setWaitForSchedule (boolean waitForSchedule) |
| 167 | | | { |
| 168 | 0 | | mWaitForSchedule = waitForSchedule; |
| 169 | 0 | | } |
| 170 | | | |
| 171 | | | |
| 172 | | | @param |
| 173 | | | |
| 174 | | | public void setToDir (String toDir) |
| 175 | | | { |
| 176 | 0 | | mToDir = toDir; |
| 177 | 0 | | } |
| 178 | | | |
| 179 | | | |
| 180 | | | |
| 181 | | | @param |
| 182 | | | |
| 183 | | | public void addArtifact (Artifact artifact) |
| 184 | | | { |
| 185 | 0 | | mArtifacts.add(artifact); |
| 186 | 0 | | } |
| 187 | | | |
| 188 | | | |
| 189 | | | |
| 190 | | | @throws |
| 191 | | | |
| 192 | | | public void execute () throws BuildException |
| 193 | | | { |
| 194 | 0 | | checkParameters(); |
| 195 | | | try |
| 196 | | | { |
| 197 | | | |
| 198 | 0 | | final ScheduleFacade schedule = getSchedule(); |
| 199 | | | final boolean startSchedule; |
| 200 | 0 | | if (schedule.getStatus() == Constants.SCHEDULE_STATUS_RUNNING) |
| 201 | | | { |
| 202 | 0 | (2) | if (mStartPolicy.equals(START_MULTIPLE)) |
| 203 | | | { |
| 204 | 0 | | startSchedule = true; |
| 205 | | | } |
| 206 | 0 | (3) | else if (mStartPolicy.equals(SKIP_IF_RUNNING)) |
| 207 | | | { |
| 208 | 0 | | startSchedule = false; |
| 209 | | | } |
| 210 | | | else |
| 211 | | | { |
| 212 | 0 | | throw new BuildException( |
| 213 | | | "Can't start build because schedule is already running"); |
| 214 | | | } |
| 215 | | | } |
| 216 | | | else |
| 217 | | | { |
| 218 | 0 | | startSchedule = true; |
| 219 | | | } |
| 220 | | | |
| 221 | 0 | | if (startSchedule) |
| 222 | | | { |
| 223 | 0 | | startSchedule(); |
| 224 | | | } |
| 225 | | | |
| 226 | | | } |
| 227 | 0 | | catch (BuildException x) |
| 228 | | | { |
| 229 | 0 | | throw x; |
| 230 | | | } |
| 231 | 0 | | catch (Exception x) |
| 232 | | | { |
| 233 | 0 | | throw new BuildException(x); |
| 234 | 0 | | } |
| 235 | 0 | | } |
| 236 | | | |
| 237 | | | private void startSchedule () throws InterruptedException, IOException |
| 238 | | | { |
| 239 | 0 | | log("Starting build for " + mProjectName + "/" + mScheduleName |
| 240 | | | + " on server " + mLuntUrl, Project.MSG_INFO); |
| 241 | 0 | | getLuntServer().triggerBuild(mProjectName, mScheduleName, getBuildParams()); |
| 242 | | | |
| 243 | 0 | | if (mWaitForSchedule) |
| 244 | | | { |
| 245 | 0 | | waitForSchedule(); |
| 246 | | | } |
| 247 | 0 | | } |
| 248 | | | |
| 249 | | | private void waitForSchedule () throws InterruptedException, IOException |
| 250 | | | { |
| 251 | 0 | | Thread.sleep(WAIT_PERIOD); |
| 252 | 0 | | log("Waiting for build " |
| 253 | | | + getLuntServer().getLastBuild(mProjectName, mScheduleName).getVersion() |
| 254 | | | + " to finish"); |
| 255 | | | |
| 256 | 0 | | while (getSchedule().getStatus() == Constants.SCHEDULE_STATUS_RUNNING) |
| 257 | | | { |
| 258 | 0 | | log("Schedule running", Project.MSG_VERBOSE); |
| 259 | 0 | | Thread.sleep(POLL_INTERVAL); |
| 260 | | | } |
| 261 | 0 | | final int termStatus = getSchedule().getStatus(); |
| 262 | 0 | | switch (termStatus) |
| 263 | | | { |
| 264 | | | case Constants.SCHEDULE_STATUS_SUCCESS: |
| 265 | 0 | | log("LuntBuild schedule " + mProjectName + "/" + mScheduleName |
| 266 | | | + " succeeded"); |
| 267 | 0 | | dumpLogFile(); |
| 268 | 0 | | retrieveArtifacts(); |
| 269 | 0 | | break; |
| 270 | | | case Constants.SCHEDULE_STATUS_FAILED: |
| 271 | 0 | | log("LuntBuild schedule " + mProjectName + "/" + mScheduleName |
| 272 | | | + " FAILED"); |
| 273 | 0 | | dumpLogFile(); |
| 274 | 0 | | throw new BuildException("LuntBuild schedule " + mProjectName + "/" |
| 275 | | | + mScheduleName + " FAILED"); |
| 276 | | | default: |
| 277 | 0 | | throw new BuildException("Unexpected status for schedule " |
| 278 | | | + mProjectName + "/" + mScheduleName + ": " + termStatus); |
| 279 | | | } |
| 280 | 0 | | } |
| 281 | | | |
| 282 | | | private ScheduleFacade getSchedule () throws MalformedURLException |
| 283 | | | { |
| 284 | 0 | | return getLuntServer().getScheduleByName(mProjectName, mScheduleName); |
| 285 | | | } |
| 286 | | | |
| 287 | | | |
| 288 | | | @throws |
| 289 | | | |
| 290 | | | |
| 291 | | | private void retrieveArtifacts () throws IOException |
| 292 | | | { |
| 293 | 0 | | final BuildFacade currentBuild |
| 294 | | | = getLuntServer().getLastBuild(mProjectName, mScheduleName); |
| 295 | 0 | | final String buildLogUrl = currentBuild.getBuildLogUrl(); |
| 296 | 0 | | final String path = buildLogUrl.substring(0, buildLogUrl.lastIndexOf('/')); |
| 297 | 0 | | final String artifactsBaseUrl = path + "/artifacts/"; |
| 298 | 0 | | log("Artifacts base URL: " + artifactsBaseUrl, Project.MSG_VERBOSE); |
| 299 | 0 | | HttpURLConnection.setFollowRedirects(true); |
| 300 | | | |
| 301 | 0 | | for (final Iterator it = mArtifacts.iterator(); it.hasNext(); ) |
| 302 | | | { |
| 303 | 0 | | final String artifactName = ((Artifact) it.next()).getName(); |
| 304 | 0 | | final File outputFile = new File(new File(mToDir), artifactName); |
| 305 | 0 | | if (outputFile.exists()) |
| 306 | | | { |
| 307 | 0 | | throw new BuildException("Output file " + outputFile |
| 308 | | | + " already exists"); |
| 309 | | | } |
| 310 | 0 | | final String artifactUrl = artifactsBaseUrl + artifactName; |
| 311 | 0 | | log("Retrieving artifact " + artifactName); |
| 312 | 0 | | log("Retrieving from URL: " + artifactUrl, Project.MSG_VERBOSE); |
| 313 | 0 | | log("Writing to file: " + mToDir + File.separator + outputFile, |
| 314 | | | Project.MSG_VERBOSE); |
| 315 | 0 | | final HttpURLConnection con |
| 316 | | | = (HttpURLConnection) new URL(artifactUrl).openConnection(); |
| 317 | 0 | | con.setDoOutput(true); |
| 318 | 0 | | con.addRequestProperty("Keep-alive", "false"); |
| 319 | | | |
| 320 | 0 | | con.connect(); |
| 321 | 0 | | if (con.getResponseCode() != HttpURLConnection.HTTP_OK) |
| 322 | | | { |
| 323 | 0 | | throw new BuildException("Failed while retrieving artifact " |
| 324 | | | + artifactUrl + ": " + con.getResponseMessage()); |
| 325 | | | } |
| 326 | | | |
| 327 | 0 | | writeArtifactToFile(outputFile, con); |
| 328 | 0 | | } |
| 329 | 0 | | } |
| 330 | | | |
| 331 | | | private void writeArtifactToFile ( |
| 332 | | | final File outputFile, final HttpURLConnection con) |
| 333 | | | throws IOException, FileNotFoundException |
| 334 | | | { |
| 335 | 0 | | InputStream artifactInput = null; |
| 336 | 0 | | OutputStream artifactOutput = null; |
| 337 | | | try |
| 338 | | | { |
| 339 | 0 | | artifactInput = con.getInputStream(); |
| 340 | 0 | | artifactOutput = new FileOutputStream(outputFile); |
| 341 | | | |
| 342 | 0 | | IoUtil.copy(artifactInput, artifactOutput); |
| 343 | | | } |
| 344 | | | finally |
| 345 | | | { |
| 346 | 0 | | IoUtil.close(artifactInput); |
| 347 | 0 | | IoUtil.close(artifactOutput); |
| 348 | 0 | | } |
| 349 | 0 | | } |
| 350 | | | |
| 351 | | | |
| 352 | | | @throws |
| 353 | | | @throws |
| 354 | | | |
| 355 | | | |
| 356 | | | private void dumpLogFile () throws InterruptedException, IOException |
| 357 | | | { |
| 358 | 0 | | Thread.sleep(WAIT_PERIOD); |
| 359 | 0 | | final BuildFacade currentBuild |
| 360 | | | = getLuntServer().getLastBuild(mProjectName, mScheduleName); |
| 361 | | | |
| 362 | 0 | | final String buildLogUrlHtml = currentBuild.getBuildLogUrl(); |
| 363 | 0 | | log("Build log URL (HTML format): " + buildLogUrlHtml, Project.MSG_VERBOSE); |
| 364 | 0 | | final String buildLogUrlTxt = buildLogUrlHtml.substring(0, |
| 365 | | | buildLogUrlHtml.lastIndexOf(".html")) + ".txt"; |
| 366 | 0 | | log("Build log URL (Text format): " + buildLogUrlTxt, Project.MSG_VERBOSE); |
| 367 | | | |
| 368 | 0 | | final URL buildLog = new URL(buildLogUrlTxt); |
| 369 | 0 | | final HttpURLConnection con = (HttpURLConnection) buildLog.openConnection(); |
| 370 | 0 | | log("Got HTTP code " + con.getResponseCode(), Project.MSG_VERBOSE); |
| 371 | | | |
| 372 | 0 | | InputStream is = null; |
| 373 | | | try |
| 374 | | | { |
| 375 | 0 | | is = con.getInputStream(); |
| 376 | 0 | | final byte[] data = IoUtil.readFully(is); |
| 377 | 0 | | final String buildLogData = new String(data); |
| 378 | 0 | | log("===== START Build log =====", Project.MSG_INFO); |
| 379 | 0 | | log(buildLogData, Project.MSG_INFO); |
| 380 | 0 | | log("===== END Build log =====", Project.MSG_INFO); |
| 381 | | | } |
| 382 | | | finally |
| 383 | | | { |
| 384 | 0 | | IoUtil.close(is); |
| 385 | 0 | | } |
| 386 | 0 | | } |
| 387 | | | |
| 388 | | | private BuildParams getBuildParams () |
| 389 | | | { |
| 390 | 0 | | final BuildParams params = new BuildParams(); |
| 391 | | | |
| 392 | 0 | | params.setBuildNecessaryCondition("always"); |
| 393 | 0 | | params.setBuildType(Constants.BUILD_TYPE_CLEAN); |
| 394 | 0 | | params.setLabelStrategy(Constants.LABEL_NONE); |
| 395 | 0 | | params.setNotifyStrategy(Constants.NOTIFY_NONE); |
| 396 | 0 | | params.setPostbuildStrategy(Constants.POSTBUILD_NONE); |
| 397 | | | |
| 398 | 0 | | params.setTriggerDependencyStrategy( |
| 399 | | | Constants.TRIGGER_NONE_DEPENDENT_SCHEDULES); |
| 400 | | | |
| 401 | 0 | | return params; |
| 402 | | | } |
| 403 | | | |
| 404 | | | |
| 405 | | | @param |
| 406 | | | |
| 407 | | | private void checkNotNull (Object obj, String name) |
| 408 | | | { |
| 409 | 0 | | if (obj == null) |
| 410 | | | { |
| 411 | 0 | | throw new BuildException("Parameter " + name + " missing"); |
| 412 | | | } |
| 413 | 0 | | } |
| 414 | | | |
| 415 | | | |
| 416 | | | |
| 417 | | | |
| 418 | | | private void checkParameters () |
| 419 | | | { |
| 420 | 0 | | checkNotNull(mLuntUrl, "luntUrl"); |
| 421 | 0 | | checkNotNull(mUserName, "userName"); |
| 422 | 0 | | checkNotNull(mPassword, "password"); |
| 423 | 0 | | checkNotNull(mProjectName, "projectName"); |
| 424 | 0 | | checkNotNull(mScheduleName, "scheduleName"); |
| 425 | 0 | (4) | if (mArtifacts.size() > 0) |
| 426 | | | { |
| 427 | 0 | | if (mToDir == null) |
| 428 | | | { |
| 429 | 0 | | throw new BuildException("'toDir' must be set if artifacts are set"); |
| 430 | | | } |
| 431 | | | else |
| 432 | | | { |
| 433 | 0 | | AntTaskUtil.ensureDirectory(new File(mToDir)); |
| 434 | | | } |
| 435 | 0 | | if (!mWaitForSchedule) |
| 436 | | | { |
| 437 | 0 | | throw new BuildException( |
| 438 | | | "Can't retrieve artifacts when waitForBuild == false"); |
| 439 | | | } |
| 440 | | | } |
| 441 | 0 | | } |
| 442 | | | |
| 443 | | | private ILuntbuild getLuntServer () throws MalformedURLException |
| 444 | | | { |
| 445 | 0 | | if (mLuntServer == null) |
| 446 | | | { |
| 447 | 0 | | final HessianProxyFactory factory = new HessianProxyFactory(); |
| 448 | 0 | | factory.setUser(mUserName); |
| 449 | 0 | | factory.setPassword(mPassword); |
| 450 | 0 | | mLuntServer = (ILuntbuild) factory.create(ILuntbuild.class, mLuntUrl); |
| 451 | | | } |
| 452 | 0 | | return mLuntServer; |
| 453 | | | } |
| 454 | | | |
| 455 | | | |
| 456 | | | |
| 457 | | | |
| 458 | | | |
| 459 | 0 | | public static final class Artifact |
| 460 | | | { |
| 461 | | | private String mName; |
| 462 | | | |
| 463 | | | |
| 464 | | | @param |
| 465 | | | |
| 466 | | | public void setName (String name) |
| 467 | | | { |
| 468 | 0 | | mName = name; |
| 469 | 0 | | } |
| 470 | | | |
| 471 | | | |
| 472 | | | @return |
| 473 | | | |
| 474 | | | public String getName () |
| 475 | | | { |
| 476 | 0 | | return mName; |
| 477 | | | } |
| 478 | | | } |
| 479 | | | } |