opengauss安装---cluster_config_template.xml修改

文章描述了一个使用Python脚本编辑OpenGauss数据库安装配置文件`cluster_config_template.xml`的过程。脚本通过ElementTree库解析XML文件,然后根据给定的主机信息和配置更新数据,动态生成和修改配置文件中的节点值,如hostname、IP地址、数据路径等,以适应不同的部署环境。
摘要由CSDN通过智能技术生成

在进行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'))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值