a }|ägþUã@sÔdZddlZddlZddlZddlZddlZddlZddlmZddlm Z ddlm Z ddlm Z ddlm Z ddl mZdd l mZdd l mZdd lmZdd lmZdd lmZe e¡ZGdd„dƒZdS)zGReverter class saves configuration checkpoints and allows for recovery.éN)ÚIterable)ÚList)ÚSet)ÚTextIO)ÚTuple)Ú configuration)Úerrors)Úutil)Ú constants)Ú filesystem)Úosc@sXeZdZdZejddœdd„Zddœdd„Zd5edd œd d „Z e e e dd œdd„Z e e e dd œdd„Z e e e e ddœdd„Ze eeee fdœdd„Ze ddœdd„Ze ddœdd„Ze e ddœdd„Zee dd œd!d"„Zeee dd#œd$d%„Zee d&œd'd(„Zddœd)d*„Ze ed+œd,d-„Ze dd.œd/d0„Ze dœd1d2„Zddœd3d4„ZdS)6ÚReverteraqReverter 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.configuration.NamespaceConfig` N)ÚconfigÚreturncCs ||_t |jtj|jj¡dS)N)rr Úmake_or_verify_dirÚ backup_dirr ÚCONFIG_DIRS_MODEÚstrict_permissions)Úselfr©rú4/usr/lib/python3.9/site-packages/certbot/reverter.pyÚ__init__BsÿzReverter.__init__)rcCsVtj |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©rrrrÚrevert_temporary_configHs þz Reverter.revert_temporary_configé)ÚrollbackrcCsü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 rollbackr!N)ÚintÚ ValueErrorrÚerrorrrr ÚlistdirrrÚsortÚwarningÚlenrÚjoinÚpoprr)rr"ZbackupsÚcp_dirrrrÚrollback_checkpoints\s8      ÿ ÿ  ÿ zReverter.rollback_checkpoints)Ú save_filesÚ save_notesrcCs| |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©rr.r/rrrÚ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_savesr0rÚin_progress_dirr1rrrÚadd_to_checkpoints  ÿzReverter.add_to_checkpoint)r,r.r/rc 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)r rr rrrÚ_read_and_appendr rr*r)rÚdebugÚshutilÚcopy2ÚbasenameÚstrÚwriteÚformatÚOSErrorÚcloser%rrÚopen) rr,r.r/Úop_fdZexisting_filepathsÚidxÚfilenameZnotes_fdrrrr0œs<  ÿ ÿ  ÿ þÿÿ  zReverter._add_to_checkpoint_dir)ÚfilepathrcCs: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)r rÚisfilerEÚreadÚ splitlines)rrIrFÚlinesrrrr;És    zReverter._read_and_append)r,rc CsDtj tj |d¡¡r*| tj |d¡¡tj tj |d¡¡rîz~ttj |d¡ƒX}| ¡ ¡}t|ƒD]2\}}t   tj |tj  |¡dt |ƒ¡|¡qjWdƒn1s²0YWn.t yìt d|¡t d|›¡‚Yn0| tj |d¡¡zt  |¡Wn.t y>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 ÚCOMMANDSr6r7NzUnable to recover files from %szUnable to recover files from Ú NEW_FILESzUnable to remove directory: %s)r rrKr*Ú_run_undo_commandsrErLrMÚ enumerater=r>r?r@rCrr%rrÚ_remove_contained_filesÚrmtree)rr,Zpaths_fdÚ filepathsrGrrrrrÚs0   þþ(   ÿzReverter._recover_checkpointc Csddi}t|dfi|¤Žb}t |¡}tt|ƒƒD]:}zt |¡Wq2tjyjt   dd  |¡¡Yq20q2Wdƒn1s‚0YdS)zRun all commands in a file.ÚnewlineÚÚrzUnable to run undo command: %sú N) rEÚcsvÚreaderÚreversedÚlistr Z run_scriptrZSubprocessErrorrr%r*)rrIÚkwargsZcsvfileZ csvreaderÚcommandrrrrQs  ÿzReverter._run_undo_commands)r.rcCsÞ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. r6rXNrPz)Attempting to overwrite challenge file - ) r rr*rrrKrEÚextendrLrMrr)rr.Zprotected_filesZ temp_pathZ protected_fdÚnew_pathrHrrrr3s   0  0zReverter._check_tempfile_saves)Ú temporaryÚfilesrcGs¶|st d¡‚| |¡}d}z‚z>| tj |d¡¡\}}|D]}||vr<| d |¡¡qëóz2Reverter._checkpoint_timestamp..éÿÿÿÿr!zyCurrent 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„?)r@ÚtimeÚglobr rr*rrÚappendr'Úfloatrr(r)r<)rÚ timestampZothersZ timetravelrrrÚ_checkpoint_timestampçs    þ zReverter._checkpoint_timestampc Cs€tdƒD]V}| ¡}tj |jj|¡}zt |jj |¡WdSt y\t   d|¡Yq0qt   d|jj |¡t d¡‚dS)zTimestamp the checkpoint.éNz(Unexpected race condition, retrying (%s)z'Unable to finalize checkpoint, %s -> %sz&Unable to finalize checkpoint renaming)Úrangerr rr*rrr Úreplacer4rCrr(r%rr)rr7r€Z final_dirrrrrtüs  þÿz Reverter._timestamp_progress_dir)r!) Ú__name__Ú __module__Ú __qualname__Ú__doc__rZNamespaceConfigrr r#r-rr@r2r5r0rrrr;rrQr3ÚboolrerrirdrjrSrurrtrrrrr s&)*  -&' &)r )rˆrZr}Zloggingr=r|rrÚtypingrrrrrZcertbotrrr Zcertbot._internalr Zcertbot.compatr r Z getLoggerr…rr rrrrÚs&