forked from kimchi-project/gingerbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swupdate.py
648 lines (548 loc) · 20.5 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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#
# Project Ginger Base
#
# Copyright IBM Corp, 2015-2017
#
# 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 threading
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.yumparser import get_dnf_package_deps
from wok.plugins.gingerbase.yumparser import get_yum_package_deps
from wok.plugins.gingerbase.yumparser import get_yum_package_info
from wok.plugins.gingerbase.yumparser import get_yum_packages_list_update
from wok.plugins.gingerbase import portageparser
swupdateLock = threading.RLock()
class SoftwareUpdate(object):
__metaclass__ = Singleton
"""
Class to represent and operate with OS software update.
"""
def __init__(self):
# Get the distro of host machine and creates an object related to
# correct package management system
self._pkg_mnger = None
for module, cls in [('dnf', DnfUpdate), ('yum', YumUpdate),
('apt', AptUpdate), ('portage', PortageUpdate)]:
try:
__import__(module)
wok_log.info("Logging %s features." % cls.__name__)
self._pkg_mnger = cls()
break
except ImportError:
continue
zypper_help = ["zypper", "--help"]
(stdout, stderr, returncode) = run_command(zypper_help)
if returncode == 0:
wok_log.info("Loading ZypperUpdate features.")
self._pkg_mnger = ZypperUpdate()
if self._pkg_mnger is None:
raise Exception("There is no compatible package "
"manager for this system.")
def getUpdates(self):
"""
Return a list of packages eligible to be updated in the system.
"""
swupdateLock.acquire()
try:
pkgs = [pkg for pkg in self._pkg_mnger.getPackagesList()]
return pkgs
except:
raise
finally:
swupdateLock.release()
def getUpdate(self, name):
"""
Return a dictionary with all info from a given package name.
"""
swupdateLock.acquire()
try:
package = self._pkg_mnger.getPackageInfo(name)
if not package:
raise NotFoundError('GGBPKGUPD0002E', {'name': name})
return package
except:
raise
finally:
swupdateLock.release()
def getPackageDeps(self, name):
"""
"""
self.getUpdate(name)
swupdateLock.acquire()
try:
return self._pkg_mnger.getPackageDeps(name)
except:
raise
finally:
swupdateLock.release()
def getNumOfUpdates(self):
"""
Return the number of packages to be updated.
"""
swupdateLock.acquire()
try:
return len(self.getUpdates())
except:
raise
finally:
swupdateLock.release()
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
"""
swupdateLock.acquire()
wok_log.info('doUpdate - swupdate lock acquired')
# reset messages
cb('')
if params is not None:
cmd = self._pkg_mnger.update_cmd['specific'] + params
else:
cmd = self._pkg_mnger.update_cmd['all']
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()
swupdateLock.release()
wok_log.info('doUpdate - swupdate lock released')
if retcode == 0:
return cb(''.join(msgs), True)
msgs.extend(proc.stderr.readlines())
return cb(''.join(msgs), False)
class GenericUpdate(object):
def getPackagesList(self):
return
def getPackageInfo(self, pkg_name):
return
def getPackageDeps(self, pkg_name):
return
def isRunning(self):
return False
def wait_pkg_manager_available(self):
while self.isRunning():
time.sleep(1)
class YumUpdate(GenericUpdate):
"""
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.update_cmd = dict.fromkeys(['all', 'specific'],
["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 getPackagesList(self):
"""
Return a list of packages eligible to be updated by Yum.
"""
self.wait_pkg_manager_available()
try:
return get_yum_packages_list_update()
except Exception, e:
raise OperationFailed('GGBPKGUPD0003E', {'err': str(e)})
def getPackageInfo(self, pkg_name):
"""
Get package information. The return is a dictionary containg the
information about a package, in the format:
package = {'package_name': <string>,
'version': <string>,
'arch': <string>,
'repository': <string>
}
"""
self.wait_pkg_manager_available()
try:
return get_yum_package_info(pkg_name)
except Exception, e:
raise NotFoundError('GGBPKGUPD0003E', {'err': str(e)})
def getPackageDeps(self, pkg_name):
try:
return get_yum_package_deps(pkg_name)
except Exception, e:
raise NotFoundError('GGBPKGUPD0003E', {'err': str(e)})
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 = dict.fromkeys(['all', 'specific'],
["dnf", "-y", "update"])
self.logfile = '/var/log/dnf.log'
def getPackageDeps(self, pkg_name):
self.wait_pkg_manager_available()
try:
return get_dnf_package_deps(pkg_name)
except Exception, e:
raise NotFoundError('GGBPKGUPD0003E', {'err': str(e)})
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(GenericUpdate):
"""
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.update_cmd = {'all': ['apt-get', 'upgrade', '-y'],
'specific': ['apt-get', '-y', '--only-upgrade',
'install']}
self.logfile = '/var/log/apt/term.log'
self._apt_cache = getattr(__import__('apt'), 'Cache')()
def getPackagesList(self):
"""
Return a list of packages eligible to be updated by apt-get.
"""
self.wait_pkg_manager_available()
try:
self._apt_cache.open()
self._apt_cache.update()
self._apt_cache.upgrade()
pkgs = self._apt_cache.get_changes()
self._apt_cache.close()
except Exception, e:
raise OperationFailed('GGBPKGUPD0003E', {'err': e.message})
return [{'package_name': pkg.shortname,
'version': pkg.candidate.version,
'arch': pkg._pkg.architecture,
'repository': pkg.candidate.origins[0].label} for pkg in pkgs]
def getPackageInfo(self, pkg_name):
"""
Get package information. The return is a dictionary containg the
information about a package, in the format:
package = {'package_name': <string>,
'version': <string>,
'arch': <string>,
'repository': <string>
}
"""
self.wait_pkg_manager_available()
package = {}
try:
self._apt_cache.open()
self._apt_cache.upgrade()
pkgs = self._apt_cache.get_changes()
self._apt_cache.close()
except Exception, e:
raise OperationFailed('GGBPKGUPD0006E', {'err': e.message})
pkg = next((x for x in pkgs if x.shortname == pkg_name), None)
if not pkg:
message = 'No package found'
raise NotFoundError('GGBPKGUPD0006E', {'err': message})
package = {'package_name': pkg.shortname,
'version': pkg.candidate.version,
'arch': pkg._pkg.architecture,
'repository': pkg.candidate.origins[0].label}
return package
def getPackageDeps(self, pkg_name):
self.wait_pkg_manager_available()
try:
self._apt_cache.open()
self._apt_cache.upgrade()
pkgs = self._apt_cache.get_changes()
self._apt_cache.close()
except Exception, e:
raise OperationFailed('GGBPKGUPD0006E', {'err': e.message})
pkg = next((x for x in pkgs if x.shortname == pkg_name), None)
if not pkg:
message = 'No package found'
raise NotFoundError('GGBPKGUPD0006E', {'err': message})
return list(set([d[0].name for d in pkg.candidate.dependencies]))
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(GenericUpdate):
"""
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.update_cmd = dict.fromkeys(['all', 'specific'],
["zypper", "--non-interactive",
"update",
"--auto-agree-with-licenses"])
self.logfile = '/var/log/zypp/history'
def getPackagesList(self):
"""
Return a list of packages eligible to be updated by Zypper.
"""
self.wait_pkg_manager_available()
packages = []
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.startswith('v |'):
line = line.split(' | ')
pkg = {'package_name': line[2].strip(),
'version': line[4].strip(), 'arch': line[5].strip(),
'repository': line[1].strip()}
packages.append(pkg)
return packages
def getPackageInfo(self, pkg_name):
"""
Get package information. The return is a dictionary containg the
information about a package, in the format:
package = {'package_name': <string>,
'version': <string>,
'arch': <string>,
'repository': <string>
}
"""
self.wait_pkg_manager_available()
cmd = ["zypper", "info", pkg_name]
(stdout, stderr, returncode) = run_command(cmd)
if len(stderr) > 0:
raise OperationFailed('GGBPKGUPD0006E', {'err': stderr})
# Zypper returns returncode == 0 and stderr <= 0, even if package is
# not found in it's base. Need check the output of the command to parse
# correctly.
message = '\'%s\' not found' % pkg_name
if message in stdout:
raise NotFoundError('GGBPKGUPD0006E', {'err': message})
package = {}
stdout = stdout.split('\n')
for (key, token) in (('repository', 'Repository:'),
('version', 'Version:'),
('arch', 'Arch:'),
('package_name', 'Name:')):
for line in stdout:
if line.startswith(token):
package[key] = line.split(': ')[1].strip()
break
return package
def getPackageDeps(self, pkg_name):
self.wait_pkg_manager_available()
cmd = ["zypper", "--non-interactive", "update", "--dry-run", pkg_name]
(stdout, stderr, returncode) = run_command(cmd)
if len(stderr) > 0:
raise OperationFailed('GGBPKGUPD0006E', {'err': stderr})
# Zypper returns returncode == 0 and stderr <= 0, even if package is
# not found in it's base. Need check the output of the command to parse
# correctly.
message = '\'%s\' not found' % pkg_name
if message in stdout:
raise NotFoundError('GGBPKGUPD0006E', {'err': message})
# get the list of dependencies
out = stdout.split('\n')
for line in out:
if line.startswith("The following"):
deps_index = out.index(line) + 1
break
deps = out[deps_index].split()
deps.remove(pkg_name)
return deps
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
class PortageUpdate(GenericUpdate):
"""
Class to represent and operate with Portage software update system.
It's loaded only on those systems listed at PORTAGE_DISTROS and loads
necessary modules in runtime.
"""
def __init__(self):
# on purpose empty, not smart to do that over a webui in gentoo
self.update_cmd = dict()
# specific updates would require the usage of '=package-$version'
# not implemented in gingerbase therefore omitted
# self.update_cmd = dict.fromkeys(['all', ],
# ["emerge", "-u", "@world"])
self.logfile = self._get_output_log()
def _get_output_log(self):
"""
Return the logfile path
"""
# TODO: find potential custom location in make.conf?
return "/var/log/emerge.log"
def getPackagesList(self):
"""
Return a list of packages eligible to be updated.
"""
self.wait_pkg_manager_available()
try:
return portageparser.packages_list_update()
except Exception, e:
raise OperationFailed('GGBPKGUPD0003E', {'err': str(e)})
def getPackageInfo(self, pkg_name):
"""
Get package information. The return is a dictionary containg the
information about a package, in the format:
package = {'package_name': <string>,
'version': <string>,
'arch': <string>,
'repository': <string>
}
"""
self.wait_pkg_manager_available()
try:
return portageparser.package_info(pkg_name)
except Exception, e:
raise NotFoundError('GGBPKGUPD0003E', {'err': str(e)})
def getPackageDeps(self, pkg_name):
try:
return portageparser.package_deps(pkg_name)
except Exception, e:
raise NotFoundError('GGBPKGUPD0003E', {'err': str(e)})
def isRunning(self):
"""
Return True whether the package manager is already running or
False otherwise.
"""
pid = None
try:
for dnf_proc in process_iter():
if 'emerge' 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)