o
    (f!                     @   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g dZG dd	 d	eeZG d
d dZdadd ZdeddfddZdeddfddZdee f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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	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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ed#d$dejdZd%S )&Noticecontract_expired5T)r   r   r   r   reboot_required10FzSystem reboot requiredenable_reboot_required11reboot_script_failed12fips_reboot_required20fips_system_reboot_required21fips_install_out_of_date22fips_disable_reboot_required23fips_proc_file_error24fips_manual_disable_url25wrong_fips_metapackage_on_cloudlp_lts_reboot_required30operation_in_progress60z"Operation in progress: {operation}auto_attach_retry_full_notice70auto_attach_total_failure71limited_to_release80N)__name__
__module____qualname__r
   r   CONTRACT_EXPIREDREBOOT_REQUIREDENABLE_REBOOT_REQUIRED_TMPLENABLE_REBOOT_REQUIREDREBOOT_SCRIPT_FAILEDFIPS_REBOOT_REQUIRED_MSGFIPS_REBOOT_REQUIREDFIPS_SYSTEM_REBOOT_REQUIREDFIPS_INSTALL_OUT_OF_DATEFIPS_DISABLE_REBOOT_REQUIREDFIPS_PROC_FILE_ERRORtmpl_msgNOTICE_FIPS_MANUAL_DISABLE_URLFIPS_MANUAL_DISABLE_URL&NOTICE_WRONG_FIPS_METAPACKAGE_ON_CLOUDWRONG_FIPS_METAPACKAGE_ON_CLOUDLIVEPATCH_LTS_REBOOT_REQUIREDOPERATION_IN_PROGRESSAUTO_ATTACH_RETRY_NOTICEAUTO_ATTACH_RETRY_FULL_NOTICE&AUTO_ATTACH_RETRY_TOTAL_FAILURE_NOTICEAUTO_ATTACH_RETRY_TOTAL_FAILURELIMITED_TO_RELEASE rI   rI   8/usr/lib/python3/dist-packages/uaclient/files/notices.pyr      s    
r   c                   @   s|   e Zd ZdedefddZdefddZdedee fd	d
Zdedede	fddZ
dedefddZdee fddZdS )NoticesManagernotice_detailsdescriptionc                 C   s^   t  std|jj dS |jjrt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.NoticesManager.add(%s) called as non-root userN{}-{})r	   we_are_currently_rootLOGwarningvaluer   r   r   NOTICES_PERMANENT_DIRECTORYNOTICES_TEMPORARY_DIRECTORYformatr   r   
write_fileospathjoin)selfrL   rM   	directoryfilenamerI   rI   rJ   addt   s"   zNoticesManager.addc                 C   s\   t  std|jj dS |jjrt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.
        z1NoticesManager.remove(%s) called as non-root userNrN   )r	   rO   rP   rQ   rR   r   r   r   rS   rT   rU   r   r   ensure_file_absentrW   rX   rY   )rZ   rL   r[   r\   rI   rI   rJ   remove   s   zNoticesManager.remover[   returnc                    s    fddt  D S )zGets the list of notice file names in the given directory.

        :param directory: The directory to search for notice files.
        :returns: List of notice file names.
        c                    s2   g | ]}t jt j |r |r|qS rI   )rW   rX   isfilerY   _is_valid_notice_file).0	file_namer[   rZ   rI   rJ   
<listcomp>   s    
z9NoticesManager._get_notice_file_names.<locals>.<listcomp>)rW   listdir)rZ   r[   rI   re   rJ   _get_notice_file_names   s   z%NoticesManager._get_notice_file_namesrd   c                    s$   |t jk  fddtD }||v S )zChecks if the notice file is valid.

        :param file_name: The name of the notice file.
        :returns: True if the file is valid, False otherwise.
        c                    s&   h | ]}|j  krd |j|jqS )rN   )r   rU   r   r   )rc   nis_permanent_dirrI   rJ   	<setcomp>   s
    
z7NoticesManager._is_valid_notice_file.<locals>.<setcomp>)r   rS   r   )rZ   r[   rd   valid_file_namesrI   rj   rJ   rb      s
   

z$NoticesManager._is_valid_notice_filec                 C   s<   | d\}}tD ]}|j|kr|j|kr|jj  S q	dS )zGets the default message for a notice file.

        :param file_name: The name of the notice file.
        :returns: The default message defined in the enum.
        - )splitr   r   r   rR   r   )rZ   rd   r   r   noticerI   rI   rJ   _get_default_message   s   z#NoticesManager._get_default_messagec              
   C   s   t jt jf}g }|D ]B}tj|sq
| |}|D ]1}zttj	||}W n t
y8   td|  Y qw |rA|| q| |}|| qq
|  |S )zdGets all the notice files currently saved.

        :returns: List of notice file contents.
        zPermission error while reading )r   rS   rT   rW   rX   existsrh   r   	load_filerY   PermissionErrorrP   rQ   appendrr   sort)rZ   notice_directoriesnoticesnotice_directorynotice_file_namesnotice_file_namenotice_file_contentsdefault_messagerI   rI   rJ   list   s8   
zNoticesManager.listN)r/   r0   r1   r   strr]   r_   r   rh   boolrb   rr   r   rI   rI   rI   rJ   rK   s   s    
rK   c                   C   s   t d u rt a t S N)_notice_clsrK   rI   rI   rI   rJ   
get_notice   s   r   rL   r`   c                 K   s(   t  }| jjdi |}|| | d S )NrI   )r   r   rU   r]   )rL   kwargsrq   rM   rI   rI   rJ   r]      s   r]   c                 C   s   t  }||  d S r   )r   r_   )rL   rq   rI   rI   rJ   r_     s   r_   c                  C   s   t  } |  S r   )r   r   )rq   rI   rI   rJ   r   	  s   r   )loggingrW   collectionsr   enumr   typingr   uaclientr   r   r   r   r	   	getLoggerreplace_top_level_logger_namer/   rP   get_event_loggereventr
   r   rK   r   r   r]   r_   r   r   rI   rI   rI   rJ   <module>   s&    c 