restart.py 6.6 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 23 24 25 26 27 28

# 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


class Restart(object):

R
Rongfeng Fu 已提交
29
    def __init__(self, plugin_context, local_home_path, start_plugin, reload_plugin, stop_plugin, connect_plugin, display_plugin, repository, new_cluster_config=None, new_clients=None, bootstrap_plugin=None):
R
Rongfeng Fu 已提交
30
        self.local_home_path = local_home_path
R
Rongfeng Fu 已提交
31 32 33 34 35 36 37

        self.namespace = plugin_context.namespace
        self.namespaces = plugin_context.namespaces
        self.deploy_name = plugin_context.deploy_name
        self.repositories = plugin_context.repositories
        self.plugin_name = plugin_context.plugin_name

R
Rongfeng Fu 已提交
38 39 40
        self.components = plugin_context.components
        self.clients = plugin_context.clients
        self.cluster_config = plugin_context.cluster_config
R
Rongfeng Fu 已提交
41 42 43
        self.cmds = plugin_context.cmds
        self.options = plugin_context.options
        self.dev_mode = plugin_context.dev_mode
R
Rongfeng Fu 已提交
44
        self.stdio = plugin_context.stdio
R
Rongfeng Fu 已提交
45 46

        self.plugin_context = plugin_context
R
Rongfeng Fu 已提交
47 48 49 50 51
        self.repository = repository
        self.start_plugin = start_plugin
        self.reload_plugin = reload_plugin
        self.connect_plugin = connect_plugin
        self.display_plugin = display_plugin
R
Rongfeng Fu 已提交
52
        self.bootstrap_plugin = bootstrap_plugin
R
Rongfeng Fu 已提交
53 54 55 56 57 58
        self.stop_plugin = stop_plugin
        self.new_clients = new_clients
        self.new_cluster_config = new_cluster_config
        self.sub_io = self.stdio.sub_io()
        self.dbs = None
        self.cursors = None
R
Rongfeng Fu 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    
    def call_plugin(self, plugin, **kwargs):
        args = {
            'namespace': self.namespace,
            'namespaces': self.namespaces,
            'deploy_name': self.deploy_name,
            'cluster_config': self.cluster_config,
            'repositories': self.repositories,
            'repository': self.repository,
            'components': self.components,
            'clients': self.clients,
            'cmd': self.cmds,
            'options': self.options,
            'stdio': self.sub_io
        }
        args.update(kwargs)
        
        self.stdio.verbose('Call %s for %s' % (plugin, self.repository))
        return plugin(**args)
R
Rongfeng Fu 已提交
78

F
v1.6.0  
frf12 已提交
79 80
    def connect(self, cluster_config):
        self.sub_io.start_loading('Connect to obproxy')
R
Rongfeng Fu 已提交
81
        ret = self.call_plugin(self.connect_plugin, cluster_config=cluster_config)
F
v1.6.0  
frf12 已提交
82 83 84 85 86 87 88
        if not ret:
            self.sub_io.stop_loading('fail')
            return False
        self.sub_io.stop_loading('succeed')
        # self.close()
        self.cursors = ret.get_return('cursor')
        self.dbs = ret.get_return('connect')
R
Rongfeng Fu 已提交
89 90 91 92 93 94 95 96 97 98
        return True

    def dir_read_check(self, client, path):
        if not client.execute_command('cd %s' % path):
            dirpath, name = os.path.split(path)
            return self.dir_read_check(client, dirpath) and client.execute_command('sudo chmod +1 %s' % path)
        return True

    def restart(self):
        clients = self.clients
F
v1.6.0  
frf12 已提交
99 100 101
        if self.new_cluster_config:
            if not self.connect(self.cluster_config):
                return False
R
Rongfeng Fu 已提交
102
            self.call_plugin(self.reload_plugin, clients=clients, cursor=self.cursors, new_cluster_config=self.new_cluster_config)
F
v1.6.0  
frf12 已提交
103

R
Rongfeng Fu 已提交
104
        if not self.call_plugin(self.stop_plugin, clients=clients):
R
Rongfeng Fu 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
            self.stdio.stop_loading('stop_loading', 'fail')
            return False
        
        if self.new_clients:
            self.stdio.verbose('use new clients')
            for server in self.cluster_config.servers:
                new_client = self.new_clients[server]
                server_config = self.cluster_config.get_server_conf(server)
                home_path = server_config['home_path']
                if not new_client.execute_command('sudo chown -R %s: %s' % (new_client.config.username, home_path)):
                    self.stdio.stop_loading('stop_loading', 'fail')
                    return False
                self.dir_read_check(new_client, home_path)
            clients = self.new_clients

        cluster_config = self.new_cluster_config if self.new_cluster_config else self.cluster_config
R
Rongfeng Fu 已提交
121
        need_bootstrap = self.bootstrap_plugin is not None
R
Rongfeng Fu 已提交
122
        if not self.call_plugin(self.start_plugin, clients=clients, cluster_config=cluster_config, local_home_path=self.local_home_path, repository=self.repository, need_bootstrap=need_bootstrap):
R
Rongfeng Fu 已提交
123 124 125 126
            self.rollback()
            self.stdio.stop_loading('stop_loading', 'fail')
            return False
        
F
v1.6.0  
frf12 已提交
127
        if self.connect(cluster_config):
R
Rongfeng Fu 已提交
128
            if self.bootstrap_plugin:
R
Rongfeng Fu 已提交
129 130
                self.call_plugin(self.bootstrap_plugin, cursor=self.cursors)
            return self.call_plugin(self.display_plugin, cursor=self.cursors)
R
Rongfeng Fu 已提交
131 132 133 134
        return False

    def rollback(self):
        if self.new_clients:
R
Rongfeng Fu 已提交
135 136
            cluster_config = self.new_cluster_config if self.new_cluster_config else self.cluster_config
            self.call_plugin(self.stop_plugin, clients=self.new_clients, cluster_config=cluster_config)
R
Rongfeng Fu 已提交
137 138 139 140 141 142 143 144
            for server in self.cluster_config.servers:
                client = self.clients[server]
                new_client = self.new_clients[server]
                server_config = self.cluster_config.get_server_conf(server)
                home_path = server_config['home_path']
                new_client.execute_command('sudo chown -R %s: %s' % (client.config.username, home_path))


R
Rongfeng Fu 已提交
145 146
def restart(plugin_context, local_home_path, start_plugin, reload_plugin, stop_plugin, connect_plugin, display_plugin, new_cluster_config=None, new_clients=None, rollback=False, bootstrap_plugin=None, *args, **kwargs):
    repository = kwargs.get('repository')
R
Rongfeng Fu 已提交
147
    task = Restart(plugin_context, local_home_path, start_plugin, reload_plugin, stop_plugin, connect_plugin, display_plugin, repository, new_cluster_config, new_clients, bootstrap_plugin=bootstrap_plugin)
R
Rongfeng Fu 已提交
148 149 150
    call = task.rollback if rollback else task.restart
    if call():
        plugin_context.return_true()