forked from kimchi-project/gingerbase
-
Notifications
You must be signed in to change notification settings - Fork 1
/
swupdate.py
451 lines (383 loc) · 14.2 KB
/
swupdate.py
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#
# Project Ginger Base
#
# Copyright IBM Corp, 2015-2016
#
# Code derived from Project Kimchi
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import fcntl
import os
import signal
import subprocess
import time
from configobj import ConfigObj, ConfigObjError
from psutil import pid_exists, process_iter
from wok.basemodel import Singleton
from wok.exception import NotFoundError, OperationFailed
from wok.utils import run_command, wok_log
from wok.plugins.gingerbase.config import gingerBaseLock
from wok.plugins.gingerbase.yumparser import get_yum_packages_list_update
class SoftwareUpdate(object):
__metaclass__ = Singleton
"""
Class to represent and operate with OS software update.
"""
def __init__(self):
# This stores all packages to be updated for Ginger Base perspective.
# It's a dictionary of dictionaries, in the format
# {'package_name': package},
# where:
# package = {'package_name': <string>, 'version': <string>,
# 'arch': <string>, 'repository': <string>
# }
self._packages = {}
# This stores the number of packages to update
self._num2update = 0
# Get the distro of host machine and creates an object related to
# correct package management system
try:
__import__('dnf')
wok_log.info("Loading YumUpdate features.")
self._pkg_mnger = DnfUpdate()
except ImportError:
try:
__import__('yum')
wok_log.info("Loading YumUpdate features.")
self._pkg_mnger = YumUpdate()
except ImportError:
try:
__import__('apt')
wok_log.info("Loading AptUpdate features.")
self._pkg_mnger = AptUpdate()
except ImportError:
zypper_help = ["zypper", "--help"]
(stdout, stderr, returncode) = run_command(zypper_help)
if returncode == 0:
wok_log.info("Loading ZypperUpdate features.")
self._pkg_mnger = ZypperUpdate()
else:
raise Exception("There is no compatible package "
"manager for this system.")
def _scanUpdates(self):
"""
Update self._packages with packages to be updated.
"""
self._packages = {}
self._num2update = 0
# Call system pkg_mnger to get the packages as list of dictionaries.
for pkg in self._pkg_mnger.getPackagesList():
# Check if already exist a package in self._packages
pkg_id = pkg.get('package_name')
if pkg_id in self._packages.keys():
# package already listed to update. do nothing
continue
# Update the self._packages and self._num2update
self._packages[pkg_id] = pkg
self._num2update = self._num2update + 1
def getUpdates(self):
"""
Return the self._packages.
"""
self._scanUpdates()
return self._packages
def getUpdate(self, name):
"""
Return a dictionary with all info from a given package name.
"""
if name not in self._packages.keys():
raise NotFoundError('GGBPKGUPD0002E', {'name': name})
return self._packages[name]
def getNumOfUpdates(self):
"""
Return the number of packages to be updated.
"""
self._scanUpdates()
return self._num2update
def preUpdate(self):
"""
Make adjustments before executing the command in
a child process.
"""
os.setsid()
signal.signal(signal.SIGTERM, signal.SIG_IGN)
def tailUpdateLogs(self, cb, params):
"""
When the package manager is already running (started outside gingerbase
or if wokd is restarted) we can only know what's happening by reading
the logfiles. This method acts like a 'tail -f' on the default package
manager logfile. If the logfile is not found, a simple '*' is
displayed to track progress. This will be until the process finishes.
"""
if not self._pkg_mnger.isRunning():
return
fd = None
try:
fd = os.open(self._pkg_mnger.logfile, os.O_RDONLY)
# cannot open logfile, print something to let users know that the
# system is being upgrading until the package manager finishes its
# job
except (TypeError, OSError):
msgs = []
while self._pkg_mnger.isRunning():
msgs.append('*')
cb(''.join(msgs))
time.sleep(1)
msgs.append('\n')
cb(''.join(msgs), True)
return
# go to the end of logfile and starts reading, if nothing is read or
# a pattern is not found in the message just wait and retry until
# the package manager finishes
os.lseek(fd, 0, os.SEEK_END)
msgs = []
progress = []
while True:
read = os.read(fd, 1024)
if not read:
if not self._pkg_mnger.isRunning():
break
if not msgs:
progress.append('*')
cb(''.join(progress))
time.sleep(1)
continue
msgs.append(read)
cb(''.join(msgs))
os.close(fd)
return cb(''.join(msgs), True)
def doUpdate(self, cb, params):
"""
Execute the update
"""
# reset messages
cb('')
cmd = self._pkg_mnger.update_cmd
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
preexec_fn=self.preUpdate)
msgs = []
while proc.poll() is None:
msgs.append(proc.stdout.readline())
cb(''.join(msgs))
time.sleep(0.5)
# read the final output lines
msgs.extend(proc.stdout.readlines())
retcode = proc.poll()
if retcode == 0:
return cb(''.join(msgs), True)
msgs.extend(proc.stderr.readlines())
return cb(''.join(msgs), False)
class YumUpdate(object):
"""
Class to represent and operate with YUM software update system.
It's loaded only on those systems listed at YUM_DISTROS and loads necessary
modules in runtime.
"""
def __init__(self):
self._pkgs = {}
self.update_cmd = ["yum", "-y", "update"]
self.logfile = self._get_output_log()
def _get_output_log(self):
"""
Return the logfile path
"""
yumcfg = None
try:
yumcfg = ConfigObj('/etc/yum.conf')
except ConfigObjError:
return None
if 'main' in yumcfg and 'logfile' in yumcfg['main']:
return yumcfg['main']['logfile']
return None
def _refreshUpdateList(self):
"""
Update the list of packages to be updated in the system.
"""
try:
gingerBaseLock.acquire()
self._pkgs = get_yum_packages_list_update()
except Exception, e:
raise OperationFailed('GGBPKGUPD0003E', {'err': str(e)})
finally:
gingerBaseLock.release()
def getPackagesList(self):
"""
Return a list of package's dictionaries. Each dictionary contains the
information about a package, in the format:
package = {'package_name': <string>, 'version': <string>,
'arch': <string>, 'repository': <string>}
"""
if self.isRunning():
raise OperationFailed('GGBPKGUPD0005E')
self._refreshUpdateList()
pkg_list = []
for pkg in self._pkgs:
package = {'package_name': pkg.name, 'version': pkg.version,
'arch': pkg.arch, 'repository': pkg.ui_from_repo}
pkg_list.append(package)
return pkg_list
def isRunning(self):
"""
Return True whether the YUM package manager is already running or
False otherwise.
"""
try:
with open('/var/run/yum.pid', 'r') as pidfile:
pid = int(pidfile.read().rstrip('\n'))
# cannot find pidfile, assumes yum is not running
except (IOError, ValueError):
return False
# the pidfile exists and it lives in process table
if pid_exists(pid):
return True
return False
class DnfUpdate(YumUpdate):
"""
Class to represent and operate with DNF software update system.
It's loaded only on those systems listed at DNF_DISTROS and loads necessary
modules in runtime.
"""
def __init__(self):
self._pkgs = {}
self.update_cmd = ["dnf", "-y", "update"]
self.logfile = '/var/log/dnf.log'
def isRunning(self):
"""
Return True whether the YUM package manager is already running or
False otherwise.
"""
pid = None
try:
for dnf_proc in process_iter():
if 'dnf' in dnf_proc.name():
pid = dnf_proc.pid
break
except:
return False
# the pidfile exists and it lives in process table
return pid_exists(pid)
class AptUpdate(object):
"""
Class to represent and operate with APT software update system.
It's loaded only on those systems listed at APT_DISTROS and loads necessary
modules in runtime.
"""
def __init__(self):
self._pkgs = {}
self.update_cmd = ['apt-get', 'upgrade', '-y']
self.logfile = '/var/log/apt/term.log'
def _refreshUpdateList(self):
"""
Update the list of packages to be updated in the system.
"""
apt_cache = getattr(__import__('apt'), 'Cache')()
try:
apt_cache.update()
apt_cache.upgrade()
self._pkgs = apt_cache.get_changes()
except Exception, e:
raise OperationFailed('GGBPKGUPD0003E', {'err': e.message})
def getPackagesList(self):
"""
Return a list of package's dictionaries. Each dictionary contains the
information about a package, in the format
package = {'package_name': <string>, 'version': <string>,
'arch': <string>, 'repository': <string>}
"""
if self.isRunning():
raise OperationFailed('GGBPKGUPD0005E')
gingerBaseLock.acquire()
try:
self._refreshUpdateList()
except Exception:
raise
finally:
gingerBaseLock.release()
pkg_list = []
for pkg in self._pkgs:
package = {'package_name': pkg.shortname,
'version': pkg.candidate.version,
'arch': pkg._pkg.architecture,
'repository': pkg.candidate.origins[0].label}
pkg_list.append(package)
return pkg_list
def isRunning(self):
"""
Return True whether the APT package manager is already running or
False otherwise.
"""
try:
with open('/var/lib/dpkg/lock', 'w') as lockfile:
fcntl.lockf(lockfile, fcntl.LOCK_EX | fcntl.LOCK_NB)
# cannot open dpkg lock file to write in exclusive mode means the
# apt is currently running
except IOError:
return True
return False
class ZypperUpdate(object):
"""
Class to represent and operate with Zypper software update system.
It's loaded only on those systems listed at ZYPPER_DISTROS and loads
necessary modules in runtime.
"""
def __init__(self):
self._pkgs = {}
self.update_cmd = ["zypper", "--non-interactive", "update",
"--auto-agree-with-licenses"]
self.logfile = '/var/log/zypp/history'
def _refreshUpdateList(self):
"""
Update the list of packages to be updated in the system.
"""
self._pkgs = []
cmd = ["zypper", "list-updates"]
(stdout, stderr, returncode) = run_command(cmd)
if len(stderr) > 0:
raise OperationFailed('GGBPKGUPD0003E', {'err': stderr})
for line in stdout.split('\n'):
if line.find('v |') >= 0:
info = line.split(' | ')
package = {'package_name': info[2], 'version': info[4],
'arch': info[5], 'repository': info[1]}
self._pkgs.append(package)
def getPackagesList(self):
"""
Return a list of package's dictionaries. Each dictionary contains the
information about a package, in the format
package = {'package_name': <string>, 'version': <string>,
'arch': <string>, 'repository': <string>}
"""
if self.isRunning():
raise OperationFailed('GGBPKGUPD0005E')
gingerBaseLock.acquire()
self._refreshUpdateList()
gingerBaseLock.release()
return self._pkgs
def isRunning(self):
"""
Return True whether the Zypper package manager is already running or
False otherwise.
"""
try:
with open('/var/run/zypp.pid', 'r') as pidfile:
pid = int(pidfile.read().rstrip('\n'))
# cannot find pidfile, assumes yum is not running
except (IOError, ValueError):
return False
# the pidfile exists and it lives in process table
if pid_exists(pid):
return True
return False