a á `ÅUã@sždZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z ddl m Z ddlmZddlmZe e¡ZGdd „d eƒZdS) zGReverter class saves configuration checkpoints and allows for recovery.éN)Úerrors)Úutil)Ú constants)Ú filesystem)Úosc@s¢eZdZdZdd„Zdd„Zd(dd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zdd „Zd!d"„Zd#d$„Zd%d&„Zd'S))ÚReverterafReverter Class - save and revert configuration checkpoints. This class can be used by the plugins, especially Installers, to undo changes made to the user's system. Modifications to files and commands to do undo actions taken by the plugin should be registered with this class before the action is taken. Once a change has been registered with this class, there are three states the change can be in. First, the change can be a temporary change. This should be used for changes that will soon be reverted, such as config changes for the purpose of solving a challenge. Changes are added to this state through calls to :func:`~add_to_temp_checkpoint` and reverted when :func:`~revert_temporary_config` or :func:`~recovery_routine` is called. The second state a change can be in is in progress. These changes are not temporary, however, they also have not been finalized in a checkpoint. A change must become in progress before it can be finalized. Changes are added to this state through calls to :func:`~add_to_checkpoint` and reverted when :func:`~recovery_routine` is called. The last state a change can be in is finalized in a checkpoint. A change is put into this state by first becoming an in progress change and then calling :func:`~finalize_checkpoint`. Changes in this state can be reverted through calls to :func:`~rollback_checkpoints`. As a final note, creating new files and registering undo commands are handled specially and use the methods :func:`~register_file_creation` and :func:`~register_undo_command` respectively. Both of these methods can be used to create either temporary or in progress changes. .. note:: Consider moving everything over to CSV format. :param config: Configuration. :type config: :class:`certbot.interfaces.IConfig` cCs ||_t |jtj|jj¡dS)N)ÚconfigrÚmake_or_verify_dirÚ backup_dirrÚCONFIG_DIRS_MODEÚstrict_permissions)Úselfr©rú2/usr/lib/python3/dist-packages/certbot/reverter.pyÚ__init__?sÿzReverter.__init__cCsVtj |jj¡rRz| |jj¡Wn.tjyPt  d|jj¡t d¡‚Yn0dS)aReload users original configuration files after a temporary save. This function should reinstall the users original configuration files for all saves with temporary=True :raises .ReverterError: when unable to revert config z$Incomplete or failed recovery for %sz!Unable to revert temporary configN) rÚpathÚisdirrÚtemp_checkpoint_dirÚ_recover_checkpointrÚ ReverterErrorÚloggerÚcritical©r rrrÚrevert_temporary_configEs þz Reverter.revert_temporary_configécCsüz t|ƒ}Wn&ty2t d¡t d¡‚Yn0|dkrPt d¡t d¡‚t |jj ¡}|  ¡|svt  d¡nt |ƒ|kr”t  d|t |ƒ¡|dkrø|røtj  |jj | ¡¡}z| |¡Wn(tjyìt d¡t d¡‚Yn0|d8}q”d S) ayRevert 'rollback' number of configuration checkpoints. :param int rollback: Number of checkpoints to reverse. A str num will be cast to an integer. So "2" is also acceptable. :raises .ReverterError: if there is a problem with the input or if the function is unable to correctly revert the configuration checkpoints z,Rollback argument must be a positive integerz Invalid InputrzHCertbot hasn't modified your configuration, so rollback isn't available.z0Unable to rollback %d checkpoints, only %d existz)Failed to load checkpoint during rollbackz)Unable to load checkpoint during rollbackrN)ÚintÚ ValueErrorrÚerrorrrrÚlistdirrr ÚsortÚwarningÚlenrÚjoinÚpoprr)r ZrollbackZbackupsÚcp_dirrrrÚrollback_checkpointsYs8      ÿ ÿ  ÿ zReverter.rollback_checkpointscCs| |jj||¡dS)z¨Add files to temporary checkpoint. :param set save_files: set of filepaths to save :param str save_notes: notes about changes during the save N)Ú_add_to_checkpoint_dirrr©r Ú save_filesÚ save_notesrrrÚadd_to_temp_checkpointƒs ÿzReverter.add_to_temp_checkpointcCs | |¡| |jj||¡dS)zªAdd files to a permanent checkpoint. :param set save_files: set of filepaths to save :param str save_notes: notes about changes during the save N)Ú_check_tempfile_savesr&rÚin_progress_dirr'rrrÚadd_to_checkpoints  ÿzReverter.add_to_checkpointc Cs t |tj|jj¡| tj  |d¡¡\}}t |ƒ}|D]š}||vr8t   d|¡z>t  |tj  |tj |¡dt|ƒ¡¡| d |¡¡Wn:tyÈ| ¡t  d||¡t d ||¡¡‚Yn0|d7}q8| ¡ttj  |d¡d ƒ}| |¡Wd ƒn1s0Yd S) agAdd save files to checkpoint directory. :param str cp_dir: Checkpoint directory filepath :param set save_files: set of files to save :param str save_notes: notes about changes made during the save :raises IOError: if unable to open cp_dir + FILEPATHS file :raises .ReverterError: if unable to add checkpoint Ú FILEPATHSzCreating backup of %sÚ_ú{0} z&Unable to add file %s to checkpoint %sz(Unable to add file {0} to checkpoint {1}rÚ CHANGES_SINCEÚaN)rr rr rr Ú_read_and_appendrrr"r!rÚdebugÚshutilÚcopy2ÚbasenameÚstrÚwriteÚformatÚIOErrorÚcloserrrÚopen) r r$r(r)Úop_fdZexisting_filepathsÚidxÚfilenameZnotes_fdrrrr&™s<  ÿ ÿ  ÿ þÿÿ  zReverter._add_to_checkpoint_dircCs:tj |¡r$t|dƒ}| ¡ ¡}ng}t|dƒ}||fS)zˆReads the file lines and returns a file obj. Read the file returning the lines, and a pointer to the end of the file. zr+Úw)rrÚisfiler=ÚreadÚ splitlines)r Úfilepathr>Úlinesrrrr3Æs    zReverter._read_and_appendc CsFtj tj |d¡¡r*| tj |d¡¡tj tj |d¡¡rðz~ttj |d¡ƒX}| ¡ ¡}t|ƒD]2\}}t   tj |tj  |¡dt |ƒ¡|¡qjWdƒn1s²0YWn0t tfyît d|¡t d|¡‚Yn0| tj |d¡¡zt  |¡Wn.ty@t d|¡t d|¡‚Yn0dS)aRecover a specific checkpoint. Recover a specific checkpoint provided by cp_dir Note: this function does not reload augeas. :param str cp_dir: checkpoint directory file path :raises errors.ReverterError: If unable to recover checkpoint ÚCOMMANDSr.r/NzUnable to recover files from %sÚ NEW_FILESzUnable to remove directory: %s)rrrBr"Ú_run_undo_commandsr=rCrDÚ enumerater5r6r7r8r;ÚOSErrorrrrrÚ_remove_contained_filesZrmtree)r r$Zpaths_fdÚ filepathsr?rrrrrÖs4   þþ( ÿ  ÿzReverter._recover_checkpointc Cs¢tjddkrddini}t|dfi|¤Žb}t |¡}tt|ƒƒD]:}zt |¡WqDt j y|t   dd  |¡¡YqD0qDWdƒn1s”0YdS) zRun all commands in a file.réÚnewlineÚÚrzUnable to run undo command: %sú N)ÚsysÚ version_infor=ÚcsvÚreaderÚreversedÚlistrZ run_scriptrZSubprocessErrorrrr")r rEÚkwargsZcsvfileZ csvreaderÚcommandrrrrIýs  ÿzReverter._run_undo_commandscCsÜg}tj |jjd¡}tj |¡r^t|dƒ"}| | ¡  ¡¡Wdƒn1sT0Ytj |jjd¡}tj |¡r¸t|dƒ"}| | ¡  ¡¡Wdƒn1s®0Y|D]}||vr¼t   d|¡‚q¼dS)zîVerify save isn't overwriting any temporary files. :param set save_files: Set of files about to be saved. :raises certbot.errors.ReverterError: when save is attempting to overwrite a temporary file. r.rQNrHz+Attempting to overwrite challenge file - %s) rrr"rrrBr=ÚextendrCrDrr)r r(Zprotected_filesZ temp_pathZ protected_fdÚnew_pathr@rrrr+ s   0  0ÿÿzReverter._check_tempfile_savesc Gsº|st d¡‚| |¡}d}z†z>| tj |d¡¡\}}|D]}||vr<| d |¡¡qt d ¡t d t ¡¡t d ¡‚Yn0| ¡dS) aZFinalize the checkpoint. Timestamps and permanently saves all changes made through the use of :func:`~add_to_checkpoint` and :func:`~register_file_creation` :param str title: Title describing checkpoint :raises certbot.errors.ReverterError: when the checkpoint is not able to be finalized. Nr1zCHANGES_SINCE.tmpz/Rollback checkpoint is empty (no changes made?)rAz No changes z -- %s -- rQz,Unable to finalize checkpoint - adding titlezException was: %szUnable to add title)rrrrr,r"ÚexistsrÚinfor=r9rCr5Zmover;rKrr4Ú tracebackÚ format_excrrÚ_timestamp_progress_dir)r ÚtitleZchanges_since_pathZchanges_since_tmp_pathÚfZ changes_tmpZ changes_origrrrÚfinalize_checkpointÆs&    (  J zReverter.finalize_checkpointcCs¸tt ¡ƒ}t tj |jjd¡¡}dd„|Dƒ}| |¡|  ¡|d|krxtt |dƒdƒ}t   d||¡|}nóóz2Reverter._checkpoint_timestamp..éÿÿÿÿrzyCurrent timestamp %s does not correspond to newest reverter checkpoint; your clock probably jumped. Time travelling to %séþÿÿÿz6Race condition with timestamp %s, incrementing by 0.01g{®Gáz„?)r8ÚtimeÚglobrrr"rr ÚappendrÚfloatrr r!r4)r Ú timestampZothersZ timetravelrrrÚ_checkpoint_timestampïs    þ zReverter._checkpoint_timestampc Cs„tj d¡D]V}| ¡}tj |jj|¡}zt   |jj |¡WdSt y`t  d|¡Yq 0q t  d|jj |¡t d¡‚dS)zTimestamp the checkpoint.rNNz1Extreme, unexpected race condition, retrying (%s)z'Unable to finalize checkpoint, %s -> %sz&Unable to finalize checkpoint renaming)ÚsixZmovesÚrangeryrrr"rr rÚreplacer,rKrr rrr)r r/rxZ final_dirrrrrjs þÿz Reverter._timestamp_progress_dirN)r)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrr%r*r-r&r3rrIr+r`rbr]rcrLrmryrjrrrrrs&) *  -''& &)r)r€rUruZloggingr5rSrtrhrzZcertbotrrZcertbot._internalrZcertbot.compatrrZ getLoggerr}rÚobjectrrrrrÚs