o
    ÙKÕiU  ã                   @   sÖ   d dl Z 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 dlmZmZ e e e¡¡Zg d	¢Zd
Zdedefdd„Zdefdd„Z deddfdd„Z!dS )é    N)Ú
exceptionsÚlockÚmessagesÚsystemÚutil)r   )ÚFullAutoAttachOptionsÚfull_auto_attach)Ú_is_attached)ÚUAConfig)ÚAUTO_ATTACH_STATUS_MOTD_FILE)ÚnoticesÚstate_files)é„  r   i  i  i   i@8  é€p  r   é€Q r   é £ r   é€ô r   é F r   é€— r   z./run/ubuntu-advantage/flags/auto-attach-failedÚeÚreturnc                 C   sÆ   t | tjƒrtjj| jd�S t | tjƒrtjS t | tj	ƒr%tj
j| jd�S t | tjƒr3tjj| jd�S t | tjƒrItjj| jd�d t| jƒ¡ S t | tjƒrUd | j¡S tjd| d� t| ƒpbtjS )	N)Údetail)Úpid)Ú	error_msg)Úurlz: "{}"z"{}"zUnexpected exception)Úexc_info)Ú
isinstanceÚapi_exceptionsÚInvalidProImager   Ú$RETRY_ERROR_DETAIL_INVALID_PRO_IMAGEÚformatr   ÚNonAutoAttachImageErrorÚ(RETRY_ERROR_DETAIL_NON_AUTO_ATTACH_IMAGEÚLockHeldErrorÚRETRY_ERROR_DETAIL_LOCK_HELDr   ÚContractAPIErrorÚ%RETRY_ERROR_DETAIL_CONTRACT_API_ERRORÚbodyÚConnectivityErrorÚ RETRY_ERROR_DETAIL_URL_ERROR_URLr   ÚstrÚcause_errorÚUbuntuProErrorÚmsgÚLOGÚerrorÚUNKNOWN_ERROR)r   © r1   úC/usr/lib/python3/dist-packages/uaclient/daemon/retry_auto_attach.pyÚ,full_auto_attach_exception_to_failure_reason)   s,   ÿÿÿþr3   Úcfgc                 C   s>   t j ¡  t j ¡  t t¡ t tj	j
¡ t tj	j¡ d S )N)r   Úretry_auto_attach_state_fileÚdeleteÚretry_auto_attach_options_filer   Úensure_file_absentr   r   ÚremoveÚNoticeÚAUTO_ATTACH_RETRY_FULL_NOTICEÚAUTO_ATTACH_RETRY_TOTAL_FAILURE)r4   r1   r1   r2   ÚcleanupA   s   


ÿÿr=   c                 C   sÖ  t | ƒjrd S tj ¡ }|d ur|j}t|d … }|j}nd}t}d }t|ƒD ]ú\}}t	j	 
t	jj¡}|t	j|d� }|jddd�}tj tj|| |d�¡ |}	|	d u rXtj}	z| ¡ }W n	 tyg   Y nw tjj|| d |	| ¡ d�}
t td|
 d ¡ z)tjd	d
�� tjtjj || d |	| ¡ d� W d   ƒ n1 s¤w   Y  W n
 t!j"y´   Y nw t# $|¡ t | ƒjrÁ naztj% ¡ }t&ƒ }|d urÖ|j'|_'|j(|_(t)|ƒ W  nE t*j+yì   t, -d¡ Y  n6 t*j.�y } zt, /|j0¡ W Y d }~ n d }~w t�y! } zt1|ƒ}t, 2|¡ W Y d }~q'd }~ww t3| ƒ t | ƒj�sitj tjt4tƒ|d�¡ |}	|	d u �rCtj}	tj5jt4tƒd |	d�}
t td|
 d ¡ tjtjj6t4tƒd |	d� d S d S )Nr   )Úseconds)ÚsecondÚmicrosecond)Úinterval_indexÚfailure_reasoné   )Únum_attemptsÚreasonÚnext_run_datestringÚ
z

z+pro.daemon.retry_auto_attach.notice_updates)Úlock_holderz&already attached, ending retry service)rD   rE   )7r	   Úis_attachedr   r5   ÚreadrA   ÚRETRY_INTERVALSrB   Ú	enumerateÚdatetimeÚnowÚtimezoneÚutcÚ	timedeltaÚreplaceÚwriteÚRetryAutoAttachStater   r0   Ú
astimezoneÚ	ExceptionÚAUTO_ATTACH_RETRY_NOTICEr    Ú	isoformatr   Ú
write_filer   r   Ú	RetryLockr   Úaddr:   r;   r   r#   ÚtimeÚsleepr7   r   ÚenableÚenable_betar   r   ÚAlreadyAttachedErrorr.   ÚinfoÚEntitlementsNotEnabledErrorÚwarningr-   r3   r/   r=   ÚlenÚ&AUTO_ATTACH_RETRY_TOTAL_FAILURE_NOTICEr<   )r4   Úpersisted_stateÚoffsetÚ	intervalsrB   ÚindexÚintervalÚlast_attemptÚnext_attemptÚ
msg_reasonÚauto_attach_status_msgÚpersisted_optionsÚoptionsr   r1   r1   r2   Úretry_auto_attachM   sÊ   

þÿÿ
ý
þÿ
üý€	ÿ

ÿ
€€þþÿ
ÿÿ
þ

ýìrq   )"rM   Úloggingr\   Úuaclientr   r   r   r   r   Úuaclient.apir   Ú2uaclient.api.u.pro.attach.auto.full_auto_attach.v1r   r   Ú(uaclient.api.u.pro.status.is_attached.v1r	   Úuaclient.configr
   Úuaclient.daemonr   Úuaclient.filesr   r   Ú	getLoggerÚreplace_top_level_logger_nameÚ__name__r.   rK   ÚFLAG_FILE_PATHrV   r*   r3   r=   rq   r1   r1   r1   r2   Ú<module>   s     