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
|
# vim: set fileencoding=utf-8 :
#
# (C) 2010,2014 Guido Günther <agx@sigxcpu.org>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import logging
import os
import subprocess
try:
import lsb_release
except ImportError:
lsb_release = None
class Distro(object):
"""
A distribution
@cvar id: distro id as returned by lsb-release
@cvar service_blacklist: services that should never be restarted
@cvar _pkg_services: A C{dict} that maps packages to services. In
case we find binaries that match a key in this hash restart
the services listed in values.
@cvar _pkg_service_blacklist: if we find binaries in the package
listed as key don't restart services listed in values
"""
id = None
service_blacklist = set()
_pkg_services = {}
_pkg_blacklist = {}
_pkg_service_blacklist = {}
@classmethod
def pkg(klass, name):
"""Return package object named name"""
raise NotImplementedError
@classmethod
def pkg_by_file(klass, path):
"""Return package object that contains path"""
raise NotImplementedError
@classmethod
def restart_service_cmd(klass, service):
"""Command to restart service"""
raise NotImplementedError
@classmethod
def restart_service(klass, service):
"""Restart a service"""
subprocess.call(klass.restart_service_cmd(service))
@classmethod
def is_service_installed(klass, service):
"""Check wether a service exists on the system"""
return True
@classmethod
def pkg_services(klass, pkg):
"""
List of services that package pkg needs restarted that aren't part
of pkg itself
"""
return [ s for s in klass._pkg_services.get(pkg.name, [])
if klass.is_service_installed(s) ]
@classmethod
def pkg_service_blacklist(klass, pkg):
"""
List of services in pkg that we don't want to be restarted even when
a binary from this package maps a shared lib that changed.
"""
return klass._pkg_service_blacklist.get(pkg.name, [])
@classmethod
def has_apt(klass):
"""Does the distribution use apt"""
return False
@staticmethod
def detect():
return detect()
import whatmaps.debiandistro
import whatmaps.redhatdistro
def detect():
"""
Detect the distribution we run on. Returns C{None} if the
distribution is unknown.
"""
id = None
if lsb_release:
id = lsb_release.get_distro_information()['ID']
else:
try:
lsb_cmd = subprocess.Popen(['lsb_release', '--id', '-s'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output = lsb_cmd.communicate()[0]
if not lsb_cmd.returncode:
id = output.strip()
except OSError:
# id is None in this case
pass
if id == whatmaps.debiandistro.DebianDistro.id:
return whatmaps.debiandistro.DebianDistro
elif id == whatmaps.redhatdistro.FedoraDistro.id:
return whatmaps.redhatdistro.FedoraDistro
else:
if os.path.exists('/usr/bin/dpkg'):
logging.warning("Unknown distro but dpkg found, assuming Debian")
return whatmaps.debiandistro.DebianDistro
elif os.path.exists('/bin/rpm'):
logging.warning("Unknown distro but rpm found, assuming Fedora")
return whatmaps.debiandistro.FedoraDistro
else:
return None
|