在进行opengauss安装时候,需要对数据库安装配置文件cluster_config_template.xml进行编辑,我这里是通过python在进行对该文件进行操作的,使其来生成我们需要的数据库配置文件。
import xml.etree.ElementTree as ET
CLUSTER_CONFIG_FILE_NAME = r'cluster_config_template.xml'
back_hosts=[
{'hostname': '127.0.0.1', 'username': 'root', 'password': 'XXXXXXXX'},
{'hostname': '127.0.0.2', 'username': 'root', 'password': 'XXXXXXXX'},
{'hostname': '127.0.0.3', 'username': 'root', 'password': 'XXXXXXXX'},
]
host_names_dict = {"all_hostname":"vm_01,vm_02,vm_03","127.0.0.1":"vm_02","127.0.0.1":"vm_02","127.0.0.3":"vm_03"}
# ###xml文件dataNode1值字符串###
xml_data_node1_str = "{data_node_params}/data,{bk_hostname1},{data_node_params}/data,{bk_hostname2},{data_node_params}/data"
xml_data_gaussdb_log_path_str = "/home/omm/opengauss/db_{db_name}/log"
xml_data_gaussdb_mpp_path_str = "/home/omm/opengauss/db_{db_name}/tmp"
# ###xml文件ip###
xml_data_backIp1s_str = "{host1},{host2},{host3}"
# ###xml配置文件要更改得数据###
CLUSTER_CONFIG_UPDATE_DATA = [
{'xpath_re':'./CLUSTER/PARAM[@name="nodeNames"]', 'update_key':'value', 'update_value':''},
{'xpath_re':'./DEVICELIST/DEVICE[1]', 'update_key':'sn', 'update_value':''},
{'xpath_re':'./DEVICELIST/DEVICE[1]/PARAM[@name="name"]', 'update_key':'value', 'update_value':'127.0.0.1'},
{'xpath_re':'./DEVICELIST/DEVICE[1]/PARAM[@name="cmServerRelation"]', 'update_key':'value', 'update_value':'127.0.0.1'},
{'xpath_re':'./DEVICELIST/DEVICE[2]', 'update_key':'sn', 'update_value':'127.0.0.2'},
{'xpath_re':'./DEVICELIST/DEVICE[2]/PARAM[@name="name"]', 'update_key':'value', 'update_value':'127.0.0.2'},
{'xpath_re':'./DEVICELIST/DEVICE[3]', 'update_key':'sn', 'update_value':'127.0.0.3'},
{'xpath_re':'./DEVICELIST/DEVICE[3]/PARAM[@name="name"]', 'update_key':'value', 'update_value':'127.0.0.3'},
{'xpath_re':'./DEVICELIST/DEVICE[1]/PARAM[@name="dataNode1"]', 'update_key':'value', 'update_value':'127.0.0.1'},
{'xpath_re':'./CLUSTER/PARAM[@name="backIp1s"]', 'update_key':'value', 'update_value':'127.0.0.1,127.0.0.2,127.0.0.3'},
{'xpath_re':'./DEVICELIST/DEVICE[1]/PARAM[@name="backIp1"]', 'update_key':'value', 'update_value':'127.0.0.1'},
{'xpath_re':'./DEVICELIST/DEVICE[1]/PARAM[@name="sshIp1"]', 'update_key':'value', 'update_value':'127.0.0.1'},
{'xpath_re':'./DEVICELIST/DEVICE[1]/PARAM[@name="cmServerListenIp1"]', 'update_key':'value', 'update_value':'127.0.0.1,127.0.0.2,127.0.0.3'},
{'xpath_re':'./DEVICELIST/DEVICE[1]/PARAM[@name="cmServerHaIp1"]', 'update_key':'value', 'update_value':'127.0.0.1,127.0.0.2,127.0.0.3'},
{'xpath_re':'./DEVICELIST/DEVICE[2]/PARAM[@name="backIp1"]', 'update_key':'value', 'update_value':'127.0.0.2'},
{'xpath_re':'./DEVICELIST/DEVICE[2]/PARAM[@name="sshIp1"]', 'update_key':'value', 'update_value':'127.0.0.2'},
{'xpath_re':'./DEVICELIST/DEVICE[3]/PARAM[@name="backIp1"]', 'update_key':'value', 'update_value':'127.0.0.3'},
{'xpath_re':'./DEVICELIST/DEVICE[3]/PARAM[@name="sshIp1"]', 'update_key':'value', 'update_value':'127.0.0.3'},
{'xpath_re':'./CLUSTER/PARAM[@name="clusterName"]', 'update_key':'value', 'update_value':'cmos_cluster_1'},
{'xpath_re':'./CLUSTER/PARAM[@name="gaussdbLogPath"]', 'update_key':'value', 'update_value':'cmos_cluster_1'},
{'xpath_re':'./CLUSTER/PARAM[@name="tmpMppdbPath"]', 'update_key':'value', 'update_value':'cmos_cluster_1'},
]
class ClusterConfig:
"""
func:cluster_config.xml文件操作
"""
def __init__(self, xml_file_name):
"""
:param xml_file_name:xml文件名称
"""
self.xml_file_name = xml_file_name
def set_xml_data(self, xml_xpath_re, set_key, set_content):
"""
:param xml_xpath_re:xpanth规则
:param set_key: 设置值得key
:param set_content: 设置得数据
"""
# 打开xml文档
doc = ET.parse(self.xml_file_name)
root = doc.getroot()
# 查找修改路劲
sub_obj = root.find(xml_xpath_re)
# print('xml查找得内容:', sub_obj)
sub_obj.set(set_key, set_content)
# 保存修改
doc.write(self.xml_file_name)
def cluster_config_xml_set(self, cluster_congfig_set_data, back_hosts):
"""
:param cluster_congfig_set_data:xml文件生成需要更改得数据,需要有查找节点得规则,更新得key和value
:return:
"""
all_hostnames = host_names_dict.get('all_hostname')
host_name0 = host_names_dict[back_hosts[0].get('hostname')]
host_name1 = host_names_dict[back_hosts[1].get('hostname')]
host_name2 = host_names_dict[back_hosts[2].get('hostname')]
cluster_congfig_set_data[0]['update_value'] = all_hostnames
cluster_congfig_set_data[3]['update_value'] = all_hostnames
cluster_congfig_set_data[1]['update_value'] = host_name0
cluster_congfig_set_data[2]['update_value'] = host_name0
cluster_congfig_set_data[4]['update_value'] = host_name1
cluster_congfig_set_data[5]['update_value'] = host_name1
cluster_congfig_set_data[6]['update_value'] = host_name2
cluster_congfig_set_data[7]['update_value'] = host_name2
cluster_congfig_set_data[8]['update_value'] = xml_data_node1_str.format(data_node_params=data_node_params,bk_hostname1=host_name1,bk_hostname2=host_name2)
cluster_congfig_set_data[9]['update_value'] = xml_data_backIp1s_str.format(host1=back_hosts[0].get('hostname'), host2=back_hosts[1].get('hostname'),host3=back_hosts[2].get('hostname'))
cluster_congfig_set_data[12]['update_value'] = xml_data_backIp1s_str.format(host1=back_hosts[0].get('hostname'), host2=back_hosts[1].get('hostname'),host3=back_hosts[2].get('hostname'))
cluster_congfig_set_data[13]['update_value'] = xml_data_backIp1s_str.format(host1=back_hosts[0].get('hostname'), host2=back_hosts[1].get('hostname'),host3=back_hosts[2].get('hostname'))
cluster_congfig_set_data[10]['update_value'], cluster_congfig_set_data[11]['update_value'] = back_hosts[0].get('hostname'), back_hosts[0].get('hostname')
cluster_congfig_set_data[14]['update_value'], cluster_congfig_set_data[15]['update_value'] = back_hosts[1].get('hostname'), back_hosts[1].get('hostname')
cluster_congfig_set_data[16]['update_value'], cluster_congfig_set_data[17]['update_value'] = back_hosts[2].get('hostname'), back_hosts[2].get('hostname')
cluster_congfig_set_data[18]['update_value'] = cluster_name_params
cluster_congfig_set_data[19]['update_value'] = xml_data_gaussdb_log_path_str
cluster_congfig_set_data[20]['update_value'] = xml_data_gaussdb_mpp_path_str
for item in cluster_congfig_set_data:
self.set_xml_data(item.get('xpath_re'), item.get('update_key'), item.get('update_value'))