
    d_/                     $   d dl Z d dlZd dlZd dlZd dlZd dlmc mZ d dlmc m	Z	 d dl
mc mc mZ d dlmZ d dlmZ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 d dlmZ d d	lm Z  d d
l!m"Z"m#Z# dZ$dZ%dZ&dZ'dZ(dZ)dZ*dZ+d Z, G d de      Z-y)    N)SingletonPerThread)ProtocolErrorOSUtilErrorProtocolNotFoundError	DhcpError)ustr)
get_osutil)get_dhcp_handler)!cleanup_metadata_server_artifacts#is_metadata_server_artifact_present)OvfEnv)WireProtocol)KNOWN_WIRESERVER_IPIOErrorCounterzovf-env.xmlProtocolih  
   WireServerEndpointz<UserPassword>.*?<z<UserPassword>*<r   c                      t               S N)ProtocolUtil     F/usr/lib/python3/dist-packages/azurelinuxagent/common/protocol/util.pyget_protocol_utilr   4   s
    >r   c                   b    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 ZddZd Zd ZddZy)r   a1  
    ProtocolUtil handles initialization for protocol instance. 2 protocol types
    are invoked, wire protocol and metadata protocols.

    Note: ProtocolUtil is a sub class of SingletonPerThread, this basically means that there would only be 1 single
    instance of ProtocolUtil object per thread.
    c                     t        j                         | _        d | _        d | _        t               | _        t               | _        y r   )		threadingRLock_lock	_protocolendpointr	   osutilr
   dhcp_handlerselfs    r   __init__zProtocolUtil.__init__@   s2    __&
 l,.r   c           	      >   t        j                         }t        j                  j	                  |t
              }t        j                  j	                  t        j                         t
              }	 | j                  j                          	 t        j                  |d      }t        |      }	 t%        j&                  t(        t*        |      }t        j,                  ||       | j/                          |S # t        $ r(}t        dj                  t        |                  d}~ww xY w# t         t"        f$ r)}t        dj                  |t        |                  d}~ww xY w# t         t"        f$ r)}t        dj                  |t        |                  d}~ww xY w)zm
        Copy ovf env file from dvd to hard disk.
        Remove password before save it to the disk
        z$[CopyOvfEnv] Error mounting dvd: {0}NT)
remove_bomz([CopyOvfEnv] Error reading file {0}: {1}z([CopyOvfEnv] Error writing file {0}: {1})confget_dvd_mount_pointospathjoinOVF_FILE_NAMEget_lib_dirr"   	mount_dvdr   r   formatr   fileutil	read_filer   IOErrorOSErrorresubPASSWORD_PATTERNPASSWORD_REPLACEMENT
write_file_cleanup_ovf_dvd)r%   dvd_mount_pointovf_file_path_on_dvdovf_file_patheovfxmlovfenvs          r   copy_ovf_envzProtocolUtil.copy_ovf_envG   sc   
 224!ww||O]KT%5%5%7G	7KK!!#
	<''(<NFF^F	<VV,0"$F v6 	1  	7 !&&,fT!Wo7 7	7 ! 	< !++162F26q',;< <	< ! 	< !++16-26q',;< <	<sH   0C5 "D) .5E$ 5	D&>#D!!D&)E!8$EE!$F3$FFc                     	 | j                   j                          | j                   j                          y # t        $ r(}t	        j
                  t        |             Y d }~y d }~ww xY wr   )r"   
