ó
½-'Nc           @   sž   d  d l  Z  d  d l m Z d  d l m Z d  d l m Z d  d l m Z m	 Z	 m
 Z
 d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   Deferred(   t   ProcessProtocol(   t   ProcessDone(   t   ManagerPlugint	   SUCCEEDEDt   FAILEDt   ShutdownFailedErrorc           B   s   e  Z d  Z d „  Z RS(   sv   Raised when a call to C{/sbin/shutdown} fails.

    @ivar data: The data that the process printed before failing.
    c         C   s   | |  _  d  S(   N(   t   data(   t   selfR   (    (    sE   /usr/lib/python2.7/dist-packages/landscape/manager/shutdownmanager.pyt   __init__   s    (   t   __name__t
   __module__t   __doc__R	   (    (    (    sE   /usr/lib/python2.7/dist-packages/landscape/manager/shutdownmanager.pyR   
   s   t   ShutdownManagerc           B   sJ   e  Z d d  „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   c         C   s,   | d  k r d d l m } n  | |  _ d  S(   Niÿÿÿÿ(   t   reactor(   t   Nonet   twisted.internetR   t   _process_factory(   R   t   process_factory(    (    sE   /usr/lib/python2.7/dist-packages/landscape/manager/shutdownmanager.pyR	      s    c         C   s-   t  t |  ƒ j | ƒ | j d |  j ƒ d S(   s   Add this plugin to C{registry}.

        The shutdown manager handles C{shutdown} activity messages broadcast
        from the server.
        t   shutdownN(   t   superR   t   registert   register_messaget   perform_shutdown(   R   t   registry(    (    sE   /usr/lib/python2.7/dist-packages/landscape/manager/shutdownmanager.pyR      s    c         C   s‘   | d } | d } t  ƒ  } | j |  j j ƒ | j j |  j | ƒ | j j |  j | ƒ |  j	 | | ƒ \ } } |  j
 j | | d | ƒd S(   sî   Request a system restart or shutdown.

        If the call to C{/sbin/shutdown} runs without errors the activity
        specified in the message will be responded as succeeded.  Otherwise,
        it will be responded as failed.
        s   operation-idt   reboott   argsN(   t   ShutdownProcessProtocolt   set_timeoutR   R   t   resultt   addCallbackt   _respond_successt
   addErrbackt   _respond_failuret   _get_command_and_argsR   t   spawnProcess(   R   t   messaget   operation_idR   t   protocolt   commandR   (    (    sE   /usr/lib/python2.7/dist-packages/landscape/manager/shutdownmanager.pyR   $   s    

	c         C   s    t  j d ƒ |  j t | | ƒ S(   Ns   Shutdown request succeeded.(   t   loggingt   infot   _respondR   (   R   R   R%   (    (    sE   /usr/lib/python2.7/dist-packages/landscape/manager/shutdownmanager.pyR   4   s    c         C   s&   t  j d ƒ |  j t | j j | ƒ S(   Ns   Shutdown request failed.(   R(   R)   R*   R   t   valueR   (   R   t   failureR%   (    (    sE   /usr/lib/python2.7/dist-packages/landscape/manager/shutdownmanager.pyR!   8   s    c         C   s8   i d d 6| d 6| d 6| d 6} |  j  j j | t ƒ S(   Ns   operation-resultt   typet   statuss   result-texts   operation-id(   R   t   brokert   send_messaget   True(   R   R.   R   R%   R$   (    (    sE   /usr/lib/python2.7/dist-packages/landscape/manager/shutdownmanager.pyR*   <   s
    

c         C   sK   d | j  d f } | r/ d d | d g } n d d | d g } d | f S(   ss   
        Returns a C{command, args} 2-tuple suitable for use with
        L{IReactorProcess.spawnProcess}.
        s   +%di<   s   /sbin/shutdowns   -rs!   Landscape is rebooting the systems   -hs%   Landscape is shutting down the system(   t   delay(   R   R&   R   t   minutesR   (    (    sE   /usr/lib/python2.7/dist-packages/landscape/manager/shutdownmanager.pyR"   C   s    			N(
   R
   R   R   R	   R   R   R   R!   R*   R"   (    (    (    sE   /usr/lib/python2.7/dist-packages/landscape/manager/shutdownmanager.pyR      s   						R   c           B   sM   e  Z d  Z e d d „ Z d „  Z d d „ Z d „  Z d „  Z d „  Z	 RS(	   s~  A ProcessProtocol for calling C{/sbin/shutdown}.

    C{shutdown} doesn't return immediately when a time specification is
    provided.  Failures are reported immediately after it starts and return a
    non-zero exit code.  The process protocol calls C{shutdown} and waits for
    failures for C{timeout} seconds.  If no failures are reported it fires
    C{result}'s callback with whatever output was received from the process.
    If failures are reported C{result}'s errback is fired.

    @ivar result: A L{Deferred} fired when C{shutdown} fails or
        succeeds.
    @ivar reboot: A flag indicating whether a shutdown or reboot should be
        performed.  Default is C{False}.
    @ivar delay: The time in seconds from now to schedule the shutdown.
        Default is 240 seconds.  The time will be converted to minutes using
        integer division when passed to C{shutdown}.
    ið   c         C   s4   t  ƒ  |  _ | |  _ | |  _ g  |  _ t |  _ d  S(   N(   R    R   R   R2   t   _dataR1   t   _waiting(   R   R   R2   (    (    sE   /usr/lib/python2.7/dist-packages/landscape/manager/shutdownmanager.pyR	   e   s
    			c         C   s   d j  |  j ƒ S(   s'   Get the data printed by the subprocess.t    (   t   joinR4   (   R   (    (    sE   /usr/lib/python2.7/dist-packages/landscape/manager/shutdownmanager.pyt   get_datal   s    i
   c         C   s   | j  | |  j ƒ d S(   si   
        Set the error checking timeout, after which C{result}'s callback will
        be fired.
        N(   t
   call_latert   _succeed(   R   R   t   timeout(    (    sE   /usr/lib/python2.7/dist-packages/landscape/manager/shutdownmanager.pyR   p   s    c         C   s    |  j  r |  j j | ƒ n  d S(   ss   Some data was received from the child.

        Add it to our buffer to pass to C{result} when it's fired.
        N(   R5   R4   t   append(   R   t   fdR   (    (    sE   /usr/lib/python2.7/dist-packages/landscape/manager/shutdownmanager.pyt   childDataReceivedw   s    	c         C   sQ   |  j  rM | j t ƒ r% |  j ƒ  qM |  j j t |  j ƒ  ƒ ƒ t |  _  n  d S(   s  Fire back the C{result} L{Deferred}.

        C{result}'s callback will be fired with the string of data received
        from the subprocess, or if the subprocess failed C{result}'s errback
        will be fired with the string of data received from the subprocess.
        N(	   R5   t   checkR   R:   R   t   errbackR   R8   t   False(   R   t   reason(    (    sE   /usr/lib/python2.7/dist-packages/landscape/manager/shutdownmanager.pyt   processEnded   s
    	c         C   s/   |  j  r+ |  j j |  j ƒ  ƒ t |  _  n  d S(   sA   Fire C{result}'s callback with data accumulated from the process.N(   R5   R   t   callbackR8   RA   (   R   (    (    sE   /usr/lib/python2.7/dist-packages/landscape/manager/shutdownmanager.pyR:      s    	(
   R
   R   R   RA   R	   R8   R   R>   RC   R:   (    (    (    sE   /usr/lib/python2.7/dist-packages/landscape/manager/shutdownmanager.pyR   R   s   			(   R(   t   twisted.internet.deferR    t   twisted.internet.protocolR   t   twisted.internet.errorR   t   landscape.manager.pluginR   R   R   t	   ExceptionR   R   R   (    (    (    sE   /usr/lib/python2.7/dist-packages/landscape/manager/shutdownmanager.pyt   <module>   s   
>