o Hcf!@sddlZddlZddlmZddlmZddlmZddlm Z m Z m Z m Z m Z ee eZe ZedgdZGdd d eeZGd d d Zdad d ZdeddfddZdeddfddZdeefddZdS)N) namedtuple)Enum)List)defaults event_loggermessagessystemutilNoticeFileDetails)order_idlabel is_permanentmessagec@s*eZdZedddejdZedddddZed d dejdZed d dej dZ ed ddej dZ edddej dZ edddej dZ edddejdZedddejjdZedddejdZedddejdZedddejdZedddejdZedddd dZed!d"dejdZed#d$dejdZd%S)&Noticecontract_expired5T)r r r rreboot_required10FzSystem reboot requiredenable_reboot_required11reboot_script_failed12fips_reboot_required20fips_system_reboot_required21fips_install_out_of_date22fips_disable_reboot_required23fips_proc_file_error24fips_manual_disable_url25wrong_fips_metapackage_on_cloudlp_lts_reboot_required30contract_refresh_warning40operation_in_progress60z"Operation in progress: {operation}auto_attach_retry_full_notice70auto_attach_total_failure71N)__name__ __module__ __qualname__r rCONTRACT_EXPIREDREBOOT_REQUIREDENABLE_REBOOT_REQUIRED_TMPLENABLE_REBOOT_REQUIREDREBOOT_SCRIPT_FAILEDFIPS_REBOOT_REQUIRED_MSGFIPS_REBOOT_REQUIREDFIPS_SYSTEM_REBOOT_REQUIREDFIPS_INSTALL_OUT_OF_DATEFIPS_DISABLE_REBOOT_REQUIREDFIPS_PROC_FILE_ERRORtmpl_msgNOTICE_FIPS_MANUAL_DISABLE_URLFIPS_MANUAL_DISABLE_URL&NOTICE_WRONG_FIPS_METAPACKAGE_ON_CLOUDWRONG_FIPS_METAPACKAGE_ON_CLOUDLIVEPATCH_LTS_REBOOT_REQUIREDNOTICE_REFRESH_CONTRACT_WARNINGCONTRACT_REFRESH_WARNINGOPERATION_IN_PROGRESSAUTO_ATTACH_RETRY_NOTICEAUTO_ATTACH_RETRY_FULL_NOTICE&AUTO_ATTACH_RETRY_TOTAL_FAILURE_NOTICEAUTO_ATTACH_RETRY_TOTAL_FAILURErJrJ8/usr/lib/python3/dist-packages/uaclient/files/notices.pyrs rc@s|eZdZdedefddZdefddZdedeefd d Zded ede fd d Z d edefddZ deefddZ dS)NoticesManagernotice_details descriptioncCs^tstd|jjdS|jjrtjntj }d |jj |jj}t tj|||dS)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_rootLOGwarningvaluer r rNOTICES_PERMANENT_DIRECTORYNOTICES_TEMPORARY_DIRECTORYformatr r write_fileospathjoin)selfrMrN directoryfilenamerJrJrKaddts"   zNoticesManager.addcCs\tstd|jjdS|jjrtjntj }d |jj |jj}t tj||dS)ziDeletes a notice file. :param notice_details: Holds details concerning the notice file. z1NoticesManager.remove(%s) called as non-root userNrO)r rPrQrRrSr r rrTrUrVr rensure_file_absentrXrYrZ)r[rMr\r]rJrJrKremoves zNoticesManager.remover\returncsfddtDS)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. cs2g|]}tjtj|r|r|qSrJ)rXrYisfilerZ_is_valid_notice_file).0 file_namer\r[rJrK s z9NoticesManager._get_notice_file_names..)rXlistdir)r[r\rJrfrK_get_notice_file_namess z%NoticesManager._get_notice_file_namesrecs$|tjkfddtD}||vS)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. cs&h|]}|jkrd|j|jqS)rO)r rVr r )rdnis_permanent_dirrJrK s  z7NoticesManager._is_valid_notice_file..)rrTr)r[r\revalid_file_namesrJrkrKrcs  z$NoticesManager._is_valid_notice_filecCs<|d\}}tD]}|j|kr|j|kr|jjSq dS)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. -)splitrr r rSr)r[rer r noticerJrJrK_get_default_messages  z#NoticesManager._get_default_messagec Cstjtjf}g}|D]B}tj|sq ||}|D]1}z ttj ||}Wnt y8t d|Yqw|rA| |q||}| |qq ||S)zdGets all the notice files currently saved. :returns: List of notice file contents. zPermission error while reading )rrTrUrXrYexistsrir load_filerZPermissionErrorrQrRappendrssort)r[notice_directoriesnoticesnotice_directorynotice_file_namesnotice_file_namenotice_file_contentsdefault_messagerJrJrKlists8      zNoticesManager.listN) r/r0r1rstrr^r`rriboolrcrsrrJrJrJrKrLss   rLcCstdurtatSN) _notice_clsrLrJrJrJrK get_noticesrrMracKs(t}|jjdi|}|||dS)NrJ)rrrVr^)rMkwargsrrrNrJrJrKr^sr^cCst}||dSr)rr`)rMrrrJrJrKr`sr`cCst}|Sr)rr)rrrJrJrKr sr)loggingrX collectionsrenumrtypingruaclientrrrrr getLoggerreplace_top_level_logger_namer/rQget_event_loggereventr rrLrrr^r`rrrJrJrJrKs&   c