HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.29
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/snapper.cpython-310.pyc
o

�N�g4l�
@sndZddlZddlZddlZddlZddlZddlZddlm	Z	z
ddl
mZdZWne
y5dZYnwzddlZdZWne
yIdZYnwddd	d
ddd
ddd�	ZdZdZdZe�e�ZdZdZdZdZer�ze��ZWnejy�ZzeZWYdZ[n2dZ[wwee��vr�z
eje� ee�ed�ZWneje!fy�ZzeZWYdZ[ndZ[wwdZdd�Z"dd�Z#dd�Z$dMdd�Z%dNdd�Z&d d!�Z'd"d#�Z(dMd$d%�Z)dMd&d'�Z*d(d)�Z+dMd*d+�Z,	dOd,d-�Z-		.			/	dPd0d1�Z.dQd2d3�Z/	dRd4d5�Z0d6d7�Z1d8d9�Z2d:d;�Z3dSd<d=�Z4dSd>d?�Z5dTd@dA�Z6dMdBdC�Z7dMdDdE�Z8dTdFdG�Z9dMdHdI�Z:dUdKdL�Z;dS)Vuj
Module to manage filesystem snapshots with snapper

.. versionadded:: 2016.11.0

:codeauthor:    Duncan Mac-Vicar P. <dmacvicar@suse.de>
:codeauthor:    Pablo Suárez Hernández <psuarezhernandez@suse.de>

:depends:       ``dbus`` Python module.
:depends:       ``snapper`` http://snapper.io, available in most distros
:maturity:      new
:platform:      Linux
�N��CommandExecutionError)�getpwuidTF�created�deletedztype changedZmodifiedzpermission changedz
owner changedz
group changedzextended attributes changedzACL info changed)	������ �@��zorg.opensuse.Snapperz/org/opensuse/Snapper)Zdbus_interfacezsnapper is missingcCsPd}tsd|�d�fStsd|�t�fStsd|�t�fSts&d|�d�fSdS)Nz(The snapper module cannot be loaded: {0}Fzmissing python dbus modulezpwd module not available�snapper)�HAS_DBUS�formatr�
snapper_error�bus�system_bus_error�HAS_PWD)Z	error_msg�r�H/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/snapper.py�__virtual__SsrcCs�i}|d|d<gd�|d|d<|ddkr|d|d<|d	d
kr+|d	|d<ntt���|d<t|d�d|d
<|d|d<|d|d<i|d<|d��D]
\}}||d|<qS|S)a�
    Returns snapshot data from a D-Bus response.

    A snapshot D-Bus response is a dbus.Struct containing the
    information related to a snapshot:

    [id, type, pre_snapshot, timestamp, user, description,
     cleanup_algorithm, userdata]

    id: dbus.UInt32
    type: dbus.UInt16
    pre_snapshot: dbus.UInt32
    timestamp: dbus.Int64
    user: dbus.UInt32
    description: dbus.String
    cleaup_algorithm: dbus.String
    userdata: dbus.Dictionary
    r�id)�single�pre�postr�typerrr�����	timestampr	�user��description��cleanup�userdata�)�int�timer�items)�snapshot�data�key�valuerrr�_snapshot_to_dataasr0cCs2|��}|dkrd�|d�S|dkrdS|��S)z>
    Returns a error message from a snapper DBusException
    zerror.unknown_configzUnknown configuration '{}'�configzerror.illegal_snapshotzInvalid snapshot)Z
get_dbus_namer)�exc�args�errorrrr�_dbus_exception_to_reason�sr5�rootc
CsLz
t�|�}dd�|D�WStjy%}ztd�t|t�����d}~ww)z�
    List available snapshots

    CLI Example:

    .. code-block:: bash

        salt '*' snapper.list_snapshots config=myconfig
    cS�g|]}t|��qSr)r0)�.0�srrr�
<listcomp>��z"list_snapshots.<locals>.<listcomp>z-Error encountered while listing snapshots: {}N)rZ
ListSnapshots�dbus�
DBusExceptionrrr5�locals)r1Z	snapshotsr2rrr�list_snapshots�s


����r?c
CsLz
t�|t|��}t|�WStjy%}ztd�t|t	�����d}~ww)z�
    Get detailed information about a given snapshot

    CLI Example:

    .. code-block:: bash

        salt '*' snapper.get_snapshot 1
    z/Error encountered while retrieving snapshot: {}N)
