ó
ÍÿŠTc           @   sÀ   d  d l  Z  d  d l 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 e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   Failure(   t   getProcessValuet   getProcessOutputAndValue(   t   succeed(   t   log_failure(   t   ManagerPlugint	   SUCCEEDEDt   FAILEDt   CharmScriptErrorc           B   s    e  Z d  Z d „  Z d „  Z RS(   s²   
    Raised when a charm-provided script fails with a non-zero exit code.

    @ivar script: the name of the failed script
    @ivar code: the exit code of the failed script
    c         C   s,   | |  _  | |  _ t j |  |  j ƒ  ƒ d  S(   N(   t   scriptt   codet	   Exceptiont   __init__t   _get_message(   t   selfR	   R
   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyR      s    		c         C   s   d |  j  |  j f S(   Ns3   Failed charm script: %s exited with return code %d.(   R	   R
   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyR      s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyR      s   	t   RunPartsErrorc           B   s    e  Z d  Z d „  Z d „  Z RS(   sË   
    Raised when a charm-provided health script run-parts directory contains
    a health script that fails with a non-zero exit code.

    @ivar stderr: the stderr from the failed run-parts command
    c         C   s:   d | j  d ƒ d j ƒ  |  _ t j |  |  j ƒ  ƒ d  S(   Ns   %st   :i   (   t   splitt   stript   messageR   R   R   (   R   t   stderr(    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyR   &   s     c         C   s   d |  j  S(   Ns   Failed charm script: %s.(   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyR   *   s    (   R   R   R   R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyR      s   	t	   HAServicec           B   sƒ   e  Z d  Z d Z d Z d Z 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(   sÏ   
    Plugin to manage this computer's active participation in a
    high-availability cluster. It depends on charms delivering both health
    scripts and cluster_add cluster_remove scripts to function.
    s   /var/lib/juju/agentst   add_to_clustert   remove_from_clusters   health_checks.du   standbyu   onlinec         C   s-   t  t |  ƒ j | ƒ | j d |  j ƒ d  S(   Ns   change-ha-service(   t   superR   t   registert   register_messaget   handle_change_ha_service(   R   t   registry(    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyR   <   s    	c         C   sV   i d d 6| d 6| d 6} | r: | j  d d ƒ | d <n  |  j j j | |  j t ƒ S(   Ns   operation-resultt   typet   statuss   operation-ids   utf-8t   replaces   result-text(   t   decodeR   t   brokert   send_messaget   _session_idt   True(   R   R!   t   datat   operation_idR   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyt   _respondA   s    

c         C   s    t  j | ƒ |  j t | | ƒ S(   N(   t   loggingt   infoR*   R   (   R   R(   R   R)   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyt   _respond_successJ   s    c         C   s#   t  | ƒ |  j t | j ƒ  | ƒ S(   s   Handle exception failures.(   R   R*   R   t   getErrorMessage(   R   t   failureR)   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyt   _respond_failureN   s    
c         C   s    t  j | ƒ |  j t | | ƒ S(   s   Only handle string failures.(   R+   t   errorR*   R   (   R   t   failure_stringR)   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyt   _respond_failure_stringS   s    c         C   s   t  j j | |  j ƒ } t  j j | ƒ s; t  j | ƒ r\ d | } t j | ƒ t | ƒ Sd „  } t	 d | g d t  j
 ƒ} | j | ƒ S(   so   
        Exercise any discovered health check scripts, will return a deferred
        success or fail.
        s4   Skipping juju charm health checks. No scripts at %s.c         S   s2   |  \ } } } | d k r* t  | ƒ ‚ n d Sd  S(   Ni    s   All health checks succeeded.(   R   (   t   .0t   stdout_datat   stderr_dataR!   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyt   parse_outputf   s    s	   run-partst   env(   t   ost   patht   joint   HEALTH_SCRIPTS_DIRt   existst   listdirR+   R,   R   R   t   environt   addCallback(   R   t   scripts_patht
   health_dirR   R7   t   result(    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyt   _run_health_checksX   s    #
	c         C   s|   | d k r |  j  } n	 |  j } t j j | | ƒ } t j j | ƒ si t j d | | f ƒ t d ƒ Sd „  } | | ƒ S(   sì   
        Enables or disables a unit's participation in a cluster based on
        running charm-delivered CLUSTER_ONLINE and CLUSTER_STANDBY scripts
        if they exist. If the charm doesn't deliver scripts, return succeed().
        u   onlinesT   Ignoring juju charm cluster state change to '%s'. Charm script does not exist at %s.sn   This computer is always a participant in its high-availabilty cluster. No juju charm cluster settings changed.c         S   s.   t  |  d t j ƒ} d „  } | j | |  ƒ S(   NR8   c         S   s*   |  d k r t  | |  ƒ ‚ n d | Sd  S(   Ni    s   %s succeeded.(   R   (   R
   R	   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyt   validate_exit_codeˆ   s    (   R   R9   R?   R@   (   R	   RC   RE   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyt
   run_script…   s    	(	   t   CLUSTER_ONLINEt   CLUSTER_STANDBYR9   R:   R;   R=   R+   R,   R   (   R   t   _RA   t   service_statet   script_nameR	   RF   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyt   _change_cluster_participationp   s    			
c         C   sG   t  d ƒ } | |  j k r- |  j | ƒ } n  | j |  j | | ƒ | S(   s  
        Handle specific state change requests through calls to available
        charm scripts like C{CLUSTER_ONLINE}, C{CLUSTER_STANDBY} and any
        health check scripts. Assume success in any case where no scripts
        exist for a given task.
        N(   R   t   Nonet   STATE_ONLINERD   R@   RL   (   R   RA   RJ   R)   t   d(    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyt   _perform_state_change‘   s    c         C   sd  | d } y8d } | d } | d } | d } d | | f } | |  j  |  j g k rf d | } n  d | j d	 d
 ƒ } t j j |  j | d ƒ }	 t j j |  j ƒ sµ d } n t j j |	 ƒ sÔ d | } n  | rê |  j | | ƒ St j j |	 d ƒ }
 |  j	 |
 | | ƒ } | j
 |  j | | ƒ | j |  j | ƒ | SWn |  j t ƒ  | ƒ | SXd S(   s)   Parse incoming change-ha-service messagess   operation-idu    s   service-names	   unit-names   service-states&   %s high-availability service set to %su1   Invalid cluster participation state requested %s.s   unit-t   /t   -t   charmuZ   This computer is not deployed with juju. Changing high-availability service not supported.uO   This computer is not juju unit %s. Unable to modify high-availability services.t   scriptsN(   t   STATE_STANDBYRN   R"   R9   R:   R;   t   JUJU_UNITS_BASER=   R3   RP   R@   R-   t
   addErrbackR0   R    (   R   R   R)   t   error_messaget   service_namet	   unit_nameRJ   t   change_messaget	   unit_patht
   charm_pathRA   RO   (    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyR   ¡   s<    




	

(   R   R   R   RV   RG   RH   R<   RU   RN   R   R*   R-   R0   R3   RD   RL   RP   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyR   .   s    								!	(   R+   R9   t   twisted.python.failureR    t   twisted.internet.utilsR   R   t   twisted.internet.deferR   t   landscape.lib.logR   t   landscape.manager.pluginR   R   R   R   R   R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/landscape/manager/haservice.pyt   <module>   s   