Debian Codegraph @

tools/1node2heartbeat

raw
1 #!/usr/bin/python
2 #
3 # Program to determine current list of enabled services for init state 3
4 # and create heartbeat CRM configuration for heartbeat to manage them
5 #
6 __copyright__='''
7 Author: Alan Robertson <[email protected]>
8 Copyright (C) 2006 International Business Machines
9 '''
10
11 # This program is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License
13 # as published by the Free Software Foundation; either version 2
14 # of the License, or (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 import os,re
25 #
26 # Here's the plan:
27 # Find out the default run level
28 # Find out what (additional?) services are enabled in that run level
29 # Figure out which of them start after the network (or heartbeat?)
30 # Ignore heartbeat :-)
31 # Figure out which services supply the $services
32 # Look to see if the SUSE /etc/insserv.conf file exists
33 # If so, then scan it for who provides the $services
34 # defined by the LSB
35 # If we're on Red Hat, then make some Red Hat type assumptions
36 # (whatever those might be)
37 # If we're not, then make some generic assumptions...
38 # Scan the init scripts for their dependencies...
39 # Eliminate anything at or before 'network'.
40 # Create resources corresponding to all active services
41 # Include monitor actions for those services
42 # that can be started after 'network'
43 # Add the start-after dependencies
44 #
45 # Things to consider doing in the future:
46 # Constrain them to only run on the local system?
47 # Put them all in a convenience group (no colocation, no ordering)
48 # Add start and stop timeouts
49
50 ServiceKeywords = {}
51 ServiceMap = {}
52 ProvidesMap = {}
53 RequiresMap = {}
54 SkipMap = {'heartbeat': None, 'random': None}
55 NoMonitor = {'microcode': None}
56 PreReqs = ['network']
57 IgnoreList = []
58 sysname = os.uname()[1]
59 InitDir = "/etc/init.d"
60
61 def service_is_hb_compatible(service):
62 scriptname = os.path.join(InitDir, service)
63 command=scriptname + " status >/dev/null 2>&1";
64 rc = os.system(command)
65 return rc == 0
66
67 def find_ordered_services(dir):
68 allscripts = os.listdir(dir)
69 allscripts.sort()
70 services = []
71 for entry in allscripts:
72 matchobj = re.match("S[0-9]+(.*)", entry)
73 if not matchobj:
74 continue
75 service = matchobj.group(1)
76 if SkipMap.has_key(service):
77 continue
78 if service_is_hb_compatible(service):
79 services.append(service)
80 else:
81 IgnoreList.append(service)
82 return services
83
84
85 def register_services(initdir, services):
86 for service in services:
87 if not ServiceMap.has_key(service):
88 ServiceMap[service] = os.path.join(initdir, service)
89 for service in services:
90 script_dependency_scan(service, os.path.join(initdir, service), ServiceMap)
91
92 #
93 # From the LSB version 3.1: "Comment Conventions for Init Scripts"
94 #
95 ### BEGIN INIT INFO
96 ### END INIT INFO
97 #
98 # The delimiter lines may contain trailing whitespace, which shall be ignored.
99 # All lines inside the block shall begin with a hash character '#' in the
100 # first column, so the shell interprets them as comment lines which do not
101 # affect operation of the script. The lines shall be of the form:
102 # {keyword}: arg1 [arg2...]
103 # with exactly one space character between the '#' and the keyword, with a
104 # single exception. In lines following a line containing the Description
105 # keyword, and until the next keyword or block ending delimiter is seen,
106 # a line where the '#' is followed by more than one space or a tab
107 # character shall be treated as a continuation of the previous line.
108 #
109
110 # Make this a class to avoid recompiling it for each script we scan.
111 class pats:
112 begin=re.compile("###\s+BEGIN\s+INIT\s+INFO")
113 end=re.compile("###\s+END\s+INIT\s+INFO")
114 desc=re.compile("# Description:\s*(.*)", re.IGNORECASE)
115 desc_continue=re.compile("#( +|\t)\s*(.*)")
116 keyword=re.compile("# ([^\s:]+):\s*(.*)\s*\Z")
117
118 def script_keyword_scan(filename, servicename):
119 keywords = {}
120 ST_START=0
121 ST_INITINFO=1
122 ST_DESCRIPTION=1
123 description=""
124 state=ST_START
125
126 try:
127 fd = open(filename)
128 except IOError:
129 return keywords
130
131 while 1:
132 line = fd.readline()
133 if not line:
134 break
135
136 if state == ST_START:
137 if pats.begin.match(line):
138 state = ST_INITINFO
139 continue
140 if pats.end.match(line):
141 break
142
143 if state == ST_DESCRIPTION:
144 match = pats.desc_continue.match(line)
145 if match:
146 description += ("\n" + match.group(2))
147 continue
148 state = ST_INITINFO
149
150 match = pats.desc.match(line)
151 if match:
152 state = ST_DESCRIPTION
153 description = match.group(1)
154 continue
155
156 match = pats.keyword.match(line)
157 if match:
158 keywords[match.group(1)] = match.group(2)
159
160 # Clean up and return
161 fd.close()
162 if description != "":
163 keywords["Description"] = description
164 keywords["_PATHNAME_"] = filename
165 keywords["_RESOURCENAME_"] = "R_" + sysname + "_" + servicename
166 return keywords
167
168 def script_dependency_scan(service, script, servicemap):
169 keywords=script_keyword_scan(script, service)
170 ServiceKeywords[service] = keywords
171
172 SysServiceGuesses = {
173 '$local_fs': ['boot.localfs'],
174 '$network': ['network'],
175 '$named': ['named'],
176 '$portmap': ['portmap'],
177 '$remote_fs': ['nfs'],
178 '$syslog': ['syslog'],
179 '$netdaemons': ['portmap', 'inetd'],
180 '$time': ['ntp'],
181 }
182
183 #
184 # For specific versions of Linux, there are often better ways
185 # to do this...
186 #
187 # (e.g., for SUSE Linux, one should look at /etc/insserv.conf file)
188 #
189 def map_sys_services(servicemap):
190 sysservicemap = {}
191 for sysserv in SysServiceGuesses.keys():
192 servlist = SysServiceGuesses[sysserv]
193 result = []
194 for service in servlist:
195 if servicemap.has_key(service):
196 result.append(service)
197
198 sysservicemap[sysserv] = result
199 return sysservicemap
200
201 #
202 #
203 #
204 def create_service_dependencies(servicekeywords, systemservicemap):
205 dependencies = {}
206 for service in servicekeywords.keys():
207 if not dependencies.has_key(service):
208 dependencies[service] = {}
209 for key in ('Required-Start', 'Should-Start'):
210 if not servicekeywords[service].has_key(key):
211 continue
212 for depserv in servicekeywords[service][key].split():
213 if systemservicemap.has_key(depserv):
214 sysserv = systemservicemap[depserv]
215 for serv in sysserv:
216 dependencies[service][serv] = None
217 else:
218 if servicekeywords.has_key(depserv):
219 dependencies[service][depserv] = None
220 if len(dependencies[service]) == 0:
221 del dependencies[service]
222 return dependencies
223
224 #
225 # Modify the service name map to include all the mappings from
226 # 'Provides' services to real service script names...
227 #
228 def map_script_services(sysservmap, servicekeywords):
229 for service in servicekeywords.keys():
230 if not servicekeywords[service].has_key('Provides'):
231 continue
232 for provided in servicekeywords[service]['Provides'].split():
233 if not sysservmap.has_key(provided):
234 sysservmap[provided] = []
235 sysservmap[provided].append(service)
236 return sysservmap
237
238 def create_cib_update(keywords, depmap):
239 services = keywords.keys()
240 services.sort()
241 result = ""
242 # Create the XML for the resources
243 result += '<cib>\n'
244 result += '<configuration>\n'
245 result += '<crm_config/>\n'
246 result += '<nodes/>\n'
247 result += '<resources>\n'
248 groupname="G_" + sysname + "_localinit"
249 result += ' <group id="'+groupname+'" ordered="0" collocated="0">\n'
250 for service in services:
251 rid = keywords[service]["_RESOURCENAME_"]
252 monid = "OPmon_" + sysname + '_' + service
253 result += \
254 ' <primitive id="' + rid + '" class="lsb" type="'+ service + \
255 '">\n' + \
256 ' <instance_attributes/>\n' + \
257 ' <operations>\n'
258 if not NoMonitor.has_key(service):
259 result += \
260 ' <op id="' + monid + '" name="monitor" interval="30s" timeout="30s"/>\n'
261 result += \
262 ' </operations>\n' \
263 ' </primitive>\n'
264 result += ' </group>\n'
265 result += '</resources>\n'
266 services = depmap.keys()
267 services.sort()
268 result += '<constraints>\n'
269 for service in services:
270 rid = keywords[service]["_RESOURCENAME_"]
271 deps = depmap[service].keys()
272 deps.sort()
273 for dep in deps:
274 if not keywords.has_key(dep):
275 continue
276 depid = keywords[dep]["_RESOURCENAME_"]
277 orderid='O_' + sysname + '_' + service + '_' + dep
278 result += ' <rsc_order id="' + orderid + '" from="' + rid + \
279 '" to="' + depid + '" type="after"/>\n'
280 loc_id="Loc_" + sysname + "_localinit"
281 rule_id="LocRule_" + sysname + "_localinit"
282 expr_id="LocExp_" + sysname + "_localinit"
283
284 result += ' <rsc_location id="' + loc_id + '" rsc="' + groupname + '">\n'
285 result += ' <rule id="' + rule_id + '" score="-INFINITY">\n'
286 result += ' <expression attribute="#uname" id="' + expr_id + \
287 '" operation="ne" value="' + sysname + '"/>\n'
288 result += ' </rule>\n'
289 result += ' </rsc_location>\n'
290 result += '</constraints>\n'
291 result += '</configuration>\n'
292 result += '<status/>\n'
293 result += '</cib>\n'
294 return result
295
296
297
298 def remove_a_prereq(service, servicemap, keywords, deps):
299 if deps.has_key(service):
300 parents = deps[service].keys()
301 del deps[service]
302 else:
303 parents = []
304 if servicemap.has_key(service):
305 del servicemap[service]
306 if keywords.has_key(service):
307 del keywords[service]
308 for parent in parents:
309 if not deps.has_key(parent):
310 continue
311 remove_a_prereq(parent, servicemap, keywords, deps)
312
313
314 def remove_important_prereqs(prereqs, servicemap, keywords, deps):
315 # Find everything these important prereqs need and get rid of them...
316 for service in prereqs:
317 remove_a_prereq(service, servicemap, keywords, deps)
318
319 ServiceList = find_ordered_services(os.path.join(InitDir, "rc3.d"))
320 register_services(InitDir, ServiceList)
321 SysServiceMap = map_sys_services(ServiceMap)
322 map_script_services(SysServiceMap, ServiceKeywords)
323 ServiceDependencies = create_service_dependencies(ServiceKeywords,SysServiceMap)
324 remove_important_prereqs(PreReqs, SysServiceMap, ServiceKeywords, ServiceDependencies)
325
326 print create_cib_update(ServiceKeywords, ServiceDependencies)