%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/lib/python2.7/site-packages/salt/modules/
Upload File :
Create Path :
Current File : //usr/lib/python2.7/site-packages/salt/modules/snapper.pyc

�
���^c@@s�dZddlmZmZmZddlZddlZddlZddlZyddl	m
Z
eZWne
k
r�eZnXddlmZddlZddlmZyddlZeZWne
k
r�eZnXi	dd6d	d
6dd6d
d6dd6dd6dd6dd6dd6ZdZdZdZeje�ZdZdZ dZ!dZ"eryej#�ZWn)ej$k
r�Z%ej&e%�e%Z qXeej'�kry%ej(ej)ee�de�Z!Wqej$e*fk
r
Z%ej&e%�e%Z"qXqdZ"nd�Z+d�Z,d�Z-d d!�Z.dd d"�Z/d#�Z0d$�Z1d d%�Z2d d&�Z3d'�Z4d d(�Z5dddddd)�Z6d d*ddd+dd,�Z7dd d-�Z8ddddd d.�Z9d/�Z:d0�Z;d1�Z<d ddd2�Z=d ddd3�Z>d dddd4�Z?d d5�Z@d d6�ZAd dddd7�ZBd d8�ZCd9d d:�ZDdS(;uj
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
i(tabsolute_importtunicode_literalstprint_functionN(tgetpwuid(tCommandExecutionError(tsixucreatediudeletediutype changediumodifiediupermission changediu
owner changedi u
group changedi@uextended attributes changedi�uACL info changediuorg.opensuse.Snapperu/org/opensuse/Snappertdbus_interfaceusnapper is missingcC@snd}tst|jd�fSts8t|jt�fStsQt|jt�fStsjt|jd�fSdS(Nu(The snapper module cannot be loaded: {0}umissing python dbus moduleupwd module not availableusnapper(tHAS_DBUStFalsetformattsnappert
snapper_errortbustsystem_bus_errortHAS_PWD(t	error_msg((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pyt__virtual__UscC@s�i}|d|d<dddg|d|d<|ddkrP|d|d<n|d	d
krq|d	|d<nttj��|d<t|d�d|d
<|d|d<|d|d<i|d<x,|dj�D]\}}||d|<q�W|S(u�
    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
    iuidusingleupreupostiutypeiii����u	timestampiuuseriudescriptioniucleanupuuserdatai(tintttimeRtitems(tsnapshottdatatkeytvalue((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pyt_snapshot_to_datacs
cC@sG|j�}|dkr)dj|d�S|dkr9dS|j�SdS(u>
    Returns a error message from a snapper DBusException
    uerror.unknown_configuUnknown configuration '{0}'uconfiguerror.illegal_snapshotuInvalid snapshotN(t
get_dbus_nameR	(texctargsterror((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pyt_dbus_exception_to_reason�surootcC@sny0tj|�}g|D]}t|�^qSWn7tjk
ri}tdjt|t�����nXdS(u�
    List available snapshots

    CLI example:

    .. code-block:: bash

        salt '*' snapper.list_snapshots config=myconfig
    u.Error encountered while listing snapshots: {0}N(	R
t
ListSnapshotsRtdbust
DBusExceptionRR	Rtlocals(tconfigt	snapshotstsR((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pytlist_snapshots�s
!cC@sdy&tj|t|��}t|�SWn7tjk
r_}tdjt|t	�����nXdS(u�
    Get detailed information about a given snapshot

    CLI example:

    .. code-block:: bash

        salt '*' snapper.get_snapshot 1
    u0Error encountered while retrieving snapshot: {0}N(
R
tGetSnapshotRRRR RR	RR!(tnumberR"RR((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pytget_snapshot�s
cC@sby$tj�}td�|D��SWn7tjk
r]}tdjt|t�����nXdS(uw
    List all available configs

    CLI example:

    .. code-block:: bash

        salt '*' snapper.list_configs
    cs@s#|]}|d|dfVqdS(iiN((t.0R"((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pys	<genexpr>�su3Error encountered while listing configurations: {0}N(	R
tListConfigstdictRR RR	RR!(tconfigsR((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pytlist_configs�s
cC@s!t|t�r|rdSdS|S(Nuyesuno(t
isinstancetbool(R((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pyt_config_filter�scK@sqy0td�|j�D��}tj||�Wn:tjk
rl}tdj|t|t	�����nXt
S(uN
    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
    cs@s<|]2\}}|jd�s|j�t|�fVqdS(u__N(t
startswithtupperR0(R)tktv((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pys	<genexpr>�s	u6Error encountered while setting configuration {0}: {1}(R+RR
t	SetConfigRR RR	RR!tTrue(tnametkwargsRR((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pyt
set_config�s	cC@s#tt|�dd��}|dS(u4
    Returns the last existing created snapshot
    RcS@s|dS(Nuid((tx((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pyt<lambda>�si����(tsortedR%(R"t
snapshot_list((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pyt_get_last_snapshot�sc
C@sf|d@|d@|d@|d@|d@|d@|d@|d@|d	@f	}g|D]}|rLt|^qLS(
u�
    Converts a numeric dbus snapper status into a string

    CLI Example:

    .. code-block:: bash

        salt '*' snapper.status_to_string <dbus_status>
    iiiiii i@i�i(tDBUS_STATUS_MAP(tdbus_statuststatus_tupletstatus((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pytstatus_to_stringscC@sUytj|�}|SWn7tjk
rP}tdjt|t�����nXdS(u�
    Retrieves all values from a given configuration

    CLI example:

    .. code-block:: bash

      salt '*' snapper.get_config
    u5Error encountered while retrieving configuration: {0}N(R
t	GetConfigRR RR	RR!(R7R"R((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pyt
get_configs
cC@s�d�}|s|d�n|s/|d�n|sB|d�n|sQd}ny:tj||||�|r�t||�nt|�SWn7tjk
r�}tdjt|t	�����nXdS(u
    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}'
    cS@stdj|���dS(Nu2You must provide a "{0}" for the new configuration(RR	(targname((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pytraise_arg_errorDsunameu	subvolumeufstypeuu;Error encountered while creating the new configuration: {0}N(
R
tCreateConfigR9RERR RR	RR!(R7t	subvolumetfstypettemplatet
extra_optsRGR((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pyt
create_config(s$	


	usingleunumberc
K@sQ|si}n|jd�}|d
krH|d
k	rHdj|�}n|d
k	ra||d<nd
}y�|dkr�tj||||�}n�|dkr�tj||||�}nZ|dkr�|d
kr�td��ntj|||||�}ntdj|���Wn7tj	k
rL}	td	jt
|	t�����nX|S(u�
    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
    u	__pub_jidusalt job {0}usalt_jidusingleupreupostuVpre snapshot number 'pre_number' needs to bespecified for snapshots of the 'post' typeuInvalid snapshot type '{0}'u2Error encountered while listing changed files: {0}N(tgettNoneR	R
tCreateSingleSnapshottCreatePreSnapshotRtCreatePostSnapshotRR RR!(
R"t
snapshot_typet
pre_numbertdescriptiontcleanup_algorithmtuserdataR8tjidtnew_nrR((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pytcreate_snapshot^s8&	
cC@s"|std��ny�gt|�D]}|d^q%}t|t�sV|g}nt|�jt|��s�tdjdjgt|�jt|��D]}t	j
|�^q�����ntj||�ii|d6dd6|6SWn.t
jk
r}tt|t����nXdS(	ux
    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]
    u'Error: No snapshot ID has been provideduidu Error: Snapshots '{0}' not foundu, uidsudeletedustatusN(RR%R.tlisttsettissubsetR	tjoint
differenceRt	text_typeR
tDeleteSnapshotsRR RR!(t
snapshots_idsR"R:tcurrent_snapshots_idsR((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pytdelete_snapshot�s#4cC@s�|std��ntd|d|�}y�i|dk	rB|n|dd6|dk	r_|n|dd6|dk	r||n|dd6}tj|||d|d|d�td|d|�SWn.tjk
r�}tt|t����nXdS(u�
    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"
    u'Error: No snapshot ID has been providedR"R'udescriptionucleanupuuserdataN(	RR(ROR
tSetSnapshotRR RR!(tsnapshot_idRURWtcleanupR"Rtupdated_optsR((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pytmodify_snapshot�s   	cC@sJ|rt|�nd}|dk	r0t|�n
t|�d}||fS(uP
    Returns numerical interval based on optionals num_pre, num_post values
    iuidN(RROR>(R"tnum_pretnum_posttposttpre((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pyt_get_num_intervals(cC@s.tjdj|�d�j�}|jd�S(u)
    Checks if a file is a text file
    ufile -bi {0}urutext(tostpopenR	treadR1(tfilenamettype_of_file((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pyt
_is_text_file
s!cO@sc|jdd�}|jddj|��}|jdd�}|jdi�}td�|j�D��}td	�|j�D��}td
d|dd
d|d|d||�}|tkr�tdj|���nyt|||�}	Wn8tk
r,}
djtj|
�t|j	g�}	nXtd
d|ddd|d|d|d||�|	S(u
    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"]'
    uconfigurootudescriptionusnapper.run[{0}]ucleanup_algorithmunumberuuserdatacs@s0|]&\}}|jd�s||fVqdS(u__N(R1(R)R3R4((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pys	<genexpr>?scs@s0|]&\}}|jd�r||fVqdS(u__N(R1(R)R3R4((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pys	<genexpr>@susnapper.create_snapshotR"RSupreRURVRWufunction "{0}" does not existu
upostRT(
tpopR	R+Rt__salt__RR^RR`t__doc__(tfunctionRR8R"RURVRWtfunc_kwargstpre_nrtretR((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pytruns:(
	)
cC@s"y�t|||�\}}tj|t|�t|��tj|t|�t|��}i}t�|d}xj|D]b}|dj|�r�|dt|�n|d}	it|d�d6|t	j
j||	�<qyW|SWn7tj
k
r}
tdjt|
t�����nXdS(u|
    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
    u	SUBVOLUMEiiustatusu2Error encountered while listing changed files: {0}N(RnR
tCreateComparisonRtGetFilesR-R1tlenRCRotpathtnormpathRR RR	RR!(R"RjRkRmRltfilest
status_rett	SUBVOLUMEtfilet	_filepathR((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pyRB_s!
1/cC@st|||�j�S(u�
    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
    (RBtkeys(R"RjRk((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pyt
changed_files�scC@s t|||�\}}t|||�}t|j��}t|pH|�}|j|�sytdj||���ntddj|||dj|���}	yM|	j	d�}
i}x-|
D]%}|j	d�\}
}|||
<q�W|SWn(t
k
r}tdj|	���nXdS(u,
    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
    uOGiven file list contains files that are not presentin the changed filelist: {0}ucmd.runu&snapper -c {0} undochange {1}..{2} {3}u u:u,Error while processing Snapper response: {0}N(RnRBR\R�R]RR	RvR^tsplitt
ValueError(R"R�RjRkRmRltchangestchangedt	requestedtcmdrett
componentsR{tcompRtvalR((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pytundo�s(

cC@s�gt|�D]%}|djd�|kr
|^q
}g|D]}|ddkr?|^q?}g|D]}|ddkrh|^qh}|s�|r�tdj|���n|dd|ddfS(	u�
    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.
    uuserdatausalt_jidutypeupreupostuJid '{0}' snapshots not foundiuid(R%RNRR	(RXR"R:t
jid_snapshotstpre_snapshott
post_snapshot((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pyt_get_jid_snapshots�s8))cC@s.t|d|�\}}t|d|d|�S(u�
    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
    R"RjRk(R�R�(RXR"R�R�((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pytundo_jid�sc
C@sy�t|||�\}}t|||�}|rQ||krH|gng}nt�|d}|r}tj||t�n|}|r�tj||t�n|}	t�}
x�g|D]}tjj	|�s�|^q�D]�}|}|j
|�r|t|�}ntjj|d|�j
dd�}
tjj|	d|�j
dd�}tjj|
�r�t}tjjj|
��5}g|j�D]}tjjj|�^q�}WdQXng}t}tjj|�r-t}tjjj|��5}g|j�D]}tjjj|�^q}WdQXng}t}t|
�sQt|�r�idd6djtj||d|
d	|��d
6|
|<|r�|r�d|
|d<n|r�|r�d|
|d<q�q�t|
�r�t|�r�id
d6|
|<|rtddj|��|
|d<n|rHtddj|��|
|d<n|rf|rfd|
|d<n|r�|r�d|
|d<q�q�q�W|r�tj||t�n|r�tj||t�n|
SWn7tjk
r}tdj t!|t"�����nXdS(u
    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
    u	SUBVOLUMEu/u//Nutext file changeducommentutfromfilettofileudiffutext file deletedutext file createdubinary file changeduhashutil.sha256_digestuold_sha256_digestunew_sha256_digestubinary file createdubinary file deleteduBError encountered while showing differences between snapshots: {0}(#RnR�R-R
t
MountSnapshotRR+RoR�tisdirR1RR�treplacetisfileR6tsalttutilsR�tfopent	readlineststringutilst
to_unicodeRtR^tdifflibtunified_diffRvtUmountSnapshotRR RR	RR!(R"RrRjRkRmRlR�R�t	pre_mountt
post_mountt
files_difftfilepathR�tpre_filet	post_filetpre_file_existstrfht_ltpre_file_contenttpost_file_existstpost_file_contentR((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pytdiff�sv!!	2&&44

$$

cC@s.t|d|�\}}t|d|d|�S(u�
    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"RjRk(R�R�(RXR"R�R�((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pytdiff_jidWsubaselinec
C@s0tdd|ddddddd	i|d
6�S(u
    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
    usnapper.create_snapshotR"RSusingleRUubaseline snapshotRVunumberRWubaseline_tag(Rv(ttagR"((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pytcreate_baselineks
(ERwt
__future__RRRtloggingRoRR�tpwdRR6RtImportErrorRtsalt.exceptionsRtsalt.utils.filesR�tsalt.extRRRR?tSNAPPER_DBUS_OBJECTtSNAPPER_DBUS_PATHtSNAPPER_DBUS_INTERFACEt	getLoggert__name__tlogRORR
R
Rt	SystemBusR Rtwarningtlist_activatable_namest	Interfacet
get_objectR�RRRR%R(R-R0R9R>RCRERMRZRdRiRnRtR|RBR�R�R�R�R�R�R�(((s8/usr/lib/python2.7/site-packages/salt/modules/snapper.pyt<module>s�








	


			*	
			2	I$1				M'-e

Zerion Mini Shell 1.0