Subversion Repositories DevTools

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
866 mhunt 1
package com.erggroup.buildtool.abt;
2
 
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileOutputStream;
6
import java.io.FileWriter;
7
import java.io.IOException;
8
 
9
import java.net.InetAddress;
10
import java.net.UnknownHostException;
11
 
12
import java.util.Iterator;
13
import java.util.Vector;
872 dpurdie 14
import java.util.Properties;
866 mhunt 15
 
16
import org.apache.log4j.Logger;
17
import org.apache.tools.ant.Project;
18
import org.apache.tools.ant.Target;
19
import org.apache.tools.ant.Task;
20
import org.apache.tools.ant.taskdefs.Execute;
21
import org.apache.tools.ant.taskdefs.Redirector;
22
import org.apache.tools.ant.BuildException;
868 mhunt 23
 
866 mhunt 24
import com.erggroup.buildtool.smtp.Smtpsend;
25
 
26
public class ABT extends Task
27
{
28
  // static fields
29
  // public
30
  // protected
31
  // private
32
 
33
  // fields
34
  // public
35
  // protected
36
  // private
37
  // System
38
  private String fs = System.getProperty( "file.separator" );
39
  private String ls = System.getProperty( "line.separator" );
40
  private String gbeMachtype = System.getenv("GBE_MACHTYPE");
41
  private String dpkg_archive = System.getenv("GBE_DPKG");
4280 dpurdie 42
  private String gatherMetricsOnly = System.getenv("GBE_GATHER_METRICS");
43
 
866 mhunt 44
  // ant built in
45
  private Project project;
46
  private String basedir;
47
  // global
4280 dpurdie 48
  private boolean master = false;
866 mhunt 49
  private String gbebuildfilter;
50
  private String mailServer;
51
  private String mailSender;
52
  // project related
53
  private String rtagId;
54
  private String daemon;
55
  private String release;
56
  private String buildtoolVersion;
57
  private String family;
58
  private String oldExtension;
59
  private String newExtension;
60
  // target related
61
  private Target target;
62
  private String packageVersionID;
63
  private String packageAlias;
64
  private String packageName;
65
  private String packageVersion;
4280 dpurdie 66
  private String packageFullVersion;
866 mhunt 67
  private String packageExtension;
68
  private String packageLoc;
924 dpurdie 69
  private String packageVcsTag;
866 mhunt 70
  private String directChange;
71
  private String doesNotRequireSourceControlInteraction;
908 mhunt 72
  private String testBuildInstruction;
866 mhunt 73
  private String generic;
74
  private String loc;
75
  private String unittests;
76
  private Vector<Depend> dependCollection = new Vector<Depend>();
77
  private Vector<Platform> platformCollection = new Vector<Platform>();
78
  private Vector<Jats> jatsCollection = new Vector<Jats>();
79
  private Vector<Ant> antCollection = new Vector<Ant>();
80
  private String owners;
4280 dpurdie 81
  private Machine mMachine;
82
 
866 mhunt 83
  // set up in execute
84
  private BuildStandards buildStandard = BuildStandards.JATS;
85
  private JavaVersions javaVersion = JavaVersions.NONE;
86
  private CompileTargets compileTarget = CompileTargets.NONE;
87
  // abt task result property
88
  private String propertyValue = "0";
89
  // output redirector
90
  private Redirector re;
91
  private File output;
92
  // exec thread
93
  private String[] command = new String[100];
94
  private int commandIndex = 0;
95
  private String cwd;
96
  private Execute thread;
97
  private File wd;
924 dpurdie 98
  private String newVcsTag;
866 mhunt 99
  private static final Logger mLogger = Logger.getLogger(ABT.class);
100
  private String fullyPublished = "no";
101
  private String hostname = new String("unknown");
102
 
103
  private void Log(String message1, String message2)
104
  {
105
    Log( message1 + message2, info );
106
  }
107
 
108
  private static final char debug = 0;
109
  private static final char info = 1;
110
  private static final char warn = 2;
111
  private static final char error = 3;
112
  private static final char fatal = 4;
113
 
114
  private void Log(String message, char facility)
115
  {
116
    if ( daemon != null )
117
    {
118
	    if ( facility == debug)
119
	    {
120
	      mLogger.debug(message);
121
	    }
122
	    else
123
	    if ( facility == info)
124
	    {
125
	      mLogger.info(message);
126
	    }
127
	    else
128
	    if ( facility == warn)
129
	    {
130
	      mLogger.warn(message);
131
	    }
132
	    else
133
	    if ( facility == error)
134
	    {
135
	      mLogger.error(message);
136
	    }
137
	    else
138
	    {
139
	      mLogger.fatal(message);
140
	    }
141
    }
142
 
143
    log( message );
144
  }
145
 
146
  private void initCommand()
147
  {
148
    for( int i = 0; i < 100; i++ )
149
    {
150
      command[ i ] = null;
151
    }
152
    commandIndex = 0;
153
  }
154
 
155
  private void addCommand( String c )
156
  {
157
    command[ commandIndex ] = c;
158
    commandIndex++;
159
  }
160
 
161
  private String printCommand( String addendum, boolean log )
162
  {
163
    String commandToPrint = "";
164
 
165
    for( int i = 0; i < 100; i++ )
166
    {
167
      if ( command[ i ] != null )
168
      {
169
        commandToPrint += command[ i ] + " ";
170
      }
171
      else
172
      {
173
        break;
174
      }
175
    }
176
 
177
    if ( addendum != null )
178
    {
179
      commandToPrint += addendum;
180
    }
181
 
182
    if ( log )
183
    {
184
      Log( commandToPrint, warn );
185
    }
186
 
187
    return commandToPrint;
188
  }
189
 
190
  private int runCommand( String message ) throws Exception
191
  {
192
    String cmd[] = new String[commandIndex];
193
    for ( int i = 0; i < commandIndex; i++ )
194
    {
195
      cmd[i] = command[i];
196
    }
197
    thread.setCommandline(cmd);
198
    printCommand( null, true );
199
    int retVal = thread.execute();
200
 
201
    if ( retVal == Execute.INVALID )
202
    {
203
      reportErrorException( "Failure to execute thread, internal ABT problem", 256 );
204
    }
205
    else
206
    {
207
      printCommand( " returned " + String.valueOf( retVal ), true );
208
 
209
      if ( message != null && retVal != 0 )
210
      {
211
        reportErrorException( message + " : " + printCommand( " returned " + String.valueOf( retVal ), true ), retVal );
212
      }
213
    }
214
 
215
    return retVal;
216
  }
217
 
4280 dpurdie 218
  /**
219
   * Check that one marker file has been published into dpkg_archive
220
   * 
221
   * @param archive     - Archive to test
222
   * @param tag         - Marker suffix
223
   * 
224
   * @return "yes" or "no" as text 
225
   */
226
  private String checkPublished(String archive, String tag) throws Exception
866 mhunt 227
  {
228
    String retVal = "no";
229
 
230
    if (archive == null)
231
    {
924 dpurdie 232
      reportErrorException( "GBE_DPKG environment variable not set", 256 );
866 mhunt 233
    }
234
 
4280 dpurdie 235
    String destination = archive + fs + packageName + fs + packageFullVersion;
866 mhunt 236
 
4280 dpurdie 237
    String filename = "built." + tag;
866 mhunt 238
 
2541 dpurdie 239
    //
240
    //  Due to NFS caching of small file requests the file may take
241
    //  a few seconds to appear. Try several times
4041 dpurdie 242
    //    5 seconds does not appear to be enough
4231 dpurdie 243
    //    Bumped to 60 seconds: 23-Sep-2013, except for escrow where its not used
2541 dpurdie 244
    //
245
    boolean found = false;
4231 dpurdie 246
    int   maxwait = (daemon == null) ? 1 : (60*2);
247
    for ( int ii = 0; ii < maxwait && !found; ii++)
866 mhunt 248
    {
4041 dpurdie 249
        File flag = new File( destination, filename );
2541 dpurdie 250
        found = flag.exists();
251
        if ( !found )
252
        {
4123 dpurdie 253
              try
254
              {
255
                String files;
256
                File folder = new File(destination);
257
                File[] listOfFiles = folder.listFiles();
258
                if ( listOfFiles == null )
259
                {
260
                  Log( "published. Destination not a dir: " + destination, fatal );
261
                }
262
                else
263
                {
264
 
265
                  for (int i = 0; i < listOfFiles.length; i++) 
266
                  {
267
                    if (listOfFiles[i].isFile())
268
                    {
269
                      files = listOfFiles[i].getName();
270
                      Log("published. DirContent:" + files , warn);
271
                    }
272
                  }
273
                }
274
              }
275
              catch( Exception e )
276
              {
277
                Log( "published. Display files caught Exception:" + e.getMessage(), fatal );
278
              }
279
 
280
 
281
           Thread.sleep(500);
4041 dpurdie 282
           Log("published. Wait for " + filename + ".(" + ii +")" , warn);
2541 dpurdie 283
        }
284
    }
285
 
286
    if ( found )
287
    {
866 mhunt 288
      retVal = "yes";
4041 dpurdie 289
      Log("published " + filename + " exists", warn);
866 mhunt 290
    }
291
    else
292
    {
4041 dpurdie 293
      Log("published " + filename + " does not exist", warn);
866 mhunt 294
    }
295
 
296
    return retVal;
297
  }
298
 
4280 dpurdie 299
  private void printPackage( FileWriter file, String packageName, String packageFullVersion ) throws IOException
866 mhunt 300
  {
301
    String line = packageName;
4280 dpurdie 302
    if ( packageFullVersion != null )
866 mhunt 303
    {
4280 dpurdie 304
      line += " " + packageFullVersion;
866 mhunt 305
    }
306
 
307
    line += ls;
308
    file.write( line );
309
  }
310
 
311
  private void copyFile( File source, File copy )
312
  {
313
    if ( source.exists() )
314
    {
315
      try
316
      {
317
        FileInputStream fis = new FileInputStream(source);
318
        FileOutputStream fos = new FileOutputStream(copy);
319
        byte[] buf = new byte[1024];
320
        int i = 0;
321
        while( (i=fis.read(buf)) != -1)
322
        {
323
          fos.write(buf, 0, i);
324
        }
325
        fis.close();
326
        fos.close();
327
      }
328
      catch( Exception e )
329
      {
330
        Log( "copyFile caught Exception", error );
331
      }
332
    }
333
  }
1351 dpurdie 334
 
335
  //
336
  //  Save the log file
337
  //    used on error build
338
  //    used on complete build
339
  //
340
  private boolean saveLogs()
866 mhunt 341
  {
342
    if ( daemon == null )
343
    {
344
      // do nothing more in escrow
1351 dpurdie 345
      return false;
866 mhunt 346
    }
1351 dpurdie 347
 
866 mhunt 348
    // persist the failure log to GBE_LOG/hostname/rtagid/timestamp
349
    String log = System.getenv("GBE_LOG");
350
 
351
    if ( log == null )
352
    {
353
      // cannot happen, GBE_LOG is checked in BuildDaemon at startup
1351 dpurdie 354
      return false;
866 mhunt 355
    }
356
 
357
    File destinationLog = new File( log );
358
    File destinationHostname = new File( destinationLog, hostname );
359
    File destinationRtagId = new File( destinationHostname, rtagId );
360
    // create directories
361
    new File( destinationRtagId, daemon).mkdirs();
362
 
363
    File destinationTimestamp = new File( destinationRtagId, daemon );
364
 
365
    if ( destinationTimestamp.exists() )
366
    {
367
      File buildsource = new File(rtagId + "build.xml");
368
      File logsource = new File(rtagId + ".log");
369
      File buildcopy = new File( destinationTimestamp, packageAlias + ".xml");
370
      copyFile( buildsource, buildcopy );
371
      File logcopy = new File( destinationTimestamp, packageAlias + ".log");
372
      copyFile( logsource, logcopy );
373
      Log("reportError persisting " + logcopy.getName(), warn);
374
    }
1351 dpurdie 375
 
376
    return true;
377
  }
378
 
379
  private void reportError(String error, int retVal)
380
  {
381
    propertyValue = String.valueOf( retVal );
382
 
383
    Log( error, ABT.error );
384
    Log( "reportError propertyValue = " + propertyValue.toString() + ", ", error);
385
 
386
    //  Save the logs
387
    //      Will return false in situations were we can't proceed
388
    if ( ! saveLogs() )
389
    {
390
        return;
391
    }
866 mhunt 392
 
4280 dpurdie 393
    if ( !ABTData.ownerCollection.isEmpty() || mailServer == null || mailSender == null )
866 mhunt 394
    {
395
      String unc = System.getenv("GBE_UNC");
396
 
397
      if ( unc == null )
398
      {
399
        // cannot happen, GBE_UNC is checked in BuildDaemon at startup
400
        return;
401
      }
402
 
922 dpurdie 403
      String href = hostname + "/" + rtagId + "/" + daemon + "/" + packageAlias + ".log";
404
      unc += "/" + href;
405
 
866 mhunt 406
      String body = new String();
407
      body = "Release: " + release + "<p>" +
408
             "Package: " + packageName + "<p>" + 
409
             "Build machine: " + hostname + "<p>" +
410
             "GBE_MACHTYPE: " + gbeMachtype + "<p>" +
922 dpurdie 411
             "Error: " + error + "<p>" +
412
             "Log File: <a href=\"" + unc + "\">" + href + "</a> ... URL expires in 5 working days time<p><hr>";
866 mhunt 413
 
414
      try
415
      {
922 dpurdie 416
        Smtpsend.send( mailServer,  // mailServer
417
                       mailSender,  // source
418
                       owners,      // target
419
                       null,        // cc
420
                       null,        // bcc
866 mhunt 421
                       "BUILD FAILURE on package " + packageName, // subject
922 dpurdie 422
                       body,        // body
423
                       null         // attachment
866 mhunt 424
                      );
425
      }
426
      catch( Exception e )
427
      {
428
        // not fatal
429
        Log( "reportError caught Exception", warn);
430
      }
431
    }
432
 
433
  }
434
 
435
  private void reportErrorException(String error, int retVal) throws Exception
436
  {
437
    reportError( error, retVal );
438
    throw new Exception( hostname );
439
  }
440
 
4280 dpurdie 441
  private void genbuild() throws Exception
866 mhunt 442
  {
443
    if ( master )
444
    {
445
      // gather metrics
886 mhunt 446
      // jats -locatefile=.jats.packageroot etool jats_metrics -mode=init -rootdir=.
447
      // -locate=.jats.packageroot causes jats to cd to the folder containing the file .jats.packageroot
448
      // this file is created in the -path folder by jats release -extract
449
      // DEVI 51370 it is essential for metrics branch calculation the metrics utility is started within a static view
866 mhunt 450
      initCommand();
451
      addCommand( "jats" );
886 mhunt 452
      addCommand( "-locatefile=.jats.packageroot" );
866 mhunt 453
      addCommand( "etool" );
454
      addCommand( "jats_metrics" );
455
      addCommand( "-mode=init" );
456
      addCommand( "-rootdir=." );
457
 
458
      runCommand( "Failure to collect metrics" );
459
    }
460
 
868 mhunt 461
    if ( gatherMetricsOnly != null )
462
    {
463
      return;
464
    }
872 dpurdie 465
 
466
    //
467
    //  Create build files with updated dependency information
468
    //  In escrow mode : No new dependency information
894 mhunt 469
    //  In daemon mode :
470
    //      Ripple     : New dependency information
471
    //      Verify     : Existing dependency information
872 dpurdie 472
    //
894 mhunt 473
    //  In all modes  : ANT builds need an auto.xml
872 dpurdie 474
    //
894 mhunt 475
    //  In order to create auto.xml/auto.pl with package dependencies need to
872 dpurdie 476
    //  create file with dependency data (auto.cfg) only because its
477
    //  needed by "jats_rewrite"
478
    //
479
    File cfg = null;
480
    boolean  create_file = false;
894 mhunt 481
    if ( daemon != null )
866 mhunt 482
    {
872 dpurdie 483
      create_file = true;
484
 
894 mhunt 485
      // always generate auto.cfg
872 dpurdie 486
      cfg = new File( cwd + fs + "auto.cfg" );
866 mhunt 487
      cfg.createNewFile();
488
 
489
      if ( !cfg.exists() || !cfg.canWrite() )
490
      {
491
        reportErrorException( "Failed to open auto.cfg", 263 );
492
      }
493
 
494
      try
495
      {
496
        FileWriter cfgfw = new FileWriter(cfg);
4280 dpurdie 497
        printPackage( cfgfw, packageName, packageFullVersion );
866 mhunt 498
 
499
        // provide releasemanager.projectname=<name[0]> and releasemanager.releasename=<name[1]>
500
        String[] name = release.split( " > ", 2 );
501
 
502
        if ( name.length == 2 )
503
        {
4280 dpurdie 504
          printPackage( cfgfw, "releasemanager.projectname=" + name[0], null );
505
          printPackage( cfgfw, "releasemanager.releasename=" + name[1], null );
866 mhunt 506
        }
507
 
508
        for (Iterator<Depend> it=dependCollection.iterator(); it.hasNext(); )
509
        {
510
          Depend depend = it.next();
4280 dpurdie 511
          printPackage( cfgfw, depend.getPackage_Alias(), null );
866 mhunt 512
        }
513
 
514
        cfgfw.close();
515
      }
516
      catch( IOException e )
517
      {
518
        reportErrorException( "Failure to write to auto.cfg", 263 );
519
      }
872 dpurdie 520
    }
521
    else
522
    if ( buildStandard == BuildStandards.ANT )
523
    {
524
      // Have not created an auto.xml with updated dependencies
525
      // Ant build still would like an auto.xml file
526
      //
527
      // ie packages with a line <import file="auto.xml" optional="true"/> are fine
528
      //    but in escrow and rebuild we still need an auto.xml to be kind
529
      //
530
      create_file = true;
531
    }
532
 
533
    //
534
    //  Create the auto.pl/auto.xml file if required
535
    //
536
    if ( create_file )
537
    {
866 mhunt 538
      initCommand();
539
      addCommand( "jats" );
872 dpurdie 540
 
866 mhunt 541
      if ( buildStandard == BuildStandards.ANT )
542
      {
872 dpurdie 543
        addCommand( "-locatefile=" + packageName + ".xml" );
866 mhunt 544
        addCommand( "etool" );
545
        addCommand( "jats_rewrite" );
546
        addCommand( "-infile=" + packageName + "depends.xml" );
547
        addCommand( "-outfile=auto.xml" );
548
      }
549
      else
550
      {
551
        addCommand( "-locatepkg=" + packageLoc );
552
        addCommand( "etool" );
553
        addCommand( "jats_rewrite" );
554
        addCommand( "-infile=build.pl" );
555
        addCommand( "-outfile=auto.pl" );
556
      }
872 dpurdie 557
 
558
      if ( cfg != null )
559
      {
560
        addCommand( "-config=" + cfg.getAbsolutePath() );
894 mhunt 561
 
908 mhunt 562
        if ( doesNotRequireSourceControlInteraction != null && testBuildInstruction.compareTo("0") == 0 )
894 mhunt 563
        {
564
            // Have a config file, but we are not rippling the package
565
            // just rebuilding it. Validate the versions in Release Manager
566
            // against those in the build files
567
            addCommand( "-validate");
568
        }
872 dpurdie 569
      }
570
      else
571
      {
572
        addCommand( "-noconfig");
573
      }
866 mhunt 574
 
575
      if ( oldExtension != null )
576
      {
577
        addCommand( "-oldproject=" + oldExtension );
578
        addCommand( "-newproject=" + newExtension );
579
      }
580
 
581
      runCommand( "Failure to generate auto.pl potentially caused by missing dependencies in the Release Manager database" );
582
    }
583
  }
584
 
4280 dpurdie 585
  private void jatsBuildPackage() throws Exception
866 mhunt 586
  {
872 dpurdie 587
    //--------------------------------------------------------------------------
588
    //  Build Phase
589
    //  Create build environment
590
 
866 mhunt 591
    initCommand();
872 dpurdie 592
    addCommand( "jats" );
866 mhunt 593
 
872 dpurdie 594
    if ( gbebuildfilter != null )
595
    {
596
      addCommand( "-buildfilter" );
597
      addCommand( gbebuildfilter);
598
    }
599
 
866 mhunt 600
    if ( buildStandard == BuildStandards.ANT )
601
    {
872 dpurdie 602
      addCommand( "-locatefile=" + packageName + ".xml" );
603
      addCommand( "abt" );
604
      addCommand( "-java=" + javaVersion.getJavaVersion() );
605
      addCommand( "-buildfile=" + packageName + ".xml" );
866 mhunt 606
      addCommand( "build" );
607
    }
608
    else
609
    {
610
      addCommand( "-locatepkg=" + packageLoc );
611
      addCommand( "build" );
872 dpurdie 612
      addCommand( "--package" );
866 mhunt 613
 
614
      if ( family.compareTo( "windows" ) == 0 )
615
      {
616
        addCommand( "--cache" );
617
      }
618
    }
619
 
620
    runCommand( "Failure to set up sandbox" );
621
 
872 dpurdie 622
    //--------------------------------------------------------------------------
623
    //  Make Package phase
624
    //  Create the package (make)
625
 
866 mhunt 626
    initCommand();
872 dpurdie 627
    addCommand( "jats" );
628
 
629
    if ( gbebuildfilter != null )
630
    {
631
      addCommand( "-buildfilter" );
632
      addCommand( gbebuildfilter);
633
    }
634
 
866 mhunt 635
    if ( buildStandard == BuildStandards.ANT )
636
    {
872 dpurdie 637
      addCommand( "-locatefile=" + packageName + ".xml" );
638
      addCommand( "abt" );
639
      addCommand( "-java=" + javaVersion.getJavaVersion() );
640
      addCommand( "-buildfile=" + packageName + ".xml" );
866 mhunt 641
      addCommand( "make_package" );
642
    }
643
    else
644
    {
645
      addCommand( "-locatepkg=" + packageLoc );
646
      addCommand( "make" );
872 dpurdie 647
      addCommand( "NODEPEND=1" );
648
 
866 mhunt 649
      if ( compileTarget == CompileTargets.PROD )
650
      {
651
        addCommand( "prod" );
652
        addCommand( "package_prod" );
653
      }
872 dpurdie 654
      else if ( compileTarget == CompileTargets.DEBUG )
866 mhunt 655
      {
656
        addCommand( "debug" );
657
        addCommand( "package_debug" );
658
      }
659
      else
660
      {
661
        addCommand( "all" );
662
      }
663
    }
872 dpurdie 664
 
665
    runCommand( "Failure to generate derived files" );
866 mhunt 666
 
667
 
872 dpurdie 668
    //--------------------------------------------------------------------------
669
    //  Unit tests phase
670
    //  Run autonimous unit tests
671
 
866 mhunt 672
    if ( unittests != null )
673
    {
674
      initCommand();
872 dpurdie 675
      addCommand( "jats" );
676
 
677
      if ( gbebuildfilter != null )
678
      {
679
        addCommand( "-buildfilter" );
680
        addCommand( gbebuildfilter);
681
      }
682
 
866 mhunt 683
      if ( buildStandard == BuildStandards.ANT )
684
      {
872 dpurdie 685
        addCommand( "-locatefile=" + packageName + ".xml" );
686
        addCommand( "abt" );
687
        addCommand( "-java=" + javaVersion.getJavaVersion() );
688
        addCommand( "-buildfile=" + packageName + ".xml" );
866 mhunt 689
        addCommand( "run_tests" );
690
      }
691
      else
692
      {
693
        addCommand( "-locatepkg=" + packageLoc );
872 dpurdie 694
        addCommand( "make" );
866 mhunt 695
        if ( compileTarget == CompileTargets.PROD )
696
        {
697
          addCommand( "run_unit_tests_prod" );
698
        }
872 dpurdie 699
        else if ( compileTarget == CompileTargets.DEBUG )
866 mhunt 700
        {
701
          addCommand( "run_unit_tests_debug" );
702
        }
703
        else
704
        {
705
          addCommand( "run_unit_tests" );
706
        }
707
      }
872 dpurdie 708
 
866 mhunt 709
      runCommand( "Failure to run unit tests" );
710
    }
872 dpurdie 711
 
712
    //--------------------------------------------------------------------------
713
    //  create_dpkg phase
714
    //  Publish the package to dpkg_archive
715
    //
716
 
866 mhunt 717
    initCommand();
718
    addCommand( "jats" );
719
    if ( buildStandard == BuildStandards.ANT )
720
    {
872 dpurdie 721
      addCommand( "-locatefile=" + packageName + ".xml" );
866 mhunt 722
    }
723
    else
724
    {
725
      addCommand( "-locatepkg=" + packageLoc );
726
    }
727
    addCommand( "create_dpkg" );
728
    addCommand( "-o" );
729
    addCommand( "-m" );
730
    addCommand( "-pname" );
731
    addCommand( packageName );
732
    addCommand( "-pversion" );
4280 dpurdie 733
    addCommand( packageFullVersion );
866 mhunt 734
 
735
    if ( generic != null )
736
    {
737
      addCommand( "-generic" );
738
    }
739
 
740
    runCommand( "Failure to publish to archive" );
741
  }
4280 dpurdie 742
 
743
  /**   Check if the package has been Published on all the required machines
744
   *    If its a 'generic' package then a tag of built.generic will have been placed in the package
745
   *    Otherwise each build machine will place a built.<MachName> file in the package
746
   *  
747
   *    All we know is which MachTypes we build for. This needs to be expanded into
748
   *    a list of MachineNames. Thus a build-set with multiple machines of the same MachType
749
   *    can indicate build failures.
750
   */
751
  private void publish() throws Exception
752
  {  
866 mhunt 753
 
4280 dpurdie 754
    //  Metrics gathering (only) does not create anything in dpkg_archive
755
    if ( gatherMetricsOnly != null )
866 mhunt 756
    {
4280 dpurdie 757
        fullyPublished = "yes";
758
        return;
866 mhunt 759
    }
4280 dpurdie 760
 
761
    if ( generic != null )
762
    {
763
        //  Generic - only one maker file
764
        fullyPublished = checkPublished( dpkg_archive, "generic" );
765
    }
766
    else
767
    {
768
        //  Multi machine package
769
        //  Scan required target machines artifacts
770
        //  Expand from MachType to one or more MachName's
771
        for (Iterator<Platform> ip=platformCollection.iterator(); ip.hasNext(); )
772
        {
773
          Platform platform = ip.next();
774
          for (Iterator<Machine> it=ABTData.machineCollection.iterator(); it.hasNext(); )
775
          {
776
            Machine ma = it.next();
777
            if (ma.getMachtype().compareTo(platform.getGbe_Machtype()) == 0 )
778
            {
779
                fullyPublished = checkPublished( dpkg_archive, ma.getName() );
780
                if ( fullyPublished.compareTo("no") == 0 )
781
                {
782
                  break;
783
                }
784
            }
785
          }
786
        }
787
    }
866 mhunt 788
 
4280 dpurdie 789
    //  Detect problem with zero length descpkg file
790
    //      May be caused by write clash from multiple machines
791
    //      Don't know
792
    //
793
    if ( fullyPublished.compareTo("yes") == 0 )
868 mhunt 794
    {
4280 dpurdie 795
        if (dpkg_archive == null)
796
        {
797
          reportErrorException( "GBE_DPKG environment variable not set", 256 );
798
        }
799
 
800
        String destination = dpkg_archive + fs + packageName + fs + packageFullVersion;
801
        String filename = "descpkg";
802
 
803
        File flag = new File( destination, filename );
804
 
805
        //  File Must
806
        //      Exist
807
        //      Be a File
808
        //      Have a length. Problem appers to be zero length file
809
        //
810
        boolean found = false;
811
        if (!flag.exists() ) {
812
            Log( "published. Descpkg Sanity Test: File not found", fatal );
813
 
814
        } else if ( !flag.isFile()) {
815
            Log( "published. Descpkg Sanity Test: Is not a file", fatal );
816
 
817
        } else if ( flag.length() <= 0 ) {
818
            Log( "published. Descpkg Sanity Test: Zero length File", fatal );
819
 
820
        } else {
821
            Log( "published. Descpkg Sanity Test: Passed", info );
822
            found = true;
823
        }
824
 
825
        if (!found) {
826
            fullyPublished = "no";
827
        }
868 mhunt 828
    }
866 mhunt 829
 
830
    if ( fullyPublished.compareTo("yes") == 0 && daemon != null )
831
    {
832
      if ( doesNotRequireSourceControlInteraction == null )
833
      {
872 dpurdie 834
        //
835
        //  Save modified build files
836
        //  Label the resultant files
837
        //  Create a command of the form:
924 dpurdie 838
        //      jats etool jats_vcssave_build
839
        //          -infile     auto.xml/auto.pl
840
        //          -outfile    xxxdepends.xml/build.pl
841
        //          -pname      package_name
842
        //          -pversion   package_version
843
        //          -infofile   path_to_info_file
844
        //          -wiplabel   Existing WIP label (optional)
845
        //          -baselabel  View label
846
 
872 dpurdie 847
        Log("publish save build files", warn);
848
 
849
        File saveinfo = new File(basedir + fs + rtagId + "abtinfo.txt");
850
        if ( saveinfo.exists() )
866 mhunt 851
        {
872 dpurdie 852
          saveinfo.delete();
866 mhunt 853
        }
854
 
855
        initCommand();
856
        addCommand( "jats" );
872 dpurdie 857
        if ( buildStandard == BuildStandards.ANT )
866 mhunt 858
        {
872 dpurdie 859
          addCommand( "-locatefile=" + packageName + ".xml" );
860
        }
861
        else
862
        {
866 mhunt 863
          addCommand( "-locatepkg=" + packageLoc );
864
        }
872 dpurdie 865
 
866 mhunt 866
        addCommand( "etool" );
924 dpurdie 867
        addCommand( "jats_vcssave_build" );
872 dpurdie 868
 
866 mhunt 869
        if ( buildStandard == BuildStandards.ANT )
870
        {
871
          addCommand( "-infile" );
872
          addCommand( "auto.xml" );
873
          addCommand( "-outfile" );
874
          addCommand( packageName + "depends.xml" );
875
        }
872 dpurdie 876
        else
877
        {
878
          addCommand( "-infile" );
879
          addCommand( "auto.pl" );
880
          addCommand( "-outfile" );
881
          addCommand( "build.pl" );
882
        }
866 mhunt 883
 
872 dpurdie 884
        addCommand( "-pname" );
885
        addCommand( packageName );
886
 
887
        addCommand( "-pversion" );
4280 dpurdie 888
        addCommand( packageFullVersion );
872 dpurdie 889
 
890
        if ( directChange != null )
891
        {
926 dpurdie 892
          addCommand( "-isawip" );
872 dpurdie 893
        }
926 dpurdie 894
 
895
        addCommand( "-infofile" );
896
        addCommand( saveinfo.getAbsolutePath() );
872 dpurdie 897
 
926 dpurdie 898
        addCommand( "-baselabel" );
899
        addCommand( packageVcsTag );
924 dpurdie 900
 
901
 
866 mhunt 902
        if ( oldExtension != null && newExtension != null && packageExtension.compareTo( newExtension ) == 0 )
903
        {
904
          // only branch build.pl where its extension is being migrated
905
          addCommand( "-branch" );
906
          addCommand( "project_migration_branch" );
907
          addCommand( "-newbranch" );
908
        }
872 dpurdie 909
 
910
        runCommand( "Failure to save build file changes into source control" );
911
 
912
        //
898 mhunt 913
        //  The command creates an info file(saveinfo) to pass information back
872 dpurdie 914
        //  to the caller (me). This is a 'properties' file
915
        //  Need to extract the following information
924 dpurdie 916
        //    newVcsTag - which is held as VCS.tag
872 dpurdie 917
        //
898 mhunt 918
        Log("publish read info file", info);
872 dpurdie 919
 
920
        Properties properties = new Properties();
921
        try
866 mhunt 922
        {
872 dpurdie 923
          properties.load(new FileInputStream(saveinfo));
866 mhunt 924
        }
872 dpurdie 925
        catch (IOException e)
866 mhunt 926
        {
872 dpurdie 927
          reportErrorException( "Failed to open save_build info file", 262 );
866 mhunt 928
        }
872 dpurdie 929
 
924 dpurdie 930
        newVcsTag = properties.getProperty("VCS.tag" );
931
        if ( newVcsTag == null )
872 dpurdie 932
        {
924 dpurdie 933
          reportErrorException( "Save_build info. Missing property:VCS.tag", 262 );
872 dpurdie 934
        }
924 dpurdie 935
        Log("publish read info file. Label:" + newVcsTag , info);
872 dpurdie 936
      }
866 mhunt 937
    }
938
  }
939
 
4280 dpurdie 940
/** determineBuildStandard
941
 *  Process the current target and set up
942
 *      buildStandard
943
 *      compileTarget
944
 *      javaVersion
945
 */
946
  private void determineBuildStandard() throws Exception
947
  {
948
      // Scan the platform collection looking for an entry that matches the current machine
949
      // Remember the index - as it used to locate a matching entry in the 
950
      // jatsCollection or jatsCollection
951
      // 
952
      // Just wanting to set up
953
      //    buildStandard and one of
954
      //        compileTarget
955
      //    OR  javaVersion
956
      //
957
      int platformIndex = 0;
958
      for ( Iterator<Platform> it = platformCollection.iterator(); it.hasNext(); )
959
      {
960
        Platform platform = it.next();
961
        platformIndex++;
962
 
963
        if ( gbeMachtype.compareTo( platform.getGbe_Machtype() ) == 0 )
964
        {
965
          break;
966
        }
967
      }
968
 
969
      int index = 0;
970
      for (Iterator<Jats> it=jatsCollection.iterator(); it.hasNext(); )
971
      {
972
        Jats j = it.next();
973
        index++;
974
 
975
        if ( platformIndex == index )
976
        {
977
          String target = j.getTarget();
978
          if ( target.compareTo( "none" ) == 0 )
979
          {
980
            compileTarget = CompileTargets.NONE;
981
          }
982
          else
983
          if ( target.compareTo( "production" ) == 0 )
984
          {
985
            compileTarget = CompileTargets.PROD;
986
          }
987
          else
988
          if ( target.compareTo( "debug") == 0 )
989
          {
990
            compileTarget = CompileTargets.DEBUG;
991
          }
992
          else
993
          if ( target.compareTo( "all" ) == 0 )
994
          {
995
            compileTarget = CompileTargets.ALL;
996
          }
997
          else
998
          {
999
            reportErrorException( "Failure, unsupported build environment " + target + ", internal ABT problem", 256 );
1000
          }
1001
          Log( "execute buildStandard: ", buildStandard.getBuildStandard() );
1002
          Log( "execute compileTarget: ", compileTarget.getCompileTarget() );
1003
          break;
1004
        }
1005
      }
1006
 
1007
      for (Iterator<Ant> it=antCollection.iterator(); it.hasNext(); )
1008
      {
1009
        Ant a = it.next();
1010
        index++;
1011
 
1012
        if ( platformIndex == index )
1013
        {
1014
          buildStandard = BuildStandards.ANT;
1015
          String version = a.getJava();
1016
          if ( version.compareTo( "none" ) == 0 )
1017
          {
1018
            javaVersion = JavaVersions.NONE;
1019
          }
1020
          else
1021
          if ( version.compareTo( "1.4" ) == 0 )
1022
          {
1023
            javaVersion = JavaVersions.FOUR;
1024
          }
1025
          else
1026
          if ( version.compareTo( "1.5" ) == 0 )
1027
          {
1028
            javaVersion = JavaVersions.FIVE;
1029
          }
1030
          else
1031
          if ( version.compareTo( "1.6" ) == 0 )
1032
          {
1033
            javaVersion = JavaVersions.SIX;
1034
          }
1035
          else
1036
          if ( version.compareTo( "1.7" ) == 0 )
1037
          {
1038
            javaVersion = JavaVersions.SEVEN;
1039
          }
1040
          else
1041
          {
1042
            reportErrorException( "Failure, unsupported build environment " + version + ", internal ABT problem", 256 );
1043
          }
1044
          Log( "execute buildStandard: ", buildStandard.getBuildStandard() );
1045
          Log( "execute javaVersion: ", javaVersion.getJavaVersion() );
1046
          break;
1047
        }
1048
      }
1049
  }
1050
 
1051
  /** execute
1052
   *  The Aant Task entry point. This is invoked to perform the
1053
   *  targets action
1054
   */
866 mhunt 1055
  public void execute()
1056
  {
1057
    boolean errorReported = false;
1058
    boolean benign = false;
4280 dpurdie 1059
 
1060
 
866 mhunt 1061
    try
1062
    {
1063
      re = new Redirector(this);
1064
 
1065
      // early ant built in
1066
      project = getProject();
1067
      // required by the Execute API on non win32 platforms, don't ask because I don't know
1068
      project.setProperty("ant.home", System.getenv("ANT_HOME"));
4280 dpurdie 1069
 
866 mhunt 1070
      daemon = project.getProperty("abt_daemon");
1071
      if ( daemon != null )
1072
      {
1073
        Log("execute daemon: yes", info);
1074
      }
4280 dpurdie 1075
 
1076
      // Get the current hostname
1077
      // Perhaps this should be injected by the calling task
1078
      // Daemon Mode:
1079
      //    When sending emails
1080
      //    Identify the target machine parameters
1081
      // Escrow Mode:
1082
      //    Deterime escrow build components    
1083
      try
1084
      {
1085
        InetAddress local = InetAddress.getLocalHost();
1086
        hostname = local.getHostName();
1087
      }
1088
      catch( UnknownHostException e )
1089
      {
1090
        // do nothing, shouldn't happen, but hostname will be "unknown" if it does
1091
      }
1092
 
866 mhunt 1093
      // early target related
1094
      target = getOwningTarget();
1095
      packageAlias = target.getName();
924 dpurdie 1096
      packageVcsTag = project.getProperty(packageAlias + "packagevcstag");
1097
      Log("execute packageVcsTag: ", packageVcsTag);
866 mhunt 1098
 
924 dpurdie 1099
      if ( packageVcsTag == null )
866 mhunt 1100
      {
1101
        // this abt task has no properties, by design
1102
        // future use to estimate build completion time
898 mhunt 1103
        Log("execute packageAlias: " + packageAlias, info);
866 mhunt 1104
        return;
1105
      }
1106
 
898 mhunt 1107
      Log("execute packageAlias: " + packageAlias, warn);
1108
 
866 mhunt 1109
      // ant built in
1110
      basedir = project.getProperty("basedir");
1111
      Log("execute basedir: ", basedir);
1112
 
1113
      // global
4280 dpurdie 1114
 
1115
      if ( gbeMachtype == null ) {
1116
        reportErrorException( "GBE_MACHTYPE environment variable not set", 256 );
866 mhunt 1117
      }
4280 dpurdie 1118
 
866 mhunt 1119
      mailServer = project.getProperty("abt_mail_server");
1120
      if ( mailServer == null )
1121
      {
4280 dpurdie 1122
          reportErrorException( "Missing ANT property: abt_mail_server", 256 );
866 mhunt 1123
      }
1124
      Log("execute mailServer: ", mailServer);
1125
 
1126
      mailSender = project.getProperty( "abt_mail_sender" );
1127
      if ( mailSender == null )
1128
      {
4280 dpurdie 1129
          reportErrorException( "Missing ANT property: abt_mail_sender", 256 );
866 mhunt 1130
      }
1131
      Log("execute mailSender: ", mailSender);
4280 dpurdie 1132
 
1133
      for (Iterator<Owner> it=ABTData.ownerCollection.iterator(); it.hasNext(); )
1134
      {
1135
        Owner owner = it.next();
1136
 
1137
        if ( owners == null )
1138
        {
1139
          owners = new String();
1140
        }
1141
        else
1142
        {
1143
          owners += ",";
1144
        }
1145
        owners += owner.getEmail();
1146
        Log("execute owner: ", owner.getEmail());
1147
      }
1148
      Log("execute owners: ", owners);
1149
 
866 mhunt 1150
 
1151
      // project related
1152
      rtagId = project.getProperty("abt_rtag_id");
1153
      Log("execute rtagId: ", rtagId);
1154
 
1155
      release = project.getProperty("abt_release");
1156
      Log("execute release: ", release);
4212 dpurdie 1157
 
866 mhunt 1158
      buildtoolVersion = project.getProperty("abt_buildtool_version");
1159
      Log("execute buildtoolVersion: ", buildtoolVersion);
4212 dpurdie 1160
 
866 mhunt 1161
      family = project.getProperty("abt_family");
1162
      Log("execute family: ", family);
4212 dpurdie 1163
 
866 mhunt 1164
      oldExtension = project.getProperty("abt_old_extension");
1165
      Log("execute oldExtension: ", oldExtension);
4212 dpurdie 1166
 
866 mhunt 1167
      newExtension = project.getProperty("abt_new_extension");
1168
      Log("execute newExtension: ", newExtension);
4212 dpurdie 1169
 
908 mhunt 1170
      testBuildInstruction = project.getProperty("abt_test_build_instruction");
1171
      Log("execute testBuildInstruction: ", testBuildInstruction);
4280 dpurdie 1172
 
1173
      // Locate Machine Information for this machine
1174
      //    It contains the build filter for the current machine
1175
      //    Only present in daemon builds
1176
      //
1177
      for (Iterator<Machine> it=ABTData.machineCollection.iterator(); it.hasNext(); )
1178
      {
1179
        Machine ma = it.next();
1180
        if (ma.getName().compareTo(hostname) == 0 )
1181
        {
1182
            mMachine = ma;
1183
            Log("execute machine: ", mMachine.getName() + ", Type:" + mMachine.getMachtype() + ", Class:" + mMachine.getMachclass() + ", Master:" + mMachine.getMaster() ); 
1184
            break;
1185
        }
1186
      }
1187
 
1188
      if ( (daemon != null) && (mMachine == null) ) {
1189
          reportErrorException( "Cannot find Machine entry for: " + hostname, 256 );
1190
      }
908 mhunt 1191
 
4280 dpurdie 1192
      //  Setup machine specific information
1193
      //    Build Filter
1194
      //    Master Mode
1195
      if (mMachine != null) 
1196
      {
1197
          if (mMachine.getMaster() != null)
1198
          {
1199
            master = true;
1200
            Log("execute master: ", "true");
1201
          }
1202
 
1203
          gbebuildfilter = mMachine.getBuildfilter(); 
1204
          if ( gbebuildfilter != null )
1205
          {
1206
            //
1207
            // Ensure the Execute task passes gbebuildfilter as one parameter...
1208
            // Replace spaces with commas
1209
            //
1210
            gbebuildfilter = gbebuildfilter.replace( " ", ",");
1211
            Log("execute gbebuildfilter: ", gbebuildfilter);
1212
          }
1213
      }
1214
 
908 mhunt 1215
      if ( testBuildInstruction == null )
1216
      {
1217
        // use a meaningful default
1218
        testBuildInstruction = "0";
1219
      }
866 mhunt 1220
 
1221
      // redirect all thread (therefore jats) output to the output file in the basedir
1222
      if ( daemon != null )
1223
      {
1224
        // thread logging is done to <rtagId>thread.log and is continually re-used every build
1225
        // full thread and non thread (ie abt logging) is done to the DefaultLogger set up by the build daemons, <rtagId>.log
1226
        // this file is copied on a build failure to cwd (if it exists) and is continually re-used every build
1227
        output = new File( rtagId + "thread.log" );
1228
        // capture full logging
1229
        re.setAlwaysLog(true);
1230
      }
1231
      else
1232
      {
1233
        output = new File( packageAlias + ".log" );
1234
      }
1235
 
1236
      re.setOutput( output );
1237
      thread = new Execute(re.createHandler());
1238
      thread.setAntRun( project );
1239
      thread.setVMLauncher(false);
1240
 
1241
      // must set up package name and owners before reportError
1242
      packageName = project.getProperty(packageAlias + "packagename");
1243
      Log("execute packageName: ", packageName);
1244
 
4280 dpurdie 1245
      //-----------------------------------------------------------------------
1246
      //    AbtSetUp
1247
      //
866 mhunt 1248
      if ( packageAlias.compareTo("AbtSetUp") == 0 )
1249
      {
924 dpurdie 1250
        // create rtag_id directory from scratch
1251
        File rId = new File( rtagId );
1252
        deleteDirectory( rId );
1253
        rId.mkdirs();
866 mhunt 1254
 
924 dpurdie 1255
        // jats jats_vcsrelease -extract -label=pkgVcsTag -path=src_path -root=rtag_id/timestamp -tag=timestamp -noprefix
1256
        initCommand();
1257
        addCommand( "jats" );
1258
        addCommand( "jats_vcsrelease" );
1259
        addCommand( "-extract" );
1260
        addCommand( "-label=" + packageVcsTag );
1261
        addCommand( "-root=" + rtagId );
1262
        addCommand( "-tag=" + daemon );
1263
        addCommand( "-noprefix" );
1264
        addCommand( "-verbose=2" );
866 mhunt 1265
 
924 dpurdie 1266
        runCommand( "Failure to extract source code from source control" );
1267
        return;
866 mhunt 1268
      }
1269
 
4280 dpurdie 1270
      //-----------------------------------------------------------------------
1271
      //    AbtTearDown
1272
      //
866 mhunt 1273
      if ( packageAlias.compareTo("AbtTearDown") == 0 )
1274
      {
924 dpurdie 1275
        // tear the build view down, regardless of build error
866 mhunt 1276
        wd = new File( basedir );
1277
 
1278
        // only now change the thread working directory back
1279
        thread.setWorkingDirectory( wd );
1280
 
868 mhunt 1281
        File buildDirectory = new File(wd + getProject().getProperty("abt_package_location"));
866 mhunt 1282
 
1283
        if ( buildDirectory.exists() )
1284
        {
4212 dpurdie 1285
          // jats jats_vcsrelease -label pkgTag -root=rtag_id/timestamp -tag=timestamp -noprefix -delete=2
866 mhunt 1286
          initCommand();
1287
          addCommand( "jats" );
924 dpurdie 1288
          addCommand( "jats_vcsrelease" );
1289
          addCommand( "-label=" + packageVcsTag );
878 mhunt 1290
          addCommand( "-root=" + rtagId );
866 mhunt 1291
          addCommand( "-tag=" + daemon );
1292
          addCommand( "-noprefix" );
924 dpurdie 1293
          addCommand( "-delete=2" );
1294
          addCommand( "-verbose=2" );
1295
 
866 mhunt 1296
          runCommand( "Failure to remove source code extraction" );
1297
        }
1298
 
878 mhunt 1299
        // delete the rtag directory
1300
        deleteDirectory( new File( rtagId ) );
1351 dpurdie 1301
 
1302
        // Always save the log files
1303
        saveLogs();
1304
 
866 mhunt 1305
        return;      
1306
      }
1307
 
4280 dpurdie 1308
      // target related    
1309
      packageVersion = project.getProperty(packageAlias + "packageversion");
1310
      Log("execute packageVersion: ", packageVersion);
1311
 
1312
      packageExtension = project.getProperty(packageAlias + "packageextension");
1313
      Log("execute packageExtension: ", packageExtension);
1314
 
1315
      packageFullVersion = packageVersion + packageExtension;
1316
      Log("execute packageFullVersion: ", packageFullVersion);
1317
 
1318
      packageLoc = packageName + packageExtension;
1319
      Log("execute packageLoc: ", packageLoc);
1320
 
1321
      generic = project.getProperty(packageAlias + "generic");
1322
      if ( generic != null ) {
1323
        Log("execute generic: yes", info);  
1324
      }
1325
 
1326
      // Extract the build standard information from the abt element
1327
      determineBuildStandard();
1328
 
866 mhunt 1329
      directChange = project.getProperty(packageAlias + "directchange");
1330
      if ( directChange != null )
1331
      {
1332
        Log("execute directChange: yes", info);  
1333
      }
1334
 
1335
      doesNotRequireSourceControlInteraction = project.getProperty(packageAlias + "doesnotrequiresourcecontrolinteraction");
1336
      if ( doesNotRequireSourceControlInteraction != null )
1337
      {
1338
        Log("execute doesNotRequireSourceControlInteraction: true", info);  
1339
      }
1340
 
4280 dpurdie 1341
      //    Display details of the Platform Jats and Ant elements within the
1342
      //    current ABT element
1343
      //
866 mhunt 1344
      for (Iterator<Platform> it=platformCollection.iterator(); it.hasNext(); )
1345
      {
1346
        Platform platform = it.next();
1347
        Log("execute platform: ", platform.getGbe_Machtype());
1348
      }
1349
 
1350
      for (Iterator<Jats> it=jatsCollection.iterator(); it.hasNext(); )
1351
      {
1352
        Jats jats = it.next();
1353
        Log("execute jats: ", jats.getTarget());
1354
      }
1355
 
1356
      for (Iterator<Ant> it=antCollection.iterator(); it.hasNext(); )
1357
      {
1358
        Ant ant = it.next();
1359
        Log("execute ant: ", ant.getJava());
1360
      }
924 dpurdie 1361
 
1362
      // Set newVsTag to the existing VcsTag
1363
      // Will be used when rebuilding a package that already exists
1364
      newVcsTag = packageVcsTag;
1365
 
866 mhunt 1366
      loc = project.getProperty(packageAlias + "loc");
1367
      Log("execute loc: ", loc);
4280 dpurdie 1368
 
866 mhunt 1369
      cwd = basedir + loc;
1370
      Log("execute cwd: ", cwd);
4280 dpurdie 1371
 
866 mhunt 1372
      wd = new File( cwd );
1373
      if ( !wd.exists() )
1374
      {
1375
        reportErrorException( "Failure " + cwd + " does not exist", 265 );
1376
      }
1377
 
1378
      // only now change the thread working directory
1379
      thread.setWorkingDirectory( wd );
1380
 
4280 dpurdie 1381
      //-----------------------------------------------------------------------
1382
      //    AbtPublish
1383
      //    Note: Have changed directory to the working directory
1384
      //
866 mhunt 1385
      if ( packageAlias.compareTo("AbtPublish") == 0 )
1386
      {
4280 dpurdie 1387
        publish();
866 mhunt 1388
        if ( daemon != null )
1389
        {
1390
          // set properties to drive BuildThread attributes
1391
          // these are used to determine what ant did
1392
          getProject().setProperty("abt_fully_published", fullyPublished);
924 dpurdie 1393
          getProject().setProperty("abt_new_vcstag", newVcsTag);
866 mhunt 1394
        }
1395
 
1396
        return;
1397
      }
1398
 
4280 dpurdie 1399
      //-----------------------------------------------------------------------
1400
      //    Build Target
1401
      //    Note: Have changed directory to the working directory
1402
      //
866 mhunt 1403
      packageVersionID = project.getProperty(packageAlias + "pv_id");
1404
      Log("execute packageVersionID: ", packageVersionID);
1405
      unittests = project.getProperty(packageAlias + "unittests");
1406
 
1407
      if ( unittests != null )
1408
      {
1409
        Log("execute unittests: yes", info);
1410
      }
1411
 
1412
      for (Iterator<Depend> it=dependCollection.iterator(); it.hasNext(); )
1413
      {
1414
        Depend depend = it.next();
1415
        Log("execute depend: ", depend.getPackage_Alias());
1416
      }
1417
 
1418
      if ( daemon == null )
1419
      {
1420
        // escrow centric
1421
        // recognise an escrow will be run multiple times on a build stage
1422
        // do not force continuous rebuilding each time
1423
        // has this package version already been published on this platform
4280 dpurdie 1424
        String published = checkPublished( dpkg_archive, ( generic != null ) ? "generic" : hostname );
1425
 
866 mhunt 1426
        if ( published.compareTo("no") != 0 )
1427
        {
1428
          Log( "execute package has been published on this platform - skipping", info);
1429
          propertyValue = "257";
1430
          throw new Exception();
1431
        }
1432
      }
910 mhunt 1433
      else
866 mhunt 1434
      {
910 mhunt 1435
        // DEVI 56714 daemon centric check
1436
        String majorV = this.getClass().getPackage().getSpecificationVersion();
1437
 
1438
        if ( buildtoolVersion.compareTo( majorV ) != 0 )
1439
        {
1440
          reportErrorException( "Failure, incompatible build.xml not generated with version " + majorV, 256 );
1441
        }
866 mhunt 1442
      }
1443
 
1444
      if ( ( buildStandard == BuildStandards.ANT && javaVersion == JavaVersions.NONE ) ||
1445
           ( buildStandard == BuildStandards.JATS && compileTarget == CompileTargets.NONE ) )
1446
      {
4280 dpurdie 1447
        //  Flag a benign build is good
1448
        //  A benign is a no-operation. The build is not rquired on the current machine
1449
        //  Issue: We have already extracted the source and will need to tear it down
1450
        //
866 mhunt 1451
        benign = true;
1452
      }
1453
 
4280 dpurdie 1454
      // Pre Build processing
1455
      //    Init metrics gathering
1456
      //    Generate auto.pl or <PackageName>depends.xml file
1457
      //
1458
      genbuild();
866 mhunt 1459
 
4280 dpurdie 1460
      if ( !benign && gatherMetricsOnly == null )
866 mhunt 1461
      {
4280 dpurdie 1462
          //
1463
          //    Build the target package
1464
          //
1465
          jatsBuildPackage();
866 mhunt 1466
      }
1467
 
1468
      if ( master )
1469
      {
1470
        File metrics = new File(basedir + fs + rtagId + "abtmetrics.txt");
1471
        if ( metrics.exists() )
1472
        {
1473
          metrics.delete();
1474
        }
1475
 
1476
        // gather metrics
886 mhunt 1477
        // jats -locatefile=.jats.packageroot etool jats_metrics -mode=finish -out=abtmetrics.txt
866 mhunt 1478
        initCommand();
1479
        addCommand( "jats" );
886 mhunt 1480
        addCommand( "-locatefile=.jats.packageroot" );
866 mhunt 1481
        addCommand( "etool" );
1482
        addCommand( "jats_metrics" );
1483
        addCommand( "-mode=finish" );
1484
        addCommand( "-out=" + metrics.getAbsolutePath() );
1485
 
1486
        runCommand( "Failure to collect metrics" );
1487
      }
1488
    }
1489
    catch( SecurityException e)
1490
    {
1491
      errorReported = true;
1492
      reportError( "Failure caught SecurityException", 256 );
1493
    }
1494
    catch( IOException e)
1495
    {
1496
      errorReported = true;
1497
      reportError( "Failure caught IOException", 256 );
1498
    }
1499
    catch( Exception e)
1500
    {
1501
      // assume this condition has been reported on
1502
      errorReported = true;
1503
 
1504
      if ( e.getMessage() != null )
1505
      {
898 mhunt 1506
        Log( "execute caught Exception " + e.getMessage(), warn );
866 mhunt 1507
      }
1508
      else
1509
      {
1510
        Log( "execute caught Exception", warn );
1511
      }
1512
    }
1513
    finally
1514
    {
1515
      try
1516
      {
1517
        if ( daemon != null )
1518
        {
1519
          // do not write to publish.log
1520
          throw new IOException();
1521
        }
1522
 
1523
        FileWriter publish = new FileWriter( basedir + fs + "publish.log", true );
1524
        if ( propertyValue.compareTo( "0" ) == 0 )
1525
        {
1526
          if ( benign )
1527
          {
4280 dpurdie 1528
            publish.write( "successfully published " + packageName + " at " + packageFullVersion + ", no action required on this platform" );
866 mhunt 1529
          }
1530
          else
1531
          {
4280 dpurdie 1532
            publish.write( "successfully published " + packageName + " at " + packageFullVersion );
866 mhunt 1533
          }
1534
 
1535
          publish.write( ls );
1536
        }
1537
        else
1538
        if ( propertyValue.compareTo( "256" ) == 0 )
1539
        {
4280 dpurdie 1540
          publish.write( "failed to publish " + packageName + " at " + packageFullVersion +  ", internal abt issue" + ls );
866 mhunt 1541
        }
1542
        else
1543
        if ( propertyValue.compareTo( "257" ) == 0 )
1544
        {
1545
          errorReported = false;
4280 dpurdie 1546
          publish.write( "previously published " + packageName + " at " + packageFullVersion + ls );
866 mhunt 1547
        }
1548
        else
872 dpurdie 1549
        if ( propertyValue.compareTo( "262" ) == 0 )
1550
        {
4280 dpurdie 1551
          publish.write( "failed to publish " + packageName + " at " + packageFullVersion + ", couldn't process save_build info" + ls );
872 dpurdie 1552
        }
1553
        else
866 mhunt 1554
        if ( propertyValue.compareTo( "263" ) == 0 )
1555
        {
4280 dpurdie 1556
          publish.write( "failed to publish " + packageName + " at " + packageFullVersion + ", couldn't write to auto.cfg" + ls );
866 mhunt 1557
        }
1558
        else
1559
        if ( propertyValue.compareTo( "265" ) == 0 )
1560
        {
4280 dpurdie 1561
          publish.write( "failed to publish " + packageName + " at " + packageFullVersion + ", " + cwd + " does not exist" + ls );
866 mhunt 1562
        }
1563
        else
1564
        {
1565
          // nb jats or ant can presumably return a value 1 to 255
4280 dpurdie 1566
          publish.write( "failed to publish " + packageName + " at " + packageFullVersion + ", jats or ant failed" + ls );
866 mhunt 1567
        }
1568
 
1569
        publish.close();
1570
      }
1571
      catch( IOException e )
1572
      {
1573
        if ( daemon == null )
1574
        {
1575
          reportError( "Failure to write to publish.log", 266 );
1576
        }
1577
      }
1578
      catch( Exception e )
1579
      {
1580
        if ( daemon == null )
1581
        {
1582
          reportError( "Failure to write to publish.log", 266 );
1583
        }
1584
      }
1585
 
1586
      getProject().setProperty(packageAlias + ".res", propertyValue);
1587
 
1588
      if ( errorReported )
1589
      {
1590
        // designed to prevent subsequent build activity on this build file
1591
        // supply the build failure log file in message with / as file sep
1592
        // takes the form hostname/rtag_id/daemon/logfile
1593
        if ( daemon != null )
1594
        {
1595
          String message = hostname + "/" + rtagId + "/" + daemon + "/" + packageAlias + ".log";
1596
          throw new BuildException( message );
1597
        }
1598
        else
1599
        {
1600
          throw new BuildException();
1601
        }
1602
      }
1603
 
1604
    }
1605
  }
1606
 
1607
  private void deleteDirectory(File directory)
1608
  {
1609
    Log("deleteDirectory " + directory.getName(), debug);
1610
    try
1611
    {
1612
      if ( directory.exists() )
1613
      {
878 mhunt 1614
        File[] children = directory.listFiles();
866 mhunt 1615
 
1616
        if ( children != null )
1617
        {
1618
          for ( int child=0; child < children.length; child++ )
1619
          {
1620
            Log("deleteDirectory deleting " + children[ child ].getName(), warn);
1621
 
1622
            if ( children[ child ].isDirectory() )
1623
            {
1624
              deleteDirectory( children[ child ] );
1625
            }
1626
            else
1627
            {
1628
              children[ child ].delete();
1629
            }
1630
          }
1631
        }
1632
        directory.delete();
1633
      }
1634
    }
1635
    catch( SecurityException e )
1636
    {
1637
      // this can be thrown by exists and delete
1638
       Log("deleteDirectory caught SecurityException", warn);
1639
    }
1640
  }
1641
 
4280 dpurdie 1642
  //---------------------------------------------------------------------------
1643
  //    Extend the <abt> task with several new elements
1644
  // //
1645
  //        <depend package_alias="${debian_dpkg.cots}"/>
1646
  //        <platform gbe_machtype="solaris10_x86"/>
1647
  //        <jats target="all"/>
1648
  //        <ant java="java 1.6"/>
1649
  //
866 mhunt 1650
  public Depend createDepend()
1651
  {
1652
    Depend depend = new Depend();
1653
    dependCollection.add(depend);
1654
    return depend;
1655
  }
1656
 
1657
  public Platform createPlatform()
1658
  {
1659
    Platform platform = new Platform();
1660
    platformCollection.add(platform);
1661
    return platform;
1662
  }
1663
 
1664
  public Jats createJats()
1665
  {
1666
    Jats jats = new Jats();
1667
    jatsCollection.add(jats);
1668
    return jats;
1669
  }
1670
 
1671
  public Ant createAnt()
1672
  {
1673
    Ant ant = new Ant();
1674
    antCollection.add(ant);
1675
    return ant;
1676
  }
1677
}