This file is indexed.

/usr/share/pyshared/archipelagentvirtualmachinesnapshoting/snapshoting.py is in archipel-agent-virtualmachine-snapshoting 0.6.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# -*- coding: utf-8 -*-
#
# snapshoting.py
#
# Copyright (C) 2010 Antoine Mercadal <antoine.mercadal@inframonde.eu>
# Copyright, 2011 - Franck Villaume <franck.villaume@trivialdev.com>
# This file is part of ArchipelProject
# http://archipelproject.org
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import libvirt
import xmpp

from archipelcore.archipelPlugin import TNArchipelPlugin
from archipel.archipelVirtualMachine import ARCHIPEL_ERROR_CODE_VM_MIGRATING
from archipelcore.utils import build_error_iq

from archipel.archipelLibvirtEntity import ARCHIPEL_NS_LIBVIRT_GENERIC_ERROR


ARCHIPEL_NS_SNAPSHOTING                 = "archipel:virtualmachine:snapshoting"
ARCHIPEL_ERROR_CODE_SNAPSHOT_TAKE       = -2001
ARCHIPEL_ERROR_CODE_SNAPSHOT_GET        = -2002
ARCHIPEL_ERROR_CODE_SNAPSHOT_CURRENT    = -2003
ARCHIPEL_ERROR_CODE_SNAPSHOT_DELETE     = -2004
ARCHIPEL_ERROR_CODE_SNAPSHOT_REVERT     = -2005
ARCHIPEL_ERROR_CODE_SNAPSHOT_NO_DRIVE   = -2006


