
    dG+              	          d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
mZ 	 dZdZdZ ej                  d       ej                  d	ej                          ej                  d
ej                          ej                  dej                          ej                  dej                          ej                  dej                          ej                  d      gZdZ ej                  dj'                  e            Z ej                  dj'                  e            ZdZdZdZdZdZdZdZdZdZ G d de      Z  G d de       Z! G d d e       Z" G d! d"e      Z# G d# d$e      Z$y)%    N)conf)logger)fileutiltimeutilhistoryzGoalState.1.xml2   z^VmSettings\.\d+\.json$z^(.*)\.(\d+)\.(agentsManifest)$z^(.*)\.(\d+)\.(manifest\.xml)$z^(.*)\.(\d+)\.(xml)$z^HostingEnvironmentConfig\.xml$z^RemoteAccess\.xml$z^waagent_status\.\d+\.json$zZ\d{4}\-\d{2}\-\d{2}T\d{2}[:-]\d{2}[:-]\d{2}(\.\d+)?((_incarnation)?_+(\d+|status)(-\d+)?)?z^{0}$z
^{0}\.zip$zGoalState.xmlzVmSettings.jsonzCertificates.jsonzHostingEnvironmentConfig.xmlzRemoteAccess.xmlzExtensionsConfig.xmlz{0}.manifest.xmlzwaagent_status.jsonzSharedConfig.xmlc                   R    e Zd Zd Zed        Zd Zd Zd Zd Z	d Z
d Zd	 Zd
 Zy)Statec                      || _         || _        y N)_path
_timestamp)selfpath	timestamps      F/usr/lib/python3/dist-packages/azurelinuxagent/common/utils/archive.py__init__zState.__init___   s    
#    c                     | j                   S r   )r   r   s    r   r   zState.timestampc   s    r   c                      y r    r   s    r   deletezState.deleteg       r   c                      y r   r   r   s    r   archivezState.archivej   r   r   c                 4    | j                   |j                  k(  S r   r   r   r   others     r   __eq__zState.__eq__m       %//11r   c                 4    | j                   |j                  k7  S r   r   r   s     r   __ne__zState.__ne__p   r"   r   c                 4    | j                   |j                  k  S r   r   r   s     r   __lt__zState.__lt__s       00r   c                 4    | j                   |j                  kD  S r   r   r   s     r   __gt__zState.__gt__v   r'   r   c                 4    | j                   |j                  k  S r   r   r   s     r   __le__zState.__le__y   r"   r   c                 4    | j                   |j                  k\  S r   r   r   s     r   __ge__zState.__ge__|   r"   r   N)__name__
__module____qualname__r   propertyr   r   r   r!   r$   r&   r)   r+   r-   r   r   r   r
   r
   ^   sC    $  221122r   r
   c                       e Zd Zd Zy)StateZipc                 B    t        j                  | j                         y r   )osremover   r   s    r   r   zStateZip.delete   s    
		$**r   N)r.   r/   r0   r   r   r   r   r3   r3      s    r   r3   c                       e Zd Zd Zd Zy)StateDirectoryc                 B    t        j                  | j                         y r   )shutilrmtreer   r   s    r   r   zStateDirectory.delete   s    djj!r   c                 B   dj                  | j                        }dj                  | j                        }d }	 t        j                  |d      }t	        j
                  | j                        D ]M  }t        j                  j                  | j                  |      }|j                  ||t        j                         O 	 ||j                          	 t	        j                  ||       t        j                  | j                         y # ||j                          w w xY w)Nz{0}.zip.tmpz{0}.zipw)formatr   zipfileZipFiler5   listdirr   joinwriteZIP_DEFLATEDcloserenamer:   r;   )r   fn_tmpfilenameziphcurrent_file	full_paths         r   r   zStateDirectory.archive   s    %%djj1##DJJ/	??63/D "

4:: 6 JGGLL\B	

