
U6_c           @   s   d  d l  Z  d  d l Z d  d l Z d  d l m Z d Z e  j j e d  Z	 d d  Z d   Z e d  Z d e f d	     YZ d
 e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d S(   iN(   t   cElementTrees   /usr/local/mgr5s   sbin/mgrctlc         C   s   t  j |  d t  j d t  j d t  j } | j |  \ } } t j d d k  rb | | | j f S| j   | j   | j f Sd S(   s#    Выполнить команду t   stdint   stdoutt   stderri    i   N(   t
   subprocesst   Popent   PIPEt   communicatet   syst   version_infot
   returncodet   decode(   t   argst   inputt   spt   outt   err(    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyt   CmdRun   s
    *c         C   s   t  t d |  d g  d S(   s    Manager Restart s   -mt   exitN(   R   t   MGRCTL(   t   mgrname(    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyt	   ReloadMgr   s    c         C   s   t  j j t d  } | |  g } | r7 | j d  n  t |  \ } } } | d k rb | j   St d j |  | |    d S(   s1  
	Создать базу данных и пользователя к ней.
	Eсли второй аргумент=True, то создаваемый пользователь - root.
	TODO: Сейчас делается через шеловый скрипт. В теории надо наоборот.
	s   etc/scripts/create_dbt   1i    s!   Can`t create database {0}
{1}
{2}N(	   t   ost   patht   joint   MGRPATHt   appendR   t   stript	   Exceptiont   format(   t   dbnamet	   user_roott   dbcreate_scriptnamet   cmdargsR   R   t   code(    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyt   CreateDbMysql   s    
t   BaseServicec           B   sD   e  Z d  Z d   Z e d    Z e d  Z d   Z d   Z	 RS(   s\    Базовый виртуальный класс для работы с сервисами c         C   s   | |  _  d |  _ d S(   s    Конструктор N(   t   namet   Nonet   exist(   t   selfR&   (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyt   __init__5   s    	c            s     f d   } | S(   Nc            s   |  j  r   |  | |  Sd  S(   N(   R(   (   R)   R   t   kwargs(   t   method(    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyt   check_is_exist<   s    	(    (   R,   R-   (    (   R,   s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyt   if_exist:   s    c         C   s'   |  j  s d  St d |  j d g  d  S(   Nt   servicet   reload(   R(   R'   R   R&   (   R)   t   hard(    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyR0   A   s    	c         C   s'   |  j  s d  St d |  j d g  d  S(   NR/   t   start(   R(   R'   R   R&   (   R)   (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyt   enableF   s    	c         C   s'   |  j  s d  St d |  j d g  d  S(   NR/   t   stop(   R(   R'   R   R&   (   R)   (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyt   disableK   s    	(
   t   __name__t
   __module__t   __doc__R*   t   staticmethodR.   t   FalseR0   R3   R5   (    (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyR%   2   s   		t   CentosServicec           B   s;   e  Z d  Z d   Z e j d    Z e j d    Z RS(   s+    Класс для сервисов centos c         C   sz   t  j |  |  t j j d |  |  _ t j j d d j |   |  _ t j j |  j  pp t j j |  j  |  _	 d  S(   Ns   /etc/init.ds   /usr/lib/systemd/system/s   {0}.service(
   R%   R*   R   R   R   t   scriptR   t   systemd_unitt   existsR(   (   R)   R&   (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyR*   T   s    !c         C   s2   t  t |   j   r. t d |  j d g  n  d  S(   Nt	   chkconfigt   on(   t   superR;   R3   R   R&   (   R)   (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyR3   Z   s    c         C   s2   t  t |   j   r. t d |  j d g  n  d  S(   NR?   t   off(   RA   R;   R5   R   R&   (   R)   (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyR5   _   s    (   R6   R7   R8   R*   R%   R.   R3   R5   (    (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyR;   Q   s   	t   DebianServicec           B   s;   e  Z d  Z d   Z e j d    Z e j d    Z RS(   s+    Класс для сервисов centos c         C   sD   t  j |  |  t j j d |  |  _ t j j |  j  |  _ d  S(   Ns   /etc/init.d(   R%   R*   R   R   R   R<   R>   R(   (   R)   R&   (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyR*   h   s    c         C   s2   t  t |   j   r. t d |  j d g  n  d  S(   Ns   update-rc.dR3   (   RA   RC   R3   R   R&   (   R)   (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyR3   m   s    c         C   s2   t  t |   j   r. t d |  j d g  n  d  S(   Ns   update-rc.dR5   (   RA   RC   R5   R   R&   (   R)   (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyR5   r   s    (   R6   R7   R8   R*   R%   R.   R3   R5   (    (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyRC   e   s   	t
   BsdServicec           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   s,    Класс для сервисов FreeBSD c         C   sq   t  j |  |  t j j d |  |  _ t j j d |  |  _ t j j |  j  pg t j j |  j  |  _ d  S(   Ns	   /etc/rc.ds   /usr/local/etc/rc.d(	   R%   R*   R   R   R   t   script_baset
   script_extR>   R(   (   R)   R&   (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyR*   z   s    c         C   s   d  S(   N(    (   R)   (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyR3      s    c         C   s   d  S(   N(    (   R)   (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyR5      s    (   R6   R7   R8   R*   R3   R5   (    (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyRD   w   s   		t   Servicec           B   s   e  Z d  Z d   Z RS(   s    Прокси класс c         C   sj   t  j j d  r t |  St  j j d  r8 t |  St d g  \ } } } d | k rf t |  Sd S(   s+     Вернуть нужный объект s   /sbin/chkconfigs   /etc/debian_versiont   unamet   FreeBSDN(   R   R   t   isfileR;   RC   R   RD   (   R)   R&   RH   t   _(    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyt   __new__   s    

(   R6   R7   R8   RL   (    (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyRG      s   t   Mgrc           B   s\   e  Z d  Z d d  Z d   Z e d  Z d   Z d   Z d   Z	 d   Z
 d	   Z RS(
   s6    Класс для работы с манагером t   corec         C   s"   | |  _  t d |  j  g |  _ d  S(   Ns   -m(   R   R   t   mgrctl_list(   R)   R   (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyR*      s    	c         C   s   |  j  i d d 6 d S(   s,    Перезагрузить манагер. R   t   funcN(   t   query(   R)   (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyR      s    c   	      C   s   d | k r d | d <n  |  j  } | j | j d   g  | D] } d | | j |  f ^ q@ } | j |  | r | j d  n  t |  \ } } } | d k r | St | |   d S(   s<    Сделать запрос. Вернуть XML ответ R   t   xmlRP   s   %s=%ss   -Ri    N(   RO   R   t   popt   gett   extendR   R   (	   R)   t   paramst   restartt
   query_listt   kt
   param_listR   R   t   errcode(    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyRQ      s    
,c         C   s[   t  j |  j i d d 6| d 6d t  } | j d  } | d k	 rW | j rW | j Sd S(   s    Получить path s   pathlist.editRP   t   elidRW   t   valueN(   t   etreet
   fromstringRQ   t   Truet   findR'   t   text(   R)   t	   path_namet   path_value_xmlt   elem(    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyt   path_get   s    
c         C   s[   t  j |  j i d d 6| d 6d t  } | j d  } | d k	 rW | j rW | j Sd S(   s$    Получить параметр  s   paramlist.editRP   R\   RW   R]   N(   R^   R_   RQ   R`   Ra   R'   Rb   (   R)   t
   param_namet   param_value_xmlRe   (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyt	   param_get   s    
c         C   s'   |  j  |  } | r | j   Sg  Sd  S(   N(   Ri   t   split(   R)   Rg   t   param_value(    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyt   param_get_list   s    
c         C   sl   |  j  |  } | | k rh | j |  |  j i d d 6| d 6d j |  d 6d d 6 |  j   t St S(   s$    Добавить параметр  s   paramlist.editRP   R\   t    R]   t   okt   sok(   Rl   R   RQ   R   R   R`   R:   (   R)   Rg   Rk   t
   cur_values(    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyt	   param_add   s    	
c         C   sl   |  j  |  } | | k rh | j |  |  j i d d 6| d 6d j |  d 6d d 6 |  j   t St S(   s$    Добавить параметр  s   paramlist.editRP   R\   Rm   R]   Rn   Ro   (   Rl   t   removeRQ   R   R   R`   R:   (   R)   Rg   Rk   Rp   (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyt   param_remove   s    	
(   R6   R7   R8   R*   R   R:   RQ   Rf   Ri   Rl   Rq   Rs   (    (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyRM      s   					(   R   R   R   t	   xml.etreeR    R^   R   R   R   R   R'   R   R   R:   R$   t   objectR%   R;   RC   RD   RG   RM   (    (    (    s/   /usr/local/mgr5/lib/pkgpython/core_pkg_funcs.pyt   <module>   s   		