rZGetSnapshotr)r0r<r=rrr5r>)�numberr1r,r2rrr�get_snapshot�s


����rAc
CsJzt��}dd�|D�WStjy$}ztd�t|t�����d}~ww)zw
    List all available configs

    CLI Example:

    .. code-block:: bash

        salt '*' snapper.list_configs
    cSsi|]	}|d|d�qS)rrr)r8r1rrr�
<dictcomp>�sz list_configs.<locals>.<dictcomp>z2Error encountered while listing configurations: {}N)rZListConfigsr<r=rrr5r>)Zconfigsr2rrr�list_configs�s

����rCcCst|t�r|r	dSdS|S)NZyes�no)�
isinstance�bool)r/rrr�_config_filter�s
rGc
KsXzdd�|��D�}t�||�WdStjy+}ztd�|t|t�����d}~ww)aN
    Set configuration values

    CLI Example:

    .. code-block:: bash

        salt '*' snapper.set_config SYNC_ACL=True

    Keys are case insensitive as they will be always uppercased to snapper
    convention. The above example is equivalent to:

    .. code-block:: bash

        salt '*' snapper.set_config sync_acl=True
    cSs(i|]\}}|�d�s|��t|��qS��__)�
startswith�upperrG�r8�k�vrrrrB�s��zset_config.<locals>.<dictcomp>z4Error encountered while setting configuration {}: {}NT)	r+rZ	SetConfigr<r=rrr5r>)�name�kwargsr-r2rrr�
set_config�s������rQcCstt|�dd�d�}|dS)z4
    Returns the last existing created snapshot
    cSs|dS)Nrr)�xrrr�<lambda>sz$_get_last_snapshot.<locals>.<lambda>)r.r )�sortedr?)r1Z
snapshot_listrrr�_get_last_snapshot�srUc
CsH|d@|d@|d@|d@|d@|d@|d@|d@|d	@f	}d
d�|D�S)z�
    Converts a numeric dbus snapper status into a string

    CLI Example:

    .. code-block:: bash

        salt '*' snapper.status_to_string <dbus_status>
    rrr	r
rrr
rrcSsg|]}|rt|�qSr)�DBUS_STATUS_MAP)r8�statusrrrr:sz$status_to_string.<locals>.<listcomp>r)Zdbus_statusZstatus_tuplerrr�status_to_strings�rXc
CsBzt�|�}|WStjy }ztd�t|t�����d}~ww)z�
    Retrieves all values from a given configuration

    CLI Example:

    .. code-block:: bash

      salt '*' snapper.get_config
    z4Error encountered while retrieving configuration: {}N)rZ	GetConfigr<r=rrr5r>)rOr1r2rrr�
get_configs


����rYc
Cs�dd�}|s
|d�|s|d�|s|d�|sd}zt�||||�|r-t|fi|��t|�WStjyI}ztd�t|t	�����d}~ww)	a
    Creates a new Snapper configuration

    name
        Name of the new Snapper configuration.
    subvolume
        Path to the related subvolume.
    fstype
        Filesystem type of the subvolume.
    template
        Configuration template to use. (Default: default)
    extra_opts
        Extra Snapper configuration opts dictionary. It will override the values provided
        by the given template (if any).

    CLI Example:

    .. code-block:: bash

      salt '*' snapper.create_config name=myconfig subvolume=/foo/bar/ fstype=btrfs
      salt '*' snapper.create_config name=myconfig subvolume=/foo/bar/ fstype=btrfs template="default"
      salt '*' snapper.create_config name=myconfig subvolume=/foo/bar/ fstype=btrfs extra_opts='{"NUMBER_CLEANUP": False}'
    cSstd|�d���)NzYou must provide a "z" for the new configurationr)�argnamerrr�raise_arg_errorOs
