|
Server : Apache/2.4.41 (Ubuntu) System : Linux vmi1525618.contaboserver.net 5.4.0-105-generic #119-Ubuntu SMP Mon Mar 7 18:49:24 UTC 2022 x86_64 User : www-data ( 33) PHP Version : 8.2.12 Disable Function : NONE Directory : /lib/python3/dist-packages/certbot/__pycache__/ |
Upload File : |
U
�]�_ � @ s� d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl
Zddlm
Z
ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ e�e�ZG d d
� d
e�ZdS )zGReverter class saves configuration checkpoints and allows for recovery.� N)� constants)�errors)�
interfaces)�util)�os)�
filesystemc @ s� e Zd ZdZdd� Zdd� Zd*dd�Zd d
� Zdd� Zd
d� Z dd� Z
dd� Zdd� Zdd� Z
dd� Zdd� Zdd� Zdd� Zdd � Zd!d"� Zd#d$� Zd%d&� Zd'd(� Zd)S )+�Reverteraf 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�|jtj| j j� d S )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__B s �zReverter.__init__c C sX t j�| jj�rTz| �| jj� W n0 tjk
rR t� d| jj� t�d��Y nX dS )a 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
z$Incomplete or failed recovery for %sz!Unable to revert temporary configN)
r �path�isdirr �temp_checkpoint_dir�_recover_checkpointr �
ReverterError�logger�critical�r r r r �revert_temporary_configH s �z Reverter.revert_temporary_config� c C s zt |�}W n( tk
r4 t�d� t�d��Y nX |dk rRt�d� t�d��t�| jj �}|�
� |sxt�d� nt|�|k r�t�d|t|�� |dkr�|r�tj
�| jj |�� �}z| �|� W n* tjk
r� t�d� t�d��Y nX |d8 }q�d S )
ay 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
z,Rollback argument must be a positive integerz
Invalid Inputr zHCertbot 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 �errorr r r �listdirr r �sort�warning�lenr �join�popr r )r Zrollback�backups�cp_dirr r r �rollback_checkpoints\ s:
� �
�
zReverter.rollback_checkpointsc
C s� t jdtdd� t�| jj�}|jdd� |s<t� d� dS z|D ]}t
|� qBW n( tk
rz t�
d�| jj���Y nX g }|D �]}|�t�t
|��� tj�| jj|�}ttj�|d ���}|�|�� � W 5 Q R X |�d
� ttj�|d���.}|�� �� }|D ]}|�d�|�� �q
W 5 Q R X tj�tj�|d
���r�ttj�|d
���8} |�d� | �� �� }|D ]}|�d�|�� �qrW 5 Q R X |�d� q�tj�tj�jd�|�ddd� dS )a Displays all saved checkpoints.
All checkpoints are printed by
:meth:`certbot.interfaces.IDisplay.notification`.
.. todo:: Decide on a policy for error handling, OSError IOError...
:raises .errors.ReverterError: If invalid directory structure.
a The view_config_changes method has been deprecated and will be removed in a future release. If you were using this method to implement the view_config_changes method of IInstaller, know that that method has been removed from the plugin interface and is no longer used by Certbot.� )�
stacklevelT)�reversez3Certbot has not saved backups of your configurationNzInvalid directories in {0}�
CHANGES_SINCEzAffected files:� FILEPATHSz {0}� NEW_FILESzNew Configuration Files:�
F)Zforce_interactive�pause)�warnings�warn�DeprecationWarningr r r r r r �info�floatr r r �format�append�time�ctimer r# �open�read�
splitlines�isfile�zopeZ componentZ
getUtilityr ZIDisplayZnotification)
r r% Zbkup�outputZcur_dirZ
changes_fd�paths_fd� filepathsr �new_fdr r r �view_config_changes� sR �
�
�zReverter.view_config_changesc C s | � | 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_dirr r �r �
save_files�
save_notesr r r �add_to_temp_checkpoint� s
�zReverter.add_to_temp_checkpointc C s | � |� | �| 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_savesrC r �in_progress_dirrD r r r �add_to_checkpoint� s
�zReverter.add_to_checkpointc
C s t �|tj| jj� | �tj� |d��\}}t
|�}|D ]�}||kr8t�d|� z>t
�|tj� |tj�|�d t|� �� |�d�|�� W n< tk
r� |�� t�d||� t�d�||���Y nX |d7 }q8|�� ttj� |d�d ��}|�|� W 5 Q R X d
S )ag Add 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
r, zCreating backup of %s�_�{0}
z&Unable to add file %s to checkpoint %sz(Unable to add file {0} to checkpoint {1}r r+ �aN)r r
r r r r
�_read_and_appendr r r# r"