diff --git a/cts/haresources2cib.py.in b/cts/haresources2cib.py.in index 7f53b53b53..50fcca2f0c 100755 --- a/cts/haresources2cib.py.in +++ b/cts/haresources2cib.py.in @@ -1,174 +1,239 @@ #!@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.minidom +using_ocf = 1 +using_mon = 1 + +hb2ocf_map = { + "apache" :["configfile"], + "IPaddr" :["ip","netmask","nic","broadcast"], + "IPaddr2":["ip","netmask","nic","broadcast"], + "db2" :["instance"] + } + def create_cib() : - doc = xml.dom.minidom.Document() - cib = doc.createElement("cib") - doc.appendChild(cib) - - configuration = doc.createElement("configuration") - cib.appendChild(configuration) - - crm_config = doc.createElement("crm_config") - configuration.appendChild(crm_config) - - nvpair = doc.createElement("nvpair") - crm_config.appendChild(nvpair) - nvpair.setAttribute("id","transition_idle_timeout") - nvpair.setAttribute("name","transition_idle_timeout") - nvpair.setAttribute("value","120s") - - nvpair = doc.createElement("nvpair") - crm_config.appendChild(nvpair) - nvpair.setAttribute("id","symmetric_cluster") - nvpair.setAttribute("name","symmetric_cluster") - nvpair.setAttribute("value","true") - - nvpair = doc.createElement("nvpair") - crm_config.appendChild(nvpair) - nvpair.setAttribute("id","no_quorum_policy") - nvpair.setAttribute("name","no_quorum_policy") - nvpair.setAttribute("value","stop") - - 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 + doc = xml.dom.minidom.Document() + cib = doc.createElement("cib") + doc.appendChild(cib) + + configuration = doc.createElement("configuration") + cib.appendChild(configuration) + + crm_config = doc.createElement("crm_config") + configuration.appendChild(crm_config) + + nvpair = doc.createElement("nvpair") + crm_config.appendChild(nvpair) + nvpair.setAttribute("id","transition_idle_timeout") + nvpair.setAttribute("name","transition_idle_timeout") + nvpair.setAttribute("value","120s") + + nvpair = doc.createElement("nvpair") + crm_config.appendChild(nvpair) + nvpair.setAttribute("id","symmetric_cluster") + nvpair.setAttribute("name","symmetric_cluster") + nvpair.setAttribute("value","true") + + nvpair = doc.createElement("nvpair") + crm_config.appendChild(nvpair) + nvpair.setAttribute("id","no_quorum_policy") + nvpair.setAttribute("name","no_quorum_policy") + nvpair.setAttribute("value","stop") + + 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("primitive") - 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 - + + id, type, params = None, None, None + + if len(string.split(string.split(rsc,'/')[0],'.')) == 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("primitive") + resource.setAttribute("id",id) + resource.setAttribute("type",type) + if using_ocf and type in hb2ocf_map : + resource.setAttribute("class","ocf") + else : + resource.setAttribute("class","heartbeat") + resource.setAttribute("provider","heartbeat") + + if using_mon : + operations = doc.createElement("operations") + resource.appendChild(operations) + mon_op = doc.createElement("op") + operations.appendChild(mon_op) + mon_op.setAttribute("id","1") + mon_op.setAttribute("name","monitor") + mon_op.setAttribute("interval", "120s") + mon_op.setAttribute("timeout", "60s") + + if len(params) != 0: + instance_attributes = doc.createElement("instance_attributes") + resource.appendChild(instance_attributes) + attributes = doc.createElement("attributes") + instance_attributes.appendChild(attributes) + if using_ocf and type in hb2ocf_map : + if type in ["IPaddr", "IPaddr2"] : + unsort = string.split(params[0], "/") + params = [None, None, None, None] + #ip + params[0] = unsort[0] + for param in unsort[1:] : + if len(string.split(param, ".")) == 4 : + #broadcast + params[3] = param + break + try : + int(param) + #netmask bits + params[1] = param + except ValueError: + #nic + params[2] = param + index = 0 + for param in params: + if param == None : + index += 1 + continue + nvpair = doc.createElement("nvpair") + name = hb2ocf_map[type][index] + nvpair.setAttribute("name",name) + nvpair.setAttribute("value",str(param)) + attributes.appendChild(nvpair) + index += 1 + else : + 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","rsc_location_"+id) - rsc_location.setAttribute("rsc",id) - rule = doc.createElement("rule") - rule.setAttribute("id","prefered_location_"+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 + rsc_location = doc.createElement("rsc_location") + rsc_location.setAttribute("id","rsc_location_"+id) + rsc_location.setAttribute("rsc",id) + rule = doc.createElement("rule") + rule.setAttribute("id","prefered_location_"+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("group") - resource_group.setAttribute("id",id) - return resource_group + resource_group = doc.createElement("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) - + 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) + 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 - - print cib[0].toprettyxml() + cib = create_cib() + resource_file = None + resource_file = "/etc/ha.d/haresources" + + # Process arguments... + + for arg in sys.argv[1:]: + if arg == "--nu-ocf" : + using_ocf = 0 + elif arg == "--nu-monitor" : + using_mon = 0 + elif arg[0] == "-" : + print "usage: " + sys.argv[0] \ + + " [--nu-ocf]"\ + + " [--nu-monitor]"\ + + " [--help|-h]"\ + + " [resourcefile]" + sys.exit(1) + else: + resource_file = arg + + 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 + print cib[0].toprettyxml()