umount_dvd	eject_dvdr   loggerwarnr   )r%   r?   s     r   r;   zProtocolUtil._cleanup_ovf_dvdl   sJ    	!KK""$KK!!# 	!KKQ  	!s   47 	A( A##A(c                     t         j                  j                  t        j                         t
              }t         j                  j                  |      r t        j                  |      }t        |      S t        dj                  |            )z(
        Load saved ovf-env.xml
        zovf-env.xml is missing from {0})r+   r,   r-   r)   r/   r.   isfiler2   r3   r   r   r1   )r%   r>   xml_texts      r   get_ovf_envzProtocolUtil.get_ovf_envs   si     T%5%5%7G77>>-())-8H(##188GI Ir   c                 n    t         j                  j                  t        j                         t
              S r   )r+   r,   r-   r)   r/   PROTOCOL_FILE_NAMEr$   s    r   _get_protocol_file_pathz$ProtocolUtil._get_protocol_file_path   &    ww||  	 r   c                 n    t         j                  j                  t        j                         t
              S r   )r+   r,   r-   r)   r/   ENDPOINT_FILE_NAMEr$   s    r   "_get_wireserver_endpoint_file_pathz/ProtocolUtil._get_wireserver_endpoint_file_path   rO   r   c                    | j                   j                          	 | j                  r&| j                  | j                   j                          S | j	                         }t
        j                  j                  |      r	 t        j                  |      | _        | j                  rFt        j                  d| j                         | j                  | j                   j                          S t        j                  d|       nt        j                  d|       t         | _        t        j                  d| j                         | j                  | j                   j                          S # t        t        f$ r*}t        j                  d|t        |             Y d }~d }~ww xY w# | j                   j                          w xY w)Nz&WireServer endpoint {0} read from filez1[GetWireserverEndpoint] Unexpected empty file {0}z3[GetWireserverEndpoint] Error reading file {0}: {1}z([GetWireserverEndpoint] Missing file {0}z'Using hardcoded Wireserver endpoint {0})r   acquirer!   releaserR   r+   r,   rI   r2   r3   rF   infoerrorr4   r5   strr   )r%   	file_pathr?   s      r   get_wireserver_endpointz$ProtocolUtil.get_wireserver_endpoint   s>   

	!}}}}, JJ ) ??AIww~~i(	k$,$6$6y$ADM}}$Ldmm\#}} JJ  LL!TV_` GS/DMKKA4==Q==JJ   ) kLL!VXacfghcijjk JJ sC   F$ /F$ >AE( *E(  AF$ (F!7 FF$ F!!F$ $G c                     	 || _         | j                         }t        j                  ||       y # t        t
        f$ r}t        t        |            d }~ww xY wr   )r!   rR   r2   r:   r4   r5   r   r   )r%   r!   rY   r?   s       r   _set_wireserver_endpointz%ProtocolUtil._set_wireserver_endpoint   sP    	'$DM??AI	84! 	'd1g&&	's   -0 AAAc                 F   d| _         | j                         }t        j                  j	                  |      sy	 t        j
                  |       y# t        t        f$ rB}|j                  t        j                  k(  rY d}~yt        j                  d|       Y d}~yd}~ww xY w)z=
        Cleanup previous saved wireserver endpoint.
        Nz'Failed to clear wiresever endpoint: {0})r!   rR   r+   r,   rI   remover4   r5   errnoENOENTrF   rW   )r%   endpoint_file_pathr?   s      r   _clear_wireserver_endpointz'ProtocolUtil._clear_wireserver_endpoint   s}    
 !DDFww~~01	GII()! 	Gww%,,&LLBAFF		Gs   A B B BB c                    | j                          t        dt              D ]  }	 | j                  j                  }|	 | j
                  j                         }|rGt        j                  d       	 | j                  j                          | j                  j                  }n%t        j                  d       | j                         }	 t        |      }|j                  |       | j!                  |       |c S  t+        d      # t        $ r}t        t        |            d}~ww xY w# t        $ r>}t        j                  d       d| j                  _        d| j                  _        |d}~ww xY w# t        $ r }t        j                  d|       Y d}~nd}~ww xY w|t        d	z
  k  st        j                  d
|       t%        j&                  t(               )z3
        Probe protocol endpoints in turn.
        r   Nz4WireServer endpoint is not found. Rerun dhcp handlerz$_detect_protocol: DHCP not availableinit_goal_statez1WireServer is not responding. Reset dhcp endpointTz Protocol endpoint not found: {0}   z Retry detect protocol: retry={0}zNo protocol found.)clear_protocolrange	MAX_RETRYr#   r!   r"   is_dhcp_availablerF   rV   runr   r   r   rZ   r   detectr\   
