start.py 11.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
# 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 os
import time
R
Rongfeng Fu 已提交
25
import hashlib
F
v1.5.0  
frf12 已提交
26
from copy import deepcopy
O
oceanbase-admin 已提交
27

R
Rongfeng Fu 已提交
28 29
import re

R
Rongfeng Fu 已提交
30 31
from _errno import EC_CONFLICT_PORT

O
oceanbase-admin 已提交
32 33 34 35 36
stdio = None


def get_port_socket_inode(client, port):
    port = hex(port)[2:].zfill(4).upper()
R
Rongfeng Fu 已提交
37
    cmd = "bash -c 'cat /proc/net/{tcp*,udp*}' | awk -F' ' '{print $2,$10}' | grep '00000000:%s' | awk -F' ' '{print $2}' | uniq" % port
O
oceanbase-admin 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    res = client.execute_command(cmd)
    if not res or not res.stdout.strip():
        return False
    stdio.verbose(res.stdout)
    return res.stdout.strip().split('\n')


def confirm_port(client, pid, port):
    socket_inodes = get_port_socket_inode(client, port)
    if not socket_inodes:
        return False
    ret = client.execute_command("ls -l /proc/%s/fd/ |grep -E 'socket:\[(%s)\]'" % (pid, '|'.join(socket_inodes)))
    if ret and ret.stdout.strip():
        return True
    return False


def confirm_command(client, pid, command):
    command = command.replace(' ', '').strip()
F
v1.5.0  
frf12 已提交
57
    if client.execute_command('bash -c \'cmd=`cat /proc/%s/cmdline`; if [ "$cmd" != "%s" ]; then exit 1; fi\'' % (pid, command)):
O
oceanbase-admin 已提交
58 59 60 61 62
        return True
    return False


def confirm_home_path(client, pid, home_path):
R
Rongfeng Fu 已提交
63
    if client.execute_command('path=`ls -l /proc/%s | grep cwd | awk -F\'-> \' \'{print $2}\'`; bash -c \'if [ "$path" != "%s" ]; then exit 1; fi\'' % (pid, home_path)):
O
oceanbase-admin 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        return True
    return False


def is_started(client, remote_bin_path, port, home_path, command):
    username = client.config.username
    ret = client.execute_command('pgrep -u %s -f "^%s"' % (username, remote_bin_path))
    if not ret:
        return False
    pids = ret.stdout.strip()
    if not pids:
        return False
    pids = pids.split('\n')
    for pid in pids:
        if confirm_port(client, pid, port):
            break
    else:
        return False
    return confirm_home_path(client, pid, home_path) and confirm_command(client, pid, command)

R
Rongfeng Fu 已提交
84

R
Rongfeng Fu 已提交
85 86 87 88 89 90 91 92 93
def obproxyd(home_path, client, ip, port):
    path = os.path.join(os.path.split(__file__)[0], 'obproxyd.sh')
    retmoe_path = os.path.join(home_path, 'obproxyd.sh')
    if os.path.exists(path):
        shell = '''bash %s %s %s %s''' % (retmoe_path, home_path, ip, port)
        return client.put_file(path, retmoe_path) and client.execute_command(shell)
    return False


F
v1.5.0  
frf12 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
class EnvVariables(object):

    def __init__(self, environments, client):
        self.environments = environments
        self.client = client
        self.env_done = {}

    def __enter__(self):
        for env_key, env_value in self.environments.items():
            self.env_done[env_key] = self.client.get_env(env_key)
            self.client.add_env(env_key, env_value, True)

    def __exit__(self, *args, **kwargs):
        for env_key, env_value in self.env_done.items():
            if env_value is not None:
                self.client.add_env(env_key, env_value, True)
            else:
                self.client.del_env(env_key)


R
Rongfeng Fu 已提交
114
def start(plugin_context, need_bootstrap=False, *args, **kwargs):
O
oceanbase-admin 已提交
115 116 117 118
    global stdio
    cluster_config = plugin_context.cluster_config
    clients = plugin_context.clients
    stdio = plugin_context.stdio
R
Rongfeng Fu 已提交
119
    options = plugin_context.options
O
oceanbase-admin 已提交
120 121 122 123
    clusters_cmd = {}
    real_cmd = {}
    pid_path = {}

R
Rongfeng Fu 已提交
124 125 126
    for comp in ['oceanbase', 'oceanbase-ce']:
        if comp in cluster_config.depends:
            root_servers = {}
