ó
ÍÿŠTc           @   sl  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z m	 Z	 m
 Z
 d d l m Z m Z d d l m Z d d l m Z m Z d d l m Z d d l m Z d d	 l m Z m Z d d
 l m Z d d l m Z m Z d d l m  Z  d d l! m" Z" d d l# m$ Z$ m% Z% m& Z& d d l' m( Z( d d l) m* Z* m+ Z+ m, Z, d d l- m. Z. d Z/ d Z0 d Z1 d Z2 d e3 f d „  ƒ  YZ4 d e3 f d „  ƒ  YZ5 d e3 f d „  ƒ  YZ6 d e7 f d „  ƒ  YZ8 d e8 f d „  ƒ  YZ9 d e8 f d  „  ƒ  YZ: d! e8 f d" „  ƒ  YZ; d# e f d$ „  ƒ  YZ< d% e7 f d& „  ƒ  YZ= d' e f d( „  ƒ  YZ> d) „  Z? d* e f d+ „  ƒ  YZ@ e$ e& d, d- d. d/ ƒ e& d0 d- d. d/ ƒ e& d1 d- d. d/ ƒ e& d2 d- d. d/ ƒ e& d3 d- d. d/ ƒ e& d4 d- d. d/ ƒ e& d5 d- d. d6 ƒ e& d7 d- d. d/ ƒ e& d8 d- d. d/ ƒ e% d9 d- d. d: ƒ g
 ƒ ZA d; „  ZB e jC d d< „ ZE d S(=   s”   See L{WatchDog}.

The WatchDog must run as root, because it spawns the Landscape Manager.

The main C{landscape-client} program uses this watchdog.
iÿÿÿÿN(   t   warningt   infot   error(   t	   setrlimitt   RLIMIT_NOFILE(   t   reactor(   t   Deferredt   succeed(   t   ProcessProtocol(   t   ProcessExitedAlready(   t   Servicet   Application(   t   startApplication(   t   init_loggingt   Configuration(   t   gather_results(   t   log_failure(   t   BootstrapListt   BootstrapFilet   BootstrapDirectory(   t   rotate_logs(   t   RemoteBrokerConnectort   RemoteMonitorConnectort   RemoteManagerConnector(   t   LandscapeReactori
   i   i   t   DaemonErrorc           B   s   e  Z d  Z RS(   s(   One of the daemons could not be started.(   t   __name__t
   __module__t   __doc__(    (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyR   )   s   t   TimeoutErrorc           B   s   e  Z d  Z RS(   s   Something took too long.(   R   R   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyR   -   s   t   ExecutableNotFoundErrorc           B   s   e  Z d  Z RS(   s   An executable was not found.(   R   R   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyR   1   s   t   Daemonc           B   s›   e  Z d  Z d Z d Z d Z d Z e e	 d d „ Z
 d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sq  A Landscape daemon which can be started and tracked.

    This class should be subclassed to specify individual daemon.

    @cvar program: The name of the executable program that will start this
        daemon.
    @cvar username: The name of the user to switch to, by default.
    @cvar service: The DBus service name that the program will be expected to
        listen on.
    @cvar max_retries: The maximum number of retries before giving up when
        trying to connect to the watched daemon.
    @cvar factor: The factor by which the delay between subsequent connection
        attempts will increase.

    @param connector: The L{ComponentConnector} of the daemon.
    @param reactor: The reactor used to spawn the process and schedule timed
        calls.
    @param verbose: Optionally, report more information when running this
        program.  Defaults to False.
    t	   landscapei   gš™™™™™ñ?c   	      C   s'  | |  _  | |  _ t j j ƒ  |  _ t j ƒ  } | d k rÛ t j |  j	 ƒ } | j
 } | j } | | k rx | |  _ n	 d  |  _ | t j ƒ  k rŸ | |  _ n	 d  |  _ | j |  j d <|  j	 |  j d <|  j	 |  j d <n d  |  _ d  |  _ | |  _ | |  _ d  |  _ d |  _ d |  _ t |  _ d  S(   Ni    t   HOMEt   USERt   LOGNAME(   t
   _connectort   _reactort   ost   environt   copyt   _envt   getuidt   pwdt   getpwnamt   usernamet   pw_uidt   pw_gidt   _uidt   Nonet   getgidt   _gidt   pw_dirt   _verboset   _configt   _processt   _last_startedt   _quick_startst   Truet   _allow_restart(	   t   selft	   connectorR   t   verboset   configt   my_uidt   pwd_infot
   target_uidt
   target_gid(    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   __init__P   s2    													c         C   si   t  j j t  j j t j d ƒ ƒ } t  j j | |  j ƒ } t  j j | ƒ se t	 d | f ƒ ‚ n  | S(   s—   Find the fully-qualified path to the executable.

        If the executable can't be found, L{ExecutableNotFoundError} will be
        raised.
        i    s   %s doesn't exist(
   R&   t   patht   dirnamet   abspatht   syst   argvt   joint   programt   existsR   (   R<   RF   t
   executable(    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   find_executableq   s
    %c         C   s<  d |  _ t j ƒ  } |  j t | k rk |  j d 7_ |  j t k rt t d |  j ƒ |  j	 j
 ƒ  d Sn	 d |  _ | |  _ t |  ƒ |  _ |  j ƒ  } | d g } |  j s½ | j d ƒ n  |  j rß | j d |  j g ƒ n  |  j d k	 r| j |  j ƒ n  |  j	 j |  j | d | d	 |  j d
 |  j d |  j ƒd S(   s   Start this daemon.i   s   Can't keep %s running. Exiting.Ni    s   --ignore-sigints   --quiets   -ct   argst   envt   uidt   gid(   R1   R7   t   timeR8   t   RESTART_BURST_DELAYR9   t   MAXIMUM_CONSECUTIVE_RESTARTSR   RK   R%   t   stopt   WatchedProcessProtocolRN   R5   t   appendR6   t   extendt   optionst   spawnProcessR)   R0   R3   (   R<   t   nowt   exeRO   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   start}   s*    					c         C   s    |  j  s t d ƒ S|  j  j ƒ  S(   s   Stop this daemon.N(   R7   R   R1   t   kill(   R<   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRV   ™   s    	
c            sg   ‡ f d †  } ˆ j  j ˆ j ˆ j d t ƒ} | j ‡  f d †  ƒ | j | ƒ | j d „  ƒ | S(   sÆ  Connect to the remote daemon over AMP and perform the given command.

        @param name: The name of the command to perform.
        @param args: Arguments list to be passed to the connect method
        @param kwargs: Keywords arguments to pass to the connect method.
        @return: A L{Deferred} resulting in C{True} if the command was
            successful or C{False} otherwise.
        @see: L{RemoteLandscapeComponentCreator.connect}.
        c            s   ˆ  j  j ƒ  t S(   N(   R$   t
   disconnectR:   (   t   ignored(   R<   (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyR`   ª   s    t   quietc            s   t  |  ˆ  ƒ ƒ  S(   N(   t   getattr(   t   remote(   t   name(    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   <lambda>°   s    c         S   s   t  S(   N(   t   False(   t   x(    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRf   ²   s    (   R$   t   connectt   max_retriest   factorR:   t   addCallbackt
   addErrback(   R<   Re   RO   t   kwargsR`   t	   connected(    (   Re   R<   s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   _connect_and_callŸ   s    	c         C   s   |  j  d ƒ S(   Nt   exit(   Rp   (   R<   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   request_exitµ   s    c         C   s   |  j  d ƒ S(   Nt   ping(   Rp   (   R<   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt
   is_running¸   s    c         C   s    |  j  s t d ƒ S|  j  j ƒ  S(   sN   
        Return a Deferred which will fire when the process has died.
        N(   R7   R   R1   t   wait(   R<   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRu   ¾   s    	
c         C   s    |  j  s t d ƒ S|  j  j ƒ  S(   s¶   
        Wait for the process to die for C{GRACEFUL_WAIT_PERIOD}. If it hasn't
        died by that point, send it a SIGTERM. If it doesn't die for
        C{SIGKILL_DELAY},
        N(   R7   R   R1   t   wait_or_die(   R<   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRv   Æ   s    	
c         C   s   t  |  _ d S(   s£   Called by the watchdog when starting to shut us down.

        It will prevent our L{WatchedProcessProtocol} to restart the process
        when it exits.
        N(   Rg   R;   (   R<   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   prepare_for_shutdownÐ   s    c         C   s   |  j  S(   s>   Return a boolean indicating if the daemon should be restarted.(   R;   (   R<   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   allow_restartØ   s    c         C   s   |  j  j ƒ  d  S(   N(   R7   R   (   R<   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyR   Ü   s    N(   R   R   R   R-   Rj   Rk   R1   RZ   R   Rg   RD   RN   R^   RV   Rp   Rr   Rt   Ru   Rv   Rw   Rx   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyR   5   s$    								
		t   Brokerc           B   s   e  Z d  Z RS(   s   landscape-broker(   R   R   RK   (    (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRy   à   s   t   Monitorc           B   s   e  Z d  Z RS(   s   landscape-monitor(   R   R   RK   (    (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRz   ä   s   t   Managerc           B   s   e  Z d  Z d Z RS(   s   landscape-managert   root(   R   R   RK   R-   (    (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyR{   è   s   RW   c           B   sq   e  Z d  Z e Z d „  Z d „  Z e d „ Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d	 „  Z d
 „  Z RS(   su   
    A process-watching protocol which sends any of its output to the log file
    and restarts it when it dies.
    c         C   s(   | |  _  d  |  _ d  |  _ d  |  _ d  S(   N(   t   daemonR1   t   _wait_resultt   _delayed_really_killt   _delayed_terminate(   R<   R}   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRD   õ   s    			c         C   s   |  j  ƒ  |  j ƒ  S(   N(   t
   _terminateRu   (   R<   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyR_   û   s    
c         C   s   |  j  d  k	 r{ | r/ t d |  j j f ƒ n  y |  j  j t j ƒ Wn t k
 rY q{ Xt	 j
 t |  j ƒ } | |  _ n  d  S(   Ns   %s didn't exit. Sending SIGTERM(   t	   transportR1   R    R}   RK   t   signalProcesst   signalt   SIGTERMR	   R   t	   callLatert   SIGKILL_DELAYt   _really_killR   (   R<   t   warnt   delayed(    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyR   ÿ   s    c         C   sL   y |  j  j t j ƒ Wn t k
 r* n Xt d |  j j ƒ d  |  _	 d  S(   Ns    %s didn't die.  Sending SIGKILL.(
   R‚   Rƒ   R„   t   SIGKILLR	   R    R}   RK   R1   R   (   R<   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRˆ     s    c         C   sA   |  j  d  k	 r= y |  j  j t j ƒ Wq= t k
 r9 q= Xn  d  S(   N(   R‚   R1   Rƒ   R„   t   SIGUSR1R	   (   R<   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyR     s
    c         C   s/   |  j  j d  k r t d  ƒ St ƒ  |  _ |  j S(   N(   R‚   t   pidR1   R   R   R~   (   R<   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRu     s    
c         C   s(   t  j t |  j d t ƒ|  _ |  j ƒ  S(   NR‰   (   R   R†   t   GRACEFUL_WAIT_PERIODR   R:   R€   Ru   (   R<   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRv   #  s    	c         C   s   t  j j | ƒ d  S(   N(   RH   t   stdoutt   write(   R<   t   data(    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   outReceived(  s    c         C   s   t  j j | ƒ d  S(   N(   RH   t   stderrR   (   R<   R‘   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   errReceived,  s    c         C   s’   |  j  d k	 r |  j  j ƒ  n  |  j d k	 rM |  j j ƒ  rM |  j j ƒ  n  |  j d k	 ro |  j j d ƒ n |  j j ƒ  rŽ |  j j	 ƒ  n  d S(   s"   The process has ended; restart it.N(
   R   R1   t   cancelR€   t   activeR~   t   callbackR}   Rx   R^   (   R<   t   reason(    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   processEnded/  s    (   R   R   R   Rg   t   _killedRD   R_   R   Rˆ   R   Ru   Rv   R’   R”   R™   (    (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRW   í   s   									t   WatchDogc           B   sk   e  Z d  Z e e d	 d	 d	 d	 d	 d „ Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z RS(
   sj   
    The Landscape WatchDog starts all other landscape daemons and ensures that
    they are working.
    c      	      sÉ  t  ƒ  } | d  k r' t t t g } n  | d  k rf t | k rf t t | | ƒ d | d | j ƒ} n  | d  k r¥ t | k r¥ t t | | ƒ d | d | j ƒ} n  | d  k rä t | k rä t t | | ƒ d | d | j ƒ} n  | ˆ  _	 | ˆ  _
 | ˆ  _ t d  ˆ  j	 ˆ  j
 ˆ  j g ƒ ˆ  _ ˆ ˆ  _ d  ˆ  _ t ˆ  _ t j t j ‡  ‡ f d †  ƒ | d  k	 r¼| j d k r¼d t | j ƒ d t | j ƒ g }	 x ˆ  j D] }
 |	 |
 _ q¦Wn  i  ˆ  _ d  S(   NR>   R?   c            s   ˆ j  ˆ  j ƒ S(   N(   t   callFromThreadt   _notify_rotate_logs(   R„   t   frame(   R<   R   (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRf   ^  s   i    s   --cloness   --start-clones-over(   R   R1   Ry   Rz   R{   R   R?   R   R   t   brokert   monitort   managert   filtert   daemonsR   t	   _checkingRg   t	   _stoppingR„   RŒ   t   clonest   strt   start_clones_overRZ   t   _ping_failures(   R<   R   R>   R?   RŸ   R    R¡   t   enabled_daemonst   landscape_reactorRZ   R}   (    (   R<   R   s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRD   B  s>    				$			c         C   sb   g  } x= |  j  D]2 } | j ƒ  } | j | d „ ƒ | j | ƒ q Wd „  } t | ƒ j | ƒ S(   s6   Return a list of any daemons that are already running.c         S   s
   |  | f S(   N(    (   Rt   t   d(    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRf   p  s    c         S   s%   g  |  D] } | d r | d ^ q S(   Ni    i   (    (   t   rRh   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   got_all_resultss  s    (   R£   Rt   Rl   RX   R   (   R<   t   resultsR}   t   resultR®   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   check_runningh  s    	c         C   s,   x |  j  D] } | j ƒ  q
 W|  j ƒ  d S(   sm  
        Start all daemons. The broker will be started first, and no other
        daemons will be started before it is running and responding to DBUS
        messages.

        @return: A deferred which fires when all services have successfully
            started. If a daemon could not be started, the deferred will fail
            with L{DaemonError}.
        N(   R£   R^   t   start_monitoring(   R<   R}   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyR^   w  s    
c         C   s   |  j  j d |  j ƒ |  _ d S(   s;   Start monitoring processes which have already been started.i   N(   R   R†   t   _checkR¤   (   R<   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyR²   …  s    c            sÁ   | r° ˆ j  r° t d ˆ  j f ƒ ˆ  ˆ j k rD d ˆ j ˆ  <n  ˆ j ˆ  c d 7<ˆ j ˆ  d k r½ t d ˆ  j f ƒ ˆ  j ƒ  } ‡  ‡ f d †  } | j | ƒ | Sn d ˆ j ˆ  <d  S(   Ns   %s failed to respond to a ping.i    i   i   s   %s died! Restarting.c            s   ˆ  j  ƒ  d ˆ j ˆ  <d  S(   Ni    (   R^   R©   (   Ra   (   R}   R<   (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   stopped–  s    
(   R¥   R    RK   R©   RV   t   addBoth(   R<   Rt   R}   t   stoppingR´   (    (   R}   R<   s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   _restart_if_not_running‹  s    c            sl   g  } x= ˆ  j  D]2 } | j ƒ  } | j ˆ  j | ƒ | j | ƒ q W‡  f d †  } t | ƒ j | ƒ d  S(   Nc            s   ˆ  j  j d ˆ  j ƒ ˆ  _ d  S(   Ni   (   R   R†   R³   R¤   (   Ra   (   R<   (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt
   reschedule¥  s    (   R£   Rt   Rl   R·   RX   R   Rµ   (   R<   t   all_runningR}   Rt   R¸   (    (   R<   s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyR³   ž  s    c            s€   ˆ  j  d  k	 r. ˆ  j  j ƒ  r. ˆ  j  j ƒ  n  t ˆ  _ x ˆ  j D] } | j ƒ  qA W‡  f d †  } ˆ  j j	 ƒ  } | j
 | ƒ S(   Nc            sa   |  r+ g  ˆ  j  D] } | j ƒ  ^ q } n, t d ƒ g  ˆ  j  D] } | j ƒ  ^ q? } t | ƒ S(   NsF   Couldn't request that broker gracefully shut down; killing forcefully.(   R£   Rv   R   RV   R   (   t   broker_stoppedR}   R¯   Rh   (   R<   (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   terminate_processes´  s
    %
"(   R¤   R1   R–   R•   R:   R¥   R£   Rw   RŸ   Rr   Rl   (   R<   R}   R»   R°   (    (   R<   s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRr   ©  s    	c         C   s)   x |  j  D] } | j ƒ  q
 Wt ƒ  d  S(   N(   R£   R   (   R<   R}   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyR   Â  s    N(   R   R   R   R   Rg   R1   RD   R±   R^   R²   R·   R³   Rr   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyR›   <  s   		$						t   WatchDogConfigurationc           B   s   e  Z d  „  Z d „  Z RS(   c         C   sd   t  t |  ƒ j ƒ  } | j d d d d d ƒ| j d d d d d	 ƒ| j d
 d d d d ƒ| S(   Ns   --daemont   actiont
   store_truet   helps   Fork and run in the background.s
   --pid-filet   typeR§   s   The file to write the PID to.s   --monitor-onlysb   Don't enable management features. This is useful if you want to run the client as a non-root user.(   t   superR¼   t   make_parsert
   add_option(   R<   t   parser(    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRÂ   Ê  s    c         C   s)   t  t g } |  j s% | j t ƒ n  | S(   N(   Ry   Rz   t   monitor_onlyRX   R{   (   R<   R£   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   get_enabled_daemonsÖ  s    	(   R   R   RÂ   RÆ   (    (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyR¼   È  s   	c          C   sÎ   t  j ƒ  r t  j d ƒ n  t  j ƒ  t  j ƒ  rB t  j d ƒ n  t  j d ƒ t  j d t  j ƒ }  xV t d ƒ D]H } y t  j |  | ƒ Wqq t	 k
 r¸ } | j
 t
 j k r¹ ‚  q¹ qq Xqq Wt  j |  ƒ d  S(   Ni    i?   s	   /dev/nulli   (   R&   t   forkt   _exitt   setsidt   umaskt   opent   O_RDWRt   ranget   dup2t   OSErrort   errnot   EBADFt   close(   t   nullt   it   e(    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt	   daemonizeÝ  s    
t   WatchDogServicec           B   s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s>   | |  _  t d | j d | d | j ƒ  ƒ |  _ d |  _ d  S(   NR>   R?   Rª   i    (   R6   R›   R}   RÆ   t   watchdogt	   exit_code(   R<   R?   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRD   ò  s
    	c            s3  t  j ˆ  ƒ t j d ˆ  j j d ˆ  j j ƒ ˆ  j j d k rÕ t t	 ˆ  j j d ˆ  j j d f ƒ d d l
 m } d | _ xQ t ˆ  j j ƒ D]: } d	 | } t j d ˆ  j j | d ˆ  j j | ƒ q” Wn  t d  ƒ } | j ‡  f d
 †  ƒ ‡  f d †  } ‡  f d †  } | j | ƒ | j | ƒ | S(   Nt	   data_patht   log_diri    id   iÈ   iÿÿÿÿ(   t   MethodCallSenderi,  s	   -clone-%dc            s   ˆ  j  j ƒ  S(   N(   RØ   R±   (   t   _(   R<   (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRf     s    c            s_   |  r> t  d d j d „  |  Dƒ ƒ ƒ d ˆ  _ t j ƒ  d  Sˆ  j ƒ  t d ƒ ˆ  j j ƒ  S(   Ns4   ERROR: The following daemons are already running: %ss   , c         s   s   |  ] } | j  Vq d  S(   N(   RK   (   t   .0Rh   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pys	   <genexpr>  s    i   s   Watchdog watching for daemons.(	   R   RJ   RÙ   R   t   crasht
   _daemonizeR   RØ   R^   (   t   running_daemons(   R<   (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   start_if_not_running  s    	


c            s$   t  |  d ƒ d ˆ  _ t j ƒ  d  S(   Ns   Unknown error occurred!i   (   R   RÙ   R   Rß   (   t   failure(   R<   (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   die  s    	(   R
   t   startServicet   bootstrap_listt	   bootstrapR6   RÚ   RÛ   R¦   R   R   t   landscape.lib.ampRÜ   t   timeoutRÍ   R   R1   Rl   Rm   (   R<   RÜ   RÔ   t   suffixR°   Râ   Rä   (    (   R<   s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRå   ù  s(    	
	c         C   sa   |  j  j r] t ƒ  |  j  j r] t |  j  j d ƒ } | j t t j ƒ  ƒ ƒ | j	 ƒ  q] n  d  S(   Nt   w(
   R6   R}   RÖ   t   pid_fileRË   R   R§   R&   t   getpidRÒ   (   R<   t   stream(    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRà   &  s    c            sV   t  d ƒ t j ˆ  ƒ t j t j t j ƒ ˆ  j j ƒ  } | j ‡  f d †  ƒ | S(   Ns   Stopping client...c            s
   ˆ  j  ƒ  S(   N(   t   _remove_pid(   R­   (   R<   (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRf   7  s    (	   R   R
   t   stopServiceR„   t   SIGINTt   SIG_IGNRØ   Rr   Rµ   (   R<   t   done(    (   R<   s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRð   .  s    
c         C   s~   |  j  j } | d  k	 rz t j | t j ƒ rz t | ƒ } | j ƒ  } | j ƒ  | t	 t j
 ƒ  ƒ k rz t j | ƒ qz n  d  S(   N(   R6   Rì   R1   R&   t   accesst   W_OKRË   t   readRÒ   R§   Rí   t   unlink(   R<   Rì   Rî   R   (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyRï   :  s    !
(   R   R   RD   Rå   Rà   Rð   Rï   (    (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyR×   ð  s
   		-		s
   $data_pathR    R|   ií  s   $data_path/packages   $data_path/package/hash-ids   $data_path/package/binariess   $data_path/package/upgrade-tools   $data_path/messagess   $data_path/socketsiè  s   $data_path/custom-graph-scriptss   $log_dirs   $data_path/package/databasei¤  c          C   sF   x? t  j j ƒ  D]. }  |  j d ƒ s1 |  d k r t  j |  =q q Wd S(   sH  Unset dangerous environment variables.

    In particular unset all variables beginning with DEBIAN_ or DEBCONF_,
    to avoid any problems when landscape-client is invoked from its
    postinst script.  Some environment variables may be set which would affect
    *other* maintainer scripts which landscape-client invokes.
    t   DEBIAN_t   DEBCONF_t   LANDSCAPE_ATTACHMENTSt   MAILN(   Rø   Rù   (   Rú   Rû   (   R&   R'   t   keyst
   startswith(   t   key(    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   clean_environmentU  s    c         C   sô   t  ƒ  t ƒ  } | j |  ƒ y t j d ƒ j } Wn t k
 rS t j d ƒ n Xt	 j
 ƒ  d | f k r| t j d ƒ n  t | d ƒ t d ƒ } t | ƒ } | j | ƒ | d	 k rÍ d d l m } n  | j d t | t ƒ | j ƒ  | j S(
   s  Start the watchdog.

    This is the topmost function that kicks off the Landscape client.  It
    cleans up the environment, loads the configuration, and starts the
    reactor.

    @param args: Command line arguments, including the program name as the
        first element.
    @param reactor: The reactor to use.  If none is specified, the global
        reactor is used.
    @raise SystemExit: if command line arguments are bad, or when landscape-
        client is not running as 'root' or 'landscape'.
    R    s#   The 'landscape' user doesn't exist!i    s:   landscape-client can only be run as 'root' or 'landscape'.RØ   s   landscape-clientiÿÿÿÿ(   R   N(   Rÿ   R¼   t   loadR+   R,   R.   t   KeyErrorRH   Rq   R&   R*   R   R   R×   t   setServiceParentR1   t   twisted.internetR   R†   R   Rg   t   runRÙ   (   RO   R   R?   t   landscape_uidt   applicationt   watchdog_service(    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyR  c  s$    	
(F   R   R&   RÐ   RH   R+   R„   RS   t   loggingR    R   R   t   resourceR   R   R  R   t   twisted.internet.deferR   R   t   twisted.internet.protocolR   t   twisted.internet.errorR	   t   twisted.application.serviceR
   R   t   twisted.application.appR   t   landscape.deploymentR   R   t   landscape.lib.twisted_utilR   t   landscape.lib.logR   t   landscape.lib.bootstrapR   R   R   t   landscape.logR   t   landscape.broker.ampR   R   R   t   landscape.reactorR   RŽ   RU   RT   R‡   t	   ExceptionR   R   R   t   objectR   Ry   Rz   R{   RW   R›   R¼   RÖ   R×   Ræ   Rÿ   RI   R1   R  (    (    (    s6   /usr/lib/python2.7/dist-packages/landscape/watchdog.pyt   <module>   sl   «OŒ	T	