U
    2d                     @   s   d dl 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	m
Z
mZmZmZ e eeZe
 Zedddd	d
gZG dd deeZG dd dZdadd ZeddddZeddddZee dddZdS )    N)
namedtuple)Enum)List)defaultsevent_loggermessagessystemutilNoticeFileDetailsorder_idlabelis_permanentmessagec                   @   s  e Zd ZedddddZedddejdZedd	d
ejdZedddej	dZ
edddejjdZeddd
ejdZedddejdZeddd
ejdZeddd
ejdZeddd
ejdZedddejdZeddd
ejdZedddddZedd dejdZed!d"d
ejdZd#S )$NoticeZreboot_requiredZ10FzSystem reboot required)r   r   r   r   Zenable_reboot_requiredZ11Zreboot_script_failedZ12TZfips_reboot_requiredZ20Zfips_system_reboot_requiredZ21Zfips_install_out_of_dateZ22Zfips_disable_reboot_requiredZ23Zfips_proc_file_errorZ24Zfips_manual_disable_urlZ25Zwrong_fips_metapackage_on_cloudZlp_lts_reboot_requiredZ30Zcontract_refresh_warningZ40Zoperation_in_progressZ60z"Operation in progress: {operation}Zauto_attach_retry_full_noticeZ70Zauto_attach_total_failureZ71N)__name__
__module____qualname__r
   ZREBOOT_REQUIREDr   ZENABLE_REBOOT_REQUIRED_TMPLZENABLE_REBOOT_REQUIREDZREBOOT_SCRIPT_FAILEDZFIPS_REBOOT_REQUIRED_MSGZFIPS_REBOOT_REQUIREDZFIPS_SYSTEM_REBOOT_REQUIREDmsgZFIPS_INSTALL_OUT_OF_DATEZFIPS_DISABLE_REBOOT_REQUIREDZFIPS_PROC_FILE_ERRORZNOTICE_FIPS_MANUAL_DISABLE_URLZFIPS_MANUAL_DISABLE_URLZ&NOTICE_WRONG_FIPS_METAPACKAGE_ON_CLOUDZWRONG_FIPS_METAPACKAGE_ON_CLOUDZLIVEPATCH_LTS_REBOOT_REQUIREDZNOTICE_REFRESH_CONTRACT_WARNINGZCONTRACT_REFRESH_WARNINGZOPERATION_IN_PROGRESSZAUTO_ATTACH_RETRY_NOTICEZAUTO_ATTACH_RETRY_FULL_NOTICEZ&AUTO_ATTACH_RETRY_TOTAL_FAILURE_NOTICEZAUTO_ATTACH_RETRY_TOTAL_FAILURE r   r   8/usr/lib/python3/dist-packages/uaclient/files/notices.pyr      s   r   c                   @   s<   e Zd ZeedddZedddZee ddd	Zd
S )NoticesManager)notice_detailsdescriptionc              	   C   sl   t  s*t   td W 5 Q R X dS |jjr8tjntj	}d
|jj|jj}ttj||| dS )zAdds a notice file. If the notice is found,
        it overwrites it.

        :param notice_details: Holds details concerning the notice file.
        :param description: The content to be written to the notice file.
        z'Trying to add a notice as non-root userN{}-{})r	   we_are_currently_rootdisable_log_to_consoleLOGwarningvaluer   r   NOTICES_PERMANENT_DIRECTORYNOTICES_TEMPORARY_DIRECTORYformatr   r   r   Z
write_fileospathjoin)selfr   r   	directoryfilenamer   r   r   addn   s     
 zNoticesManager.add)r   c              	   C   sj   t  s*t   td W 5 Q R X dS |jjr8tjntj	}d
|jj|jj}ttj|| dS )ziDeletes a notice file.

        :param notice_details: Holds details concerning the notice file.
        z*Trying to remove a notice as non-root userNr   )r	   r   r   r   r   r   r   r   r   r    r!   r   r   r   Zensure_file_absentr"   r#   r$   )r%   r   r&   r'   r   r   r   remove   s    
 zNoticesManager.removereturnc           
         s  t jt jf}g }|D ] tj s&q fddt D }|D ]}ttj	 |}|rj|
| qBzT|d\}}d}tD ]}	|	j|kr|	j|kr|	}q|dkrt |
|jj W qB tk
r   t  td| W 5 Q R X Y qBX qBq|  |S )zdGets all the notice files currently saved.

        :returns: List of notice file contents.
        c                    s&   g | ]}t jt j |r|qS r   )r"   r#   isfiler$   ).0	file_nameZnotice_directoryr   r   
<listcomp>   s   z'NoticesManager.list.<locals>.<listcomp>-Nz1Something went wrong while processing notice: {}.)r   r   r    r"   r#   existslistdirr   Z	load_filer$   appendsplitr   r   r   	Exceptionr   r   r	   r   r   r   r!   sort)
r%   Znotice_directoriesZnoticesZnotice_file_namesZnotice_file_nameZnotice_file_contentsr   r   noticenr   r/   r   list   sD    

zNoticesManager.listN)	r   r   r   r   strr(   r)   r   r:   r   r   r   r   r   m   s
   r   c                   C   s   t d krt a t S N)_notice_clsr   r   r   r   r   
get_notice   s    r>   )r   r+   c                 K   s$   t  }| jjf |}|| | d S r<   )r>   r   r!   r(   )r   kwargsr8   r   r   r   r   r(      s    r(   c                 C   s   t  }||  d S r<   )r>   r)   )r   r8   r   r   r   r)      s    r)   r*   c                  C   s   t  } |  S r<   )r>   r:   )r8   r   r   r   r:      s    r:   )Zloggingr"   collectionsr   enumr   typingr   Zuaclientr   r   r   r   r	   Z	getLoggerZreplace_top_level_logger_namer   r   Zget_event_loggerZeventr
   r   r   r=   r>   r(   r)   r;   r:   r   r   r   r   <module>   s$    
]b