�
���_c @ s� d Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z 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 d d l m Z e j e � Z d e f d � � YZ d S(
sG Reverter class saves configuration checkpoints and allows for recovery.i����N( t errors( t util( t constants( t
filesystem( t ost Reverterc B s� e Z d Z d � Z d � Z d d � Z d � Z d � Z d � Z d � Z d � Z
d
� Z d � Z d � Z
d
� Z d � Z d � Z d � Z d � Z d � Z d � Z RS( sf Reverter 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`
c C s, | | _ t j | j t j | j j � d S( N( t configR t make_or_verify_dirt
backup_dirR t CONFIG_DIRS_MODEt strict_permissions( t selfR ( ( s4 /usr/lib/python2.7/site-packages/certbot/reverter.pyt __init__? s c C sr t j j | j j � rn y | j | j j � Wqn t j k
rj t j d | j j � t j d � � qn Xn d S( s Reload 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
s$ Incomplete or failed recovery for %ss! Unable to revert temporary configN(
R t patht isdirR t temp_checkpoint_dirt _recover_checkpointR t
ReverterErrort loggert critical( R ( ( s4 /usr/lib/python2.7/site-packages/certbot/reverter.pyt revert_temporary_configE s
i c C sZ y t | � } Wn- t k
r? t j d � t j d � � n X| d k rk t j d � t j d � � n t j | j j � } | j
� | s� t j d � n. t | � | k r� t j d | t | � � n x� | d k rU| rUt j
j | j j | j � � } y | j | � Wn0 t j k
rGt j d � t j d � � n X| d 8} q� Wd S(
sy Revert '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
s, Rollback argument must be a positive integers
Invalid Inputi sH Certbot hasn't modified your configuration, so rollback isn't available.s0 Unable to rollback %d checkpoints, only %d exists) Failed to load checkpoint during rollbacks) Unable to load checkpoint during rollbacki N( t intt
ValueErrorR t errorR R R t listdirR R t sortt warningt lenR
t joint popR R ( R t rollbackt backupst cp_dir( ( s4 /usr/lib/python2.7/site-packages/certbot/reverter.pyt rollback_checkpointsY s2
!
c C s | j | j j | | � d S( s� 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( t _add_to_checkpoint_dirR R ( R t
save_filest
save_notes( ( s4 /usr/lib/python2.7/site-packages/certbot/reverter.pyt add_to_temp_checkpoint� s c C s* | j | � | j | j j | | � d S( s� 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( t _check_tempfile_savesR"