Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
814 mhunt 1
package com.erggroup.buildtool.daemon;
2
 
3
import com.erggroup.buildtool.ripple.MutableChar;
4
import com.erggroup.buildtool.ripple.MutableInt;
5
import com.erggroup.buildtool.ripple.Package;
6
import com.erggroup.buildtool.ripple.ReleaseManager;
1350 dpurdie 7
import com.erggroup.buildtool.daemon.NagiosThread;
814 mhunt 8
 
854 mhunt 9
import java.io.File;
1352 dpurdie 10
import java.io.IOException;
1350 dpurdie 11
 
814 mhunt 12
import java.net.InetAddress;
13
import java.net.UnknownHostException;
1350 dpurdie 14
import java.net.ServerSocket;
814 mhunt 15
 
16
import java.sql.SQLException;
17
 
18
import java.util.Iterator;
19
import java.util.Vector;
20
 
21
import org.apache.log4j.Logger;
22
import org.apache.log4j.xml.DOMConfigurator;
23
 
24
/**BuildDaemon sub component and entry point (main BuildDaemon thread)
25
 */
26
public class BuildDaemon
27
{
28
 
29
  /**hostname
30
   * @attribute
31
   */
32
  static String mHostname = new String();
33
 
854 mhunt 34
  /**GBE_LOG
35
   * @attribute
36
   */
37
  static String mGbeLog = new String();
38
 
814 mhunt 39
  /**Logger
40
   * @attribute
41
   */
42
  private static final Logger mLogger = Logger.getLogger(BuildDaemon.class);
43
 
44
  /**Collection of ThreadIdentifier objects.
45
   * @attribute
46
   */
864 mhunt 47
  private Vector<ThreadIdentifier> mThreadCollection = new Vector<ThreadIdentifier>();
814 mhunt 48
 
1350 dpurdie 49
  /**Nagios
50
   * @attribute
51
   */
52
  ServerSocket nagiosSrv;
53
  NagiosThread nagiosChecker;
54
 
814 mhunt 55
  /**mThreadCollection items
56
   */
57
  private class ThreadIdentifier
58
  {
59
    /**rcon_id associated with the thread
60
     * @attribute
61
     */
62
    private final int mRcon_id;
63
 
64
    /**thread identifier
65
     * @attribute
66
     */
1361 dpurdie 67
    private final BuildThread mThread;
814 mhunt 68
 
69
    /**constructor
70
     */
1361 dpurdie 71
    ThreadIdentifier(int rcon_id, BuildThread thread)
814 mhunt 72
    {
73
      mLogger.debug("ThreadIdentifier " + rcon_id);
74
      mRcon_id = rcon_id;
75
      mThread = thread;
76
    }
77
 
78
    /**accessor
79
     */
80
    int get_rcon_id()
81
    {
82
      mLogger.debug("get_rcon_id");
83
      mLogger.info("get_rcon_id returned " + mRcon_id);
84
      return mRcon_id;
85
    }
86
 
87
    /**accessor
88
     */
1361 dpurdie 89
    BuildThread get_thread()
814 mhunt 90
    {
91
      mLogger.debug("get_thread");
92
      return mThread;
93
    }
94
  }
95
 
96
  /**main method for the Build Daemon program
97
   * instantiates a BuildDaemon object
98
   */
99
  public static void main(String[] args)
100
  {
858 mhunt 101
    String abtdXml = System.getenv("ABT_HOME");
102
 
103
    if ( abtdXml != null )
104
    {
105
      abtdXml += System.getProperty( "file.separator" ) + "abtd.xml";
106
    }
107
    else
108
    {
109
      abtdXml = new String("abtd.xml");
110
    }
111
    DOMConfigurator.configure(abtdXml);
112
    mLogger.debug("main");
854 mhunt 113
    String antHome = System.getenv("ANT_HOME");
114
 
115
    if ( antHome == null )
116
    {
117
      mLogger.fatal("main ANT_HOME not set");
118
      System.exit(1);
119
    }
120
 
121
    mGbeLog = System.getenv("GBE_LOG");
122
 
123
    if ( mGbeLog == null )
124
    {
125
      mLogger.fatal("main GBE_LOG not set");
126
      System.exit(1);
127
    }
128
 
129
    File gl = new File( mGbeLog );
130
 
131
    if ( !gl.isDirectory() )
132
    {
133
      mLogger.fatal("main GBE_LOG is not a directory");
134
    }
135
 
136
    String gbeUNC = System.getenv("GBE_UNC");
137
 
138
    if ( gbeUNC == null )
139
    {
140
      mLogger.fatal("main GBE_UNC not set");
141
      System.exit(1);
142
    }
143
 
814 mhunt 144
    String connectionString = System.getenv("GBE_RM_LOCATION");
145
    String username = System.getenv("GBE_RM_USERNAME");
146
    String password = System.getenv("GBE_RM_PASSWORD");
147
 
148
    for (int optind = 0; optind < args.length; optind++)
149
    {
150
      if (args[optind].equals("-c") && optind < (args.length - 1))
151
      {
152
        connectionString = args[++optind];
153
      }
154
      else if (args[optind].equals("-u") && optind < (args.length - 1))
155
      {
156
        username = args[++optind];
157
      }
158
      else if (args[optind].equals("-p") && optind < (args.length - 1))
159
      {
160
        password = args[++optind];
161
      }
162
    }
163
 
854 mhunt 164
    if (connectionString == null ||
165
        connectionString.length() == 0 ||
166
        username == null ||
167
        username.length() == 0 ||
168
        password == null ||
169
        password.length() == 0)
814 mhunt 170
    {
854 mhunt 171
      mLogger.fatal("Usage: java -jar abtdD.jar -c connectionString -u username -p password");
814 mhunt 172
      System.exit(1);
173
    }
1350 dpurdie 174
 
814 mhunt 175
    BuildDaemon buildDaemon = new BuildDaemon(connectionString, username, password);
176
    buildDaemon.cleanUp();
177
 
178
  }
179
 
180
  /**constructor, implements the sequence diagram spawn thread
181
   */
182
  public BuildDaemon(String connectionString, String username, 
183
                     String password)
184
  {
185
    mLogger.debug("BuildDaemon");
186
    ReleaseManager releaseManager = 
906 mhunt 187
      new ReleaseManager(connectionString, username + "[release_manager]", password);
814 mhunt 188
    boolean run = true;
189
    MutableInt rtag_id = new MutableInt();
190
    MutableInt rcon_id = new MutableInt();
191
    MutableChar daemon_mode = new MutableChar();
192
    String hostname = new String("");
193
 
194
    try
195
    {
196
      InetAddress local = InetAddress.getLocalHost();
197
      hostname = local.getHostName();
198
      mHostname = hostname;
199
      mLogger.info("BuildDaemon set hostname " + mHostname);
200
 
924 dpurdie 201
      if ( Package.mGenericMachtype == null )
814 mhunt 202
      {
924 dpurdie 203
        mLogger.fatal("run GBE_MACHTYPE not set");
204
        throw new Exception("run GBE_MACHTYPE not set");
814 mhunt 205
      }
924 dpurdie 206
 
207
      if ( Package.mGbeDpkg == null )
208
      {
209
        mLogger.fatal("run GBE_DPKG not set");
210
        throw new Exception("run GBE_DPKG not set");
211
      }
212
 
1350 dpurdie 213
      //
214
      //  Start the Nagios Interface Thread
215
      //
1352 dpurdie 216
      try {
1354 dpurdie 217
        nagiosSrv = new ServerSocket(1111);
1361 dpurdie 218
        nagiosChecker = new NagiosThread(nagiosSrv, this);
1352 dpurdie 219
        nagiosChecker.start();
220
      } catch ( IOException e ) {
221
            mLogger.fatal("Failed to start Nagios Service. Port already in use");
222
            throw new Exception("Nagios port in use");
223
      }
1350 dpurdie 224
 
820 mhunt 225
      boolean firstTime = true;
226
 
814 mhunt 227
      while (run)
228
      {
229
        try
230
        {
820 mhunt 231
          if ( !firstTime )
232
          {
233
            mLogger.warn("BuildDaemon sleep for 10 mins");
234
            Thread.sleep(600000);
235
            mLogger.info("BuildDaemon sleep returned");
236
          }
237
          else
238
          {
239
            firstTime = false;    
240
          }
241
 
814 mhunt 242
          releaseManager.queryReleaseConfig(hostname);
243
 
244
          rtag_id.value = 0;
245
          rcon_id.value = 0;
246
          daemon_mode.value = 'X';
247
 
248
          boolean moreReleasesConfigured = 
249
            releaseManager.getFirstReleaseConfig(rtag_id, rcon_id, 
896 mhunt 250
                                                 daemon_mode);
814 mhunt 251
 
252
          do
253
          {
254
            if (moreReleasesConfigured)
255
            {
256
              if (!isActive(rcon_id.value))
257
              {
896 mhunt 258
                mLogger.warn("BuildDaemon activating " + rtag_id.value + " " + rcon_id.value + " " + daemon_mode.value);
814 mhunt 259
 
896 mhunt 260
                String utf = null;
261
 
262
                if ( connectionString.compareTo("unit test spawn thread") == 0 )
263
                {
264
                  utf = connectionString;
265
                }
266
 
814 mhunt 267
                // spawn and run the BuildThread
268
                if (daemon_mode.value == 'M')
269
                {
896 mhunt 270
                  MasterThread thread = new MasterThread(rtag_id.value, rcon_id.value, utf);
814 mhunt 271
                  ThreadIdentifier threadIdentifier = 
272
                    new ThreadIdentifier(rcon_id.value, thread);
273
                  mThreadCollection.add(threadIdentifier);
274
                  // begin thread execution and invoke thread.run();
275
                  thread.start();
276
                }
277
                else if (daemon_mode.value == 'S')
278
                {
896 mhunt 279
                  SlaveThread thread = new SlaveThread(rtag_id.value, rcon_id.value, utf);
814 mhunt 280
                  ThreadIdentifier threadIdentifier = 
281
                    new ThreadIdentifier(rcon_id.value, thread);
282
                  mThreadCollection.add(threadIdentifier);
283
                  // begin thread execution and invoke thread.run();
284
                  thread.start();
285
                }
286
              }
287
 
288
              moreReleasesConfigured = 
289
                  releaseManager.getNextReleaseConfig(rtag_id, rcon_id, 
896 mhunt 290
                                                      daemon_mode);
814 mhunt 291
            }
292
          }
293
          while (moreReleasesConfigured);
294
 
295
          if ( connectionString.compareTo("unit test spawn thread") == 0)
296
          {
820 mhunt 297
            run = false;
814 mhunt 298
          }
299
 
300
        }
301
        catch (SQLException e)
302
        {
303
          mLogger.warn("BuildDaemon caught SQLException");
304
        }
305
        catch (InterruptedException e)
306
        {
307
          mLogger.warn("BuildDaemon caught InterruptedException");
308
        }
309
        catch (Exception e)
310
        {
820 mhunt 311
          mLogger.warn("BuildDaemon caught Exception");
814 mhunt 312
        }
313
      }
314
    }
315
    catch( UnknownHostException e )
316
    {
317
      mLogger.fatal("BuildDaemon caught UnknownHostException");
318
    }
319
    catch( Exception e )
320
    {
321
      mLogger.fatal("BuildDaemon caught Exception");
322
    }
323
  }
324
 
325
  /**calls isAlive on the Thread object associated with the rcon_id
326
   */
327
  public boolean isActive(final int rcon_id)
328
  {
329
    mLogger.debug("isActive " + rcon_id);
330
    boolean retVal = false;
331
 
864 mhunt 332
    for (Iterator<ThreadIdentifier> it = mThreadCollection.iterator(); it.hasNext(); )
814 mhunt 333
    {
864 mhunt 334
      ThreadIdentifier threadIdentifier = it.next();
814 mhunt 335
 
336
      if (threadIdentifier.get_rcon_id() == rcon_id)
337
      {
338
        if (threadIdentifier.get_thread().isAlive())
339
        {
340
          retVal = true;
844 dpurdie 341
          break;
814 mhunt 342
        }
844 dpurdie 343
        else
344
        {
345
            mLogger.warn("isActive found dead thread " + rcon_id );
346
        }
814 mhunt 347
      }
348
    }
349
    mLogger.info("isActive returned " + retVal);
350
    return retVal;
351
  }
352
 
1361 dpurdie 353
    /**
354
     *  Nagios interface
355
     *      Returns true if ALL the thread looks OK
356
     *      Must have one active thread
357
    */
358
    boolean checkThreads()
359
    {
360
      boolean retVal = false;
361
        mLogger.info("checkThreads called");
362
      for (Iterator<ThreadIdentifier> it = mThreadCollection.iterator(); it.hasNext(); )
363
      {
364
        ThreadIdentifier threadIdentifier = it.next();
365
 
366
        if (threadIdentifier.get_thread().checkThread())
367
        {
368
          retVal = true;
369
        }
370
        else
371
        {
372
          retVal = false;
373
          break;
374
        }
375
      }
376
 
377
      mLogger.info("checkThreads returned " + retVal);
378
      return retVal;
379
    }
380
 
381
 
814 mhunt 382
  /**terminates all BuildThreads
383
   */
384
  public void cleanUp()
385
  {
386
    mLogger.debug("cleanUp");
1354 dpurdie 387
    if ( nagiosChecker != null )
388
    {
389
        nagiosChecker.terminate();
390
    }
1350 dpurdie 391
 
864 mhunt 392
    for (Iterator<ThreadIdentifier> it = mThreadCollection.iterator(); it.hasNext(); )
814 mhunt 393
    {
864 mhunt 394
      ThreadIdentifier threadIdentifier = it.next();
814 mhunt 395
 
396
      if (threadIdentifier.get_thread().isAlive())
397
      {
398
        try
399
        {
400
          threadIdentifier.get_thread().interrupt();
401
          threadIdentifier.get_thread().join();
402
        }
403
        catch( InterruptedException e )
404
        {
405
        }
406
      }
407
    }
408
  }
409
}
1361 dpurdie 410