Spade
Mini Shell
| Directory:~$ /lib/python2.7/site-packages/certbot/ |
| [Home] [System Details] [Kill Me] |
�
���_c@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl m
Z
ddl mZddlm
Z
ddlmZddlmZeje�Zdefd ��YZdS(
sGReverter class saves configuration checkpoints and allows for
recovery.i����N(terrors(tutil(t constants(t
filesystem(tostRevertercBs�eZdZd�Zd�Zdd�Zd�Zd�Zd�Zd�Z d �Z
d
�Zd�Zd�Z
d
�Zd�Zd�Zd�Zd�Zd�Zd�ZRS(sfReverter
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,||_tj|jtj|jj�dS(N(tconfigRtmake_or_verify_dirt
backup_dirRtCONFIG_DIRS_MODEtstrict_permissions(tselfR((s4/usr/lib/python2.7/site-packages/certbot/reverter.pyt__init__?s cCsrtjj|jj�rny|j|jj�Wqntjk
rjtj d|jj�tjd��qnXndS(sReload 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(
RtpathtisdirRttemp_checkpoint_dirt_recover_checkpointRt
ReverterErrortloggertcritical(R((s4/usr/lib/python2.7/site-packages/certbot/reverter.pytrevert_temporary_configEs
icCsZyt|�}Wn-tk
r?tjd�tjd��nX|dkrktjd�tjd��ntj|jj �}|j
�|s�tjd�n.t|�|kr�tjd|t|��nx�|dkrU|rUtj
j|jj |j��}y|j|�Wn0tjk
rGtjd�tjd��nX|d8}q�Wd S(
syRevert '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
InputisHCertbot hasn't modified your configuration, so rollback
isn't available.s0Unable to rollback %d checkpoints, only %d
exists)Failed to load checkpoint during rollbacks)Unable to load checkpoint
during rollbackiN(tintt
ValueErrorRterrorRRRtlistdirRRtsorttwarningtlenR
tjointpopRR(Rtrollbacktbackupstcp_dir((s4/usr/lib/python2.7/site-packages/certbot/reverter.pytrollback_checkpointsYs2
!
cCs|j|jj||�dS(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_dirRR(Rt
save_filest
save_notes((s4/usr/lib/python2.7/site-packages/certbot/reverter.pytadd_to_temp_checkpoint�scCs*|j|�|j|jj||�dS(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"Rtin_progress_dir(RR#R$((s4/usr/lib/python2.7/site-packages/certbot/reverter.pytadd_to_checkpoint�s
c Csgtj|tj|jj�|jtjj |d��\}}t
|�}x�|D]�}||krStjd|�ySt
j|tjj |tjj|�dt|���|jdj|��WnItk
r|j�tjd||�tjdj||���nX|d7}qSqSW|j�ttjj |d�d ��}|j|�Wd
QXd
S(sgAdd 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
t FILEPATHSsCreating backup of %st_s{0}
s&Unable to add file %s to checkpoint %ss(Unable to add file {0} to
checkpoint {1}it
CHANGES_SINCEtaN(RRRR RR
t_read_and_appendRR
RRRtdebugtshutiltcopy2tbasenametstrtwritetformattIOErrortcloseRRRtopen( RR
R#R$top_fdtexisting_filepathstidxtfilenametnotes_fd((s4/usr/lib/python2.7/site-packages/certbot/reverter.pyR"�s0
'
!cCsUtjj|�r6t|d�}|j�j�}ng}t|d�}||fS(s�Reads
the file lines and returns a file obj.
Read the file returning the lines, and a pointer to the end of the
file.
sr+tw(RR
tisfileR7treadt
splitlines(RtfilepathR8tlines((s4/usr/lib/python2.7/site-packages/certbot/reverter.pyR-�scCs�tjjtjj|d��r@|jtjj|d��ntjjtjj|d��r1y�ttjj|d���o}|j�j�}xSt|�D]E\}}t j
tjj|tjj|�dt|��|�q�WWdQXWq1t
tfk
r-tjd|�tjd|��q1Xn|jtjj|d��yt j|�Wn4tk
r�tjd|�tjd|��nXdS(sRecover 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
tCOMMANDSR)R*NsUnable to recover files from %st NEW_FILESsUnable
to remove directory:
%s(RR
R>Rt_run_undo_commandsR7R?R@t enumerateR/R0R1R2R5tOSErrorRRRRt_remove_contained_filestrmtree(RR
tpaths_fdt filepathsR:R
((s4/usr/lib/python2.7/site-packages/certbot/reverter.pyR�s*!!5
c
Cs�tjddkr
idd6ni}t|d|��t}tj|�}x[tt|��D]G}ytj|�Wq]t j
k
r�tjddj
|��q]Xq]WWdQXdS( sRun all commands in a
file.iittnewlinetrsUnable to run undo command: %st
N(tsystversion_infoR7tcsvtreadertreversedtlistRt
run_scriptRtSubprocessErrorRRR(RRAtkwargstcsvfilet csvreadertcommand((s4/usr/lib/python2.7/site-packages/certbot/reverter.pyRE�s&cCs�g}tjj|jjd�}tjj|�rgt|d��
}|j|j�j ��WdQXntjj|jjd�}tjj|�r�t|d��
}|j|j�j ��WdQXnx0|D](}||kr�t
jd|��q�q�WdS(s�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)RNNRDs+Attempting to overwrite challenge file -
%s(RR
RRRR>R7textendR?R@RR(RR#tprotected_filest temp_pathtprotected_fdtnew_pathR;((s4/usr/lib/python2.7/site-packages/certbot/reverter.pyR&
s ""
cGs�|stjd��n|j|�}d}z�y^|jtjj|d��\}}x3|D]+}||kr^|jdj |��q^q^WWn?t
tfk
r�tj
d|�tjdj |���nXWd|dk r�|j�nXdS(sARegister
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.
s,Forgot to provide files to registration callRDs{0}
s(Unable to register file creation(s) - %ss)Unable to register file
creation(s) -
{0}N(RRt_get_cp_dirtNoneR-RR
RR3R4R5RGRRR6(Rt temporarytfilesR
tnew_fdtex_filesR
((s4/usr/lib/python2.7/site-packages/certbot/reverter.pytregister_file_creation+s
$
!cCs�tjj|j|�d�}d
}tjddkrDidd6ni}z�yYtjj|�rwt|d|�}nt|d|�}t j
|�}|j|�Wn3tt
fk
r�tjd�tjd ��nXWd
|d
k r�|j�nXd
S(s�Register 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
RCiiRLRMR,R=sUnable to register undo commands Unable to register
undo
command.N(RR
RRaRbRPRQR>R7RRtwritertwriterowR5RGRRRRR6(RRcR[tcommands_fptcommand_fileRXt csvwriter((s4/usr/lib/python2.7/site-packages/certbot/reverter.pytregister_undo_commandRs
&
cCsA|r|jj}n|jj}tj|tj|jj�|S(s%Return
the proper reverter directory.(RRR'RRRR R
(RRcR
((s4/usr/lib/python2.7/site-packages/certbot/reverter.pyRaxscCs�|j�tjj|jj�r�y|j|jj�Wq�tjk
r~t j
d|jj�tjd|jj��q�XndS(sCRevert 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
s=Incomplete or failed recovery for IN_PROGRESS checkpoint -
%sN(RRR
RRR'RRRRR(R((s4/usr/lib/python2.7/site-packages/certbot/reverter.pytrecovery_routine�s
cCs�tjj|�stSyqt|d��\}|j�j�}x@|D]8}tjj|�rltj|�qDt j
d|�qDWWdQXWn?ttfk
r�t j
d|�tjdj|���nXtS(s 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
RNsVFile: %s - Could not be found to be deleted
- Certbot probably shut down unexpectedlyNs.Unable to remove filepaths
contained within %ss/Unable to remove filepaths contained within
{0}(RR
R>tFalseR7R?R@tlexiststremoveRRR5RGRRRR4tTrue(Rt file_listtlist_fdRKR
((s4/usr/lib/python2.7/site-packages/certbot/reverter.pyRH�s$
cCs[tjj|jj�sdStjj|jjd�}tjj|jjd�}tjj|�s�tjd�t |d��}|j
d�WdQXnyht |d��C}|j
d|�t |d��}|j
|j��WdQXWdQXtj
||�WnIttfk
rLtjd �tjd
tj��tjd��nX|j�dS(sZFinalize 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.
NR+sCHANGES_SINCE.tmps/Rollback checkpoint is empty (no changes
made?)R=sNo changes
s -- %s --
RNs,Unable to finalize checkpoint - adding titlesException was:
%ssUnable to add
title(RR
RRR'RtexistsRtinfoR7R3R?R/tmoveR5RGRR.t tracebackt
format_excRRt_timestamp_progress_dir(Rttitletchanges_since_pathtchanges_since_tmp_pathtftchanges_tmptchanges_orig((s4/usr/lib/python2.7/site-packages/certbot/reverter.pytfinalize_checkpoint�s&
cCsttj��}tjtjj|jjd��}g|D]}tjj|�^q=}|j |�|j
�|d|kr�tt|d�d�}tj
d||�|}nUt|�dkr
|d|kr
tjd|�tt|d�d�}|}n|S(sBDetermine
the timestamp of the checkpoint, enforcing
monotonicity.s[0-9]*i����isyCurrent timestamp %s does not
correspond to newest reverter checkpoint; your clock probably jumped. Time
travelling to %si����s6Race condition with timestamp %s,
incrementing by
0.01g{�G�z�?(R2ttimetglobRR
RRRR1tappendRtfloatRRRR.(Rt timestamptotherstdt
timetravel((s4/usr/lib/python2.7/site-packages/certbot/reverter.pyt_checkpoint_timestamp�s$%
" cCs�x�tjjd�D]o}|j�}tjj|jj|�}yt j
|jj|�dSWqtk
r�t
jd|�qXqWt
jd|jj|�tjd��dS(sTimestamp
the checkpoint.iNs1Extreme, unexpected race condition, retrying
(%s)s'Unable to finalize checkpoint, %s -> %ss&Unable to
finalize checkpoint
renaming(tsixtmovestrangeR�RR
RRRRtreplaceR'RGRRRRR(RR*R�t final_dir((s4/usr/lib/python2.7/site-packages/certbot/reverter.pyRzs
(t__name__t
__module__t__doc__RRR!R%R(R"R-RRER&RgRmRaRnRHR�R�Rz(((s4/usr/lib/python2.7/site-packages/certbot/reverter.pyRs&) *
- ' ' & & ) (R�RRR�tloggingR/RPR�RxR�tcertbotRRtcertbot._internalRtcertbot.compatRRt getLoggerR�RtobjectR(((s4/usr/lib/python2.7/site-packages/certbot/reverter.pyt<module>s