connect.py 4.4 KB
Newer Older
O
oceanbase-admin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
# 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

import sys
import time
if sys.version_info.major == 2:
    import MySQLdb as mysql
else:
    import pymysql as mysql


R
Rongfeng Fu 已提交
31 32 33 34 35
stdio = None


def _connect(ip, port, user, password=''):
    stdio.verbose('connect %s -P%s -u%s -p%s' % (ip, port, user, password))
O
oceanbase-admin 已提交
36
    if sys.version_info.major == 2:
R
Rongfeng Fu 已提交
37
        db = mysql.connect(host=ip, user=user, port=int(port), passwd=str(password))
O
oceanbase-admin 已提交
38 39
        cursor = db.cursor(cursorclass=mysql.cursors.DictCursor)
    else:
R
Rongfeng Fu 已提交
40
        db = mysql.connect(host=ip, user=user, port=int(port), password=str(password), cursorclass=mysql.cursors.DictCursor)
O
oceanbase-admin 已提交
41 42 43 44
        cursor = db.cursor()
    return db, cursor


R
Rongfeng Fu 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57
def execute(cursor, query, args=None):
    msg = query % tuple(args) if args is not None else query
    stdio.verbose('execute sql: %s' % msg)
    # stdio.verbose("query: %s. args: %s" % (query, args))
    try:
        cursor.execute(query, args)
        return cursor.fetchone()
    except:
        msg = 'execute sql exception: %s' % msg
        stdio.exception(msg)
        raise Exception(msg)


O
oceanbase-admin 已提交
58
def connect(plugin_context, target_server=None, sys_root=True, *args, **kwargs):
R
Rongfeng Fu 已提交
59
    global stdio
O
oceanbase-admin 已提交
60 61 62 63 64 65 66 67 68 69
    count = 10
    cluster_config = plugin_context.cluster_config
    stdio = plugin_context.stdio
    if target_server:
        servers = [target_server]
        server_config = cluster_config.get_server_conf(target_server)
        stdio.start_loading('Connect obproxy(%s:%s)' % (target_server, server_config['listen_port']))
    else:
        servers = cluster_config.servers
        stdio.start_loading('Connect to obproxy')
R
Rongfeng Fu 已提交
70 71 72 73 74 75 76 77 78 79
    user = kwargs.get('user')
    password = kwargs.get('password')
    if not user:
        if sys_root:
            user = 'root@proxysys'
        else:
            user = 'root'

    for comp in ['oceanbase', 'oceanbase-ce']:
        if comp in cluster_config.depends:
R
Rongfeng Fu 已提交
80
            ob_config = cluster_config.get_depend_config(comp)
R
Rongfeng Fu 已提交
81 82 83 84 85 86
            if not ob_config:
                continue
            odp_config = cluster_config.get_global_conf()
            config_map = {
                'observer_sys_password': 'proxyro_password',
                'cluster_name': 'appname',
R
Rongfeng Fu 已提交
87
                'observer_root_password': 'root_password'
R
Rongfeng Fu 已提交
88 89 90 91 92 93
            }
            for key in config_map:
                ob_key = config_map[key]
                if key not in odp_config and ob_key in ob_config:
                    cluster_config.update_global_conf(key, ob_config.get(ob_key), save=False)
            break
O
oceanbase-admin 已提交
94 95 96 97 98 99 100 101
    dbs = {}
    cursors = {}
    while count and servers:
        count -= 1
        tmp_servers = []
        for server in servers:
            try:
                server_config = cluster_config.get_server_conf(server)
R
Rongfeng Fu 已提交
102 103 104 105 106 107 108
                if sys_root:
                    pwd_key = 'obproxy_sys_password'
                else:
                    pwd_key = 'observer_root_password'
                r_password = password if password else server_config.get(pwd_key)
                if r_password is None:
                    r_password = ''
R
Rongfeng Fu 已提交
109
                db, cursor = _connect(server.ip, server_config['listen_port'], user, r_password if count % 2 else '')
O
oceanbase-admin 已提交
110 111 112 113 114 115 116 117
                dbs[server] = db
                cursors[server] = cursor
            except:
                tmp_servers.append(server)
                pass
        servers = tmp_servers
        servers and time.sleep(3)
    
R
Rongfeng Fu 已提交
118
    if servers:
O
oceanbase-admin 已提交
119 120 121 122 123 124 125 126
        stdio.stop_loading('fail')
        return plugin_context.return_false()
    else:
        stdio.stop_loading('succeed')
        if target_server:
            return plugin_context.return_true(connect=dbs[target_server], cursor=cursors[target_server])
        else:
            return plugin_context.return_true(connect=dbs, cursor=cursors)