class TNSnapshoting (TNArchipelPlugin):

    def __init__(self, configuration, entity, entry_point_group):
        """
        Initialize the plugin.
        @type configuration: Configuration object
        @param configuration: the configuration
        @type entity: L{TNArchipelEntity}
        @param entity: the entity that owns the plugin
        @type entry_point_group: string
        @param entry_point_group: the group name of plugin entry_point
        """
        TNArchipelPlugin.__init__(self, configuration=configuration, entity=entity, entry_point_group=entry_point_group)
        # permissions
        self.entity.permission_center.create_permission("snapshot_take", "Authorizes user to get take a snapshot", False)
        self.entity.permission_center.create_permission("snapshot_delete", "Authorizes user to delete a snapshot", False)
        self.entity.permission_center.create_permission("snapshot_get", "Authorizes user to get all snapshots", False)
        self.entity.permission_center.create_permission("snapshot_current", "Authorizes user to get current used snapshot", False)
        self.entity.permission_center.create_permission("snapshot_revert", "Authorizes user to revert to a snapshot", False)

    ### Plugin interface

    def register_handlers(self):
        """
        This method will be called by the plugin user when it will be
        necessary to register module for listening to stanza.
        """
        self.entity.xmppclient.RegisterHandler('iq', self.process_iq, ns=ARCHIPEL_NS_SNAPSHOTING)

    def unregister_handlers(self):
        """
        Unregister the handlers.
        """
        self.entity.xmppclient.UnregisterHandler('iq', self.process_iq, ns=ARCHIPEL_NS_SNAPSHOTING)

    @staticmethod
    def plugin_info():
        """
        Return informations about the plugin.
        @rtype: dict
        @return: dictionary contaning plugin informations
        """
        plugin_friendly_name           = "Virtual Machine Snapshoting"
        plugin_identifier              = "snapshoting"
        plugin_configuration_section   = None
        plugin_configuration_tokens    = None
        return {    "common-name"               : plugin_friendly_name,
                    "identifier"                : plugin_identifier,
                    "configuration-section"     : plugin_configuration_section,
                    "configuration-tokens"      : plugin_configuration_tokens }


    ### XMPP Processing

    def process_iq(self, conn, iq):
        """
        This method is invoked when a ARCHIPEL_NS_SNAPSHOTING IQ is received.
        It understands IQ of type:
            - take
            - delete
            - get
            - current
            - revert
        @type conn: xmpp.Dispatcher
        @param conn: ths instance of the current connection that send the stanza
        @type iq: xmpp.Protocol.Iq
        @param iq: the received IQ
        """
        reply = None
        action = self.entity.check_acp(conn, iq)
        self.entity.check_perm(conn, iq, action, -1, prefix="snapshot_")
        if not self.entity.domain:
            raise xmpp.protocol.NodeProcessed
        if self.entity.is_migrating and (not action in ("current", "get")):
            reply = build_error_iq(self, "Virtual machine is migrating. Can't perform any snapshoting operation.", iq, ARCHIPEL_ERROR_CODE_VM_MIGRATING)
        elif action == "take":
            reply = self.iq_take(iq)
        elif action == "delete":
            reply = self.iq_delete(iq)
        elif action == "get":
            reply = self.iq_get(iq)
        elif action == "current":
            reply = self.iq_getcurrent(iq)
        elif action == "revert":
            reply = self.iq_revert(iq)
        if reply:
            conn.send(reply)
            raise xmpp.protocol.NodeProcessed

    def iq_take(self, iq):
        """
        Creating a snapshot.
        @type iq: xmpp.Protocol.Iq
        @param iq: the received IQ
        @rtype: xmpp.Protocol.Iq
        @return: a ready to send IQ containing the result of the action
        """
        reply = iq.buildReply("result")
        try:
            xmlDesc     = iq.getTag('query').getTag("archipel").getTag('domainsnapshot')
            name        = xmlDesc.getTag('name').getData()
            old_status  = self.entity.xmppstatus
            old_show    = self.entity.xmppstatusshow
            try:
                devices_node = self.entity.definition.getTag('devices')
                disk_nodes = devices_node.getTags('disk', attrs={'type': 'file'})
                if not disk_nodes:
                    raise
            except:
                return build_error_iq(self, Exception("Virtual machine hasn't any drive to snapshot."), iq, code=ARCHIPEL_ERROR_CODE_SNAPSHOT_NO_DRIVE)
            self.entity.log.info("Creating snapshot with name %s desc :%s" % (name, xmlDesc))
            self.entity.change_presence(presence_show="dnd", presence_status="Snapshoting...")
            self.entity.domain.snapshotCreateXML(str(xmlDesc), 0)
            self.entity.change_presence(presence_show=old_show, presence_status=old_status)
            self.entity.log.info("Snapshot with name %s created" % name)
            self.entity.push_change("snapshoting", "taken")
            self.entity.shout("Snapshot", "I've created a snapshot named %s as asked by %s" % (name, iq.getFrom()))
        except libvirt.libvirtError as ex:
            self.entity.change_presence(presence_show=old_show, presence_status="Error while snapshoting")
            reply = build_error_iq(self, ex, iq, ex.get_error_code(), ns=ARCHIPEL_NS_LIBVIRT_GENERIC_ERROR)
            try:
                snapshotObject = self.entity.domain.snapshotLookupByName(name, 0)
                snapshotObject.delete(libvirt.VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN)
            except:
                pass
        except Exception as ex:
            self.entity.change_presence(presence_show=old_show, presence_status="Error while snapshoting")
            reply = build_error_iq(self, ex, iq, ARCHIPEL_ERROR_CODE_SNAPSHOT_TAKE)
            try:
                snapshotObject = self.entity.domain.snapshotLookupByName(name, 0)
                snapshotObject.delete(libvirt.VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN)
            except:
                pass
        return reply

    def iq_get(self, iq):
        """
        List all snapshots.
        @type iq: xmpp.Protocol.Iq
        @param iq: the received IQ
        @rtype: xmpp.Protocol.Iq
        @return: a ready to send IQ containing the result of the action
        """
        try:
            reply = iq.buildReply("result")
            nodes = []
            if self.entity.domain.hasCurrentSnapshot(0):
                snapshot_names = self.entity.domain.snapshotListNames(0)
                for snapshot_name in snapshot_names:
                    snapshotObject = self.entity.domain.snapshotLookupByName(snapshot_name, 0)
                    desc = snapshotObject.getXMLDesc(0)
                    n = xmpp.simplexml.NodeBuilder(data=desc).getDom()
                    nodes.append(n)
            reply.setQueryPayload(nodes)
        except libvirt.libvirtError as ex:
            reply = build_error_iq(self, ex, iq, ex.get_error_code(), ns=ARCHIPEL_NS_LIBVIRT_GENERIC_ERROR)
        except Exception as ex:
            reply = build_error_iq(self, ex, iq, ARCHIPEL_ERROR_CODE_SNAPSHOT_GET)
        return reply

    def iq_getcurrent(self, iq):
        """
        Return current snapshot.
        @type iq: xmpp.Protocol.Iq
        @param iq: the received IQ
        @rtype: xmpp.Protocol.Iq
        @return: a ready to send IQ containing the result of the action
        """
        try:
            reply = iq.buildReply("result")
            if self.entity.domain.hasCurrentSnapshot(0):
                snapshotObject = self.entity.domain.snapshotCurrent(0)
                desc = snapshotObject.getXMLDesc(0)
                n = xmpp.simplexml.NodeBuilder(data=desc).getDom()
                reply.setQueryPayload([n])
        except libvirt.libvirtError as ex:
            if ex.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN_SNAPSHOT:
                reply = iq.buildReply("result")
            else:
                reply = build_error_iq(self, ex, iq, ex.get_error_code(), ns=ARCHIPEL_NS_LIBVIRT_GENERIC_ERROR)
        except Exception as ex:
            reply = build_error_iq(self, ex, iq, ARCHIPEL_ERROR_CODE_SNAPSHOT_CURRENT)
        return reply

    def iq_delete(self, iq):
        """
        Delete snapshot.
        @type iq: xmpp.Protocol.Iq
        @param iq: the received IQ
        @rtype: xmpp.Protocol.Iq
        @return: a ready to send IQ containing the result of the action
        """
        try:
            reply = iq.buildReply("result")
            # xmlDesc = iq.getTag('query').getTag('uuid') would be better but not in API at this time.
            name = iq.getTag('query').getTag("archipel").getAttr('name')
            old_status  = self.entity.xmppstatus
            old_show    = self.entity.xmppstatusshow
            self.entity.log.info("Deleting snapshot with name %s" % name)
            self.entity.change_presence(presence_show="dnd", presence_status="Removing snapshot...")
            snapshotObject = self.entity.domain.snapshotLookupByName(name, 0)
            snapshotObject.delete(libvirt.VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN)
            self.entity.change_presence(presence_show=old_show, presence_status=old_status)
            self.entity.log.info("Snapshot with name %s deleted." % name)
            self.entity.push_change("snapshoting", "deleted")
            self.entity.shout("Snapshot", "I've deleted the snapshot named %s as asked by %s" % (name, iq.getFrom()))
        except libvirt.libvirtError as ex:
            self.entity.change_presence(presence_show=old_show, presence_status="Error while deleting snapshot.")
            reply = build_error_iq(self, ex, iq, ex.get_error_code(), ns=ARCHIPEL_NS_LIBVIRT_GENERIC_ERROR)
        except Exception as ex:
            self.entity.change_presence(presence_show=old_show, presence_status="Error while deleting snapshot.")
            reply = build_error_iq(self, ex, iq, ARCHIPEL_ERROR_CODE_SNAPSHOT_DELETE)
        return reply

    def iq_revert(self, iq):
        """
        Restore from a snapshot.
        @type iq: xmpp.Protocol.Iq
        @param iq: the received IQ
        @rtype: xmpp.Protocol.Iq
        @return: a ready to send IQ containing the result of the action
        """
        try:
            reply = iq.buildReply("result")
            # xmlDesc = iq.getTag('query').getTag('uuid') would be better but not in API at this time.
            name = iq.getTag('query').getTag("archipel").getAttr('name')
            old_show = self.entity.xmppstatusshow
            old_status = self.entity.xmppstatus
            self.entity.log.info("Restoring snapshot with name %s" % name)
            self.entity.change_presence(presence_show="dnd", presence_status="Restoring snapshot...")
            snapshotObject = self.entity.domain.snapshotLookupByName(name, 0)
            self.entity.domain.revertToSnapshot(snapshotObject, 0)
            self.entity.change_presence(presence_show=old_show, presence_status=old_status)
            self.entity.log.info("Reverted to snapshot with name %s " % name)
            self.entity.push_change("snapshoting", "restored")
            self.entity.shout("Snapshot", "I've been reverted to the snapshot named %s as asked by %s" % (name, iq.getFrom()))
        except libvirt.libvirtError as ex:
            self.entity.change_presence(presence_show=old_show, presence_status="Error while reverting.")
            reply = build_error_iq(self, ex, iq, ex.get_error_code(), ns=ARCHIPEL_NS_LIBVIRT_GENERIC_ERROR)
        except Exception as ex:
            self.entity.change_presence(presence_show=old_show, presence_status="Error while reverting.")
            reply = build_error_iq(self, ex, iq, ARCHIPEL_ERROR_CODE_SNAPSHOT_REVERT)
        return reply