def __req(self, url=None, method='GET'): if not url: raise exceptions.TiOPSArgumentError('No URL specified to request.') resp, code = utils.request_url(url, method=method) if not code or not (code >= 200 and code < 400): raise exceptions.TiOPSRequestError(url, code, resp) return resp
def __init__(self, host=None, port=None, tls=False): if not host: raise exceptions.TiOPSArgumentError('Host must be set.') else: self.host = host self.port = port if port else self._default_pd_client_port self.scheme = 'https' if tls else 'http'
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 __check_version(self): new_ver = self.arg_ver.lstrip('v') curr_ver = self.topology.version.lstrip('v') _cmp = semver.compare(curr_ver, new_ver) if _cmp == 0: raise exceptions.TiOPSArgumentError( 'Already running version {}.'.format(curr_ver)) elif _cmp > 0: raise exceptions.TiOPSRuntimeError( 'Downgrade is not supported, keep running {}.'.format(curr_ver), operation='upgrade') # update version and related variables self.old_ver = curr_ver self.new_ver = new_ver self.topology.version = 'v{}'.format(new_ver) self.topology.tiversion_dir = os.path.join( self.topology.tidown_dir, '{}'.format(self.topology.version)) self.topology.resource_dir = utils.profile_path( 'downloads', '{}/resources'.format(self.topology.version)) self.topology.dashboard_dir = utils.profile_path( 'downloads', '{}/dashboards'.format(self.topology.version)) self.topology.package_dir = utils.profile_path( 'downloads', '{}/packages'.format(self.topology.version)) self.topology.config_dir = utils.profile_path( 'downloads', '{}/configs'.format(self.topology.version))
def _prepare(self, component=None, pattern=None, node=None, role=None): try: self.cmd = ' '.join(self._args.cmd) except AttributeError: raise exceptions.TiOPSArgumentError( 'No command specified, do nothing.') term.notice('Run raw shell command on {} cluster.'.format( self.topology.cluster_name)) term.normal('{}'.format(self.cmd))
def __init__(self, topology=None): if not topology: raise exceptions.TiOPSAttributeError( 'Topology is empty, can not get cluster info.') else: self.config = topology() try: pd_node = self.config['pd_servers'][0] except KeyError: raise exceptions.TiOPSArgumentError( 'Topolgoy does not contain any PD node.') try: tls = topology.enable_tls except AttributeError: tls = False super(ClusterAPI, self).__init__(pd_node['ip'], pd_node['client_port'], tls)
def evict_pd_leader(self, uuid=None): if not uuid: raise exceptions.TiOPSArgumentError( 'UUID is not set, can not transfer leader from None.') _members = self.__req(self.__url(self.uris['members'])) if len(json.loads(_members)['members']) == 1: # force continue when there is only one PD logging.warning('Only 1 PD node, skip leader transfer.') return while True: curr_leader = self.pd_leader() if uuid != curr_leader: return _url = '{}/resign'.format(self.__url(self.uris['leader'])) try: utils.request_url(_url, method='POST') except Exception as e: logging.warning('Failed to transfer PD leader: {}'.format(e)) pass utils.wait(5)
def __init__(self, host=None, port=None): if not host: raise exceptions.TiOPSArgumentError('Host must be set.') else: self.host = host self.port = port if port else self._default_tidb_status_port
def del_store(self, id=None): if not id: raise exceptions.TiOPSArgumentError( 'Store ID is not set, can not delete store.') _url = '{}/{}'.format(self.__url(self.uris['store']), id) self.__req(_url, method='DELETE')
def del_pd(self, uuid=None): if not uuid: raise exceptions.TiOPSArgumentError( 'UUID is not set, can not delete PD node.') _url = '{}/name/{}'.format(self.__url(self.uris['members']), uuid) self.__req(_url, method='DELETE')