ó
ÍÿŠTc           @   sæ   d  Z  d d l Z d d l m Z d d l m Z d d l m Z d d l m	 Z	 d d l
 m Z m Z d d l m Z d	 e f d
 „  ƒ  YZ d „  Z d „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   sÍ  
Handle client registration against the server.

When the service is started for the first time it connects to the server
as a new client without providing any identification credentials, and the
server replies with the available registration mechanisms. At this point
the machinery in this module will notice that we have no identification
credentials yet and that the server accepts registration messages, so it
will craft an appropriate one and send it out.
iÿÿÿÿN(   t   Deferred(   t   get_juju_info(   t   is_valid_tag_list(   t   get_fqdn(   t   get_vm_infot   get_container_info(   t   is_version_highert   InvalidCredentialsErrorc           B   s   e  Z d  Z RS(   sx   
    Raised when an invalid account title and/or registration key
    is used with L{RegistrationManager.register}.
    (   t   __name__t
   __module__t   __doc__(    (    (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyR      s   c            s+   ‡  f d †  } ‡  f d †  } t  | | ƒ S(   Nc            s   |  j  j ˆ  ƒ S(   N(   t   _persistt   get(   t   self(   t   name(    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyR      s    c            s   |  j  j ˆ  | ƒ d  S(   N(   R   t   set(   R   t   value(   R   (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyR   "   s    (   t   property(   R   R   R   (    (   R   sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyt   persist_property   s    c            s   ‡  f d †  } t  | ƒ S(   Nc            s   t  |  j ˆ  ƒ S(   N(   t   getattrt   _config(   R   (   R   (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyR   *   s    (   R   (   R   R   (    (   R   sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyt   config_property(   s    t   Identityc           B   sk   e  Z d  Z e d ƒ Z e d ƒ Z e d ƒ Z e d ƒ Z e d ƒ Z	 e d ƒ Z
 e d ƒ Z d „  Z RS(	   sf  Maintains details about the identity of this Landscape client.

    @ivar secure_id: A server-provided ID for secure message exchange.
    @ivar insecure_id: Non-secure server-provided ID, mainly used with
        the ping server.
    @ivar computer_title: See L{BrokerConfiguration}.
    @ivar account_name: See L{BrokerConfiguration}.
    @ivar registration_password: See L{BrokerConfiguration}.
    @ivar tags: See L{BrokerConfiguration}

    @param config: A L{BrokerConfiguration} object, used to set the
        C{computer_title}, C{account_name} and C{registration_password}
        instance variables.
    s	   secure-ids   insecure-idt   computer_titlet   account_namet   registration_keyt   tagst   access_groupc         C   s   | |  _  | j d ƒ |  _ d  S(   Nt   registration(   R   t   root_atR   (   R   t   configt   persist(    (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyt   __init__I   s    	(   R   R	   R
   R   t	   secure_idt   insecure_idR   R   R   R   R   R   R    (    (    (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyR   0   s   t   RegistrationHandlerc           B   sb   e  Z d  Z d
 d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z RS(   sÉ   
    An object from which registration can be requested of the server,
    and which will handle forced ID changes from the server.

    L{register} should be used to perform initial registration.
    c         C   sâ   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ |  j j d |  j ƒ |  j j d |  j ƒ |  j j d |  j	 ƒ |  j j
 d |  j ƒ |  j j
 d |  j ƒ |  j j
 d |  j ƒ d  |  _ | |  _ d  |  _ d  |  _ d  S(   Nt   runs   pre-exchanges   exchange-dones   set-ids
   unknown-idR   (   R   t	   _identityt   _reactort	   _exchanget   _pingert   _message_storet   call_ont   _get_juju_datat   _handle_pre_exchanget   _handle_exchange_donet   register_messaget   _handle_set_idt   _handle_unknown_idt   _handle_registrationt   Nonet   _should_registert   _fetch_asynct	   _ec2_datat
   _juju_data(   R   R   t   identityt   reactort   exchanget   pingert   message_storet   fetch_async(    (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyR    V   s"    						
			c         C   s>   |  j  } | j r t St | j o: | j o: |  j j d ƒ ƒ S(   Nt   register(   R%   R!   t   Falset   boolR   R   R)   t   accepts(   R   t   id(    (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyt   should_registerj   s
    		c         C   s;   d |  j _ d |  j _ t |  j ƒ j } |  j j ƒ  | S(   sô   
        Attempt to register with the Landscape server.

        @return: A L{Deferred} which will either be fired with None if
            registration was successful or will fail with an
            L{InvalidCredentialsError} if not.
        N(	   R2   R%   R!   R"   t   RegistrationResponseR&   t   deferredR'   R9   (   R   t   result(    (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyR=   r   s
    c         C   s,   t  |  j ƒ } | d k r d S| |  _ d S(   s   Load Juju information.N(   R   R   R2   R6   (   R   t	   juju_info(    (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyR+   €   s    c         C   s*   |  j  ƒ  r& |  j r& |  j j ƒ  n  d S(   sM  Registered handler for the C{"exchange-done"} event.

        If we are not registered yet, schedule another message exchange.

        The first exchange made us accept the message type "register", so
        the next "pre-exchange" event will make L{_handle_pre_exchange}
        queue a registration message for delivery.
        N(   RB   R3   R'   R9   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyR-   ‡   s    	c         C   sÅ  |  j  ƒ  |  _ |  j s d S|  j } | j } | sH |  j j d ƒ d S| j } | j } | j } |  j	 j
 ƒ  t | ƒ s’ d } t j d ƒ n  i d d 6t ƒ  d 6| d 6| j d 6| j d	 6| d
 6t ƒ  d 6t ƒ  d 6} | rò | | d <n  |  j	 j ƒ  } |  j rPt | d ƒ rPi |  j d d 6|  j d d 6|  j d d 6| d <n  t | ƒ rbd n d } | rxd | n d }	 | rŽd | n d }
 t j d | |
 |	 | f ƒ |  j j | ƒ d S(   sÈ   
        An exchange is about to happen.  If we don't have a secure id already
        set, and we have the needed information available, queue a registration
        message with the server.
        Ns   registration-faileds'   Invalid tags provided for registration.R=   t   typet   hostnameR   R   t   registration_passwordR   s   container-infos   vm-infoR   s   3.3s   environment-uuids   api-addressess
   machine-ids	   juju-infot   witht   withouts   and tags %s t    s   in access group '%s' u?   Queueing message to register with account %r %s%s%s a password.(   RB   R3   R%   R   R&   t   fireR   R   R   R)   t   delete_all_messagesR   R2   t   loggingt   errorR   R   R   R   t   get_server_apiR6   R   R?   t   infoR'   t   send(   R   R7   R   R   t   groupR   t   messaget
   server_apit	   with_wordt	   with_tagst
   with_group(    (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyR,   “   sL    						




c         C   s¡   |  j  } | j r) t j d | j ƒ n  | j d ƒ | _ | j d ƒ | _ t j d | j d | j ƒ t j d | j ƒ |  j j	 d ƒ |  j j	 d ƒ d	 S(
   s×   Registered handler for the C{"set-id"} event.

        Record and start using the secure and insecure IDs from the given
        message.

        Fire C{"registration-done"} and C{"resynchronize-clients"}.
        s   Overwriting secure_id with '%s'RA   s   insecure-ids2   Using new secure-id ending with %s for account %s.iöÿÿÿs   Using new secure-id: %ss   registration-dones   resynchronize-clientsN(
   R%   R!   RO   RR   R   R"   R   t   debugR&   RM   (   R   RU   RA   (    (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyR/   Ø   s    			c         C   s'   | d d k r# |  j  j d ƒ n  d  S(   NRR   s   unknown-accounts   registration-failed(   R&   RM   (   R   RU   (    (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyR1   ì   s    c         C   s©   |  j  } | j d ƒ } | d  k r; t j d | j ƒ nX t j d | ƒ | |  j j k rq d |  j j } n d |  j j | f } | |  j _ d  | _ d  | _	 d  S(   Ns   clone-ofs,   Client has unknown secure-id for account %s.s   Client is clone of computer %ss
   %s (clone)s   %s (clone of %s)(
   R%   R   R2   RO   RR   R   R   R   R!   R"   (   R   RU   RA   t   clonet   title(    (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyR0   ð   s    		
	N(   R   R	   R
   R2   R    RB   R=   R+   R-   R,   R/   R1   R0   (    (    (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyR#   N   s   					E		RC   c           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sµ   A helper for dealing with the response of a single registration request.

    @ivar deferred: The L{Deferred} that will be fired as per
        L{RegistrationHandler.register}.
    c         C   sI   | |  _  | j d |  j ƒ |  _ | j d |  j ƒ |  _ t ƒ  |  _ d  S(   Ns   registration-dones   registration-failed(   R&   R*   t   _donet   _done_idt   _failedt
   _failed_idR    RD   (   R   R8   (    (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyR      s    	c         C   s*   |  j  j |  j ƒ |  j  j |  j ƒ d  S(   N(   R&   t   cancel_callR^   R`   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyt   _cancel_calls  s    c         C   s   |  j  j d  ƒ |  j ƒ  d  S(   N(   RD   t   callbackR2   Rb   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyR]     s    c         C   s!   |  j  j t ƒ  ƒ |  j ƒ  d  S(   N(   RD   t   errbackR   Rb   (   R   (    (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyR_     s    (   R   R	   R
   R    Rb   R]   R_   (    (    (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyRC     s
   			(   R
   RO   t   twisted.internet.deferR    t   landscape.lib.jujuR   t   landscape.lib.tagR   t   landscape.lib.networkR   t   landscape.lib.vm_infoR   R   t   landscape.lib.versioningR   t	   ExceptionR   R   R   t   objectR   R#   RC   (    (    (    sA   /usr/lib/python2.7/dist-packages/landscape/broker/registration.pyt   <module>
   s   		¸