R
Rongfeng Fu 已提交
127
            ob_config = cluster_config.get_depend_config(comp)
R
Rongfeng Fu 已提交
128 129 130
            if not ob_config:
                continue
            odp_config = cluster_config.get_global_conf()
R
Rongfeng Fu 已提交
131 132
            for server in cluster_config.get_depend_servers(comp):
                config = cluster_config.get_depend_config(comp, server)
R
Rongfeng Fu 已提交
133 134 135 136 137 138 139 140 141 142 143 144
                zone = config['zone']
                if zone not in root_servers:
                    root_servers[zone] = '%s:%s' % (server.ip, config['mysql_port'])
            depend_rs_list = ';'.join([root_servers[zone] for zone in root_servers])
            cluster_config.update_global_conf('rs_list', depend_rs_list, save=False)

            config_map = {
                'observer_sys_password': 'proxyro_password',
                'cluster_name': 'appname'
            }
            for key in config_map:
                ob_key = config_map[key]
R
Rongfeng Fu 已提交
145
                if key not in odp_config and ob_key in ob_config:
R
Rongfeng Fu 已提交
146 147 148
                    cluster_config.update_global_conf(key, ob_config.get(ob_key), save=False)
            break

O
oceanbase-admin 已提交
149 150 151 152 153 154 155 156 157 158
    error = False
    for server in cluster_config.servers:
        server_config = cluster_config.get_server_conf(server)
        if 'rs_list' not in server_config and 'obproxy_config_server_url' not in server_config:
            error = True
            stdio.error('%s need config "rs_list" or "obproxy_config_server_url"' % server)
    if error:
        return plugin_context.return_false()

    stdio.start_loading('Start obproxy')
R
Rongfeng Fu 已提交
159 160 161 162 163 164 165 166 167

    for server in cluster_config.servers:
        client = clients[server]
        server_config = cluster_config.get_server_conf(server)
        home_path = server_config['home_path']
        if not client.execute_command('ls %s/etc/obproxy_config.bin' % home_path):
            need_bootstrap = True
            break

R
Rongfeng Fu 已提交
168 169 170 171 172 173 174
    if getattr(options, 'without_parameter', False) and need_bootstrap is False:
        use_parameter = False
    else:
        # Bootstrap is required when starting with parameter, ensure the passwords are correct.
        need_bootstrap = True
        use_parameter = True

O
oceanbase-admin 已提交
175 176 177
    for server in cluster_config.servers:
        client = clients[server]
        server_config = cluster_config.get_server_conf(server)
R
Rongfeng Fu 已提交
178 179 180
        home_path = server_config['home_path']

        pid_path[server] = "%s/run/obproxy-%s-%s.pid" % (home_path, server.ip, server_config["listen_port"])
R
Rongfeng Fu 已提交
181 182 183 184 185 186 187 188

        if use_parameter:
            not_opt_str = [
                'listen_port',
                'prometheus_listen_port',
                'rs_list',
                'cluster_name'
            ]
R
Rongfeng Fu 已提交
189
            start_unuse = ['home_path', 'observer_sys_password', 'obproxy_sys_password', 'observer_root_password']
R
Rongfeng Fu 已提交
190
            get_value = lambda key: "'%s'" % server_config[key] if isinstance(server_config[key], str) else server_config[key]
R
Rongfeng Fu 已提交
191 192 193 194 195 196
            opt_str = []
            if server_config.get('obproxy_sys_password'):
                obproxy_sys_password = hashlib.sha1(server_config['obproxy_sys_password'].encode("utf-8")).hexdigest()
            else:
                obproxy_sys_password = ''
            opt_str.append("obproxy_sys_password='%s'" % obproxy_sys_password)
R
Rongfeng Fu 已提交
197 198 199 200 201 202 203 204 205 206 207 208
            for key in server_config:
                if key not in start_unuse and key not in not_opt_str:
                    value = get_value(key)
                    opt_str.append('%s=%s' % (key, value))
            cmd = ['-o %s' % ','.join(opt_str)]
            for key in not_opt_str:
                if key in server_config:
                    value = get_value(key)
                    cmd.append('--%s %s' % (key, value))
        else:
            cmd = ['--listen_port %s' % server_config.get('listen_port')]

R
Rongfeng Fu 已提交
209 210
        real_cmd[server] = '%s/bin/obproxy %s' % (home_path, ' '.join(cmd))
        clusters_cmd[server] = 'cd %s; %s' % (home_path, real_cmd[server])
O
oceanbase-admin 已提交
211 212

    for server in clusters_cmd:
