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
|
# This is the bcfg2 support for solaris
'''This provides bcfg2 support for Solaris'''
__revision__ = '$Revision$'
import os, lxml.etree
from glob import glob
from os import stat, unlink
from re import compile as regcompile
from tempfile import mktemp
from Bcfg2.Client.Toolset import Toolset
noask = '''
mail=
instance=overwrite
partial=nocheck
runlevel=nocheck
idepend=nocheck
rdepend=nocheck
space=ask
setuid=nocheck
conflict=nocheck
action=nocheck
basedir=default
'''
class ToolsetImpl(Toolset):
'''This class implelements support for SYSV/blastware/encap packages
and standard SMF services'''
pkgtool = {'sysv':("/usr/sbin/pkgadd %s -d %%s -n %%%%s", (("%s", ["name"]))),
'blast':("/opt/csw/bin/pkg-get install %s", ("%s", ["name"])),
'encap':("/local/sbin/epkg -l -q %s", ("%s", ["url"]))}
splitter = regcompile('.*/(?P<name>[\w-]+)\-(?P<version>[\w\.-]+)')
ptypes = {}
__name__ = 'Solaris'
def __init__(self, cfg, setup):
Toolset.__init__(self, cfg, setup)
self.extra_services = []
self.snames = {}
self.noaskname = mktemp()
try:
open(self.noaskname, 'w+').write(noask)
self.pkgtool['sysv'] = (self.pkgtool['sysv'][0] % ("-a %s" % (self.noaskname)), self.pkgtool['sysv'][1])
except:
self.pkgtool['sysv'] = (self.pkgtool['sysv'][0] % (""), self.pkgtool['sysv'][1])
try:
stat("/opt/csw/bin/pkg-get")
self.saferun("/opt/csw/bin/pkg-get -U > /dev/null")
except OSError:
pass
self.Refresh()
for pkg in [cpkg for cpkg in self.cfg.findall(".//Package") if not cpkg.attrib.has_key('type')]:
pkg.set('type', 'sysv')
for pkg in [cpkg for cpkg in self.cfg.findall(".//Package") if cpkg.get('type') == 'sysv']:
if pkg.attrib.has_key('url'):
pkg.set('url', '/'.join(pkg.get('url').split('/')[:-1]))
pkg.set('type', "sysv-%s" % (pkg.get('url')))
if not self.pkgtool.has_key(pkg.get('type')):
self.pkgtool[pkg.get('type')] = (self.pkgtool['sysv'][0] % (pkg.get('url')),
self.pkgtool['sysv'][1])
def Refresh(self):
'''Refresh memory hashes of packages'''
self.installed = {}
self.ptypes = {}
# Build list of packages
lines = self.saferun("/usr/bin/pkginfo -x")[1]
while lines:
version = lines.pop().split()[1]
pkg = lines.pop().split()[0]
self.installed[pkg] = version
self.ptypes[pkg] = 'sysv'
# try to find encap packages
for pkg in glob("/local/encap/*"):
match = self.splitter.match(pkg)
if match:
self.installed[match.group('name')] = match.group('version')
self.ptypes[match.group('name')] = 'encap'
else:
print "Failed to split name %s" % pkg
#print self.installed.keys()
def VerifyService(self, entry):
'''Verify Service status for entry'''
if not entry.attrib.has_key('FMRI'):
name = self.saferun("/usr/bin/svcs -H -o FMRI %s 2>/dev/null" % entry.get('name'))[1]
if name:
entry.set('FMRI', name[0])
else:
self.logger.info('Failed to locate FMRI for service %s' % entry.get('name'))
return False
if entry.get('FMRI').startswith('lrc'):
self.logger.debug("Starting lrc validation for %s" % (entry.get('name')))
filename = entry.get('FMRI').split('/')[-1]
# this is a legacy service
gname = "/etc/rc*.d/%s" % filename
files = glob(gname.replace('_', '.'))
if files:
self.logger.debug("Matched %s with %s" % \
(entry.get("FMRI"), ":".join(files)))
return entry.get('status') == 'on'
else:
self.logger.debug("No service matching %s" % (entry.get("FMRI")))
return entry.get('status') == 'off'
self.logger.debug("starting non-lrc validatiaon for %s" % (entry.get('name')))
try:
srvdata = self.saferun("/usr/bin/svcs -H -o STA %s" % entry.attrib['name'])[1][0].split()
except IndexError:
# Ocurrs when no lines are returned (service not installed)
return False
if entry.get('status') == 'on':
return srvdata[0] == 'ON'
else:
return srvdata[0] in ['OFF', 'UN', 'MNT', 'DIS', 'DGD']
def InstallService(self, entry):
'''Install Service entry'''
print lxml.etree.tostring(entry)
if not entry.attrib.has_key('status'):
self.logger.info('Insufficient information for Service %s; cannot Install' % entry.get('name'))
return False
if not entry.attrib.has_key('FMRI'):
name = self.saferun("/usr/bin/svcs -H -o FMRI %s 2>/dev/null" % entry.get('name'))[1]
if name:
entry.set('FMRI', name[0])
else:
self.logger.info('Failed to locate FMRI for service %s' % entry.get('name'))
return False
self.logger.info("Installing Service %s" % (entry.get('name')))
if entry.attrib['status'] == 'off':
if self.setup['dryrun']:
print "Disabling Service %s" % (entry.get('name'))
else:
if entry.get("FMRI").startswith('lrc'):
try:
loc = entry.get("FMRI")[4:].replace('_', '.')
self.logger.debug("Renaming file %s to %s" % \
(loc, loc.replace('/S', '/DISABLED.S')))
os.rename(loc, loc.replace('/S', '/DISABLED.S'))
return True
except OSError:
self.logger.error("Failed to rename init script %s" % (loc))
return False
cmdrc = self.saferun("/usr/sbin/svcadm disable -r %s" % (entry.attrib['FMRI']))[0]
else:
if self.setup['dryrun']:
print "Enabling Service %s" % (entry.attrib['name'])
else:
if entry.get('FMRI').startswith('lrc'):
loc = entry.get("FMRI")[4:].replace('_', ',')
try:
stat(loc.replace('/S', '/Disabled.'))
self.logger.debug("Renaming file %s to %s" % \
(loc.replace('/S', '/DISABLED.S'), loc))
os.rename(loc.replace('/S', '/DISABLED.S'), loc)
cmdrc = 0
except:
self.logger.debug("Failed to rename %s to %s" \
% (loc.replace('/S', '/DISABLED.S'), loc))
cmdrc = 1
else:
cmdrc = self.saferun("/usr/sbin/svcadm enable -r %s" % (entry.attrib['FMRI']))[0]
return cmdrc == 0
def VerifyPackage(self, entry, modlist):
'''Verify Package status for entry'''
if not entry.get('version'):
self.logger.info("Insufficient information of Package %s; cannot Verify" % entry.get('name'))
return False
if entry.get('type') in ['sysv', 'blast'] or entry.get('type')[:4] == 'sysv':
cmdrc = self.saferun("/usr/bin/pkginfo -q -v \"%s\" %s" % (entry.get('version'), entry.get('name')))[0]
elif entry.get('type') in ['encap']:
cmdrc = self.saferun("/local/sbin/epkg -q -k %s-%s >/dev/null" %
(entry.get('name'), entry.get('version')))[0]
if cmdrc != 0:
self.logger.debug("Package %s version incorrect" % entry.get('name'))
else:
if self.setup['quick'] or entry.get('type') == 'encap' or \
entry.attrib.get('verify', 'true') == 'false':
return True
(vstat, odata) = self.saferun("/usr/sbin/pkgchk -n %s" % (entry.get('name')))
if vstat == 0:
return True
else:
output = [line for line in odata if line[:5] == 'ERROR']
if len([name for name in output if name.split()[-1] not in modlist]):
self.logger.debug("Package %s content verification failed" % \
(entry.get('name')))
else:
return True
return False
def Inventory(self):
'''Do standard inventory plus debian extra service check'''
Toolset.Inventory(self)
allsrv = [name for name, version in [ srvc.strip().split() for srvc in
self.saferun("/usr/bin/svcs -a -H -o FMRI,STATE")[1] ]
if version != 'disabled']
csrv = self.cfg.findall(".//Service")
# need to build a service name map. services map to a fullname if they are already installed
for srv in csrv:
name = self.saferun("/usr/bin/svcs -H -o FMRI %s 2>/dev/null" % srv.get('name'))[1]
if name:
srv.set('FMRI', name[0])
else:
self.logger.info("Failed to locate FMRI for service %s" % srv.get('name'))
#nsrv = [ r for r in [ popen("/usr/bin/svcs -H -o FMRI %s " % s).read().strip() for s in csrv ] if r ]
[allsrv.remove(svc.get('FMRI')) for svc in csrv if
svc.get('status') == 'on' and svc.get("FMRI") in allsrv]
self.extra_services = allsrv
def HandleExtra(self):
'''Deal with extra configuration detected'''
if len(self.pkgwork) > 0:
if self.setup['remove'] in ['all', 'packages']:
self.logger.info("Removing packages: %s" % (self.pkgwork['remove']))
sysvrmpkgs = [pkg for pkg in self.pkgwork['remove'] if self.ptypes[pkg] == 'sysv']
enrmpkgs = [pkg for pkg in self.pkgwork['remove'] if self.ptypes[pkg] == 'encap']
if sysvrmpkgs:
if not self.saferun("/usr/sbin/pkgrm -a %s -n %s" % \
(self.noaskname, " ".join(sysvrmpkgs)))[0]:
[self.pkgwork['remove'].remove(pkg) for pkg in sysvrmpkgs]
if enrmpkgs:
if not self.saferun("/local/sbin/epkg -l -q -r %s" % " ".join(enrmpkgs))[0]:
[self.pkgwork['remove'].remove(pkg) for pkg in enrmpkgs]
else:
self.logger.info("Need to remove packages: %s" % (self.pkgwork['remove']))
if len(self.extra_services) > 0:
if self.setup['remove'] in ['all', 'services']:
self.logger.info("Removing services: %s" % (self.extra_services))
for service in self.extra_services:
if not self.saferun("/usr/sbin/svcadm disable %s" % service)[0]:
self.extra_services.remove(service)
else:
self.logger.info("Need to remove services: %s" % (self.extra_services))
def Install(self):
'''Local install method handling noaskfiles'''
Toolset.Install(self)
try:
unlink(self.noaskname)
except:
pass
def RestartService(self, service):
'''Restart a service'''
if service.get("FMRI").startswith('lrc'):
Toolset.RestartService(self, service)
else:
if service.get('status') == 'on':
self.logger.debug("Restarting service %s" % (service.get("FMRI")))
self.saferun("svcadm restart %s" % (service.get("FMRI")))
|