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