-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathzypp-plugin.py
More file actions
executable file
·290 lines (217 loc) · 8.71 KB
/
Copy pathzypp-plugin.py
File metadata and controls
executable file
·290 lines (217 loc) · 8.71 KB
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
285
286
287
288
289
290
#!/usr/bin/python
#
# Copyright (c) [2011-2014] Novell, Inc.
# Copyright (c) [2015] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# 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 General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact Novell, Inc.
#
# To contact Novell about this file by physical or electronic mail, you may
# find current contact information at www.novell.com.
#
# Author: Arvin Schnell <aschnell@suse.de>
#
from os import readlink, getppid, environ
from os.path import basename
import sys
import fnmatch
import re
import logging
from dbus import SystemBus, Interface, DBusException
import xml.dom.minidom as minidom
import xml.parsers.expat as expat
import json
from zypp_plugin import Plugin
class Solvable:
def __init__(self, pattern, important):
self.pattern = re.compile(pattern)
self.important = important
def __repr__(self):
return "pattern:%s important:%s" % (self.pattern, self.important)
def match(self, name):
return self.pattern.match(name)
class Config:
def __init__(self):
self.solvables = []
self.zypper_extended_description = []
self.zypper_extended_description.append("false")
self.zypper_extended_description.append("0")
self.load_file("/etc/snapper/zypp-plugin.conf")
def load_file(self, filename):
try:
self.load_dom(minidom.parse(filename))
except IOError:
logging.error("failed to open %s" % filename)
except expat.ExpatError:
logging.error("failed to parse %s" % filename)
except:
logging.error("failed to load %s" % filename)
def load_dom(self, dom):
try:
for tmp1 in dom.getElementsByTagName("solvables"):
for tmp2 in tmp1.getElementsByTagName("solvable"):
pattern = tmp2.childNodes[0].data
match = tmp2.getAttribute("match")
important = tmp2.getAttribute("important") == "true"
if not match in [ "w", "re" ]:
logging.error("unknown match attribute %s" % match)
continue
if match == "w":
pattern = fnmatch.translate(pattern)
self.solvables.append(Solvable(pattern, important))
except:
pass
try:
for tmp3 in dom.getElementsByTagName("description"):
for tmp4 in tmp3.getElementsByTagName("zypper-extended-description"):
string_size = tmp4.childNodes[0].data
description_enabled = tmp4.getAttribute("enabled")
if not description_enabled in [ "true", "false" ]:
loggin.error("unknown extended-config enabled attribute %s" % description_enabled)
continue
if description_enabled == "true":
self.zypper_extended_description[0] = "true"
self.zypper_extended_description[1] = string_size
except:
pass
class MyPlugin(Plugin):
def __init__(self):
Plugin.__init__(self)
self.num1 = self.num2 = None
self.description = ""
self.cleanup = "number"
self.userdata = {}
def parse_userdata(self, s):
userdata = {}
for kv in s.split(","):
k, v = kv.split("=", 1)
k = k.strip()
if not k:
raise ValueError
userdata[k] = v.strip()
return userdata
def get_userdata(self, headers):
try:
return self.parse_userdata(headers['userdata'])
except KeyError:
pass
except ValueError:
logging.error("invalid userdata")
return {}
def get_solvables(self, body, todo):
tmp = json.loads(body)
tsl = tmp["TransactionStepList"]
solvables = set()
for ts in tsl:
if "type" in ts:
if todo or "stage" in ts:
solvables.add(ts["solvable"]["n"])
return solvables
def match_solvables(self, names):
found = important = False
for name in names:
for solvable in config.solvables:
if solvable.match(name):
found = True
important = important or solvable.important
if found and important:
return True, True
return found, important
def zypper_arguments(self):
if basename(readlink("/proc/%d/exe" % getppid())) == "zypper":
argument = " " + " ".join(open("/proc/%s/cmdline" % getppid()).read().split('\x00')[1:])
else:
return ""
if config.zypper_extended_description[1] == "0":
return argument
else:
return argument[0:int(config.zypper_extended_description[1])]
def PLUGINBEGIN(self, headers, body):
logging.info("PLUGINBEGIN")
logging.debug("headers: %s" % headers)
if config.zypper_extended_description[0] != "true":
self.description = "zypp(%s)" % basename(readlink("/proc/%d/exe" % getppid()))
elif config.zypper_extended_description[0] == "true":
self.description = "zypp(%s)%s" % (basename(readlink("/proc/%d/exe" % getppid())), self.zypper_arguments())
self.userdata = self.get_userdata(headers)
self.ack()
def COMMITBEGIN(self, headers, body):
logging.info("COMMITBEGIN")
solvables = self.get_solvables(body, True)
logging.debug("solvables: %s" % solvables)
found, important = self.match_solvables(solvables)
logging.info("found: %s, important: %s" % (found, important))
if found or important:
self.userdata["important"] = "yes" if important else "no"
try:
logging.info("creating pre snapshot")
self.num1 = snapper.CreatePreSnapshot("root", self.description, self.cleanup,
self.userdata)
logging.debug("created pre snapshot %d" % self.num1)
except DBusException as e:
logging.error("creating snapshot failed:")
logging.error(" %s", e)
self.ack()
def COMMITEND(self, headers, body):
logging.info("COMMITEND")
if self.num1:
solvables = self.get_solvables(body, False)
logging.debug("solvables: %s" % solvables)
found, important = self.match_solvables(solvables)
logging.info("found: %s, important: %s" % (found, important))
if found or important:
self.userdata["important"] = "yes" if important else "no"
try:
snapper.SetSnapshot("root", self.num1, self.description, self.cleanup,
self.userdata)
except DBusException as e:
logging.error("setting snapshot data failed:")
logging.error(" %s", e)
try:
logging.info("creating post snapshot")
self.num2 = snapper.CreatePostSnapshot("root", self.num1, "", self.cleanup,
self.userdata)
logging.debug("created post snapshot %d" % self.num2)
except DBusException as e:
logging.error("creating snapshot failed:")
logging.error(" %s", e)
else:
try:
logging.info("deleting pre snapshot")
snapper.DeleteSnapshots("root", [ self.num1 ])
logging.debug("deleted pre snapshot %d" % self.num1)
except DBusException as e:
logging.error("deleting snapshot failed:")
logging.error(" %s", e)
self.ack()
def PLUGINEND(self, headers, body):
logging.info("PLUGINEND")
self.ack()
if "DISABLE_SNAPPER_ZYPP_PLUGIN" in environ:
logging.info("$DISABLE_SNAPPER_ZYPP_PLUGIN is set - disabling snapper-zypp-plugin")
# a dummy Plugin is needed
plugin = Plugin()
plugin.main()
else:
config = Config()
try:
bus = SystemBus()
snapper = Interface(bus.get_object('org.opensuse.Snapper', '/org/opensuse/Snapper'),
dbus_interface='org.opensuse.Snapper')
except DBusException as e:
logging.error("connect to snapperd failed:")
logging.error(" %s", e)
sys.exit(1)
plugin = MyPlugin()
plugin.main()