9lG4H4HIJ 


		&(#djj!	 

  s   BD	 	DN)r.   r/   r0   r   r   r   r   r   r8   r8      s    ""r   r8   c                   .    e Zd Zd Zed        Zd Zd Zy)StateArchiverc                    t         j                  j                  |t              | _        t         j                  j                  | j                        s#	 t        j                  | j                  d       y y # t        $ rW}|j                  t        j                  k7  r0t        j                  d| j                  |j                         Y d }~y Y d }~y d }~ww xY w)N  modez	{0} : {1})r5   r   rB   ARCHIVE_DIRECTORY_NAME_sourceisdirr   mkdirIOErrorerrnoEEXISTr   warnstrerror)r   lib_dir	exceptions      r   r   zStateArchiver.__init__   s    ww||G-CDww}}T\\*Ot||%8 +  O??ell2KKT\\9;M;MNN 3Os   !A7 7	C ACCc            	         t        j                         } t        j                  |       D ]b  }|t        k(  rt        j
                  j                  | |      }t        D ]-  }|j                  |      }|	 t        j                  |        b d y # t        $ r/}t        j                  dj                  ||             Y d }~9d }~ww xY w)Nz,Cannot delete legacy history file '{0}': {1})r   get_lib_dirr5   rA   _PLACEHOLDER_FILE_NAMEr   rB   _CACHE_PATTERNSmatchr6   	Exceptionr   rY   r>   )r[   rJ   rK   patternra   es         r   purge_legacy_goal_state_historyz-StateArchiver.purge_legacy_goal_state_history   s    ""$JJw/ 	L 55Wl;I* l3$i		), 	 % i$R$Y$YZcef$ghhis   5B	C%CCc                 v    | j                         }t        |      dkD  r|dd  D ]  }|j                           y y )Nr      )_get_archive_stateslenr   )r   statesstates      r   r   zStateArchiver.archive   s>    ))+v;?    r   c           	         g }t        j                  | j                        D ]  }t         j                  j	                  | j                  |      }t
        j                  |      }|*|j                  t        ||j                  d                   t        j                  |      }||j                  t        ||j                  d                    |j                  d d       |S )Nr   c                 T    t         j                  j                  | j                        S r   )r5   r   getctimer   )rk   s    r   <lambda>z3StateArchiver._get_archive_states.<locals>.<lambda>   s    bgg&6&6u{{&C r   Tkeyreverse)r5   rA   rS   r   rB   _ARCHIVE_PATTERNS_DIRECTORYra   appendr8   group_ARCHIVE_PATTERNS_ZIPr3   sort)r   rj   rJ   rK   ra   s        r   rh   z!StateArchiver._get_archive_states   s    JJt||4 	CLT\\<@I/55lCE nYAGH)//=E hy%++a.AB	C 	CTRr   N)r.   r/   r0   r   staticmethodre   r   rh   r   r   r   rM   rM      s&    O  $ r   rM   c                   |    e Zd Zd Zed        Zd ZdZed        Zed        Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zy)GoalStateHistoryc                    d| _         t        j                  |      }t        j                  j                  t        j                         t        |dj                  ||      n|      | _
        t        j                          y )NFz{0}__{1})_errorsr   create_history_timestampr5   r   rB   r   r^   rR   r>   _rootrz   _purge)r   timetagr   s       r   r   zGoalStateHistory.__init__   sj    55d;	WW\\$"2"2"46Lsv  tCjN_N_`iknNo  IR  S
!r   c                     t        t        j                  t        j                  j	                  t        j                         t        dj                  |                         dkD  S )zh
        Returns True when an item with the given 'tag' already exists in the history directory
        z*_{0}r   )	ri   globr5   r   rB   r   r^   rR   r>   )r   s    r   
tag_existszGoalStateHistory.tag_exists   sF    
 499RWW\\$*:*:*<>TV]VdVdehVijklopppr   c                    	 t         j                  j                  | j                        s!t	        j
                  | j                  d       t        t         j                  j                  | j                  |      d      5 }|j                  |       d d d        y # 1 sw Y   y xY w# t        $ rG}| j                  s1d| _
        t        j                  dj                  ||             Y d }~y Y d }~y d }~ww xY w)NrO   rP   r=   TzoFailed to save {0} to the goal state history: {1} [no additional errors saving the goal state will be reported])r5   r   existsr~   r   rU   openrB   rC   rb   r|   r   rY   r>   )r   data	file_namehandlerd   s        r   savezGoalStateHistory.save   s    	d77>>$**-tzz6bggll4::y93? #6T"# # # 	d<<#  N  U  U  V_  ab  c  d  d  	ds6   A>B'  BB' B$ B' $B' '	C708C22C7r   c                  
   	 t         j                  j                  t        j                         t
              } t         j                  j                  |       syg }t        j                  |       D ]3  }t         j                  j                  | |      }|j                  |       5 |j                  t         j                  j                  d       |t        d D ]L  }t         j                  j                  |      rt        j                  |       8t        j                  |       N t         j"                  dkD  r!dt         _        t%        j&                  d       yy# t(        $ r}t         xj"                  dz  c_        t         j"                  dk  r%t%        j*                  dj-                  |             nAt         j"                  dk(  r)t%        j*                  d	j-                  |             Y d}~yY d}~yY d}~yd}~ww xY w)
