Subversion Repositories DevTools

Rev

Go to most recent revision | 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
      //
1365 dpurdie 216
      if ( connectionString.compareTo("unit test spawn thread") != 0 )
217
      {
218
        try {
219
          nagiosSrv = new ServerSocket(1111);
220
          nagiosChecker = new NagiosThread(nagiosSrv, this);
221
          nagiosChecker.start();
222
        } catch ( IOException e ) {
223
              mLogger.fatal("Failed to start Nagios Service. Port already in use");
224
              throw new Exception("Nagios port in use");
225
        }
1352 dpurdie 226
      }
1350 dpurdie 227
 
820 mhunt 228
      boolean firstTime = true;
229
 
814 mhunt 230
      while (run)
231
      {
232
        try
233
        {
820 mhunt 234
          if ( !firstTime )
235
          {
236
            mLogger.warn("BuildDaemon sleep for 10 mins");
237
            Thread.sleep(600000);
238
            mLogger.info("BuildDaemon sleep returned");
239
          }
240
          else
241
          {
242
            firstTime = false;    
243
          }
244
 
814 mhunt 245
          releaseManager.queryReleaseConfig(hostname);
246
 
247
          rtag_id.value = 0;
248
          rcon_id.value = 0;
249
          daemon_mode.value = 'X';
250
 
251
          boolean moreReleasesConfigured = 
252
            releaseManager.getFirstReleaseConfig(rtag_id, rcon_id, 
896 mhunt 253
                                                 daemon_mode);
814 mhunt 254
 
255
          do
256
          {
257
            if (moreReleasesConfigured)
258
            {
259
              if (!isActive(rcon_id.value))
260
              {
896 mhunt 261
                mLogger.warn("BuildDaemon activating " + rtag_id.value + " " + rcon_id.value + " " + daemon_mode.value);
814 mhunt 262
 
896 mhunt 263
                String utf = null;
264
 
265
                if ( connectionString.compareTo("unit test spawn thread") == 0 )
266
                {
267
                  utf = connectionString;
268
                }
269
 
814 mhunt 270
                // spawn and run the BuildThread
271
                if (daemon_mode.value == 'M')
272
                {
896 mhunt 273
                  MasterThread thread = new MasterThread(rtag_id.value, rcon_id.value, utf);
814 mhunt 274
                  ThreadIdentifier threadIdentifier = 
275
                    new ThreadIdentifier(rcon_id.value, thread);
276
                  mThreadCollection.add(threadIdentifier);
277
                  // begin thread execution and invoke thread.run();
278
                  thread.start();
279
                }
280
                else if (daemon_mode.value == 'S')
281
                {
896 mhunt 282
                  SlaveThread thread = new SlaveThread(rtag_id.value, rcon_id.value, utf);
814 mhunt 283
                  ThreadIdentifier threadIdentifier = 
284
                    new ThreadIdentifier(rcon_id.value, thread);
285
                  mThreadCollection.add(threadIdentifier);
286
                  // begin thread execution and invoke thread.run();
287
                  thread.start();
288
                }
289
              }
290
 
291
              moreReleasesConfigured = 
292
                  releaseManager.getNextReleaseConfig(rtag_id, rcon_id, 
896 mhunt 293
                                                      daemon_mode);
814 mhunt 294
            }
295
          }
296
          while (moreReleasesConfigured);
297
 
298
          if ( connectionString.compareTo("unit test spawn thread") == 0)
299
          {
820 mhunt 300
            run = false;
814 mhunt 301
          }
302
 
303
        }
304
        catch (SQLException e)
305
        {
306
          mLogger.warn("BuildDaemon caught SQLException");
307
        }
308
        catch (InterruptedException e)
309
        {
310
          mLogger.warn("BuildDaemon caught InterruptedException");
311
        }
312
        catch (Exception e)
313
        {
820 mhunt 314
          mLogger.warn("BuildDaemon caught Exception");
814 mhunt 315
        }
316
      }
317
    }
318
    catch( UnknownHostException e )
319
    {
320
      mLogger.fatal("BuildDaemon caught UnknownHostException");
321
    }
322
    catch( Exception e )
323
    {
324
      mLogger.fatal("BuildDaemon caught Exception");
325
    }
326
  }
327
 
328
  /**calls isAlive on the Thread object associated with the rcon_id
329
   */
330
  public boolean isActive(final int rcon_id)
331
  {
332
    mLogger.debug("isActive " + rcon_id);
333
    boolean retVal = false;
334
 
864 mhunt 335
    for (Iterator<ThreadIdentifier> it = mThreadCollection.iterator(); it.hasNext(); )
814 mhunt 336
    {
864 mhunt 337
      ThreadIdentifier threadIdentifier = it.next();
814 mhunt 338
 
339
      if (threadIdentifier.get_rcon_id() == rcon_id)
340
      {
341
        if (threadIdentifier.get_thread().isAlive())
342
        {
343
          retVal = true;
844 dpurdie 344
          break;
814 mhunt 345
        }
844 dpurdie 346
        else
347
        {
348
            mLogger.warn("isActive found dead thread " + rcon_id );
349
        }
814 mhunt 350
      }
351
    }
352
    mLogger.info("isActive returned " + retVal);
353
    return retVal;
354
  }
355
 
1361 dpurdie 356
    /**
357
     *  Nagios interface
358
     *      Returns true if ALL the thread looks OK
359
     *      Must have one active thread
360
    */
361
    boolean checkThreads()
362
    {
363
      boolean retVal = false;
364
        mLogger.info("checkThreads called");
365
      for (Iterator<ThreadIdentifier> it = mThreadCollection.iterator(); it.hasNext(); )
366
      {
367
        ThreadIdentifier threadIdentifier = it.next();
368
 
369
        if (threadIdentifier.get_thread().checkThread())
370
        {
371
          retVal = true;
372
        }
373
        else
374
        {
375
          retVal = false;
376
          break;
377
        }
378
      }
379
 
380
      mLogger.info("checkThreads returned " + retVal);
381
      return retVal;
382
    }
383
 
384
 
814 mhunt 385
  /**terminates all BuildThreads
386
   */
387
  public void cleanUp()
388
  {
389
    mLogger.debug("cleanUp");
1354 dpurdie 390
    if ( nagiosChecker != null )
391
    {
392
        nagiosChecker.terminate();
393
    }
1350 dpurdie 394
 
864 mhunt 395
    for (Iterator<ThreadIdentifier> it = mThreadCollection.iterator(); it.hasNext(); )
814 mhunt 396
    {
864 mhunt 397
      ThreadIdentifier threadIdentifier = it.next();
814 mhunt 398
 
399
      if (threadIdentifier.get_thread().isAlive())
400
      {
401
        try
402
        {
403
          threadIdentifier.get_thread().interrupt();
404
          threadIdentifier.get_thread().join();
405
        }
406
        catch( InterruptedException e )
407
        {
408
        }
409
      }
410
    }
411
  }
412
}
1361 dpurdie 413