diff --git a/cts/haresources2cib.py.in b/cts/haresources2cib.py.in new file mode 100755 index 0000000000..7dad522574 --- /dev/null +++ b/cts/haresources2cib.py.in @@ -0,0 +1,155 @@ +#!@PYTHON@ + +'''haresources2cib.py.in, convert the haresources file of heartbeat 1.x + to cib.xml for heartbeat 2.x +''' + +__copyright__=''' +Author: Huang Zhen +Copyright (C) 2005 International Business Machines +''' + +# +# 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 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, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +import sys,string +import xml.dom.ext +import xml.dom.minidom + + +def create_cib() : + doc = xml.dom.minidom.Document() + cib = doc.createElement("cib") + doc.appendChild(cib) + + configuration = doc.createElement("configuration") + cib.appendChild(configuration) + + configuration.appendChild(doc.createElement("crm_config")) + configuration.appendChild(doc.createElement("nodes")) + resources = doc.createElement("resources") + configuration.appendChild(resources) + constraints = doc.createElement("constraints") + configuration.appendChild(constraints) + + status = doc.createElement("status") + cib.appendChild(status) + + return doc, resources, constraints + +def cib_resource(doc,index, rsc): + + id, type, params = None, None, None + + if len(string.split(rsc,'.')) == 4 : + id = "IPaddr"+"_"+str(index) + type = "IPaddr" + params = [rsc] + else : + fields = string.split(rsc,"::") + id = fields[0]+"_"+str(index) + type = fields[0] + params = fields[1:] + + resource = doc.createElement("resource") + resource.setAttribute("id",id) + resource.setAttribute("type",type) + resource.setAttribute("class","heartbeat") + resource.setAttribute("provider","heartbeat") + if len(params) != 0: + instance_attributes = doc.createElement("instance_attributes") + resource.appendChild(instance_attributes) + attributes = doc.createElement("attributes") + instance_attributes.appendChild(attributes) + index = 1 + for param in params : + nvpair = doc.createElement("nvpair") + nvpair.setAttribute("name",str(index)) + nvpair.setAttribute("value",str(param)) + attributes.appendChild(nvpair) + index += 1 + return id, resource + +def cib_rsc_location(doc, id, node): + rsc_location = doc.createElement("rsc_location") + rsc_location.setAttribute("id","run_"+id) + rsc_location.setAttribute("rsc",id) + rule = doc.createElement("rule") + rule.setAttribute("id","pre_run_"+id) + rule.setAttribute("score","100") + rsc_location.appendChild(rule) + expression = doc.createElement("expression") + expression.setAttribute("attribute","#uname") + expression.setAttribute("operation","eq") + expression.setAttribute("value", node) + rule.appendChild(expression) + return rsc_location + +def cib_resource_group(doc, id): + resource_group = doc.createElement("resource_group") + resource_group.setAttribute("id",id) + return resource_group + +def add_resource(cib,index,node,rsc): + id,resource = cib_resource(cib[0],index,rsc) + cib[1].appendChild(resource) + rsc_location = cib_rsc_location(cib[0],id,node) + cib[2].appendChild(rsc_location) + +def add_resource_group(cib,index,node,rscs): + groupid = "group"+str(index) + resource_group = cib_resource_group(cib[0],groupid) + cib[1].appendChild(resource_group) + for rsc in rscs : + rid,resource = cib_resource(cib[0],index,rsc) + resource_group.appendChild(resource) + index += 1 + rsc_location = cib_rsc_location(cib[0],groupid,node) + cib[2].appendChild(rsc_location) + +if __name__=="__main__" : + cib = create_cib() + resource_file = None + if len(sys.argv) == 1 : + resource_file = "/etc/ha.d/haresources" + elif len(sys.argv) == 2 : + resource_file = sys.argv[1] + else : + print "Usage: %s or %s resourcefile"%(sys.argv[0],sys.argv[0]) + sys.exit(1) + file = open(resource_file, "r") + pre_line = "" + id_index = 1 + for line in file.readlines() : + line = string.strip(line) + if len (line) == 0 or line[0] == '#' : + continue + if line[-1] == '\\' : + pre_line += line[:-1] + " " + continue + else : + line = pre_line + line + pre_line = "" + fields = string.split(line) + if len(fields) == 2 : + add_resource(cib, id_index, fields[0], fields[1]) + id_index += 1 + elif len(fields) > 2 : + add_resource_group(cib, id_index, fields[0], fields[1:]) + id_index += len(fields) + else : + print "can not parse this line:"+line + + xml.dom.ext.PrettyPrint(cib[0]) \ No newline at end of file