예제 #1
0
파일: scale.py 프로젝트: nrc/tiup
class OprScaleOut(OperationBase):
    def __init__(self, args=None, topology=None, new_srvs=None):
        if os.path.exists(topology.topology_file):
            term.warn(
                'Check TiDB cluster {} status, it may take a few minutes.'.
                format(topology.cluster_name))
            self.check_tombstone(topology, args)
        self._new_topo, self._diff = topology.add(new_srvs)
        topology.replace(self._new_topo, write=False)
        super(OprScaleOut, self).__init__(args, topology, action='deploy')
        self.act = Action(ans=self.ans, topo=self.topology)

    def _prepare(self, component=None, pattern=None, node=None, role=None):
        if not self._diff:
            msg = 'No new nodes to scale out.'
            term.error(msg)
            raise exceptions.TiOPSConfigError(msg)
        term.notice('Begin add node for TiDB cluster.')

        # copy template
        utils.create_dir(self.topology.cache_template_dir)
        utils.copy_template(source=os.path.join(self.topology.titemplate_dir),
                            target=os.path.join(
                                self.topology.cache_template_dir))

        # update scripts when scale-out.
        for service in ['pd', 'tikv', 'tidb', 'pump', 'drainer']:
            if '{}_servers'.format(service) in self._diff:
                template_path = os.path.join(
                    self.topology.cache_template_dir,
                    'scripts/run_{}.sh.j2'.format(service))
                _original, new_template = utils.script_template(
                    path=self.topology.cluster_dir,
                    template=template_path,
                    service=service)
                utils.write_template(template_path, new_template)

    def _process(self, component=None, pattern=None, node=None, role=None):
        term.info('Check ssh connection.')
        self.act.check_ssh_connection()
        self.act.edit_file()
        try:
            term.info('Create directory in all add nodes.')
            for service in self.topology.service_group:
                component, pattern = self.check_exist(service, self._diff)
                if not component and not pattern:
                    continue
                uuid = [x['uuid'] for x in self._diff[pattern]]
                self.act.create_directory(component=component,
                                          pattern=pattern,
                                          node=','.join(uuid))

            # check machine cpu / memory / disk
            self.act.check_machine_config(self._diff)
            # start run scale-out
            for service in self.topology.service_group:
                component, pattern = self.check_exist(service, self._diff)
                if not component and not pattern:
                    continue
                uuid = [x['uuid'] for x in self._diff[pattern]]
                term.normal('Add {}, node list: {}.'.format(
                    component, ','.join(uuid)))
                _template_dir = self.topology.cache_template_dir
                self.act.deploy_component(component=component,
                                          pattern=pattern,
                                          node=','.join(uuid),
                                          template_dir=_template_dir)
                self.act.deploy_firewall(component=component,
                                         pattern=pattern,
                                         node=','.join(uuid))
                self.act.start_component(component=component,
                                         pattern=pattern,
                                         node=','.join(uuid))
        finally:
            os.popen('rm -rf {}'.format(self.topology.cache_template_dir))

    def _post(self, component=None, pattern=None, node=None, role=None):
        # if 'pd_servers' in self._diff:
        #    reload_pd = True
        # else:
        #    reload_pd = False
        self.topology.replace(self._new_topo)
        term.info('Update configuration.')
        ans = ansibleapi.ANSRunner(user=self.topology.user,
                                   topology=self.topology._topology(
                                       self._new_topo),
                                   tiargs=self._args)
        act = Action(ans=ans, topo=self.topology)
        if 'pd_servers' in self._diff:
            act.deploy_component(component='pd', pattern='pd_servers')
            act.deploy_component(component='tikv', pattern='tikv_servers')
            act.deploy_component(component='tidb', pattern='tidb_servers')
            act.deploy_component(component='pump', pattern='pump_servers')
            act.deploy_component(component='drainer',
                                 pattern='drainer_servers')

        act.deploy_component(component='prometheus',
                             pattern='monitoring_server')
        act.stop_component(component='prometheus', pattern='monitoring_server')
        act.start_component(component='prometheus',
                            pattern='monitoring_server')
        term.notice('Finished scaling out.')
예제 #2
0
class OprDeploy(OperationBase):
    def __init__(self, args=None, topology=None, demo=False):
        super(OprDeploy, self).__init__(args, topology, demo=demo)
        self.act = Action(ans=self.ans, topo=self.topology)
        self.demo = demo

    def _check_config(self):
        _servers = [
            {
                'pd': 'pd_servers'
            },
            {
                'tikv': 'tikv_servers'
            },
            {
                'tidb': 'tidb_servers'
            },
        ]

        for _service in _servers:
            _component, _pattern = self.check_exist(_service,
                                                    config=self.topology())
            if not _component and not _pattern:
                continue
            term.normal('Check {} configuration.'.format(_component))
            self.act.configCheck(component=_component,
                                 pattern=_pattern,
                                 node=self.topology()[_pattern][0]['uuid'])

    def _prepare(self, component=None, pattern=None, node=None, role=None):
        if self.topology.version and self._args.tidb_version:
            new_ver = self._args.tidb_version.lstrip('v')
            curr_ver = self.topology.version.lstrip('v')
            _cmp = semver.compare(curr_ver, new_ver)
            if _cmp > 0:
                raise exceptions.TiOPSArgumentError(
                    'Running version is {}, can\'t downgrade.'.format(
                        curr_ver))

        term.notice('Begin installing TiDB cluster.')
        # download packages
        term.info(
            'Downloading TiDB related binary, it may take a few minutes.')
        try:
            _local = self._args.local_pkg
        except AttributeError:
            _local = None
        self.act.download(local_pkg=_local)

        if not self.demo:
            # edit config
            self.act.edit_file()

            term.info('Check ssh connection.')
            self.act.check_ssh_connection()

            if self._args.enable_check_config:
                self._check_config()

    def _process(self, component=None, pattern=None, node=None, role=None):
        # creart directory
        term.info('Create directory in all nodes.')
        for service in self.topology.service_group:
            component, pattern = self.check_exist(service,
                                                  config=self.topology())
            if not component and not pattern:
                continue
            self.act.create_directory(component=component, pattern=pattern)

        if not self.demo:
            self.act.check_machine_config()

        # start run deploy
        if self.demo:
            term.warn(
                'FirewallD is being disabled on deployment machines in quick deploy mode.'
            )
        for service in self.topology.service_group:
            component, pattern = self.check_exist(service,
                                                  config=self.topology())
            if not component and not pattern:
                continue
            term.normal('Deploy {}.'.format(component))
            self.act.deploy_component(component=component, pattern=pattern)
            self.act.deploy_firewall(component=component, pattern=pattern)

        if not self.demo:
            self.act.deploy_tool()

    def _post(self, component=None, pattern=None, node=None, role=None):
        self.topology.set_meta()
        self.topology._save_topology()
        if self.demo:
            term.notice('Finished deploying TiDB cluster {} ({}).'.format(
                self.topology.cluster_name, self.topology.version))
        else:
            term.notice(
                'Finished deploying TiDB cluster {} ({}), don\'t forget to start it.'
                .format(self.topology.cluster_name, self.topology.version))