Page Menu
Home
ClusterLabs Projects
Search
Configure Global Search
Log In
Files
F4832449
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
78 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/shell/modules/help.py.in b/shell/modules/help.py.in
index e4d16884aa..25f15cf9f3 100644
--- a/shell/modules/help.py.in
+++ b/shell/modules/help.py.in
@@ -1,276 +1,277 @@
# Copyright (C) 2008 Dejan Muhamedagic <dmuhamedagic@suse.de>
#
# 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 2.1 of the License, or (at your option) any later version.
#
# This software 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 library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
import os
import re
from cache import WCache
-from utils import odict, page_string, gethomedir
+from utils import odict, page_string
+from vars import gethomedir
from msg import *
#
# help or make users feel less lonely
#
def add_shorthelp(topic,shorthelp,topic_help):
'''
Join topics ("%s,%s") if they share the same short
description.
'''
for i in range(len(topic_help)):
if topic_help[i][1] == shorthelp:
topic_help[i][0] = "%s,%s" % (topic_help[i][0], topic)
return
topic_help.append([topic, shorthelp])
def dump_short_help(help_tab):
topic_help = []
for topic in help_tab:
if topic == '.':
continue
# with odict, for whatever reason, python parses differently:
# help_tab["..."] = ("...","...") and
# help_tab["..."] = ("...","""
# ...""")
# a parser bug?
if type(help_tab[topic][0]) == type(()):
shorthelp = help_tab[topic][0][0]
else:
shorthelp = help_tab[topic][0]
add_shorthelp(topic,shorthelp,topic_help)
for t,d in topic_help:
print "\t%-16s %s" % (t,d)
def overview(help_tab):
print ""
print help_tab['.'][1]
print ""
print "Available commands:"
print ""
dump_short_help(help_tab)
print ""
def topic_help(help_tab,topic):
if topic not in help_tab:
print "There is no help for topic %s" % topic
return
if type(help_tab[topic][0]) == type(()):
shorthelp = help_tab[topic][0][0]
longhelp = help_tab[topic][0][1]
else:
shorthelp = help_tab[topic][0]
longhelp = help_tab[topic][1]
if longhelp:
page_string(longhelp)
else:
print shorthelp
def cmd_help(help_tab,topic = ''):
"help!"
# help_tab is an odict (ordered dictionary):
# help_tab[topic] = (short_help,long_help)
# topic '.' is a special entry for the top level
if not help_tab:
common_info("sorry, help not available")
return
if not topic:
overview(help_tab)
else:
topic_help(help_tab,topic)
def is_level(s):
return len(s.split("_")) == 2
def help_short(s):
r = re.search("help_[^,]+,(.*)\]\]", s)
return r and r.group(1) or ''
class HelpSystem(object):
'''
The help system. All help is in the following form in the
manual:
[[cmdhelp_<level>_<cmd>,<short help text>]]
=== ...
Long help text.
...
[[cmdhelp_<level>_<cmd>,<short help text>]]
Help for the level itself is like this:
[[cmdhelp_<level>,<short help text>]]
'''
help_text_file = "@datadir@/@PACKAGE@/crm_cli.txt"
index_file = os.path.join(gethomedir(),".crm_help_index")
def __init__(self):
self.key_pos = {}
self.leveld = {}
self.no_help_file = False # don't print repeatedly messages
self.bad_index = False # don't print repeatedly warnings for bad index
def open_file(self,name,mode):
try:
f = open(name,mode)
return f
except IOError,msg:
common_err("%s open: %s"%(name,msg))
common_err("extensive help system is not available")
self.no_help_file = True
return None
def drop_index(self):
common_info("removing index")
os.unlink(self.index_file)
self.key_pos = {}
self.leveld = {}
self.bad_index = True
def mk_index(self):
'''
Prepare an index file, sorted by topic, with seek positions
Do we need a hash on content?
'''
if self.no_help_file:
return False
crm_help_v = os.getenv("CRM_HELP_FILE")
if crm_help_v:
self.help_text_file = crm_help_v
help_f = self.open_file(self.help_text_file,"r")
if not help_f:
return False
idx_f = self.open_file(self.index_file,"w")
if not idx_f:
return False
common_debug("building help index")
key_pos = odict()
while 1:
pos = help_f.tell()
s = help_f.readline()
if not s:
break
if s.startswith("[["):
r = re.search(r'..([^,]+),', s)
if r:
key_pos[r.group(1)] = pos
help_f.close()
for key in key_pos:
print >>idx_f, '%s %d' % (key,key_pos[key])
idx_f.close()
return True
def is_index_old(self):
try:
t_idx = os.path.getmtime(self.index_file)
except:
return True
try:
t_help = os.path.getmtime(self.help_text_file)
except:
return True
return t_help > t_idx
def load_index(self):
if self.is_index_old():
self.mk_index()
self.key_pos = {}
self.leveld = {}
idx_f = self.open_file(self.index_file,"r")
if not idx_f:
return False
cur_lvl = ''
for s in idx_f:
a = s.split()
if len(a) != 2:
if not self.bad_index:
common_err("index file corrupt")
idx_f.close()
self.drop_index()
return self.load_index() # this runs only once
return False
key = a[0]
fpos = long(a[1])
if key.startswith("cmdhelp_"):
if is_level(key):
if key != cur_lvl:
cur_lvl = key
self.leveld[cur_lvl] = []
else:
self.leveld[cur_lvl].append(key)
self.key_pos[key] = fpos
idx_f.close()
return True
def __filter(self,s):
if '<<' in s:
return re.sub(r'<<[^,]+,(.+)>>', r'\1', s)
else:
return s
def __load_help_one(self,key,skip = 2):
longhelp = ''
self.help_f.seek(self.key_pos[key])
shorthelp = help_short(self.help_f.readline())
for i in range(skip-1):
self.help_f.readline()
l = []
for s in self.help_f:
if s.startswith("[[") or s.startswith("="):
break
l.append(self.__filter(s))
if l and l[-1] == '\n': # drop the last line of empty
l.pop()
if l:
longhelp = ''.join(l)
if not shorthelp or not longhelp:
if not self.bad_index:
common_warn("help topic %s not found" % key)
self.drop_index()
return shorthelp,longhelp
def cmdhelp(self,s):
if not self.key_pos and not self.load_index():
return None,None
if not s in self.key_pos:
if not self.bad_index:
common_warn("help topic %s not found" % s)
self.drop_index()
return None,None
return self.__load_help_one(s)
def __load_level(self,lvl):
'''
For the given level, create a help table.
'''
if wcache.is_cached("lvl_help_tab_%s" % lvl):
return wcache.retrieve("lvl_help_tab_%s" % lvl)
if not self.key_pos and not self.load_index():
return None
self.help_f = self.open_file(self.help_text_file,"r")
if not self.help_f:
return None
lvl_s = "cmdhelp_%s" % lvl
if not lvl_s in self.leveld:
if not self.bad_index:
common_warn("help table for level %s not found" % lvl)
self.drop_index()
return None
common_debug("loading help table for level %s" % lvl)
help_tab = odict()
help_tab["."] = self.__load_help_one(lvl_s)
try:
for key in self.leveld[lvl_s]:
cmd = key[len(lvl_s)+1:]
help_tab[cmd] = self.__load_help_one(key)
except: pass
self.help_f.close()
help_tab["quit"] = ("exit the program", "")
help_tab["help"] = ("show help", "")
help_tab["end"] = ("go back one level", "")
return help_tab
def load_level(self,lvl):
help_tab = self.__load_level(lvl)
if self.bad_index: # try again
help_tab = self.__load_level(lvl)
return wcache.store("lvl_help_tab_%s" % lvl, help_tab)
wcache = WCache.getInstance()
# vim:ts=4:sw=4:et:
diff --git a/shell/modules/ra.py.in b/shell/modules/ra.py.in
index 022f76755a..b4771bd61a 100644
--- a/shell/modules/ra.py.in
+++ b/shell/modules/ra.py.in
@@ -1,665 +1,665 @@
# Copyright (C) 2008 Dejan Muhamedagic <dmuhamedagic@suse.de>
#
# 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 2.1 of the License, or (at your option) any later version.
#
# This software 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 library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
import os
import sys
import subprocess
import copy
import xml.dom.minidom
import re
import glob
from userprefs import Options, UserPrefs
from cache import WCache
-from vars import Vars
+from vars import Vars, getuser
from utils import *
from msg import *
#
# Resource Agents interface (meta-data, parameters, etc)
#
ocf_root = os.getenv("OCF_ROOT")
if not ocf_root:
ocf_root = "@OCF_ROOT_DIR@"
if not ocf_root:
ocf_root = "/usr/lib/ocf"
os.putenv("OCF_ROOT",ocf_root)
class RaLrmd(object):
'''
Getting information from the resource agents.
'''
lrmadmin_prog = "lrmadmin"
def __init__(self):
self.good = self.is_lrmd_accessible()
def lrmadmin(self, opts, xml = False):
'''
Get information directly from lrmd using lrmadmin.
'''
l = stdout2list("%s %s" % (self.lrmadmin_prog,opts))
if l and not xml:
l = l[1:] # skip the first line
return l
def is_lrmd_accessible(self):
if not (is_program(self.lrmadmin_prog) and is_process("lrmd")):
return False
return subprocess.call(\
add_sudo(">/dev/null 2>&1 %s -C" % self.lrmadmin_prog), \
shell=True) == 0
def meta(self, ra_class,ra_type,ra_provider):
return self.lrmadmin("-M %s %s %s"%(ra_class,ra_type,ra_provider),True)
def providers(self, ra_type,ra_class = "ocf"):
'List of providers for a class:type.'
return self.lrmadmin("-P %s %s" % (ra_class,ra_type),True)
def classes(self):
'List of classes.'
return self.lrmadmin("-C")
def types(self, ra_class = "ocf", ra_provider = ""):
'List of types for a class.'
return self.lrmadmin("-T %s" % ra_class)
class RaOS(object):
'''
Getting information from the resource agents (direct).
'''
def __init__(self):
self.good = True
def meta(self, ra_class,ra_type,ra_provider):
l = []
if ra_class == "ocf":
l = stdout2list("%s/resource.d/%s/%s meta-data" % \
(ocf_root,ra_provider,ra_type))
elif ra_class == "stonith":
l = stdout2list("stonith -m -t %s" % ra_type)
return l
def providers(self, ra_type,ra_class = "ocf"):
'List of providers for a class:type.'
l = []
if ra_class == "ocf":
for s in glob.glob("%s/resource.d/*/%s" % (ocf_root,ra_type)):
a = s.split("/")
if len(a) == 7:
l.append(a[5])
return l
def classes(self):
'List of classes.'
return "heartbeat lsb ocf stonith".split()
def types(self, ra_class = "ocf", ra_provider = ""):
'List of types for a class.'
l = []
prov = ra_provider and ra_provider or "*"
if ra_class == "ocf":
l = os_types_list("%s/resource.d/%s/*" % (ocf_root,prov))
elif ra_class == "lsb":
l = os_types_list("/etc/init.d/*")
elif ra_class == "stonith":
l = stdout2list("stonith -L")
l = list(set(l))
l.sort()
return l
def ra_if():
if vars.ra_if:
return vars.ra_if
if getuser() in ("root",vars.crm_daemon_user):
vars.ra_if = RaLrmd()
if not vars.ra_if or not vars.ra_if.good:
vars.ra_if = RaOS()
return vars.ra_if
def ra_classes():
'''
List of RA classes.
'''
if wcache.is_cached("ra_classes"):
return wcache.retrieve("ra_classes")
l = ra_if().classes()
l.sort()
return wcache.store("ra_classes",l)
def ra_providers(ra_type,ra_class = "ocf"):
'List of providers for a class:type.'
id = "ra_providers-%s-%s" % (ra_class,ra_type)
if wcache.is_cached(id):
return wcache.retrieve(id)
l = ra_if().providers(ra_type,ra_class)
l.sort()
return wcache.store(id,l)
def ra_providers_all(ra_class = "ocf"):
'''
List of providers for a class.
'''
id = "ra_providers_all-%s" % ra_class
if wcache.is_cached(id):
return wcache.retrieve(id)
dir = ocf_root + "/resource.d"
l = []
for s in os.listdir(dir):
if os.path.isdir("%s/%s" % (dir,s)):
l.append(s)
l.sort()
return wcache.store(id,l)
def ra_types(ra_class = "ocf", ra_provider = ""):
'''
List of RA type for a class.
'''
if not ra_class:
ra_class = "ocf"
id = "ra_types-%s-%s" % (ra_class,ra_provider)
if wcache.is_cached(id):
return wcache.retrieve(id)
if ra_provider:
list = []
for ra in ra_if().types(ra_class):
if ra_provider in ra_providers(ra,ra_class):
list.append(ra)
else:
list = ra_if().types(ra_class)
list.sort()
return wcache.store(id,list)
def get_pe_meta():
if not vars.pe_metadata:
vars.pe_metadata = RAInfo("pengine","metadata")
return vars.pe_metadata
def get_crmd_meta():
if not vars.crmd_metadata:
vars.crmd_metadata = RAInfo("crmd","metadata")
vars.crmd_metadata.set_advanced_params(vars.crmd_advanced)
return vars.crmd_metadata
def get_stonithd_meta():
if not vars.stonithd_metadata:
vars.stonithd_metadata = RAInfo("stonithd","metadata")
return vars.stonithd_metadata
def get_cib_meta():
if not vars.cib_metadata:
vars.cib_metadata = RAInfo("cib","metadata")
return vars.cib_metadata
def get_properties_meta():
if not vars.crm_properties_metadata:
get_pe_meta()
get_crmd_meta()
get_cib_meta()
vars.crm_properties_metadata = copy.deepcopy(vars.crmd_metadata)
vars.crm_properties_metadata.add_ra_params(vars.pe_metadata)
vars.crm_properties_metadata.add_ra_params(vars.cib_metadata)
return vars.crm_properties_metadata
def get_properties_list():
try:
return get_properties_meta().params().keys()
except:
return []
def prog_meta(prog):
'''
Do external program metadata.
'''
l = []
if is_program(prog):
l = stdout2list("%s metadata" % prog)
return l
def get_nodes_text(n,tag):
try:
node = n.getElementsByTagName(tag)[0]
for c in node.childNodes:
if c.nodeType == c.TEXT_NODE:
return c.data.strip()
except: return ''
def mk_monitor_name(role,depth):
depth = depth != "0" and ("_%s" % depth) or ""
return role and role != "Started" and \
"monitor_%s%s" % (role,depth) or \
"monitor%s" % depth
def monitor_name_node(node):
depth = node.getAttribute("depth") or '0'
role = node.getAttribute("role")
return mk_monitor_name(role,depth)
def monitor_name_pl(pl):
depth = find_value(pl, "depth") or '0'
role = find_value(pl, "role")
return mk_monitor_name(role,depth)
def crm_msec(t):
'''
See lib/common/utils.c:crm_get_msec().
'''
convtab = {
'ms': (1,1),
'msec': (1,1),
'us': (1,1000),
'usec': (1,1000),
'': (1000,1),
's': (1000,1),
'sec': (1000,1),
'm': (60*1000,1),
'min': (60*1000,1),
'h': (60*60*1000,1),
'hr': (60*60*1000,1),
}
if not t:
return -1
r = re.match("\s*(\d+)\s*([a-zA-Z]+)?", t)
if not r:
return -1
if not r.group(2):
q = ''
else:
q = r.group(2).lower()
try:
mult,div = convtab[q]
except:
return -1
return (int(r.group(1))*mult)/div
def crm_time_cmp(a, b):
return crm_msec(a) - crm_msec(b)
class RAInfo(object):
'''
A resource agent and whatever's useful about it.
'''
ra_tab = " " # four horses
required_ops = ("start", "stop")
skip_ops = ("meta-data", "validate-all")
skip_op_attr = ("name", "depth", "role")
def __init__(self,ra_class,ra_type,ra_provider = "heartbeat"):
self.advanced_params = []
self.ra_class = ra_class
self.ra_type = ra_type
self.ra_provider = ra_provider
if not self.ra_provider:
self.ra_provider = "heartbeat"
self.ra_node = None
def ra_string(self):
return self.ra_class == "ocf" and \
"%s:%s:%s" % (self.ra_class, self.ra_provider, self.ra_type) or \
"%s:%s" % (self.ra_class, self.ra_type)
def error(self, s):
common_err("%s: %s" % (self.ra_string(), s))
def warn(self, s):
common_warn("%s: %s" % (self.ra_string(), s))
def set_advanced_params(self, l):
self.advanced_params = l
def filter_crmd_attributes(self):
for n in self.ra_node.getElementsByTagName("parameter"):
if not n.getAttribute("name") in vars.crmd_user_attributes:
n.parentNode.removeChild(n)
def add_ra_params(self,ra):
'''
Add parameters from another RAInfo instance.
'''
try:
if not self.mk_ra_node() or not ra.mk_ra_node():
return
except:
return
try:
params_node = self.doc.getElementsByTagName("parameters")[0]
except:
params_node = self.doc.createElement("parameters")
self.ra_node.appendChild(params_node)
for n in ra.ra_node.getElementsByTagName("parameter"):
params_node.appendChild(self.doc.importNode(n,1))
def mk_ra_node(self):
'''
Return the resource_agent node.
'''
if self.ra_node:
return self.ra_node
meta = self.meta()
try:
self.doc = xml.dom.minidom.parseString('\n'.join(meta))
except:
self.error("could not parse meta-data: %s" % '\n'.join(meta))
self.ra_node = None
return None
try:
self.ra_node = self.doc.getElementsByTagName("resource-agent")[0]
except:
self.error("meta-data contains no resource-agent element")
self.ra_node = None
return None
if self.ra_class == "stonith":
self.add_ra_params(get_stonithd_meta())
return self.ra_node
def param_type_default(self,n):
try:
content = n.getElementsByTagName("content")[0]
type = content.getAttribute("type")
default = content.getAttribute("default")
return type,default
except:
return None,None
def params(self):
'''
Construct a dict of dicts: parameters are keys and
dictionary of attributes/values are values. Cached too.
'''
id = "ra_params-%s" % self.ra_string()
if wcache.is_cached(id):
return wcache.retrieve(id)
if not self.mk_ra_node():
return None
d = {}
for pset in self.ra_node.getElementsByTagName("parameters"):
for c in pset.getElementsByTagName("parameter"):
name = c.getAttribute("name")
if not name:
continue
required = c.getAttribute("required")
unique = c.getAttribute("unique")
type,default = self.param_type_default(c)
d[name] = {
"required": required,
"unique": unique,
"type": type,
"default": default,
}
return wcache.store(id,d)
def completion_params(self):
'''
Extra method for completion, for we want to filter some
(advanced) parameters out. And we want this to be fast.
'''
if not self.mk_ra_node():
return None
return [c.getAttribute("name")
for c in self.ra_node.getElementsByTagName("parameter")
if c.getAttribute("name")
and c.getAttribute("name") not in self.advanced_params
]
def actions(self):
'''
Construct a dict of dicts: actions are keys and
dictionary of attributes/values are values. Cached too.
'''
id = "ra_actions-%s" % self.ra_string()
if wcache.is_cached(id):
return wcache.retrieve(id)
if not self.mk_ra_node():
return None
d = {}
for pset in self.ra_node.getElementsByTagName("actions"):
for c in pset.getElementsByTagName("action"):
name = c.getAttribute("name")
if not name or name in self.skip_ops:
continue
if name == "monitor":
name = monitor_name_node(c)
d[name] = {}
for a in c.attributes.keys():
if a in self.skip_op_attr:
continue
v = c.getAttribute(a)
if v:
d[name][a] = v
# add monitor ops without role, if they don't already
# exist
d2 = {}
for op in d.keys():
if re.match("monitor_[^0-9]", op):
norole_op = re.sub(r'monitor_[^0-9_]+_(.*)', r'monitor_\1', op)
if not norole_op in d:
d2[norole_op] = d[op]
d.update(d2)
return wcache.store(id,d)
def reqd_params_list(self):
'''
List of required parameters.
'''
d = self.params()
if not d: return []
return [x for x in d if d[x]["required"] == '1']
def param_default(self,pname):
'''
Parameter's default.
'''
d = self.params()
try: return d[pname]["default"]
except: return None
def sanity_check_params(self, id, pl):
'''
pl is a list of (attribute,value) pairs.
- are all required parameters defined
- do all parameters exist
'''
rc = 0
d = {}
for p,v in pl:
d[p] = v
for p in self.reqd_params_list():
if p not in d:
common_err("%s: required parameter %s not defined" % (id,p))
rc |= user_prefs.get_check_rc()
for p in d:
if p not in self.params():
common_err("%s: parameter %s does not exist" % (id,p))
rc |= user_prefs.get_check_rc()
return rc
def get_adv_timeout(self, op, node = None):
if node and op == "monitor":
name = monitor_name_node(node)
else:
name = op
try:
return self.actions()[name]["timeout"]
except:
return None
def sanity_check_ops(self, id, ops, default_timeout):
'''
ops is a dict, operation names are keys and values are
lists of (attribute,value) pairs.
- do all operations exist
- are timeouts sensible
'''
rc = 0
n_ops = {}
for op in ops:
n_op = op == "monitor" and monitor_name_pl(ops[op]) or op
n_ops[n_op] = {}
for p,v in ops[op]:
if p in self.skip_op_attr:
continue
n_ops[n_op][p] = v
for req_op in self.required_ops:
if req_op not in n_ops:
n_ops[req_op] = {}
for op in n_ops:
if op not in self.actions():
common_warn("%s: action %s not advertised in meta-data, it may not be supported by the RA" % (id,op))
rc |= 1
continue
if "interval" in n_ops[op]:
if n_ops[op]["interval"] != "0":
if op == "start" or op == "stop":
v = n_ops[op]["interval"]
common_warn("%s: Specified interval for %s is %s, this is greater than 0 thus invalid" %(id,op,v))
try:
adv_timeout = self.actions()[op]["timeout"]
except:
continue
if "timeout" in n_ops[op]:
v = n_ops[op]["timeout"]
timeout_string = "specified timeout"
else:
v = default_timeout
timeout_string = "default timeout"
if crm_msec(v) < 0:
continue
if crm_time_cmp(adv_timeout,v) > 0:
common_warn("%s: %s %s for %s is smaller than the advised %s" % \
(id,timeout_string,v,op,adv_timeout))
rc |= 1
return rc
def meta(self):
'''
RA meta-data as raw xml.
'''
id = "ra_meta-%s" % self.ra_string()
if wcache.is_cached(id):
return wcache.retrieve(id)
if self.ra_class in vars.meta_progs:
l = prog_meta(self.ra_class)
else:
l = ra_if().meta(self.ra_class,self.ra_type,self.ra_provider)
return wcache.store(id, l)
def meta_pretty(self):
'''
Print the RA meta-data in a human readable form.
'''
if not self.mk_ra_node():
return ''
l = []
title = self.meta_title()
l.append(title)
longdesc = get_nodes_text(self.ra_node,"longdesc")
if longdesc:
l.append(longdesc)
if self.ra_class != "heartbeat":
params = self.meta_parameters()
if params:
l.append(params.rstrip())
actions = self.meta_actions()
if actions:
l.append(actions)
return '\n\n'.join(l)
def get_shortdesc(self,n):
name = n.getAttribute("name")
shortdesc = get_nodes_text(n,"shortdesc")
longdesc = get_nodes_text(n,"longdesc")
if shortdesc and shortdesc not in (name,longdesc,self.ra_type):
return shortdesc
return ''
def meta_title(self):
s = self.ra_string()
shortdesc = self.get_shortdesc(self.ra_node)
if shortdesc:
s = "%s (%s)" % (shortdesc,s)
return s
def meta_param_head(self,n):
name = n.getAttribute("name")
if not name:
return None
s = name
if n.getAttribute("required") == "1":
s = s + "*"
type,default = self.param_type_default(n)
if type and default:
s = "%s (%s, [%s])" % (s,type,default)
elif type:
s = "%s (%s)" % (s,type)
shortdesc = self.get_shortdesc(n)
s = "%s: %s" % (s,shortdesc)
return s
def format_parameter(self,n):
l = []
head = self.meta_param_head(n)
if not head:
self.error("no name attribute for parameter")
return ""
l.append(head)
longdesc = get_nodes_text(n,"longdesc")
if longdesc:
longdesc = self.ra_tab + longdesc.replace("\n","\n"+self.ra_tab) + '\n'
l.append(longdesc)
return '\n'.join(l)
def meta_parameter(self,param):
if not self.mk_ra_node():
return ''
l = []
for pset in self.ra_node.getElementsByTagName("parameters"):
for c in pset.getElementsByTagName("parameter"):
if c.getAttribute("name") == param:
return self.format_parameter(c)
def meta_parameters(self):
if not self.mk_ra_node():
return ''
l = []
for pset in self.ra_node.getElementsByTagName("parameters"):
for c in pset.getElementsByTagName("parameter"):
s = self.format_parameter(c)
if s:
l.append(s)
if l:
return "Parameters (* denotes required, [] the default):\n\n" + '\n'.join(l)
def meta_action_head(self,n):
name = n.getAttribute("name")
if not name:
return ''
if name in self.skip_ops:
return ''
if name == "monitor":
name = monitor_name_node(n)
s = "%-13s" % name
for a in n.attributes.keys():
if a in self.skip_op_attr:
continue
v = n.getAttribute(a)
if v:
s = "%s %s=%s" % (s,a,v)
return s
def meta_actions(self):
l = []
for aset in self.ra_node.getElementsByTagName("actions"):
for c in aset.getElementsByTagName("action"):
s = self.meta_action_head(c)
if s:
l.append(self.ra_tab + s)
if l:
return "Operations' defaults (advisory minimum):\n\n" + '\n'.join(l)
#
# resource type definition
#
def ra_type_validate(s, ra_class, provider, rsc_type):
'''
Only ocf ra class supports providers.
'''
if not rsc_type:
common_err("bad resource type specification %s"%s)
return False
if ra_class == "ocf":
if not provider:
common_err("provider could not be determined for %s"%s)
return False
else:
if provider:
common_warn("ra class %s does not support providers"%ra_class)
return True
return True
def disambiguate_ra_type(s):
'''
Unravel [class:[provider:]]type
'''
l = s.split(':')
if not l or len(l) > 3:
return None
if len(l) == 3:
return l
elif len(l) == 2:
ra_class,ra_type = l
else:
ra_class = "ocf"
ra_type = l[0]
ra_provider = ''
if ra_class == "ocf":
pl = ra_providers(ra_type,ra_class)
if pl and len(pl) == 1:
ra_provider = pl[0]
elif not pl:
ra_provider = 'heartbeat'
return ra_class,ra_provider,ra_type
wcache = WCache.getInstance()
vars = Vars.getInstance()
# vim:ts=4:sw=4:et:
diff --git a/shell/modules/utils.py b/shell/modules/utils.py
index 240c37c000..a2db8e00da 100644
--- a/shell/modules/utils.py
+++ b/shell/modules/utils.py
@@ -1,429 +1,406 @@
# Copyright (C) 2008 Dejan Muhamedagic <dmuhamedagic@suse.de>
#
# 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 2 of the License, or (at your option) any later version.
#
# This software 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 library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
import os
import pwd
from tempfile import mkstemp
import subprocess
import re
import glob
import time
from userprefs import Options, UserPrefs
+from vars import Vars
from msg import *
def is_program(prog):
return subprocess.call("which %s >/dev/null 2>&1"%prog, shell=True) == 0
def ask(msg):
# if there's no terminal, no use asking and default to "no"
if not sys.stdin.isatty():
return False
print_msg = True
while True:
try:
ans = raw_input(msg + ' ')
except EOFError:
ans = 'n'
if not ans or ans[0].lower() not in ('n','y'):
if print_msg:
print "Please answer with y[es] or n[o]"
print_msg = False
else:
return ans[0].lower() == 'y'
def verify_boolean(opt):
return opt.lower() in ("yes","true","on") or \
opt.lower() in ("no","false","off")
def is_boolean_true(opt):
return opt.lower() in ("yes","true","on")
def keyword_cmp(string1, string2):
return string1.lower() == string2.lower()
from UserDict import DictMixin
class odict(DictMixin):
def __init__(self, data=None, **kwdata):
self._keys = []
self._data = {}
def __setitem__(self, key, value):
if key not in self._data:
self._keys.append(key)
self._data[key] = value
def __getitem__(self, key):
if key not in self._data:
return self._data[key.lower()]
return self._data[key]
def __delitem__(self, key):
del self._data[key]
self._keys.remove(key)
def keys(self):
return list(self._keys)
def copy(self):
copyDict = odict()
copyDict._data = self._data.copy()
copyDict._keys = self._keys[:]
return copyDict
class olist(list):
def __init__(self, keys):
#print "Init %s" % (repr(keys))
super(olist, self).__init__()
for key in keys:
self.append(key)
self.append(key.upper())
def setup_help_aliases(obj):
for cmd in obj.cmd_aliases.keys():
for alias in obj.cmd_aliases[cmd]:
if obj.help_table:
obj.help_table[alias] = obj.help_table[cmd]
def setup_aliases(obj):
for cmd in obj.cmd_aliases.keys():
for alias in obj.cmd_aliases[cmd]:
obj.cmd_table[alias] = obj.cmd_table[cmd]
-def getpwdent():
- try: euid = os.geteuid()
- except Exception, msg:
- common_err(msg)
- return None
- try: pwdent = pwd.getpwuid(euid)
- except Exception, msg:
- common_err(msg)
- return None
- return pwdent
-def getuser():
- user = os.getenv("USER")
- if not user:
- try: return getpwdent()[0]
- except: return None
- else:
- return user
-def gethomedir():
- homedir = os.getenv("HOME")
- if not homedir:
- try: return getpwdent()[5]
- except: return None
- else:
- return homedir
-
def os_types_list(path):
l = []
for f in glob.glob(path):
if os.access(f,os.X_OK) and os.path.isfile(f):
a = f.split("/")
l.append(a[-1])
return l
def listtemplates():
l = []
for f in os.listdir(vars.tmpl_dir):
if os.path.isfile("%s/%s" % (vars.tmpl_dir,f)):
l.append(f)
return l
def listconfigs():
l = []
for f in os.listdir(vars.tmpl_conf_dir):
if os.path.isfile("%s/%s" % (vars.tmpl_conf_dir,f)):
l.append(f)
return l
def add_sudo(cmd):
if user_prefs.crm_user:
return "sudo -E -u %s %s"%(user_prefs.crm_user,cmd)
return cmd
def pipe_string(cmd,s):
rc = -1 # command failed
cmd = add_sudo(cmd)
p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE)
try:
p.communicate(s)
p.wait()
rc = p.returncode
except IOError, msg:
common_err(msg)
return rc
def filter_string(cmd,s,stderr_on = True):
rc = -1 # command failed
if stderr_on:
stderr = None
else:
stderr = subprocess.PIPE
cmd = add_sudo(cmd)
p = subprocess.Popen(cmd, shell=True, \
stdin = subprocess.PIPE, \
stdout = subprocess.PIPE, stderr = stderr)
try:
outp = p.communicate(s)[0]
p.wait()
rc = p.returncode
except IOError, msg:
common_err(msg)
return rc,outp
def str2tmp(s):
'''
Write the given string to a temporary file. Return the name
of the file.
'''
fd,tmp = mkstemp()
try: f = os.fdopen(fd,"w")
except IOError, msg:
common_err(msg)
return
f.write(s)
f.close()
return tmp
def str2file(s,fname):
'''
Write a string to a file.
'''
try: f = open(fname,"w")
except IOError, msg:
common_err(msg)
return False
f.write(s)
f.close()
return True
def is_filename_sane(name):
if re.search("['`/#*?$\[\]]",name):
common_err("%s: bad name"%name)
return False
return True
def is_name_sane(name):
if re.search("[']",name):
common_err("%s: bad name"%name)
return False
return True
def is_value_sane(name):
if re.search("[']",name):
common_err("%s: bad name"%name)
return False
return True
def show_dot_graph(dotfile):
p = subprocess.Popen("%s %s" % (user_prefs.dotty,dotfile), shell=True, bufsize=0, stdin=None, stdout=None, stderr=None, close_fds=True)
common_info("starting %s to show transition graph"%user_prefs.dotty)
def ext_cmd(cmd):
if options.regression_tests:
print ".EXT", cmd
return subprocess.call(add_sudo(cmd), shell=True)
def get_stdout(cmd, stderr_on = True):
'''
Run a cmd, return stdin output.
stderr_on controls whether to show output which comes on stderr.
'''
if stderr_on:
stderr = None
else:
stderr = subprocess.PIPE
proc = subprocess.Popen(cmd, shell = True, \
stdout = subprocess.PIPE, stderr = stderr)
outp = proc.communicate()[0]
proc.wait()
outp = outp.strip()
return outp
def stdout2list(cmd, stderr_on = True):
'''
Run a cmd, fetch output, return it as a list of lines.
stderr_on controls whether to show output which comes on stderr.
'''
s = get_stdout(add_sudo(cmd), stderr_on)
return s.split('\n')
def wait4dc(what = "", show_progress = True):
'''
Wait for the DC to get into the S_IDLE state. This should be
invoked only after a CIB modification which would exercise
the PE. Parameter "what" is whatever the caller wants to be
printed if showing progress.
It is assumed that the DC is already in a different state,
usually it should be either PENGINE or TRANSITION. This
assumption may not be true, but there's a high chance that it
is since crmd should be faster to move through states than
this shell.
Further, it may also be that crmd already calculated the new
graph, did transition, and went back to the idle state. This
may in particular be the case if the transition turned out to
be empty.
Tricky. Though in practice it shouldn't be an issue.
There's no timeout, as we expect the DC to eventually becomes
idle.
'''
cmd = "crmadmin -D"
s = get_stdout(add_sudo(cmd))
if not s.startswith("Designated"):
common_warn("%s unexpected output: %s" % (cmd,s))
return False
dc = s.split()[-1]
if not dc:
common_warn("can't find DC in: %s" % s)
return False
cmd = "crmadmin -S %s" % dc
cnt = 0
output_started = 0
while True:
s = get_stdout(add_sudo(cmd))
if not s.startswith("Status"):
common_warn("%s unexpected output: %s" % (cmd,s))
return False
try: dc_status = s.split()[-2]
except:
common_warn("%s unexpected output: %s" % (cmd,s))
return False
if dc_status == "S_IDLE":
if output_started:
sys.stderr.write(" done\n")
return True
time.sleep(0.1)
if show_progress:
cnt += 1
if cnt % 10 == 0:
if not output_started:
output_started = 1
sys.stderr.write("waiting for %s to finish " % what)
sys.stderr.write(".")
def is_id_valid(id):
"""
Verify that the id follows the definition:
http://www.w3.org/TR/1999/REC-xml-names-19990114/#ns-qualnames
"""
if not id:
return False
id_re = "^[A-Za-z_][\w._-]*$"
return re.match(id_re,id)
def check_filename(fname):
"""
Verify that the string is a filename.
"""
fname_re = "^[^/]+$"
return re.match(fname_re,id)
def is_process(s):
proc = subprocess.Popen("ps -e -o pid,command | grep -qs '%s'" % s, \
shell=True, stdout=subprocess.PIPE)
proc.wait()
return proc.returncode == 0
def cluster_stack():
if is_process("heartbeat:.[m]aster"):
return "heartbeat"
elif is_process("[a]isexec"):
return "openais"
return ""
def edit_file(fname):
'Edit a file.'
if not fname:
return
if not user_prefs.editor:
return
return ext_cmd("%s %s" % (user_prefs.editor,fname))
def page_string(s):
'Write string through a pager.'
if not s:
return
w,h = get_winsize()
if s.count('\n') <= h:
print s
elif not user_prefs.pager or not options.interactive:
print s
else:
opts = ""
if user_prefs.pager == "less":
opts = "-R"
pipe_string("%s %s" % (user_prefs.pager,opts), s)
def get_winsize():
try:
import curses
curses.setupterm()
w = curses.tigetnum('cols')
h = curses.tigetnum('lines')
except:
try:
w = os.environ['COLS']
h = os.environ['LINES']
except:
w = 80; h = 25
return w,h
def multicolumn(l):
'''
A ls-like representation of a list of strings.
A naive approach.
'''
min_gap = 2
w,h = get_winsize()
max_len = 8
for s in l:
if len(s) > max_len:
max_len = len(s)
cols = w/(max_len + min_gap) # approx.
col_len = w/cols
for i in range(len(l)/cols + 1):
s = ''
for j in range(i*cols,(i+1)*cols):
if not j < len(l):
break
if not s:
s = "%-*s" % (col_len,l[j])
elif (j+1)%cols == 0:
s = "%s%s" % (s,l[j])
else:
s = "%s%-*s" % (s,col_len,l[j])
if s:
print s
def find_value(pl,name):
for n,v in pl:
if n == name:
return v
return None
def lines2cli(s):
'''
Convert a string into a list of lines. Replace continuation
characters. Strip white space, left and right. Drop empty lines.
'''
cl = []
l = s.split('\n')
cum = []
for p in l:
p = p.strip()
if p.endswith('\\'):
p = p.rstrip('\\')
cum.append(p)
else:
cum.append(p)
cl.append(''.join(cum).strip())
cum = []
if cum: # in case s ends with backslash
cl.append(''.join(cum))
return [x for x in cl if x]
user_prefs = UserPrefs.getInstance()
options = Options.getInstance()
+vars = Vars.getInstance()
# vim:ts=4:sw=4:et:
diff --git a/shell/modules/vars.py.in b/shell/modules/vars.py.in
index ea2c40a790..c61a1c9639 100644
--- a/shell/modules/vars.py.in
+++ b/shell/modules/vars.py.in
@@ -1,187 +1,211 @@
# Copyright (C) 2008 Dejan Muhamedagic <dmuhamedagic@suse.de>
#
# 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 2.1 of the License, or (at your option) any later version.
#
# This software 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 library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
-import os
+import os, pwd
from singletonmixin import Singleton
-from utils import gethomedir
+
+def getpwdent():
+ try: euid = os.geteuid()
+ except Exception, msg:
+ common_err(msg)
+ return None
+ try: pwdent = pwd.getpwuid(euid)
+ except Exception, msg:
+ common_err(msg)
+ return None
+ return pwdent
+def getuser():
+ user = os.getenv("USER")
+ if not user:
+ try: return getpwdent()[0]
+ except: return None
+ else:
+ return user
+def gethomedir():
+ homedir = os.getenv("HOME")
+ if not homedir:
+ try: return getpwdent()[5]
+ except: return None
+ else:
+ return homedir
class Vars(Singleton):
cib_cli_map = {
"node": "node",
"primitive": "primitive",
"group": "group",
"clone": "clone",
"master": "ms",
"rsc_location": "location",
"rsc_colocation": "colocation",
"rsc_order": "order",
"cluster_property_set": "property",
"rsc_defaults": "rsc_defaults",
"op_defaults": "op_defaults",
"acl_user": "user",
"acl_role": "role",
}
container_tags = ("group", "clone", "ms", "master")
clonems_tags = ("clone", "ms", "master")
resource_tags = ("primitive","group","clone","ms","master")
constraint_tags = ("rsc_location","rsc_colocation","rsc_order")
constraint_rsc_refs = ("rsc","with-rsc","first","then")
children_tags = ("group", "primitive")
nvpairs_tags = ("meta_attributes", "instance_attributes", "utilization")
defaults_tags = ("rsc_defaults","op_defaults")
resource_cli_names = ("primitive","group","clone","ms","master")
constraint_cli_names = ("location","colocation","collocation","order")
nvset_cli_names = ("property","rsc_defaults","op_defaults")
op_cli_names = ("monitor", "start", "stop", "migrate_to", "migrate_from","promote","demote","notify")
ra_operations = ("probe", "monitor", "start", "stop",
"promote", "demote", "notify", "migrate_to", "migrate_from")
subpfx_list = {
"instance_attributes": "instance_attributes",
"meta_attributes": "meta_attributes",
"utilization": "utilization",
"operations": "ops",
"rule": "rule",
"expression": "expression",
"date_expression": "expression",
"duration": "duration",
"date_spec": "date_spec",
"read": "read",
"write": "write",
"deny": "deny",
}
acl_rule_names = ("read","write","deny")
acl_spec_map = {
"xpath": "xpath",
"ref": "ref",
"tag": "tag",
"attribute": "attribute",
}
acl_shortcuts = {
"meta":
(r"//primitive\[@id='@@'\]/meta_attributes",r"/nvpair\[@name='@@'\]"),
"params":
(r"//primitive\[@id='@@'\]/instance_attributes",r"/nvpair\[@name='@@'\]"),
"utilization":
(r"//primitive\[@id='@@'\]/utilization",),
"location":
(r"//rsc_location\[@id='cli-prefer-@@' and @rsc='@@'\]",),
"property":
(r"//crm_config/cluster_property_set",r"/nvpair\[@name='@@'\]"),
"nodeattr":
(r"//nodes/node/instance_attributes",r"/nvpair\[@name='@@'\]"),
"nodeutil":
(r"//nodes/node/utilization",r"\[@uname='@@'\]"),
"node":
(r"//nodes/node",r"\[@uname='@@'\]"),
"status":
(r"/cib/status",),
"cib":
(r"/cib",),
}
lrm_exit_codes = {
"success": "0",
"unknown": "1",
"args": "2",
"unimplemented": "3",
"perm": "4",
"installed": "5",
"configured": "6",
"not_running": "7",
"master": "8",
"failed_master": "9",
}
lrm_status_codes = {
"pending": "-1",
"done": "0",
"cancelled": "1",
"timeout": "2",
"notsupported": "3",
"error": "4",
}
node_states = ("online", "offline", "unclean")
precious_attrs = ("id-ref",)
time_op_attrs = ("timeout",)
req_op_attributes = ("name", "id")
req_sections = ("crm_config", "nodes", "resources", "constraints")
op_attributes = (
"interval", "timeout", "requires", "enabled", "role",
"on-fail", "start-delay", "interval-origin",
"record-pending", "description",
)
rsc_meta_attributes = (
"allow-migrate", "is-managed", "interval-origin",
"migration-threshold", "priority", "multiple-active",
"failure-timeout", "resource-stickiness", "target-role",
"restart-type", "description",
)
clone_meta_attributes = (
"ordered", "notify", "interleave", "globally-unique",
"clone-max", "clone-node-max", "clone-state", "description",
)
ms_meta_attributes = (
"master-max", "master-node-max", "description",
)
score_types = {'advisory': '0','mandatory': 'INFINITY'}
boolean_ops = ('or','and')
binary_ops = ('lt','gt','lte','gte','eq','ne')
binary_types = ('string' , 'version' , 'number')
unary_ops = ('defined','not_defined')
simple_date_ops = ('lt','gt')
date_ops = ('lt','gt','in_range','date_spec')
date_spec_names = '''hours monthdays weekdays yearsdays months \
weeks years weekyears moon'''.split()
in_range_attrs = ('start','end')
roles_names = ('Stopped', 'Started', 'Master', 'Slave')
actions_names = ( 'start', 'promote', 'demote', 'stop')
node_default_type = "normal"
node_attributes_keyw = ("attributes","utilization")
shadow_envvar = "CIB_shadow"
prompt = ''
tmpfiles = []
this_node = os.uname()[1]
cib_in_use = os.getenv(shadow_envvar)
homedir = gethomedir()
if not homedir:
hist_file = ''
else:
hist_file = os.path.join(homedir,".crm_history")
rc_file = os.path.join(homedir,".crm.rc")
tmpl_conf_dir = os.path.join(homedir,".crmconf")
tmpl_dir = "@datadir@/@PACKAGE@/templates"
pe_dir = "@PE_STATE_DIR@"
crm_conf_dir = "@CRM_CONFIG_DIR@"
crm_daemon_dir = "@CRM_DAEMON_DIR@"
crm_daemon_user = "@CRM_DAEMON_USER@"
crm_version = "@VERSION@ (Build @BUILD_VERSION@)"
ra_if = None # class interface to RA
stonithd_metadata = None # stonithd meta data
pe_metadata = None # PE meta data
crmd_metadata = None # crmd meta data
cib_metadata = None # cib meta data
crm_properties_metadata = None # PE + crmd + cib meta data
meta_progs = ("crmd","pengine","stonithd","cib")
crmd_advanced = (
"dc-version",
"cluster-infrastructure",
"crmd-integration-timeout",
"crmd-finalization-timeout",
"expected-quorum-votes",
)
# vim:ts=4:sw=4:et:
diff --git a/shell/modules/xmlutil.py b/shell/modules/xmlutil.py
index 3beaca881b..b4735042b2 100644
--- a/shell/modules/xmlutil.py
+++ b/shell/modules/xmlutil.py
@@ -1,809 +1,809 @@
# Copyright (C) 2008 Dejan Muhamedagic <dmuhamedagic@suse.de>
#
# 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 2 of the License, or (at your option) any later version.
#
# This software 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 library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
import os
import subprocess
import xml.dom.minidom
from userprefs import Options, UserPrefs
-from vars import Vars
+from vars import Vars, getuser, gethomedir
from msg import *
from utils import *
def xmlparse(f):
try:
doc = xml.dom.minidom.parse(f)
except xml.parsers.expat.ExpatError,msg:
common_err("cannot parse xml: %s" % msg)
return None
return doc
def file2doc(s):
try: f = open(s,'r')
except IOError, msg:
common_err(msg)
return None
doc = xmlparse(f)
f.close()
return doc
cib_dump = "cibadmin -Ql"
def cibdump2file(fname):
cmd = add_sudo(cib_dump)
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
try:
s = ''.join(p.stdout)
p.wait()
except IOError, msg:
common_err(msg)
return None
return str2file(s,fname)
def cib2tmp():
cmd = add_sudo(cib_dump)
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
try:
tmpf = str2tmp(''.join(p.stdout))
p.wait()
except IOError, msg:
common_err(msg)
return None
return tmpf
def cibdump2doc(section = None):
doc = None
if section:
cmd = "%s -o %s" % (cib_dump,section)
else:
cmd = cib_dump
cmd = add_sudo(cmd)
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
try:
doc = xmlparse(p.stdout)
p.wait()
except IOError, msg:
common_err(msg)
return None
return doc
cib_piped = "cibadmin -p"
def commit_rsc(node):
"Replace a resource definition using cibadmin -R"
rc = pipe_string("%s -R -o %s" % \
(cib_piped, "resources"), node.toxml())
return rc == 0
def get_conf_elem(doc, tag):
try:
return doc.getElementsByTagName(tag)[0]
except:
return None
def read_cib(fun, params = None):
doc = fun(params)
if not doc:
return doc,None
cib = doc.childNodes[0]
if not is_element(cib) or cib.tagName != "cib":
cib_no_elem_err("cib")
return doc,None
return doc,cib
def sanity_check_nvpairs(id,node,attr_list):
rc = 0
for nvpair in node.childNodes:
if not is_element(nvpair) or nvpair.tagName != "nvpair":
continue
n = nvpair.getAttribute("name")
if n and not n in attr_list:
common_err("%s: attribute %s does not exist" % (id,n))
rc |= user_prefs.get_check_rc()
return rc
def sanity_check_meta(id,node,attr_list):
rc = 0
if not node or not attr_list:
return rc
for c in node.childNodes:
if not is_element(c):
continue
if c.tagName == "meta_attributes":
rc |= sanity_check_nvpairs(id,c,attr_list)
return rc
def get_interesting_nodes(node,nodes):
for c in node.childNodes:
if is_element(c) and c.tagName in vars.cib_cli_map:
nodes.append(c)
get_interesting_nodes(c,nodes)
return nodes
def resources_xml():
return cibdump2doc("resources")
def rsc2node(id):
doc = resources_xml()
if not doc:
return None
nodes = get_interesting_nodes(doc,[])
for n in nodes:
if is_resource(n) and n.getAttribute("id") == id:
return n
def get_meta_param(id,param):
rsc_meta_show = "crm_resource --meta -r '%s' -g '%s'"
return get_stdout(rsc_meta_show % (id,param), stderr_on = False)
def listnodes():
nodes = []
doc = cibdump2doc("nodes")
if not doc:
return []
nodes_node = get_conf_elem(doc, "nodes")
if not nodes_node:
return []
for c in nodes_node.childNodes:
if not is_element(c):
continue
if c.tagName != "node":
continue
if c.getAttribute("type") == 'normal':
nodes.append(c.getAttribute("uname"))
return nodes
def is_live_cib():
'''We working with the live cluster?'''
return not vars.cib_in_use and not os.getenv("CIB_file")
def cib_shadow_dir():
if os.getenv("CIB_shadow_dir"):
return os.getenv("CIB_shadow_dir")
if getuser() in ("root",vars.crm_daemon_user):
return vars.crm_conf_dir
home = gethomedir()
if home and home.startswith(os.path.sep):
return os.path.join(home,".cib")
return os.getenv("TMPDIR") or "/tmp"
def listshadows():
dir = cib_shadow_dir()
if os.path.isdir(dir):
return stdout2list("ls %s | fgrep shadow. | sed 's/^shadow\.//'" % dir)
else:
return []
def shadowfile(name):
return "%s/shadow.%s" % (cib_shadow_dir(), name)
def shadow2doc(name):
return file2doc(shadowfile(name))
def is_rsc_running(id):
if not is_live_cib():
return False
rsc_node = rsc2node(id)
if not rsc_node:
return False
if not is_resource(rsc_node):
return False
rsc_status = "crm_resource -W -r '%s'"
test_id = rsc_clone(id) or id
outp = get_stdout(rsc_status % test_id, stderr_on = False)
return outp.find("running") > 0 and outp.find("NOT") == -1
def is_rsc_clone(rsc_id):
rsc_node = rsc2node(rsc_id)
return is_clone(rsc_node)
def is_rsc_ms(rsc_id):
rsc_node = rsc2node(rsc_id)
return is_ms(rsc_node)
def rsc_clone(rsc_id):
'''Get a clone of a resource.'''
rsc_node = rsc2node(rsc_id)
if not rsc_node or not rsc_node.parentNode:
return None
pnode = rsc_node.parentNode
if is_group(pnode):
pnode = pnode.parentNode
if is_clonems(pnode):
return pnode.getAttribute("id")
def get_topmost_rsc(node):
'''
Return a topmost node which is a resource and contains this resource
'''
if is_container(node.parentNode):
return get_topmost_rsc(node.parentNode)
return node
def get_cloned_rsc(rsc_id):
rsc_node = rsc2node(rsc_id)
if not rsc_node:
return ""
for c in rsc_node.childNodes:
if is_child_rsc(c):
return c.getAttribute("id")
return ""
attr_defaults_missing = {
}
def add_missing_attr(node):
try:
for defaults in attr_defaults_missing[node.tagName]:
if not node.hasAttribute(defaults[0]):
node.setAttribute(defaults[0],defaults[1])
except: pass
attr_defaults = {
"rule": (("boolean-op","and"),),
"expression": (("type","string"),),
}
def drop_attr_defaults(node, ts = 0):
try:
for defaults in attr_defaults[node.tagName]:
if node.getAttribute(defaults[0]) == defaults[1]:
node.removeAttribute(defaults[0])
except: pass
def is_element(xmlnode):
return xmlnode and xmlnode.nodeType == xmlnode.ELEMENT_NODE
def nameandid(xmlnode,level):
if xmlnode.nodeType == xmlnode.ELEMENT_NODE:
print level*' ',xmlnode.tagName,xmlnode.getAttribute("id"),xmlnode.getAttribute("name")
def xmltraverse(xmlnode,fun,ts=0):
for c in xmlnode.childNodes:
if is_element(c):
fun(c,ts)
xmltraverse(c,fun,ts+1)
def xmltraverse_thin(xmlnode,fun,ts=0):
'''
Skip elements which may be resources themselves.
NB: Call this only on resource (or constraint) nodes, but
never on cib or configuration!
'''
for c in xmlnode.childNodes:
if is_element(c) and not c.tagName in ('primitive','group'):
xmltraverse_thin(c,fun,ts+1)
fun(xmlnode,ts)
def xml_processnodes(xmlnode,node_filter,proc):
'''
Process with proc all nodes that match filter.
'''
node_list = []
for child in xmlnode.childNodes:
if node_filter(child):
node_list.append(child)
if child.hasChildNodes():
xml_processnodes(child,node_filter,proc)
if node_list:
proc(node_list)
# filter the cib
def is_whitespace(node):
return node.nodeType == node.TEXT_NODE and not node.data.strip()
def is_comment(node):
return node.nodeType == node.COMMENT_NODE
def is_status_node(node):
return is_element(node) and node.tagName == "status"
def is_emptynvpairs(node):
if is_element(node) and node.tagName in vars.nvpairs_tags:
for a in vars.precious_attrs:
if node.getAttribute(a):
return False
for n in node.childNodes:
if is_element(n):
return False
return True
else:
return False
def is_group(node):
return is_element(node) \
and node.tagName == "group"
def is_ms(node):
return is_element(node) \
and node.tagName in ("master","ms")
def is_clone(node):
return is_element(node) \
and node.tagName == "clone"
def is_clonems(node):
return is_element(node) \
and node.tagName in vars.clonems_tags
def is_container(node):
return is_element(node) \
and node.tagName in vars.container_tags
def is_primitive(node):
return is_element(node) \
and node.tagName == "primitive"
def is_resource(node):
return is_element(node) \
and node.tagName in vars.resource_tags
def is_child_rsc(node):
return is_element(node) \
and node.tagName in vars.children_tags
def is_constraint(node):
return is_element(node) \
and node.tagName in vars.constraint_tags
def is_defaults(node):
return is_element(node) \
and node.tagName in vars.defaults_tags
def rsc_constraint(rsc_id,cons_node):
if not is_element(cons_node):
return False
for attr in cons_node.attributes.keys():
if attr in vars.constraint_rsc_refs \
and rsc_id == cons_node.getAttribute(attr):
return True
for rref in cons_node.getElementsByTagName("resource_ref"):
if rsc_id == rref.getAttribute("id"):
return True
return False
def sort_container_children(node_list):
'''
Make sure that attributes's nodes are first, followed by the
elements (primitive/group). The order of elements is not
disturbed, they are just shifted to end!
'''
for node in node_list:
children = []
for c in node.childNodes:
if is_element(c) and c.tagName in vars.children_tags:
children.append(c)
for c in children:
node.removeChild(c)
for c in children:
node.appendChild(c)
def rmnode(node):
if node and node.parentNode:
if node.parentNode:
node.parentNode.removeChild(node)
node.unlink()
def rmnodes(node_list):
for node in node_list:
rmnode(node)
def printid(node_list):
for node in node_list:
id = node.getAttribute("id")
if id: print "node id:",id
def sanitize_cib(doc):
xml_processnodes(doc,is_status_node,rmnodes)
#xml_processnodes(doc,is_element,printid)
xml_processnodes(doc,is_emptynvpairs,rmnodes)
xml_processnodes(doc,is_whitespace,rmnodes)
#xml_processnodes(doc,is_comment,rmnodes)
xml_processnodes(doc,is_container,sort_container_children)
xmltraverse(doc,drop_attr_defaults)
def is_simpleconstraint(node):
return len(node.getElementsByTagName("resource_ref")) == 0
match_list = {
"node": ("uname",),
"crm_config": (),
"rsc_defaults": (),
"op_defaults": (),
"cluster_property_set": (),
"instance_attributes": (),
"meta_attributes": (),
"utilization": (),
"operations": (),
"nvpair": ("name",),
"op": ("name","interval"),
"rule": ("score","score-attribute","role"),
"expression": ("attribute","operation","value"),
}
def add_comment(node,s):
'''
Add comment s to node from doc.
'''
if not node or not s:
return
comm_node = node.ownerDocument.createComment(s)
firstelem = None
for n in node.childNodes:
if is_element(n):
firstelem = n
break
node.insertBefore(comm_node, firstelem)
def stuff_comments(node,comments):
for s in comments:
add_comment(node,s)
def fix_comments(node):
'Make sure that comments start with #'
cnodes = [x for x in node.childNodes if is_comment(x)]
for n in cnodes:
n.data = n.data.strip()
if not n.data.startswith("#"):
n.data = "# %s" % n.data
def set_id_used_attr(node):
node.setAttribute("__id_used", "Yes")
def is_id_used_attr(node):
return node.getAttribute("__id_used") == "Yes"
def remove_id_used_attr(node,lvl):
if is_element(node) and is_id_used_attr(node):
node.removeAttribute("__id_used")
def remove_id_used_attributes(node):
if node:
xmltraverse(node, remove_id_used_attr)
def lookup_node(node,oldnode,location_only = False):
'''
Find a child of oldnode which matches node.
This is used to "harvest" existing ids in order to prevent
irrelevant changes to the XML code.
The list of attributes to match is in the dictionary
match_list.
The "id" attribute is treated differently. In case the new node
(the first parameter here) contains the id, then the "id"
attribute is added to the match list.
'''
#print "lookup:",node.tagName,node.getAttribute("id")
if not oldnode:
return None
#print " in:",oldnode.tagName,oldnode.getAttribute("id")
try:
attr_list = list(match_list[node.tagName])
except KeyError:
attr_list = []
if node.getAttribute("id"):
#print " add id attribute"
attr_list.append("id")
for c in oldnode.childNodes:
if not is_element(c):
continue
if not location_only and is_id_used_attr(c):
continue
#print " checking:",c.tagName,c.getAttribute("id")
if node.tagName == c.tagName:
failed = False
for a in attr_list:
if node.getAttribute(a) != c.getAttribute(a):
failed = True
break
if not failed:
#print " found:",c.tagName,c.getAttribute("id")
return c
return None
def find_operation(rsc_node,name,interval):
op_node_l = rsc_node.getElementsByTagName("operations")
for ops in op_node_l:
for c in ops.childNodes:
if not is_element(c):
continue
if c.tagName != "op":
continue
if c.getAttribute("name") == name \
and c.getAttribute("interval") == interval:
return c
def filter_on_tag(nl,tag):
return [node for node in nl if node.tagName == tag]
def nodes(node_list):
return filter_on_tag(node_list,"node")
def primitives(node_list):
return filter_on_tag(node_list,"primitive")
def groups(node_list):
return filter_on_tag(node_list,"group")
def clones(node_list):
return filter_on_tag(node_list,"clone")
def mss(node_list):
return filter_on_tag(node_list,"master")
def constraints(node_list):
return filter_on_tag(node_list,"rsc_location") \
+ filter_on_tag(node_list,"rsc_colocation") \
+ filter_on_tag(node_list,"rsc_order")
def properties(node_list):
return filter_on_tag(node_list,"cluster_property_set") \
+ filter_on_tag(node_list,"rsc_defaults") \
+ filter_on_tag(node_list,"op_defaults")
def acls(node_list):
return filter_on_tag(node_list,"acl_role") \
+ filter_on_tag(node_list,"acl_user")
def processing_sort(nl):
'''
It's usually important to process cib objects in this order,
i.e. simple objects first.
'''
return nodes(nl) + primitives(nl) + groups(nl) + mss(nl) + clones(nl) \
+ constraints(nl) + properties(nl) + acls(nl)
def obj_cmp(obj1,obj2):
return cmp(obj1.obj_id,obj2.obj_id)
def filter_on_type(cl,obj_type):
if type(cl[0]) == type([]):
l = [cli_list for cli_list in cl if cli_list[0][0] == obj_type]
if user_prefs.get_sort_elems():
l.sort(cmp = cmp)
else:
l = [obj for obj in cl if obj.obj_type == obj_type]
if user_prefs.get_sort_elems():
l.sort(cmp = obj_cmp)
return l
def nodes_cli(cl):
return filter_on_type(cl,"node")
def primitives_cli(cl):
return filter_on_type(cl,"primitive")
def groups_cli(cl):
return filter_on_type(cl,"group")
def clones_cli(cl):
return filter_on_type(cl,"clone")
def mss_cli(cl):
return filter_on_type(cl,"ms") + filter_on_type(cl,"master")
def constraints_cli(node_list):
return filter_on_type(node_list,"location") \
+ filter_on_type(node_list,"colocation") \
+ filter_on_type(node_list,"collocation") \
+ filter_on_type(node_list,"order")
def properties_cli(cl):
return filter_on_type(cl,"property") \
+ filter_on_type(cl,"rsc_defaults") \
+ filter_on_type(cl,"op_defaults")
def acls_cli(cl):
return filter_on_type(cl,"role") \
+ filter_on_type(cl,"user")
def ops_cli(cl):
return filter_on_type(cl,"op")
def processing_sort_cli(cl):
'''
Return the given list in this order:
nodes, primitives, groups, ms, clones, constraints, rest
Both a list of objects (CibObject) and list of cli
representations accepted.
'''
return nodes_cli(cl) + primitives_cli(cl) + groups_cli(cl) + mss_cli(cl) + clones_cli(cl) \
+ constraints_cli(cl) + properties_cli(cl) + ops_cli(cl) + acls_cli(cl)
def is_resource_cli(s):
return s in olist(vars.resource_cli_names)
def is_constraint_cli(s):
return s in olist(vars.constraint_cli_names)
def referenced_resources(node):
if not is_constraint(node):
return []
xml_obj_type = node.tagName
if xml_obj_type == "rsc_location":
node_list = node.getElementsByTagName("rsc")
elif node.getElementsByTagName("resource_ref"): # resource sets
node_list = node.getElementsByTagName("resource_ref")
elif xml_obj_type == "rsc_colocation":
node_list = node.getElementsByTagName("rsc") + \
node.getElementsByTagName("with-rsc")
elif xml_obj_type == "rsc_order":
node_list = node.getElementsByTagName("first") + \
node.getElementsByTagName("then")
return [x.getAttribute("id") for x in node_list]
def rename_id(node,old_id,new_id):
if node.getAttribute("id") == old_id:
node.setAttribute("id", new_id)
def rename_rscref_simple(c_obj,old_id,new_id):
c_modified = False
for attr in c_obj.node.attributes.keys():
if attr in vars.constraint_rsc_refs and \
c_obj.node.getAttribute(attr) == old_id:
c_obj.node.setAttribute(attr, new_id)
c_obj.updated = True
c_modified = True
return c_modified
def delete_rscref_simple(c_obj,rsc_id):
c_modified = False
for attr in c_obj.node.attributes.keys():
if attr in vars.constraint_rsc_refs and \
c_obj.node.getAttribute(attr) == rsc_id:
c_obj.node.removeAttribute(attr)
c_obj.updated = True
c_modified = True
return c_modified
def rset_uniq(c_obj,d):
'''
Drop duplicate resource references.
'''
l = []
for rref in c_obj.node.getElementsByTagName("resource_ref"):
rsc_id = rref.getAttribute("id")
if d[rsc_id] > 1: # drop one
l.append(rref)
d[rsc_id] -= 1
rmnodes(l)
def delete_rscref_rset(c_obj,rsc_id):
'''
Drop all reference to rsc_id.
'''
c_modified = False
l = []
for rref in c_obj.node.getElementsByTagName("resource_ref"):
if rsc_id == rref.getAttribute("id"):
l.append(rref)
c_obj.updated = True
c_modified = True
rmnodes(l)
l = []
for rset in c_obj.node.getElementsByTagName("resource_set"):
if len(rset.getElementsByTagName("resource_ref")) == 0:
l.append(rset)
c_obj.updated = True
c_modified = True
rmnodes(l)
return c_modified
def rset_convert(c_obj):
l = c_obj.node.getElementsByTagName("resource_ref")
if len(l) != 2:
return # eh?
c_obj.modified = True
cli = c_obj.repr_cli(format = -1)
newnode = c_obj.cli2node(cli)
if newnode:
c_obj.node.parentNode.replaceChild(newnode,c_obj.node)
c_obj.node.unlink()
def rename_rscref_rset(c_obj,old_id,new_id):
c_modified = False
d = {}
for rref in c_obj.node.getElementsByTagName("resource_ref"):
rsc_id = rref.getAttribute("id")
if rsc_id == old_id:
rref.setAttribute("id", new_id)
rsc_id = new_id
c_obj.updated = True
c_modified = True
if not rsc_id in d:
d[rsc_id] = 0
else:
d[rsc_id] += 1
rset_uniq(c_obj,d)
# if only two resource references remained then, to preserve
# sanity, convert it to a simple constraint (sigh)
cnt = 0
for key in d:
cnt += d[key]
if cnt == 2:
rset_convert(c_obj)
return c_modified
def rename_rscref(c_obj,old_id,new_id):
if rename_rscref_simple(c_obj,old_id,new_id) or \
rename_rscref_rset(c_obj,old_id,new_id):
err_buf.info("resource references in %s updated" % c_obj.obj_string())
def delete_rscref(c_obj,rsc_id):
return delete_rscref_simple(c_obj,rsc_id) or \
delete_rscref_rset(c_obj,rsc_id)
def silly_constraint(c_node,rsc_id):
'''
Remove a constraint from rsc_id to rsc_id.
Or an invalid one.
'''
if c_node.getElementsByTagName("resource_ref"):
# it's a resource set
# the resource sets have already been uniq-ed
return len(c_node.getElementsByTagName("resource_ref")) <= 1
cnt = 0 # total count of referenced resources have to be at least two
rsc_cnt = 0
for attr in c_node.attributes.keys():
if attr in vars.constraint_rsc_refs:
cnt += 1
if c_node.getAttribute(attr) == rsc_id:
rsc_cnt += 1
if c_node.tagName == "rsc_location": # locations are never silly
return cnt < 1
else:
return rsc_cnt == 2 or cnt < 2
def get_rsc_children_ids(node):
return [x.getAttribute("id") \
for x in node.childNodes if is_child_rsc(x)]
def get_rscop_defaults_meta_node(node):
for c in node.childNodes:
if not is_element(c) or c.tagName != "meta_attributes":
continue
return c
return None
def new_cib():
doc = xml.dom.minidom.Document()
cib = doc.createElement("cib")
doc.appendChild(cib)
configuration = doc.createElement("configuration")
cib.appendChild(configuration)
for name in vars.req_sections:
node = doc.createElement(name)
configuration.appendChild(node)
return doc
def get_topnode(doc, tag):
"Get configuration element or create/append if there's none."
try:
e = doc.getElementsByTagName(tag)[0]
except:
e = doc.createElement(tag)
conf = doc.getElementsByTagName("configuration")[0]
if conf:
conf.appendChild(e)
else:
return None
return e
def new_cib_element(node,tagname,id_pfx):
base_id = node.getAttribute("id")
newnode = node.ownerDocument.createElement(tagname)
newnode.setAttribute("id", "%s-%s" % (base_id,id_pfx))
node.appendChild(newnode)
return newnode
def get_attr_in_set(node,attr):
for c in node.childNodes:
if not is_element(c):
continue
if c.tagName == "nvpair" and c.getAttribute("name") == attr:
return c
return None
def set_attr(node,attr,value):
'''
Set an attribute in the attribute set.
'''
nvpair = get_attr_in_set(node,attr)
if not nvpair:
nvpair = new_cib_element(node,"nvpair",attr)
nvpair.setAttribute("name",attr)
nvpair.setAttribute("value",value)
def get_set_nodes(node,setname,create = 0):
'Return the attributes set nodes (create one if requested)'
l = []
for c in node.childNodes:
if not is_element(c):
continue
if c.tagName == setname:
l.append(c)
if l:
return l
if create:
l.append(new_cib_element(node,setname,setname))
return l
def xml_cmp(n, m, show = False):
rc = hash(n.toxml()) == hash(m.toxml())
if not rc and show and user_prefs.get_debug():
print "original:",n.toprettyxml()
print "processed:",m.toprettyxml()
return hash(n.toxml()) == hash(m.toxml())
def merge_nvpairs(dnode,snode):
rc = False
add_children = []
for c in snode.childNodes:
if not is_element(c):
continue
if c.tagName == "nvpair":
dc = lookup_node(c,dnode)
if dc:
dc.setAttribute("value",c.getAttribute("value"))
else:
add_children.append(c)
rc = True
for c in add_children:
dnode.appendChild(c)
return rc
def merge_nodes(dnode,snode):
'''
Import elements from snode into dnode.
If an element is attributes set (vars.nvpairs_tags), then
merge nvpairs by the name attribute.
Otherwise, replace the whole element. (TBD)
'''
#print "1:",dnode.toprettyxml()
#print "2:",snode.toprettyxml()
#vars.nvpairs_tags
rc = False # any changes done?
if not dnode or not snode:
return rc
for c in snode.childNodes:
dc = lookup_node(c,dnode)
if not dc:
if c.tagName in vars.nvpairs_tags:
dnode.appendChild(c)
rc = True
continue
if dc.tagName in vars.nvpairs_tags:
rc = rc or merge_nvpairs(dc,c)
return rc
user_prefs = UserPrefs.getInstance()
vars = Vars.getInstance()
# vim:ts=4:sw=4:et:
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Jul 20, 7:30 PM (3 h, 17 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2081336
Default Alt Text
(78 KB)
Attached To
Mode
rP Pacemaker
Attached
Detach File
Event Timeline
Log In to Comment