ó
½-'Nc           @   sØ   d  d l  Z  d  d l Z d  d l m Z m Z m Z 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 d „  Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d S(   iÿÿÿÿN(   t   getLevelNamet	   getLoggert   FileHandlert   StreamHandlert	   Formatter(   t   OptionParser(   t   ConfigParsert   NoSectionError(   t   VERSION(   t   Persist(   t   UPGRADE_MANAGERSc         C   sÜ   g  } t  j j |  j ƒ s. t  j |  j ƒ n  t  j j |  j | d ƒ } | j t | ƒ ƒ |  j s | j t	 t
 j ƒ ƒ n  t ƒ  j t |  j j ƒ  ƒ ƒ x7 | D]/ } t ƒ  j | ƒ d } | j t | ƒ ƒ q¥ Wd S(   s,   Given a basic configuration, set up logging.s   .logs;   %(asctime)s %(levelname)-8s [%(threadName)-10s] %(message)sN(   t   ost   patht   existst   log_dirt   makedirst   joint   appendR   t   quietR   t   syst   stdoutR   t   setLevelR    t	   log_levelt   uppert
   addHandlert   setFormatterR   (   t   configurationt   program_namet   handlerst   log_filenamet   handlert   format(    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyt   init_logging   s    	t   BaseConfigurationc           B   sú   e  Z d  Z d Z d Z d g Z e j j e j j	 e
 j d ƒ ƒ e j j	 d ƒ k rk e j d d ƒ n  e e ƒ Z d Z d „  Z d „  Z d d „ Z d	 „  Z d
 „  Z e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s  Base class for configuration implementations.

    @cvar required_options: Optionally, a sequence of key names to require when
        reading or writing a configuration.
    @cvar unsaved_options: Optionally, a sequence of key names to never write
        to the configuration file.  This is useful when you want to provide
        command-line options that should never end up in a configuration file.
    @cvar default_config_filenames: A sequence of filenames to check when
        reading or writing a configuration.
    s   /etc/landscape/client.confi    t   scriptss   landscape-client.conft   clientc         C   se   i  |  _  g  |  _ i  |  _ d |  _ i  |  _ |  j ƒ  |  _ |  j j j	 ƒ  |  _
 |  j j j ƒ  d S(   sk   Default configuration.

        Default values for supported options are set as in L{make_parser}.
        N(   t   _set_optionst   _command_line_argst   _command_line_optionst   Nonet   _config_filenamet   _config_file_optionst   make_parsert   _parsert   defaultst   copyt   _command_line_defaultst   clear(   t   self(    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyt   __init__6   s    					c         C   sÓ   xw |  j  |  j |  j |  j g D]  } | | k r | | } Pq q W|  j j d | j d d ƒ ƒ rn d } n t | ƒ ‚ t	 | t
 ƒ rÏ |  j j d | j d d ƒ ƒ } | d k	 rÏ | j d | ƒ } qÏ n  | S(   sø  Find and return the value of the given configuration parameter.

        The following sources will be searched:
          - The attributes that were explicitly set on this object,
          - The parameters specified on the command line,
          - The parameters specified in the configuration file, and
          - The defaults.

        If no values are found and the parameter does exist as a possible
        parameter, C{None} is returned.

        Otherwise C{AttributeError} is raised.
        s   --t   _t   -N(   R$   R&   R)   R.   R+   t
   has_optiont   replaceR'   t   AttributeErrort
   isinstancet
   basestringt
   get_optiont   convert_value(   R0   t   namet   optionst   valuet   option(    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyt   __getattr__F   s    	
"	"c         C   s*   y |  j  | ƒ SWn t k
 r% | SXd S(   s5   Return the value of the C{name} option or C{default}.N(   R?   R6   (   R0   R;   t   default(    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyt   getf   s    c         C   s<   | j  d ƒ r+ t t |  ƒ j | | ƒ n | |  j | <d S(   s£   Set a configuration parameter.

        If the name begins with C{_}, it will only be set on this object and
        not stored in the configuration file.
        R2   N(   t
   startswitht   superR!   t   __setattr__R$   (   R0   R;   R=   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyRD   m   s    c         C   s   |  j  |  j ƒ d S(   sh   Reload options using the configured command line arguments.

        @see: L{load_command_line}
        N(   t   loadR%   (   R0   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyt   reloadx   s    c         C   sï   |  j  | ƒ |  j r^ t j j |  j ƒ r> |  j |  j ƒ q˜ | s˜ t j d |  j ƒ q˜ n: x7 |  j D], } t j	 | t j
 ƒ rh |  j | ƒ Pqh qh W|  j ƒ  xF |  j D]; } t |  | ƒ s¬ t j d | j d d ƒ | f ƒ q¬ q¬ Wd S(   s   
        Load configuration data from command line arguments and a config file.

        @raise: A SystemExit if the arguments are bad.
        s   error: file not found: %ssB   error: must specify --%s or the '%s' directive in the config file.R2   R3   N(   t   load_command_linet   configR   R   t   isfilet   load_configuration_fileR   t   exitt   default_config_filenamest   accesst   R_OKt   _load_external_optionst   required_optionst   getattrR5   (   R0   t   argst   accept_nonexistent_configt   potential_config_fileR>   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyRE      s    	
	c         C   s   d S(   s<   Hook for loading options from elsewhere (e.g. for --import).N(    (   R0   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyRO   œ   s    c         C   s2   | |  _  |  j j | ƒ d } t | ƒ |  _ d S(   s4   Load configuration data from the given command line.i    N(   R%   R+   t
   parse_argst   varsR&   (   R0   RR   t   values(    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyRG   Ÿ   s    	c         C   sV   | |  _  t ƒ  } | j | ƒ y t | j |  j ƒ ƒ |  _ Wn t k
 rQ n Xd S(   s²   Load configuration data from the given file name.

        If any data has already been set on this configuration object,
        then the old data will take precedence.
        N(   R(   R   t   readt   dictt   itemst   config_sectionR)   R   (   R0   t   filenamet   config_parser(    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyRJ   ¥   s    		c         C   s  |  j  ƒ  } t ƒ  } | j | ƒ | j |  j ƒ sG | j |  j ƒ n  |  j j ƒ  } | j |  j	 ƒ | j |  j
 ƒ x| | j ƒ  D]n \ } } | d k rƒ | |  j k rƒ | |  j j | ƒ k rØ | j |  j | ƒ qñ | j |  j | | ƒ qƒ qƒ Wt | d ƒ } | j | ƒ | j ƒ  d S(   sì  Write back configuration to the configuration file.

        Values which match the default option in the parser won't be saved.

        Options are considered in the following precedence:

          1. Manually set options (C{config.option = value})
          2. Options passed in the command line
          3. Previously existent options in the configuration file

        The filename picked for saving configuration options is the one
        returned by L{get_config_filename}.
        RH   t   wN(   t   get_config_filenameR   RX   t   has_sectionR[   t   add_sectionR)   R-   t   updateR&   R$   RZ   t   unsaved_optionsR.   RA   t   remove_optiont   sett   opent   writet   close(   R0   R\   R]   t   all_optionsR;   R=   t   config_file(    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyRg   ´   s"    	c         C   s/   t  d t ƒ } | j d d d d d d ƒ| S(   sÐ   Parser factory for supported options

        @return: An L{OptionParser} preset with options that all
            landscape-related programs accept. These include
              - C{config} (C{None})
        t   versions   -cs   --configt   metavart   FILEt   helps}   Use config from this file (any command line options override settings from the file) (default: '/etc/landscape/client.conf').(   R   R   t
   add_option(   R0   t   parser(    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyR*   Ù   s    c         C   se   |  j  r |  j  S|  j r  |  j S|  j ra x* |  j D] } t j | t j ƒ r3 | Sq3 W|  j d Sd S(   sñ   Pick the proper configuration file.

        The picked filename is:
          1. C{self.config}, if defined
          2. The last loaded configuration file, if any
          3. The first filename in C{self.default_config_filenames}
        i    N(   RH   R(   RL   R   RM   RN   R'   (   R0   RT   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyR_   ç   s    			c         C   s   |  j  S(   sW   Get currently loaded command line options.

        @see: L{load_command_line}
        (   R&   (   R0   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyt   get_command_line_optionsú   s    (    (    N(   t   __name__t
   __module__t   __doc__RP   Rc   RL   R   R   t   dirnamet   abspathR   t   argvt   insertt   tupleR[   R1   R?   R'   RA   RD   RF   t   FalseRE   RO   RG   RJ   Rg   R*   R_   Rq   (    (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyR!   !   s,   
	"		 						%		t   Configurationc           B   s&   e  Z d  Z d „  Z e d „  ƒ Z RS(   sh   Configuration data for Landscape client.

    This contains all simple data, some of it calculated.
    c      	   C   sÖ   t  t |  ƒ j ƒ  } | j d d d d d d d d ƒ| j d	 d
 d t d d d d ƒ| j d d d d d d d d ƒ| j d d d d d ƒ| j d d d d t d d ƒ| j d d d d t d d ƒ| S(   s  Parser factory for supported options.

        @return: An L{OptionParser} preset for all options
            from L{BaseConfiguration.make_parser} plus:
              - C{data_path} (C{"/var/lib/landscape/client/"})
              - C{quiet} (C{False})
              - C{log_dir} (C{"/var/log/landscape"})
              - C{log_level} (C{"info"})
              - C{ignore_sigint} (C{False})
        s   -ds   --data-pathRl   t   PATHR@   s   /var/lib/landscape/client/Rn   sM   The directory to store data files in (default: '/var/lib/landscape/client/').s   -qs   --quiett   actiont
   store_trues"   Do not log to the standard output.s   -ls	   --log-dirRm   sD   The directory to write log files to (default: '/var/log/landscape').s   /var/log/landscapes   --log-levelt   infos/   One of debug, info, warning, error or critical.s   --ignore-sigints   Ignore interrupt signals.s   --ignore-sigusr1s%   Ignore SIGUSR1 signal to rotate logs.(   RC   R{   R*   Ro   Rz   (   R0   Rp   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyR*     s     c         C   s   t  j j |  j d ƒ S(   s@   Return the path to the directory where Unix sockets are created.t   sockets(   R   R   R   t	   data_path(   R0   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyt   sockets_path)  s    (   Rr   Rs   Rt   R*   t   propertyR‚   (    (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyR{     s   	!c         C   se   t  d |  j ƒ } t |  j } t j j |  j ƒ rD | j | ƒ n | j | ƒ | j	 |  j ƒ | S(   s¥   Get a L{Persist} database with upgrade rules applied.

    Load a L{Persist} database for the given C{service} and upgrade or
    mark as current, as necessary.
    R\   (
   R	   t   persist_filenameR
   t   service_nameR   R   R   t   applyt
   initializet   save(   t   servicet   persistt   upgrade_manager(    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyt   get_versioned_persist/  s    (   R   R   t   loggingR    R   R   R   R   t   optparseR   R   R   t	   landscapeR   t   landscape.lib.persistR	   t   landscape.upgradersR
   R    t   objectR!   R{   RŒ   (    (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyt   <module>   s   (	á-