z
        Delete "old" history directories and .zip archives. Old is defined as any directories or files older than the X newest ones.
        NTrp   r   z8Successfully cleaned up the goal state history directoryrg      z8Failed to clean up the goal state history directory: {0}z[Failed to clean up the goal state history directory [will stop reporting these errors]: {0})r5   r   rB   r   r^   rR   r   rA   rt   rw   rn   _MAX_ARCHIVED_STATESisfiler6   r:   r;   rz   _purge_error_countr   inforb   rY   r>   )history_rootitemscurrent_itemrK   rd   s        r   r   zGoalStateHistory._purge   s   
	E77<<(8(8(:<RSL77>>,/E "

< 8 (GGLL|D	Y'( JJ277++TJ: %&:&; < 077>>,/IIl+MM,/	0  22Q667 3VW 7
  	E//14/22Q6V]]^_`a!449y  A  A  BC  D  E  E : b	Es    AE DE 	H&BG==Hc                  T   	 t         j                  j                  t        j                         t
              } t        | d      5 }|j                  d       ddd       y# 1 sw Y   yxY w# t        $ r3}t        j                  dj                  t
        |             Y d}~yd}~ww xY w)aE  
        Some internal components took a dependency in the legacy GoalState.*.xml file. We create it here while those components are updated to remove the dependency.
        When removing this code, also remove the check in StateArchiver.purge_legacy_goal_state_history, and the definition of _PLACEHOLDER_FILE_NAME
        r=   z!<xml>empty placeholder file</xml>Nz*Failed to save placeholder file ({0}): {1})r5   r   rB   r   r^   r_   r   rC   rb   r   rY   r>   )placeholderr   rd   s      r   _save_placeholderz"GoalStateHistory._save_placeholder  s    	h'',,t'7'7'9;QRKk3' B6@AB B B 	hKKDKKLbdefgg	hs6   AA+ AA+ A($A+ (A+ +	B'4)B""B'c                 P    | j                  |t               | j                          y r   )r   _GOAL_STATE_FILE_NAMEr   r   texts     r   save_goal_statez GoalStateHistory.save_goal_state   s    		$-. r   c                 0    | j                  |t               y r   )r   _EXT_CONF_FILE_NAMEr   s     r   save_extensions_configz'GoalStateHistory.save_extensions_config$  s    		$+,r   c                 0    | j                  |t               y r   )r   _VM_SETTINGS_FILE_NAMEr   s     r   save_vm_settingsz!GoalStateHistory.save_vm_settings'      		$./r   c                 0    | j                  |t               y r   )r   _REMOTE_ACCESS_FILE_NAMEr   s     r   save_remote_accessz#GoalStateHistory.save_remote_access*  s    		$01r   c                 0    | j                  |t               y r   )r   _CERTIFICATES_FILE_NAMEr   s     r   save_certificatesz"GoalStateHistory.save_certificates-  s    		$/0r   c                 0    | j                  |t               y r   )r   _HOSTING_ENV_FILE_NAMEr   s     r   save_hosting_envz!GoalStateHistory.save_hosting_env0  r   r   c                 0    | j                  |t               y r   )r   SHARED_CONF_FILE_NAMEr   s     r   save_shared_confz!GoalStateHistory.save_shared_conf3  s    		$-.r   c                 N    | j                  |t        j                  |             y r   )r   _MANIFEST_FILE_NAMEr>   )r   namer   s      r   save_manifestzGoalStateHistory.save_manifest6  s    		$+22489r   N)r.   r/   r0   r   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rz   rz      s~    " q q	d  E  EF 
h 
h!-0210/:r   rz   )%rW   r   r5   rer:   r?   azurelinuxagent.commonr   r   azurelinuxagent.common.utilsr   r   rR   r_   r   compile
IGNORECASEr`   _ARCHIVE_BASE_PATTERNr>   rs   rv   r   r   r   r   r   r   r   AGENT_STATUS_FILEr   objectr
   r3   r8   rM   rz   r   r   r   <module>r      s     	 	   ' ) ;0 #  +    BJJ)*BJJ12==ABJJ0"--@BJJ&6BJJ12==ABJJ%r}}5BJJ-.6 v (bjj9N)OP "

=#7#78M#NO ' * - 7 - , ( ) * 2F 2Du 
"U ".4F 4nd:v d:r   