2424import java .io .InputStream ;
2525import java .lang .reflect .Constructor ;
2626import java .lang .reflect .InvocationTargetException ;
27- import java .lang .reflect .Method ;
2827import java .net .HttpURLConnection ;
2928import java .util .ArrayList ;
3029import java .util .Collections ;
3837
3938import javax .naming .ConfigurationException ;
4039
40+ import org .apache .commons .daemon .Daemon ;
41+ import org .apache .commons .daemon .DaemonContext ;
42+ import org .apache .commons .daemon .DaemonInitException ;
4143import org .apache .commons .httpclient .HttpClient ;
4244import org .apache .commons .httpclient .MultiThreadedHttpConnectionManager ;
4345import org .apache .commons .httpclient .methods .GetMethod ;
4749import com .cloud .agent .Agent .ExitStatus ;
4850import com .cloud .agent .dao .StorageComponent ;
4951import com .cloud .agent .dao .impl .PropertiesStorage ;
50- import com .cloud .host .Host ;
5152import com .cloud .resource .ServerResource ;
5253import com .cloud .utils .LogUtils ;
5354import com .cloud .utils .NumbersUtil ;
5859import com .cloud .utils .exception .CloudRuntimeException ;
5960import com .cloud .utils .script .Script ;
6061
61- public class AgentShell implements IAgentShell {
62+ public class AgentShell implements IAgentShell , Daemon {
6263 private static final Logger s_logger = Logger .getLogger (AgentShell .class
6364 .getName ());
6465 private static final MultiThreadedHttpConnectionManager s_httpClientManager = new MultiThreadedHttpConnectionManager ();
@@ -79,7 +80,6 @@ public class AgentShell implements IAgentShell {
7980 private int _nextAgentId = 1 ;
8081 private volatile boolean _exit = false ;
8182 private int _pingRetries ;
82- private Thread _consoleProxyMain = null ;
8383 private final List <Agent > _agents = new ArrayList <Agent >();
8484
8585 public AgentShell () {
@@ -376,7 +376,17 @@ protected boolean parseCommand(final String[] args)
376376
377377 return true ;
378378 }
379-
379+
380+ @ Override
381+ public void init (DaemonContext dc ) throws DaemonInitException {
382+ s_logger .debug ("Initializing AgentShell from JSVC" );
383+ try {
384+ init (dc .getArguments ());
385+ } catch (ConfigurationException ex ) {
386+ throw new DaemonInitException ("Initialization failed" , ex );
387+ }
388+ }
389+
380390 public void init (String [] args ) throws ConfigurationException {
381391
382392 // PropertiesUtil is used both in management server and agent packages,
@@ -402,11 +412,13 @@ public void init(String[] args) throws ConfigurationException {
402412 loadProperties ();
403413 parseCommand (args );
404414
405- List <String > properties = Collections .list ((Enumeration <String >)_properties .propertyNames ());
406- for (String property :properties ){
407- s_logger .debug ("Found property: " + property );
415+ if (s_logger .isDebugEnabled ()) {
416+ List <String > properties = Collections .list ((Enumeration <String >)_properties .propertyNames ());
417+ for (String property :properties ){
418+ s_logger .debug ("Found property: " + property );
419+ }
408420 }
409-
421+
410422 s_logger .info ("Defaulting to using properties file for storage" );
411423 _storage = new PropertiesStorage ();
412424 _storage .configure ("Storage" , new HashMap <String , Object >());
@@ -434,71 +446,6 @@ private void launchAgent() throws ConfigurationException {
434446 launchAgentFromTypeInfo ();
435447 }
436448
437- private boolean needConsoleProxy () {
438- for (Agent agent : _agents ) {
439- if (agent .getResource ().getType ().equals (Host .Type .ConsoleProxy )
440- || agent .getResource ().getType ().equals (Host .Type .Routing ))
441- return true ;
442- }
443- return false ;
444- }
445-
446- private int getConsoleProxyPort () {
447- int port = NumbersUtil .parseInt (
448- getProperty (null , "consoleproxy.httpListenPort" ), 443 );
449- return port ;
450- }
451-
452- private void openPortWithIptables (int port ) {
453- // TODO
454- }
455-
456- private void launchConsoleProxy () throws ConfigurationException {
457- if (!needConsoleProxy ()) {
458- if (s_logger .isInfoEnabled ())
459- s_logger .info ("Storage only agent, no need to start console proxy on it" );
460- return ;
461- }
462-
463- int port = getConsoleProxyPort ();
464- openPortWithIptables (port );
465-
466- _consoleProxyMain = new Thread (new Runnable () {
467- @ Override
468- public void run () {
469- try {
470- Class <?> consoleProxyClazz = Class .forName ("com.cloud.consoleproxy.ConsoleProxy" );
471-
472- try {
473- Method method = consoleProxyClazz .getMethod ("start" ,
474- Properties .class );
475- method .invoke (null , _properties );
476- } catch (SecurityException e ) {
477- s_logger .error ("Unable to launch console proxy due to SecurityException" );
478- System .exit (ExitStatus .Error .value ());
479- } catch (NoSuchMethodException e ) {
480- s_logger .error ("Unable to launch console proxy due to NoSuchMethodException" );
481- System .exit (ExitStatus .Error .value ());
482- } catch (IllegalArgumentException e ) {
483- s_logger .error ("Unable to launch console proxy due to IllegalArgumentException" );
484- System .exit (ExitStatus .Error .value ());
485- } catch (IllegalAccessException e ) {
486- s_logger .error ("Unable to launch console proxy due to IllegalAccessException" );
487- System .exit (ExitStatus .Error .value ());
488- } catch (InvocationTargetException e ) {
489- s_logger .error ("Unable to launch console proxy due to InvocationTargetException" );
490- System .exit (ExitStatus .Error .value ());
491- }
492- } catch (final ClassNotFoundException e ) {
493- s_logger .error ("Unable to launch console proxy due to ClassNotFoundException" );
494- System .exit (ExitStatus .Error .value ());
495- }
496- }
497- }, "Console-Proxy-Main" );
498- _consoleProxyMain .setDaemon (true );
499- _consoleProxyMain .start ();
500- }
501-
502449 private void launchAgentFromClassInfo (String resourceClassNames )
503450 throws ConfigurationException {
504451 String [] names = resourceClassNames .split ("\\ |" );
@@ -591,14 +538,6 @@ public void start() {
591538
592539 launchAgent ();
593540
594- //
595- // For both KVM & Xen-Server hypervisor, we have switched to
596- // VM-based console proxy solution, disable launching
597- // of console proxy here
598- //
599- // launchConsoleProxy();
600- //
601-
602541 try {
603542 while (!_exit )
604543 Thread .sleep (1000 );
@@ -618,9 +557,6 @@ public void start() {
618557
619558 public void stop () {
620559 _exit = true ;
621- if (_consoleProxyMain != null ) {
622- _consoleProxyMain .interrupt ();
623- }
624560 }
625561
626562 public void destroy () {
@@ -629,11 +565,13 @@ public void destroy() {
629565
630566 public static void main (String [] args ) {
631567 try {
568+ s_logger .debug ("Initializing AgentShell from main" );
632569 AgentShell shell = new AgentShell ();
633570 shell .init (args );
634571 shell .start ();
635572 } catch (ConfigurationException e ) {
636573 System .out .println (e .getMessage ());
637574 }
638575 }
576+
639577}
0 commit comments