F
v1.5.0  
frf12 已提交
213
        environments = deepcopy(cluster_config.get_environments())
O
oceanbase-admin 已提交
214 215 216 217 218 219 220 221
        client = clients[server]
        server_config = cluster_config.get_server_conf(server)
        port = int(server_config["listen_port"])
        prometheus_port = int(server_config["prometheus_listen_port"])
        stdio.verbose('%s port check' % server)
        remote_pid = client.execute_command("cat %s" % pid_path[server]).stdout.strip()
        cmd = real_cmd[server].replace('\'', '')
        if remote_pid:
R
Rongfeng Fu 已提交
222
            ret = client.execute_command('ls /proc/%s/' % remote_pid)
O
oceanbase-admin 已提交
223
            if ret:
R
Rongfeng Fu 已提交
224
                if confirm_port(client, remote_pid, port):
O
oceanbase-admin 已提交
225 226
                    continue
                stdio.stop_loading('fail')
R
Rongfeng Fu 已提交
227
                stdio.error(EC_CONFLICT_PORT.format(server=server.ip, port=port))
O
oceanbase-admin 已提交
228 229 230
                return plugin_context.return_false()

        stdio.verbose('starting %s obproxy', server)
F
v1.5.0  
frf12 已提交
231 232 233 234
        if 'LD_LIBRARY_PATH' not in environments:
            environments['LD_LIBRARY_PATH'] = '%s/lib:' % server_config['home_path']
        with EnvVariables(environments, client):
            ret = client.execute_command(clusters_cmd[server])
O
oceanbase-admin 已提交
235 236 237 238
        if not ret:
            stdio.stop_loading('fail')
            stdio.error('failed to start %s obproxy: %s' % (server, ret.stderr))
            return plugin_context.return_false()
R
Rongfeng Fu 已提交
239
        client.execute_command('''ps -aux | grep -e '%s$' | grep -v grep | awk '{print $2}' > %s''' % (cmd, pid_path[server]))
O
oceanbase-admin 已提交
240 241 242 243
    stdio.stop_loading('succeed')
        
    stdio.start_loading('obproxy program health check')
    failed = []
R
Rongfeng Fu 已提交
244
    servers = cluster_config.servers
R
Rongfeng Fu 已提交
245
    count = 20
R
Rongfeng Fu 已提交
246 247 248 249 250 251 252 253 254
    while servers and count:
        count -= 1
        tmp_servers = []
        for server in servers:
            server_config = cluster_config.get_server_conf(server)
            client = clients[server]
            stdio.verbose('%s program health check' % server)
            remote_pid = client.execute_command("cat %s" % pid_path[server]).stdout.strip()
            if remote_pid:          
R
Rongfeng Fu 已提交
255
                for pid in re.findall('\d+',remote_pid):
R
Rongfeng Fu 已提交
256 257
                    confirm = confirm_port(client, pid, int(server_config["listen_port"]))
                    if confirm:
R
Rongfeng Fu 已提交
258 259 260 261 262 263 264
                        proxyd_Pid_path = os.path.join(server_config["home_path"], 'run/obproxyd-%s-%d.pid' % (server.ip, server_config["listen_port"]))
                        if client.execute_command("pid=`cat %s` && ls /proc/$pid" % proxyd_Pid_path):
                            stdio.verbose('%s obproxy[pid: %s] started', server, pid)
                        else:
                            client.execute_command('echo %s > %s' % (pid, pid_path[server]))
                            obproxyd(server_config["home_path"], client, server.ip, server_config["listen_port"])
                            tmp_servers.append(server)
R
Rongfeng Fu 已提交
265 266 267 268 269 270 271
                        break
                    stdio.verbose('failed to start %s obproxy, remaining retries: %d' % (server, count))
                    if count:
                        tmp_servers.append(server)
                    else:
                        failed.append('failed to start %s obproxy' % server)
            else:
O
oceanbase-admin 已提交
272
                failed.append('failed to start %s obproxy' % server)
R
Rongfeng Fu 已提交
273 274 275
        servers = tmp_servers
        if servers and count:
            time.sleep(1)
O
oceanbase-admin 已提交
276 277 278 279 280 281 282
    if failed:
        stdio.stop_loading('fail')
        for msg in failed:
            stdio.warn(msg)
        plugin_context.return_false()
    else:
        stdio.stop_loading('succeed')
R
Rongfeng Fu 已提交
283
        plugin_context.return_true(need_bootstrap=need_bootstrap)