ó
½-'Nc           @   s  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 d  d l	 m
 Z
 d  d l m Z d  d l m Z m Z m Z m Z m Z d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   failt   DeferredListt   succeed(   t   generate_script_hash(   t   Accumulator(   t   ManagerPlugin(   t   ProcessFailedErrort   ScriptRunnerMixint   ProcessTimeLimitReachedErrort   get_user_infot   UnknownUserErrort
   StoreProxyc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sI   
    Persist-like interface to store graph-points into SQLite store.
    c         C   s   | |  _  d  S(   N(   t   store(   t   selfR   (    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyt   __init__   s    c         C   s(   |  j  j | ƒ } | r  | d S| Sd  S(   Ni   (   R   t   get_graph_accumulate(   R   t   keyt   defaultt   graph_accumulate(    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyt   get   s    c         C   s"   |  j  j | | d | d ƒ d  S(   Ni    i   (   R   t   set_graph_accumulate(   R   R   t   value(    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyt   set   s    (   t   __name__t
   __module__t   __doc__R   R   R   (    (    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyR      s   		t   InvalidFormatErrorc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s#   | |  _  t j |  |  j ƒ  ƒ d  S(   N(   R   t	   ExceptionR   t   _get_message(   R   R   (    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyR   $   s    	c         C   s   d |  j  S(   Nu!   Failed to convert to number: '%s'(   R   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyR   (   s    (   R   R   R   R   (    (    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyR   "   s   	t   NoOutputErrorc           B   s   e  Z d  „  Z RS(   c         C   s   t  j |  d ƒ d  S(   Nu   Script did not output any value(   R   R   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyR   .   s    (   R   R   R   (    (    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyR   ,   s   t   ProhibitedUserErrorc           B   s    e  Z d  Z d „  Z d „  Z RS(   s   
    Raised when an attempt to run a script as a user that is not allowed.

    @ivar username: The username that was used
    c         C   s#   | |  _  t j |  |  j ƒ  ƒ d  S(   N(   t   usernameR   R   R   (   R   R   (    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyR   9   s    	c         C   s   d |  j  S(   Nu%   Custom graph cannot be run as user %s(   R   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyR   =   s    (   R   R   R   R   R   (    (    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyR   2   s   	t   CustomGraphPluginc           B   sž   e  Z d  Z d Z d Z d Z d Z d e j d „ Z	 d „  Z
 d „  Z d „  Z d	 „  Z e d
 „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sb   
    Manage adding and deleting custom graph scripts, and then run the scripts
    in a loop.
    i,  iè  i
   s   custom-graphc         C   s5   t  t |  ƒ j | ƒ | |  _ i  |  _ t |  _ d S(   sn   
        @param process_factory: The L{IReactorProcess} provider to run the
            process with.
        N(   t   superR    R   t   _create_timet   _datat   Truet   do_send(   R   t   process_factoryt   create_time(    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyR   K   s    		c         C   sm   t  t |  ƒ j | ƒ | j d |  j ƒ | j d |  j ƒ t |  j j ƒ |  _	 t
 |  j	 |  j ƒ |  _ d  S(   Ns   custom-graph-adds   custom-graph-remove(   R!   R    t   registert   register_messaget   _handle_custom_graph_addt   _handle_custom_graph_removeR   t   registryR   t   _persistR   t   run_intervalt   _accumulate(   R   R,   (    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyR(   U   s    c         C   sx   t  | d ƒ } |  j j j | ƒ } | rE | d } t j | ƒ n  |  j j j | ƒ | |  j k rt |  j | =n  d S(   sk   
        Handle remove custom-graph operation, deleting the custom graph scripts
        if found.
        s   graph-idi   N(   t   intR,   R   t	   get_grapht   ost   unlinkt   remove_graphR#   (   R   t   messaget   graph_idt   grapht   filename(    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyR+   ^   s    
c         C   s3  | d } | d } | d } t  | d ƒ } |  j j j } t j j | d ƒ } t j j | d | f ƒ } t j j | ƒ r t j | ƒ n  y t	 | ƒ d  \ }	 }
 Wn" t
 k
 rÎ t j d | ƒ nH Xt | d	 ƒ } |  j | | | | |	 |
 ƒ | |  j k r|  j | =n  |  j j j | | | ƒ d
 S(   ss   
        Handle add custom-graph operation, which can also update an existing
        custom graph script.
        R   t   interpretert   codes   graph-ids   custom-graph-scriptss   graph-%di   u)   Attempt to add graph with unknown user %st   wN(   R0   R,   t   configt	   data_pathR2   t   patht   joint   existsR3   R	   R
   t   loggingt   errort   filet   write_script_fileR#   R   t	   add_graph(   R   R5   t   usert   shellR:   R6   R=   t   scripts_directoryR8   t   uidt   gidt   script_file(    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyR*   m   s*    


		c         C   s   d | j  j | j d f S(   Nu   %s: %si    (   t	   __class__R   t   args(   R   t   e(    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyt   _format_exceptionŒ   s    c         C   s#   |  j  j j |  j |  j | ƒ d  S(   N(   R,   t   brokert   call_if_acceptedt   message_typet   send_message(   R   t   urgent(    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyt   exchange   s    c   
      C   s/  |  j  s d  St |  _  t |  j j j ƒ  ƒ } xo | D]g \ } } } | |  j k r5 t j j	 | ƒ rœ |  j
 | ƒ } i g  d 6d d 6| d 6|  j | <qœ q5 q5 Wi |  j d 6|  j d 6} i  } xF |  j j ƒ  D]5 \ } }	 |	 d } i g  d 6d d 6| d 6| | <qÐ W| |  _ |  j j j | d | ƒd  S(   Nt   valuesu    RB   s   script-hasht   typet   dataRT   (   R%   t   Falset   listR,   R   t
   get_graphsR#   R2   R>   t   isfilet   _get_script_hashRR   t	   iteritemsRP   RS   (
   R   RT   t   graphsR6   R8   RF   t   script_hashR5   t   new_datat   item(    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyRS   “   s$    		)
 	c         C   s   | |  j  k r d  Sy t | ƒ } Wn/ t k
 rT | rH t | ƒ ‚ qU t ƒ  ‚ n X|  j | | | ƒ } | r‹ |  j  | d j | ƒ n  d  S(   NRV   (   R#   t   floatt
   ValueErrorR   R   R/   t   append(   R   t   outputR6   t   nowRX   t	   step_data(    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyt   _handle_dataª   s    c         C   s¾   | |  j  k r d  S| j t ƒ rp | j j j d ƒ } | j j r\ d | | j j f } n  | |  j  | d <nJ | j t ƒ r d |  j f |  j  | d <n |  j	 | j ƒ |  j  | d <d  S(   Ns   utf-8s    %s (process exited with code %d)RB   u%   Process exceeded the %d seconds limit(
   R#   t   checkR   R   RX   t   decodet	   exit_codeR   t
   time_limitRO   (   R   t   failureR6   t   failure_value(    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyt   _handle_error¹   s    c         C   s,   t  | ƒ } | j ƒ  } | j ƒ  t | ƒ S(   N(   RC   t   readt   closeR   (   R   R8   t   file_objectt   script_content(    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyR]   É   s    
c         C   sP   t  |  _ t |  j j j ƒ  ƒ } | s1 t g  ƒ S|  j j j |  j	 |  j
 | ƒ S(   sr   
        Iterate all the custom graphs stored and then execute each script and
        handle the output.
        (   R$   R%   RZ   R,   R   R[   R   RP   RQ   RR   t   _continue_run(   R   R_   (    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyt   runÏ   s    	
c         C   s¿  g  } t  |  j ƒ  ƒ } xš| D]’\ } } } t j j | ƒ rR |  j | ƒ } n d } | |  j k rŒ i g  d 6d d 6| d 6|  j | <n | |  j | d <y t | ƒ \ } }	 }
 WnB t k
 rú } t	 | ƒ } | j
 |  j | ƒ | j | ƒ q n X|  j | ƒ sBt	 t | ƒ ƒ } | j
 |  j | ƒ | j | ƒ q n  t j j | ƒ sZq n  |  j | | |	 |
 i  |  j ƒ } | j |  j | | ƒ | j
 |  j | ƒ | j | ƒ q Wt | ƒ S(   Nt    RV   u    RB   s   script-hash(   R0   R"   R2   R>   R\   R]   R#   R	   R
   R    t
   addErrbackRp   Re   t   is_user_allowedR   t   _run_scriptRm   t   addCallbackRi   R   (   R   R_   t   deferred_listRg   R6   R8   RF   R`   RI   RJ   R>   RN   t   dt   result(    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyRu   Þ   s<    "N(   R   R   R   R.   t
   size_limitRm   RR   t   Nonet   timeR   R(   R+   R*   RO   RY   RU   RS   Ri   Rp   R]   Rv   Ru   (    (    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyR    A   s"   
										(   R2   R   RA   t   twisted.internet.deferR    R   R   t   landscape.lib.scriptcontentR   t   landscape.accumulateR   t   landscape.manager.pluginR   t!   landscape.manager.scriptexecutionR   R   R   R	   R
   t   objectR   R   R   R   R   R    (    (    (    sA   /usr/lib/python2.7/dist-packages/landscape/manager/customgraph.pyt   <module>   s   (