skip_cachetimesleepPROBE_INTERVALr   )r%   re   retryr!   dhcp_availabler?   protocols          r   _detect_protocolzProtocolUtil._detect_protocol   s    	1i( &	+E!C,,55# &*[[%B%B%DN%$Z[9 --113 $(#4#4#=#=$JK#'#?#?#A
+H5HOOOOD11(;#O3&	+N $$8993  ) 9"/Q"889 % KK ST15D%%.37D%%0G	 ! C>BBC y1}$>F

>*sZ   A
E40D
<E4/D*	D'D""D''E4*	E139E,,E11E44	F=FFc                     | j                         }	 t        j                  ||       y# t        t        f$ r }t        j                  d|       Y d}~yd}~ww xY w)z(
        Save protocol endpoint
        z%Failed to save protocol endpoint: {0}N)rN   r2   r:   r4   r5   rF   rW   )r%   protocol_nameprotocol_file_pathr?   s       r   _save_protocolzProtocolUtil._save_protocol   sS     "99;	E 2MB! 	ELL@!DD	Es   ) AAAc                    | j                   j                          	 t        j                  d       | j	                          d| _        | j                         }t        j                  j                  |      s	 | j                   j                          y	 t        j                  |       | j                   j                          y# t        t        f$ r\}|j                  t        j                  k(  rY d}~| j                   j                          yt        j                   d|       Y d}~d}~ww xY w# | j                   j                          w xY w)z;
        Cleanup previous saved protocol endpoint.
        z&Clean protocol and wireserver endpointNz&Failed to clear protocol endpoint: {0})r   rT   rF   rV   rb   r    rN   r+   r,   rI   rU   r^   r4   r5   r_   r`   rW   )r%   rw   r?   s      r   rg   zProtocolUtil.clear_protocol   s     	

	!KK@A++-!DN!%!=!=!?77>>"45 JJ J		,- JJ  W% J77ell* JJ  EqII	J JJ s<   AD2 C D/D*0D2 D*%D2 *D//D2 2Ec                    | j                   j                          	 | j                  &| j                  | j                   j                          S | j	                         }t
        j                  j                  |      rt        j                  |      t        k(  re| j                         }t        |      | _        t               rt        | j                         | j                  | j                   j                          S t!        j"                  d       | j%                  |      }t'        j(                  |j+                                | j-                  t               || _        t               rt        | j                         | j                  | j                   j                          S # | j                   j                          w xY w)zR
        Detect protocol by endpoint.
        :returns: protocol instance
        zDetect protocol endpointrd   )r!   )r   rT   r    rU   rN   r+   r,   rI   r2   r3   WIRE_PROTOCOL_NAMErZ   r   r   r   r"   rF   rV   rt   r   set_protocol_endpointget_endpointrx   )r%   re   rw   r!   rs   s        r   get_protocolzProtocolUtil.get_protocol  sE   
 	

%	!~~)~~F JJ ? "&!=!=!?ww~~01h6H6HI[6\`r6r779!-h!7 785dkkB~~& JJ # KK23,,_,MH00(:O:O:QR 23%DN
 341$++>>>JJ DJJ s   F* BF* >BF* *GN)T)__name__
__module____qualname____doc__r&   rB   r;   rK   rN   rR   rZ   r\   rb   rt   rx   rg   r~   r   r   r   r   r   7   sM    /#J!
I 
 
!8'G$-:^E!.+!r   r   ).r_   r+   r6   rn   r   azurelinuxagent.common.confcommonr)   azurelinuxagent.common.loggerrF   %azurelinuxagent.common.utils.fileutilutilsr2   )azurelinuxagent.common.singletonperthreadr    azurelinuxagent.common.exceptionr   r   r   r   azurelinuxagent.common.futurer   azurelinuxagent.common.osutilr	   azurelinuxagent.common.dhcpr
   >azurelinuxagent.common.protocol.metadata_server_migration_utilr   r   &azurelinuxagent.common.protocol.ovfenvr   $azurelinuxagent.common.protocol.wirer   %azurelinuxagent.common.utils.restutilr   r   r.   rM   ri   rp   rQ   r8   r9   r{   r   r   r   r   r   <module>r      s   (  	 	   * * . . 8 8 HG G . 4 8o 9 =A  	) ' ) # C!% C!r   