connect.py 3.0 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 45
        cursor = db.cursor()
    return db, cursor


def connect(plugin_context, target_server=None, sys_root=True, *args, **kwargs):
R
Rongfeng Fu 已提交
46
    global stdio
O
oceanbase-admin 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    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')
    if sys_root:
        user = 'root@proxysys'
    else:
        user = 'root'
    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 已提交
69 70
                pwd_key = 'obproxy_sys_password' if sys_root else 'observer_sys_password'
                db, cursor = _connect(server.ip, server_config['listen_port'], user, server_config.get(pwd_key, '') if count % 2 else '')
O
oceanbase-admin 已提交
71 72 73 74 75 76 77 78
                dbs[server] = db
                cursors[server] = cursor
            except:
                tmp_servers.append(server)
                pass
        servers = tmp_servers
        servers and time.sleep(3)
    
R
Rongfeng Fu 已提交
79
    if  servers:
O
oceanbase-admin 已提交
80 81 82 83 84 85 86 87
        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)