�z&create_config.<locals>.raise_arg_errorrO�	subvolume�fstype�z:Error encountered while creating the new configuration: {}N)
rZCreateConfigrQrYr<r=rrr5r>)rOr\r]�templateZ
extra_optsr[r2rrr�
create_config4s,

����r`rr@c

Ks�|si}|�d�}|dur|durd|��}|dur||d<d}z>|dkr0t�||||�}W|S|dkr?t�||||�}W|S|dkrW|durKtd��t�|||||�}W|Std	|�d
���tjyv}	ztd�t	|	t
�����d}	~	ww)a�
    Creates an snapshot

    config
        Configuration name.
    snapshot_type
        Specifies the type of the new snapshot. Possible values are
        single, pre and post.
    pre_number
        For post snapshots the number of the pre snapshot must be
        provided.
    description
        Description for the snapshot. If not given, the salt job will be used.
    cleanup_algorithm
        Set the cleanup algorithm for the snapshot.

    number
        Deletes old snapshots when a certain number of snapshots
        is reached.
    timeline
        Deletes old snapshots but keeps a number of hourly,
        daily, weekly, monthly and yearly snapshots.
    empty-pre-post
        Deletes pre/post snapshot pairs with empty diffs.
    userdata
        Set userdata for the snapshot (key-value pairs).

    Returns the number of the created snapshot.

    CLI Example:

    .. code-block:: bash

        salt '*' snapper.create_snapshot
    Z	__pub_jidNz	salt job �salt_jidrrrzVpre snapshot number 'pre_number' needs to bespecified for snapshots of the 'post' typezInvalid snapshot type '�'�1Error encountered while listing changed files: {})�getrZCreateSingleSnapshotZCreatePreSnapshotrZCreatePostSnapshotr<r=rr5r>)
r1�
snapshot_type�
pre_numberr$�cleanup_algorithmr'rP�jidZnew_nrr2rrr�create_snapshotjsJ,

�����
��
����ric
Cs�|std��z>dd�t|�D�}t|t�s|g}t|��t|��s7td�d�dd�t|��t|��D�����t	�
||�||dd�iWStjyY}ztt
|t����d	}~ww)
ax
    Deletes an snapshot

    config
        Configuration name. (Default: root)

    snapshots_ids
        List of the snapshots IDs to be deleted.

    CLI Example:

    .. code-block:: bash

        salt '*' snapper.delete_snapshot 54
        salt '*' snapper.delete_snapshot config=root 54
        salt '*' snapper.delete_snapshot config=root snapshots_ids=[54,55,56]
    �'Error: No snapshot ID has been providedcSsg|]}|d�qS)rr�r8rRrrrr:�r;z#delete_snapshot.<locals>.<listcomp>zError: Snapshots '{}' not foundz, cSr7r)�strrkrrrr:�s��r)ZidsrWN)rr?rE�list�set�issubsetr�join�
differencerZDeleteSnapshotsr<r=r5r>)Z
snapshots_idsr1Zcurrent_snapshots_idsr2rrr�delete_snapshot�s0
�������rrc
Cs�|std��t||d�}z4|dur|n|d|dur|n|d|dur%|n|dd�}t�|||d|d|d�t||d�WStjyU}ztt|t����d}~ww)a�
    Modify attributes of an existing snapshot.

    config
        Configuration name. (Default: root)

    snapshot_id
        ID of the snapshot to be modified.

    cleanup
        Change the cleanup method of the snapshot. (str)

    description
        Change the description of the snapshot. (str)

    userdata
        Change the userdata dictionary of the snapshot. (dict)

    CLI Example:

    .. code-block:: bash

        salt '*' snapper.modify_snapshot 54 description="my snapshot description"
        salt '*' snapper.modify_snapshot 54 description="my snapshot description"
        salt '*' snapper.modify_snapshot 54 userdata='{"foo": "bar"}'
        salt '*' snapper.modify_snapshot snapshot_id=54 cleanup="number"
    rj)r1r@Nr$r&r')r$r&r')rrArZSetSnapshotr<r=r5r>)Zsnapshot_idr$r'r&r1r,Zupdated_optsr2rrr�modify_snapshot�s(����rscCs4|rt|�nd}|durt|�nt|�d}||fS)zP
    Returns numerical interval based on optionals num_pre, num_post values
    rNr)r)rU)r1�num_pre�num_postrrrrr�_get_num_interval srvcCs&tjdd|gdtjdd�j}|�d�S)z)
    Checks if a file is a text file
    �filez-biFT)�check�stdout�textrz)�
subprocess�run�PIPEryrJ)�filenameZtype_of_filerrr�
_is_text_file)s��
rc
Os
|�dd�}|�dd|�d��}|�dd�}|�di�}d	d
�|��D�}dd
�|��D�}tdd|d
|||d�|��}|tvrItd|�d���zt||i|��}	Wntyr}
zd�t|
�t|jg�}	WYd}
~
nd}
~
wwtdd|d||||d�|��|	S)a
    Runs a function from an execution module creating pre and post snapshots
    and associating the salt job id with those snapshots for easy undo and
    cleanup.

    function
        Salt function to call.

    config
        Configuration name. (default: "root")

    description
        A description for the snapshots. (default: None)

    userdata
        Data to include in the snapshot metadata. (default: None)

    cleanup_algorithm
        Snapper cleanup algorithm. (default: "number")

    `*args`
        args for the function to call. (default: None)

    `**kwargs`
        kwargs for the function to call (default: None)

    This  would run append text to /etc/motd using the file.append
    module, and will create two snapshots, pre and post with the associated
    metadata. The jid will be available as salt_jid in the userdata of the
    snapshot.

    You can immediately see the changes

    CLI Example:

    .. code-block:: bash

        salt '*' snapper.run file.append args='["/etc/motd", "some text"]'
    r1r6r$zsnapper.run[�]rgr@r'cSs i|]\}}|�d�s||�qSrH�rJrLrrrrBc� zrun.<locals>.<dictcomp>cSs i|]\}}|�d�r||�qSrHr�rLrrrrBdr��snapper.create_snapshotr�r1rer$rgr'z
function "z" does not exist�
Nr)r1rerfr$rgr'r)�popr+�__salt__rrprl�__doc__)�functionr3rPr1r$rgr'Zfunc_kwargsZpre_nr�retr2rrrr|6sD(��	$����	r|c
Cs�zTt|||�\}}t�|t|�t|��t�|t|�t|��}i}t�|d}|D]'}|d�|�r=|dt|�d�n|d}	dt|d�i|t	j
�||	�<q*|WStj
yl}
ztd�t|
t�����d}
~
ww)a|
    Returns a comparison between two snapshots

    config
        Configuration name.

    num_pre
        first snapshot ID to compare. Default is last snapshot

    num_post
        last snapshot ID to compare. Default is 0 (current state)

    CLI Example:

    .. code-block:: bash

        salt '*' snapper.status
        salt '*' snapper.status num_pre=19 num_post=20
    �	SUBVOLUMErNrWrrc)rvrZCreateComparisonr)ZGetFilesrCrJ�lenrX�os�path�normpathr<r=rrr5r>)r1rtrurr�filesZ
status_retr\rw�	_filepathr2rrrrW�s((��
����rWcCst|||���S)a�
    Returns the files changed between two snapshots

    config
        Configuration name.

    num_pre
        first snapshot ID to compare. Default is last snapshot

    num_post
        last snapshot ID to compare. Default is 0 (current state)

    CLI Example:

    .. code-block:: bash

        salt '*' snapper.changed_files
        salt '*' snapper.changed_files num_pre=19 num_post=20
    )rW�keys)r1rtrurrr�
changed_files�sr�c
Cs�t|||�\}}t|||�}t|���}t|p|�}|�|�s(td�||���tdd�|||d�|���}	z|	�	d�}
i}|
D]
}|�	d�\}
}|||
<qA|WSt
yd}ztd|	����d}~ww)a,
    Undo all file changes that happened between num_pre and num_post, leaving
    the files into the state of num_pre.

    .. warning::
        If one of the files has changes after num_post, they will be overwritten
        The snapshots are used to determine the file list, but the current
        version of the files will be overwritten by the versions in num_pre.

        You to undo changes between num_pre and the current version of the
        files use num_post=0.

    CLI Example:

    .. code-block:: bash

        salt '*' snapper.undo
    zNGiven file list contains files that are not presentin the changed filelist: {}zcmd.runz"snapper -c {} undochange {}..{} {}� �:z)Error while processing Snapper response: N)rvrWrnr�rorrr�rp�split�
ValueError)r1r�rtrurrZchanges�changed�	requestedZcmdret�
componentsr��compr.�valr2rrr�undo�s6

���

���r�csb�fdd�t|�D�}dd�|D�}dd�|D�}|r|s%td��d���|dd|ddfS)	z�
    Returns pre/post snapshots made by a given Salt jid

    Looks for 'salt_jid' entries into snapshots userdata which are created
    when 'snapper.run' is executed.
    cs"g|]
}|d�d��kr|�qS)r'ra)rdrk�rhrrr:�sz&_get_jid_snapshots.<locals>.<listcomp>cS�g|]
}|ddkr|�qS)rrrrkrrrr:�cSr�)rrrrkrrrr:r�zJid 'z' snapshots not foundrr)r?r)rhr1Z
jid_snapshots�pre_snapshot�
post_snapshotrr�r�_get_jid_snapshots�s
�r�cC�t||d�\}}t|||d�S)z�
    Undo the changes applied by a salt job

    jid
        The job id to lookup

    config
        Configuration name.

    CLI Example:

    .. code-block:: bash

        salt '*' snapper.undo_jid jid=20160607130930720112
    �r1�rtru)r�r��rhr1r�r�rrr�undo_jid�r�c
Cs��zUt|||�\}}t|||�}|r||vr|gng}t�|d}|r+t�||d�n|}|r6t�||d�n|}	t�}
dd�|D�D]�}|}|�|�rS|t|�d�}tj	�
|d|��dd�}
tj	�
|	d|��dd�}tj	�|
�r�d}t
jj�|
��}d	d�|��D�}Wd�n1s�wYng}d}tj	�|�r�d}t
jj�|��}d
d�|��D�}Wd�n1s�wYng}d}t|
�s�t|�r�dd�tj|||
|d
��d�|
|<|r�|s�d|
|d<|s�|r�d|
|d<qBt|
��s?t|��s?ddi|
|<|�rtdd�|��|
|d<|�r'tdd�|��|
|d<|�r3|�s3d|
|d<|�r?|�s?d|
|d<qB|�rJt�||d�|�rTt�||d�|
WStj�yo}ztd�t|t�����d}~ww)a
    Returns the differences between two snapshots

    config
        Configuration name.

    filename
        if not provided the showing differences between snapshots for
        all "text" files

    num_pre
        first snapshot ID to compare. Default is last snapshot

    num_post
        last snapshot ID to compare. Default is 0 (current state)

    CLI Example:

    .. code-block:: bash

        salt '*' snapper.diff
        salt '*' snapper.diff filename=/var/log/snapper.log num_pre=19 num_post=20
    r�FcSsg|]
}tj�|�s|�qSr)r�r��isdir)r8�filepathrrrr:Cr�zdiff.<locals>.<listcomp>N�/z//TcS�g|]	}tjj�|��qSr��salt�utilsZstringutilsZ
to_unicode�r8Z_lrrrr:R��cSr�rr�r�rrrr:\r�ztext file changedr^)�fromfile�tofile)�comment�diffztext file deletedr�ztext file createdzbinary file changedzhashutil.sha256_digestZold_sha256_digestZnew_sha256_digestzbinary file createdzbinary file deletedzAError encountered while showing differences between snapshots: {})rvr�rCrZ
MountSnapshot�dictrJr�r�r�r��replace�isfiler�r�r�Zfopen�	readlinesrrp�difflibZunified_diffr�ZUmountSnapshotr<r=rrr5r>)r1r~rtrurrr�r\Z	pre_mountZ
post_mountZ
files_diffr�r�Zpre_fileZ	post_fileZpre_file_existsZrfhZpre_file_contentZpost_file_existsZpost_file_contentr2rrrr�s�
���������
�������
����r�cCr�)z�
    Returns the changes applied by a `jid`

    jid
        The job id to lookup

    config
        Configuration name.

    CLI Example:

    .. code-block:: bash

        salt '*' snapper.diff_jid jid=20160607130930720112
    r�r�)r�r�r�rrr�diff_jid�r�r��baselinecCstd|dddd|id�S)a
    Creates a snapshot marked as baseline

    tag
        Tag name for the baseline

    config
        Configuration name.

    CLI Example:

    .. code-block:: bash

        salt '*' snapper.create_baseline
        salt '*' snapper.create_baseline my_custom_baseline
    r�rzbaseline snapshotr@Zbaseline_tagr�)r�)�tagr1rrr�create_baseline�s�r�)r6)rr6)NNNNN)r6rNNr@N)Nr6)NNNNr6)r6NN)r6NNN)r�r6)<r�r��loggingr�r{r*Zsalt.utils.filesr�Zsalt.exceptionsr�pwdrr�ImportErrorr<rrVZSNAPPER_DBUS_OBJECTZSNAPPER_DBUS_PATHZSNAPPER_DBUS_INTERFACE�	getLogger�__name__�logrrrrZ	SystemBusr=r2Zlist_activatable_namesZ	InterfaceZ
get_objectr�rr0r5r?rArCrGrQrUrXrYr`rirrrsrvrr|rWr�r�r�r�r�r�r�rrrr�<module>s����
��

���*



!

�7
�
T,
�7	
M
,

2


s