ó
ÍÿŠTc           @   sô   d  d l  Z  d  d l Z d  d l m Z m Z d  d l m Z m Z m Z m	 Z	 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 e f d „  ƒ  YZ d „  Z d S(   iÿÿÿÿN(   t	   ConfigObjt   ConfigObjError(   t   getLevelNamet	   getLoggert   FileHandlert   StreamHandlert	   Formatter(   t   OptionParsert   SUPPRESS_HELP(   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   ConfigSpecOptionParserc           B   s#   e  Z i  Z d d  „ Z d „  Z RS(   c         C   s   t  j |  | ƒ d  S(   N(   R   t   __init__(   t   selft   unsaved_options(    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyR#   &   s    c         O   s/   t  j |  | | Ž } t | ƒ GH| j ƒ  GH| S(   N(   R   t
   add_optiont   dirt   get_opt_string(   R$   t   argst   kwargst   option(    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyR&   )   s    N(   t   __name__t
   __module__t   _config_spec_definitionst   NoneR#   R&   (    (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyR"   "   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 „  Z d d	 „ Z d
 „  Z d „  Z e d „ Z d „  Z d „  Z d „  Z d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   sV  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.

    Default values for supported options are set as in L{make_parser}.
    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(   N(   t   _set_optionst   _command_line_argst   _command_line_optionsR/   t   _config_filenamet   _config_file_optionst   make_parsert   _parsert   defaultst   copyt   _command_line_defaultst   clear(   R$   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyR#   G   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(   R3   R5   R7   R<   R9   t
   has_optiont   replaceR/   t   AttributeErrort
   isinstancet
   basestringt
   get_optiont   convert_value(   R$   t   namet   optionst   valueR+   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyt   __getattr__S   s    	
"	"c         C   sR   |  j  ƒ  } |  j j ƒ  | _ |  j j ƒ  | _ |  j | _ |  j j ƒ  | _ | S(   sX   
        Return a new configuration object, with the same settings as this one.
        (   t	   __class__R3   R;   R5   R6   R7   (   R$   t   config(    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyt   clones   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(   RJ   RB   (   R$   RG   t   default(    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyt   get~   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.
        R>   N(   t
   startswitht   superR0   t   __setattr__R3   (   R$   RG   RI   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyRR   …   s    c         C   s   |  j  |  j ƒ d S(   sh   Reload options using the configured command line arguments.

        @see: L{load_command_line}
        N(   t   loadR4   (   R$   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyt   reload   s    c         C   s  |  j  | ƒ |  j r+ |  j g } t } n |  j } | } x… | D]> } t j j | ƒ rA t j | t j ƒ rA |  j	 | ƒ PqA qA W| sÂ t
 | ƒ d k r¬ d | d } n d } t j | ƒ n  |  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.

        @param accept_nonexistent_default_config: If True, don't complain if
            default configuration files aren't found

        @raise: A SystemExit if the arguments are bad.

        i   s#   error: config file %s can't be readi    s#   error: no config file could be readsB   error: must specify --%s or the '%s' directive in the config file.R>   R?   N(   t   load_command_lineRL   t   Falset   default_config_filenamesR   R   t   isfilet   accesst   R_OKt   load_configuration_filet   lenR   t   exitt   _load_external_optionst   required_optionst   getattrRA   (   R$   R)   t!   accept_nonexistent_default_configt   config_filenamest   allow_missingt   config_filenamet   messageR+   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyRS   —   s,    
			
	c         C   s   d S(   s<   Hook for loading options from elsewhere (e.g. for --import).N(    (   R$   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyR^   Ä   s    c         C   s2   | |  _  |  j j | ƒ d } t | ƒ |  _ d S(   s4   Load configuration data from the given command line.i    N(   R4   R9   t
   parse_argst   varsR5   (   R$   R)   t   values(    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyRU   Ç   s    	c         C   sA   | |  _  |  j ƒ  } y | |  j |  _ Wn t k
 r< 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(   R6   t   _get_config_objectt   config_sectionR7   t   KeyError(   R$   t   filenamet
   config_obj(    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyR[   Í   s    	c      	   C   ss   | p |  j  ƒ  } y" t | d t d t d t ƒ} Wn8 t k
 rn } t ƒ  } | j t | ƒ ƒ | j } n X| S(   s»   Create a L{ConfigObj} consistent with our preferences.

        @param config_source: Optional readable source to read from instead of
            the default configuration file.
        t   list_valuest   raise_errorst   write_empty_values(	   t   get_config_filenameR    RV   t   TrueR   R   t   warnt   strRL   (   R$   t   alternative_configt   config_sourceRm   t   et   logger(    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyRi   Ú   s    	c         C   s9  |  j  ƒ  } |  j ƒ  } |  j | k r7 i  | |  j <n  |  j j ƒ  } | j |  j ƒ | j |  j ƒ | |  j } xŸ | j ƒ  D]‘ \ } } | d k r€ | |  j	 k r€ | |  j
 j | ƒ k r| |  j k r| |  j k r| | |  j k r| |  j | =qq| | | <q€ q€ W| | |  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}.
        RL   N(   Rq   Ri   Rj   R7   R;   t   updateR5   R3   t   itemsR%   R<   RO   Rl   t   write(   R$   Rl   Rm   t   all_optionst   sectionRG   RI   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyR{   õ   s&    	c      	   C   sQ   t  d t ƒ } | j d d d d d d ƒ| j d d	 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})
              - C{data_path} (C{"/var/lib/landscape/client/"})
        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').s   -ds   --data-patht   PATHRN   s   /var/lib/landscape/client/sM   The directory to store data files in (default: '/var/lib/landscape/client/').(   R   R	   R&   (   R$   t   parser(    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyR8      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(   RL   R6   RW   R   RY   RZ   R/   (   R$   t   potential_config_file(    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyRq   3  s    			c         C   s   |  j  S(   sW   Get currently loaded command line options.

        @see: L{load_command_line}
        (   R5   (   R$   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyt   get_command_line_optionsF  s    (    (    N(    R,   R-   t   __doc__R_   R%   RW   R   R   t   dirnamet   abspathR   t   argvt   insertt   tupleRj   R#   RJ   RM   R/   RO   RR   RT   RV   RS   R^   RU   R[   Ri   R{   R8   Rq   R…   (    (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyR0   0   s0   	"		 			-				+		t   Configurationc           B   sJ   e  Z d  Z d Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z RS(   sh   Configuration data for Landscape client.

    This contains all simple data, some of it calculated.
    s.   https://landscape.canonical.com/message-systemc      
   C   s£  t  t |  ƒ j ƒ  } | 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 |  j d d ƒ| j d d d d d ƒ| j d d d d ƒ| j d d d d t d d ƒ| j d d d d t d d ƒ| j d d d/ d! d" d d# ƒ| j d$ d d1 d! d" d d& ƒ| j d' d d2 d! d" d
 d) d d* ƒ| j d+ d d, d! t d t ƒ| j d- d d3 d! t d t ƒ| S(4   s  Parser factory for supported options.

        @return: An L{OptionParser} preset for all options
            from L{BaseConfiguration.make_parser} plus:
              - C{quiet} (C{False})
              - C{log_dir} (C{"/var/log/landscape"})
              - C{log_level} (C{"info"})
              - C{url} (C{"http://landscape.canonical.com/message-system"})
              - C{ping_url} (C{"http://landscape.canonical.com/ping"})
              - C{ssl_public_key}
              - C{ignore_sigint} (C{False})
        s   -qs   --quietRN   t   actiont
   store_trueR   s"   Do not log to the standard output.s   -ls	   --log-dirR   R€   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   -us   --urls   The server URL to connect to.s
   --ping-urls8   The URL to perform lightweight exchange initiation with.s#   http://landscape.canonical.com/pings   -ks   --ssl-public-keysM   The public SSL key to verify the server. Only used if the given URL is https.s   --ignore-sigints   Ignore interrupt signals.s   --ignore-sigusr1s%   Ignore SIGUSR1 signal to rotate logs.s   --package-monitor-intervali   i<   t   typet   ints:   The interval between package monitor runs (default: 1800).s   --apt-update-intervali   s6   The interval between apt update runs (default: 21600).s   --flush-intervali   t   INTERVALsB   The number of seconds between flushes to disk for persistent data.s   --clonesi    s   --start-clones-overi   i  ih  i`T  i,  iÜ  (   RQ   RŒ   R8   R&   RV   t   DEFAULT_URLR‘   R   (   R$   Rƒ   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyR8   V  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(   R$   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyt   sockets_path  s    c         C   s   t  j j |  j d ƒ S(   sk   
        Return the path to the directory where additional annotation files can
        be stored.
        s   annotations.d(   R   R   R   R•   (   R$   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyt   annotations_path”  s    c         C   s   t  j j |  j d ƒ S(   sU   The path to the previously sinlge juju-info file for
        backwards-compatibility.s   juju-info.json(   R   R   R   R•   (   R$   (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyt   juju_filenameœ  s    (	   R,   R-   R†   R“   R8   t   propertyR–   R—   R˜   (    (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyRŒ   N  s   	9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.
    Rl   (
   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	   configobjR    R   t   loggingR   R   R   R   R   t   optparseR   R   t	   landscapeR	   t   landscape.lib.persistR
   t   landscape.upgradersR   R!   R"   t   objectR0   RŒ   R¢   (    (    (    s8   /usr/lib/python2.7/dist-packages/landscape/deployment.pyt   <module>   s   (	ÿ U