3 گazV@sdZddlZddlZddlZddlZddlZddlZddlmZddlm Z ddlm Z ddlm Z ddlm Z ddl mZdd l mZdd l mZdd lmZdd lmZdd lmZejeZGdddZdS)zGReverter class saves configuration checkpoints and allows for recovery.N)Iterable)List)Set)TextIO)Tuple) configuration)errors)util) constants) filesystem)osc@sXeZdZdZejddddZddddZd5edd d d Z e e e dd ddZ e e e dd ddZ e e e e ddddZe eeee fdddZe ddddZe ddddZe e ddddZee dd d!d"Zeee dd#d$d%Zee d&d'd(Zddd)d*Ze ed+d,d-Ze dd.d/d0Ze dd1d2Zddd3d4ZdS)6ReverteraqReverter 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)configreturncCs ||_tj|jtj|jjdS)N)rr make_or_verify_dir backup_dirr CONFIG_DIRS_MODEstrict_permissions)selfrr/usr/lib/python3.6/reverter.py__init__BszReverter.__init__)rc CsXtjj|jjrTy|j|jjWn0tjk rRtj d|jjtjdYnXdS)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 pathisdirrtemp_checkpoint_dir_recover_checkpointr ReverterErrorloggercritical)rrrrrevert_temporary_configHs  z Reverter.revert_temporary_config)rollbackrcCsy t|}Wn(tk r4tjdtjdYnX|dkrRtjdtjdtj|jj }|j |sxtj dnt ||krtj d|t |xj|dko|rtj j|jj |j}y|j|Wn*tjk rtjdtjdYnX|d8}qWd 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 ValueErrorrerrorrrr listdirrrsortwarninglenrjoinpoprr)rr!Zbackupscp_dirrrrrollback_checkpoints\s2         zReverter.rollback_checkpoints) save_files save_notesrcCs|j|jj||dS)zAdd 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.rrradd_to_temp_checkpointszReverter.add_to_temp_checkpointcCs |j||j|jj||dS)zAdd 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/rin_progress_dir)rr-r.rrradd_to_checkpoints zReverter.add_to_checkpoint)r+r-r.rc Cstj|tj|jj|jtjj |d\}}t |}x|D]}||kr:t j d|y>t j|tjj |tjj|dt||jdj|Wn<tk r|jt jd||tjdj||YnX|d7}q:W|jttjj |dd }|j|Wd QRXd 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_z{0} z&Unable to add file %s to checkpoint %sz(Unable to add file {0} to checkpoint {1}r CHANGES_SINCEaN)r rr rrr_read_and_appendr rr)r(rdebugshutilcopy2basenamestrwriteformatIOErrorcloser$rropen) rr+r-r.op_fdZexisting_filepathsidxfilenameZnotes_fdrrrr/s0     zReverter._add_to_checkpoint_dir)filepathrcCs:tjj|r$t|d}|jj}ng}t|d}||fS)zReads 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 risfilerBread splitlines)rrFrClinesrrrr8s    zReverter._read_and_append)r+rcCs:tjjtjj|dr*|jtjj|dtjjtjj|drynttjj|dR}|jj}x>t|D]2\}}t j tjj|tjj |dt ||qlWWdQRXWn2t tfk rtjd|tjd|YnX|jtjj|dyt j|Wn0tk r4tjd|tjd|YnXdS)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 COMMANDSr4r5NzUnable to recover files from %s NEW_FILESzUnable to remove directory: %s)r rrHr)_run_undo_commandsrBrIrJ enumerater:r;r<r=r@OSErrorrr$rr_remove_contained_filesZrmtree)rr+Zpaths_fd filepathsrDrrrrrs*   0  zReverter._recover_checkpointcCs~ddi}t|df|^}tj|}xLtt|D]<}ytj|Wq0tjk rjt j ddj |Yq0Xq0WWdQRXdS)zRun all commands in a file.newlinerzUnable to run undo command: %s N) rBcsvreaderreversedlistr Z run_scriptrZSubprocessErrorrr$r))rrFkwargsZcsvfileZ csvreadercommandrrrrNs zReverter._run_undo_commands)r-rcCsg}tjj|jjd}tjj|rJt|d}|j|jj WdQRXtjj|jjd}tjj|rt|d}|j|jj WdQRXx"|D]}||krt j d|qWdS)zVerify 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. r4rUNrMz+Attempting to overwrite challenge file - %s) r rr)rrrHrBextendrIrJrr)rr-Zprotected_filesZ temp_pathZ protected_fdnew_pathrErrrr1s      zReverter._check_tempfile_saves) temporaryfilesrcGs|stjd|j|}d}z|yB|jtjj|d\}}x$|D]}||kr>|jdj|q>WWn4t t fk rt j d|tjdj|YnXWd|dk r|j XdS)aARegister the creation of all files during certbot execution. Call this method before writing to the file to make sure that the file will be cleaned up if the program exits unexpectedly. (Before a save occurs) :param bool temporary: If the file creation registry is for a temp or permanent save. :param \*files: file paths (str) to be registered :raises certbot.errors.ReverterError: If call does not contain necessary parameters or if the file creation is unable to be registered. z,Forgot to provide files to registration callNrMz{0} z(Unable to register file creation(s) - %sz)Unable to register file creation(s) - {0})rr _get_cp_dirr8r rr)r>r?r@rPrr$rA)rr_r`r+Znew_fdZex_filesrrrrregister_file_creation.s    zReverter.register_file_creation)r_r\rcCstjj|j|d}ddi}yFtjj|r.dnd}t||f|}tj|}|j|WdQRXWn,t t fk rt j dt jdYnXdS) aRegister a command to be run to undo actions taken. .. warning:: This function does not enforce order of operations in terms of file modification vs. command registration. All undo commands are run first before all normal files are reverted to their previous state. If you need to maintain strict order, you may create checkpoints before and after the the command registration. This function may be improved in the future based on demand. :param bool temporary: Whether the command should be saved in the IN_PROGRESS or TEMPORARY checkpoints. :param command: Command to be run. :type command: list of str rLrSrTr7rGNzUnable to register undo commandz Unable to register undo command.)r rr)rarHrBrWwriterZwriterowr@rPrr$rr)rr_r\Z commands_fpr[modefZ csvwriterrrrregister_undo_commandUs  zReverter.register_undo_command)r_rcCs.|r|jj}n|jj}tj|tj|jj|S)z%Return the proper reverter directory.)rrr2r rr rr)rr_r+rrrrass  zReverter._get_cp_dirc Csh|jtjj|jjrdy|j|jjWn8tjk rbt j d|jjtjd|jjYnXdS)aCRevert configuration to most recent finalized checkpoint. Remove all changes (temporary and permanent) that have not been finalized. This is useful to protect against crashes and other execution interruptions. :raises .errors.ReverterError: If unable to recover the configuration z=Incomplete or failed recovery for IN_PROGRESS checkpoint - %sN) rr rrrr2rrrrr)rrrrrecovery_routines zReverter.recovery_routine) file_listrcCstjj|sdSyXt|dD}|jj}x0|D](}tjj|rLtj|q0tj d|q0WWdQRXWn4t t fk rtj d|t jdj|YnXdS)a Erase all files contained within file_list. :param str file_list: file containing list of file paths to be deleted :returns: Success :rtype: bool :raises certbot.errors.ReverterError: If all files within file_list cannot be removed FrUzVFile: %s - Could not be found to be deleted - Certbot probably shut down unexpectedlyNz.Unable to remove filepaths contained within %sz/Unable to remove filepaths contained within {0}T)r rrHrBrIrJlexistsremoverr'r@rPrrrr?)rrhZlist_fdrRrrrrrQs$      z Reverter._remove_contained_files)titlercCstjj|jjsdStjj|jjd}tjj|jjd}tjj|sntjdt |d}|j dWdQRXyXt |d8}|j d|t |d}|j |j WdQRXWdQRXt j ||Wn>ttfk rtjd tjd tjtjd YnX|jdS) 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. Nr6zCHANGES_SINCE.tmpz/Rollback checkpoint is empty (no changes made?)rGz No changes z -- %s -- rUz,Unable to finalize checkpoint - adding titlezException was: %szUnable to add title)r rrrr2r)existsrinforBr>rIr:Zmover@rPr$r9 traceback format_excrr_timestamp_progress_dir)rrkZchanges_since_pathZchanges_since_tmp_pathreZ changes_tmpZ changes_origrrrfinalize_checkpoints&      " zReverter.finalize_checkpointcCsttj}tjtjj|jjd}dd|D}|j||j |d |krxtt |d d}t j d|||}nsz2Reverter._checkpoint_timestamp..r zyCurrent timestamp %s does not correspond to newest reverter checkpoint; your clock probably jumped. Time travelling to %sz6Race condition with timestamp %s, incrementing by 0.01g{Gz?rvrv)r=timeglobr rr)rrappendr&floatrr'r(r9)r timestampZothersZ timetravelrrr_checkpoint_timestamps    zReverter._checkpoint_timestampc Csx`tdD]T}|j}tjj|jj|}ytj|jj |dSt k r\t j d|Yq Xq Wt j d|jj |tjddS)zTimestamp the checkpoint.ruNz(Unexpected race condition, retrying (%s)z'Unable to finalize checkpoint, %s -> %sz&Unable to finalize checkpoint renaming)ranger}r rr)rrr replacer2rPrr'r$rr)rr5r|Z final_dirrrrrps z Reverter._timestamp_progress_dir)r ) __name__ __module__ __qualname____doc__rZNamespaceConfigrrr"r,rr=r0r3r/rrrr8rrNr1boolrbrrfrargrQrqr}rprrrrr s&)*  -'' &)r )rrWryZloggingr:rxrnZtypingrrrrrZcertbotrrr Zcertbot._internalr Zcertbot.compatr r Z getLoggerrrr rrrrs&