generate_config.py 3.3 KB
Newer Older
R
Rongfeng Fu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# coding: utf-8
# OceanBase Deploy.
# Copyright (C) 2021 OceanBase
#
# This file is part of OceanBase Deploy.
#
# OceanBase Deploy 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 3 of the License, or
# (at your option) any later version.
#
# OceanBase Deploy 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 OceanBase Deploy.  If not, see <https://www.gnu.org/licenses/>.


from __future__ import absolute_import, division, print_function

R
Rongfeng Fu 已提交
23
from tool import ConfigUtil
R
Rongfeng Fu 已提交
24

R
Rongfeng Fu 已提交
25 26

def generate_config(plugin_context, auto_depend=False, return_generate_keys=False, only_generate_password=False, generate_password=True, *args, **kwargs):
R
Rongfeng Fu 已提交
27
    if return_generate_keys:
R
Rongfeng Fu 已提交
28 29 30 31 32 33
        generate_keys = []
        if generate_password:
            generate_keys += ['http_basic_auth_password']
        if not only_generate_password:
            generate_keys += ['ob_monitor_status']
        return plugin_context.return_true(generate_keys=generate_keys)
R
Rongfeng Fu 已提交
34

R
Rongfeng Fu 已提交
35
    cluster_config = plugin_context.cluster_config
R
Rongfeng Fu 已提交
36 37 38 39 40
    if generate_password:
        generate_random_password(cluster_config)
    if only_generate_password:
        return plugin_context.return_true()

R
Rongfeng Fu 已提交
41 42 43 44
    stdio = plugin_context.stdio
    have_depend = False
    depends = ['oceanbase', 'oceanbase-ce']
    server_depends = {}
R
Rongfeng Fu 已提交
45 46
    generate_configs = {'global': {}}
    plugin_context.set_variable('generate_configs', generate_configs)
R
Rongfeng Fu 已提交
47 48 49 50 51 52
    stdio.start_loading('Generate obagent configuration')

    for comp in cluster_config.depends:
        if comp in depends:
            have_depend = True
            for server in cluster_config.servers:
R
Rongfeng Fu 已提交
53
                server_depends[server] = []
R
Rongfeng Fu 已提交
54
                obs_config = cluster_config.get_depend_config(comp, server)
R
Rongfeng Fu 已提交
55 56
                if obs_config is not None:
                    server_depends[server].append(comp)
R
Rongfeng Fu 已提交
57

R
Rongfeng Fu 已提交
58 59 60 61 62 63 64
    if have_depend:
        for server in cluster_config.servers:
            for comp in depends:
                if comp in server_depends[server]:
                    break
            else:
                cluster_config.update_server_conf(server, 'ob_monitor_status', 'inactive', False)
R
Rongfeng Fu 已提交
65
                generate_configs[server]['ob_monitor_status'] = 'inactive'
R
Rongfeng Fu 已提交
66 67
    else:
        cluster_config.update_global_conf('ob_monitor_status', 'inactive', False)
R
Rongfeng Fu 已提交
68
        generate_configs['global']['ob_monitor_status'] = 'inactive'
F
v1.6.0  
frf12 已提交
69 70 71 72
        if auto_depend:
            for depend in depends:
                if cluster_config.add_depend_component(depend):
                    cluster_config.update_global_conf('ob_monitor_status', 'active', False)
R
Rongfeng Fu 已提交
73
                    generate_configs['global']['ob_monitor_status'] = 'active'
F
v1.6.0  
frf12 已提交
74
                    break
R
Rongfeng Fu 已提交
75 76 77

    stdio.stop_loading('succeed')
    plugin_context.return_true()
R
Rongfeng Fu 已提交
78 79 80 81 82 83


def generate_random_password(cluster_config):
    global_config = cluster_config.get_original_global_conf()
    if 'http_basic_auth_password' not in global_config:
        cluster_config.update_global_conf('http_basic_auth_password', ConfigUtil.get_random_pwd_by_total_length())