def login(self, ip=None): """ # 登陆 /v1/authentication/login """ domain = self.domain if ip is None else "%s:%s" % (ip, MANAGER_HOST_PORT) url = "http://" + domain + '/v1/authentication/login' data = {"username": self.username, "password": getMD5(self.password)} response = self.do_post(url, data, '', domain=domain) #response.log('VeSpaceClient.login') if response.success: data = response.json_data() if data is None or not isinstance(data, dict): raise InternalException( 'VeSpaceClient.login data parse to json fail.', VESPACE_RESPONSE_DATA_INVALID_ERR) if data.get('ecode') != 0: raise InternalException( 'VeSpaceClient.login fail as[%s].' % (data.get('message')), VESPACE_RESPONSE_DATA_INVALID_ERR) self.token = data.get('data', {}).get('token') self._cache_cluster_info(data.get('data', {}).get('clusters')) self.token_expire_time = NowMilli() + TOKEN_EXPIRE_TIME else: raise InternalException( 'VeSpaceClient.login fail,as[%s].' % (response['body']), CALL_VESPACE_INSTERFACE_FAIL_ERROR)
def _call_remote(self, current_domain, url, method, token, post_data, retry=False, **args): if token is None: token = self.get_token(current_domain) Headers = self.getBasicHeaders(token, **args) Headers.append(HEADER_HOST + ": " + current_domain) response = self.send_http_request(url, Headers, method, post_data) Log( 4, "HA _call_remote[%s][%s]return[%s]" % (method, url, response.respond_body)) if response.success: data = response.json_data() if data and data.get('ecode') == 2799: raise InternalException(data.get('message'), 2799) return response
def get_cert_path(self): workdir = os.path.dirname(os.path.abspath(__file__)) workdir = os.path.join(workdir,"ssl") filepath = os.path.join(workdir,"server.crt") if os.path.isfile(filepath): return filepath raise InternalException("cert not exist", CERT_NOT_EXIST_ERR)
def is_service_ready(self): if CluNodedb.instance().is_node_exist(self.cluster_name, self.ip): return self.client.test_storage_service(self.ip) \ and self.client.test_application_service(self.ip) else: Log(1, 'The host[%s][%s] lost' % (self.cluster_name, self.ip)) raise InternalException("host deleted.", TASK_CANCEL_ERR)
def get_logger(self, logger_name, log_home, **args): log_name = args.get('log_name', "%s.log" % (logger_name)) log_level = args.get('log_level', logging.DEBUG) log_size = args.get('log_size', 10240000) backupcount = args.get('backupcount', 10) log_path = os.path.join(log_home, log_name) if not os.path.isfile(log_path): try: f = open(log_path, "a") f.close() except Exception as e: traceback.print_exc() raise InternalException("init logging fail: " + str(e)) formatter = logging.Formatter( "%(asctime)-15s %(levelname)s [%(process)d-%(thread)d] %(message)s" ) handler = handlers.RotatingFileHandler(log_path, "a", log_size, backupcount) handler.setFormatter(formatter) logger = logging.getLogger(logger_name) logger.addHandler(handler) logger.setLevel(log_level) return logger
def send_http_request(self, thisURL, thisArrHeader, thisHttpMethod, thisHttpBody=""): try: ch = "" ch = pycurl.Curl() ch.setopt(pycurl.URL, str(thisURL)) ch.setopt(pycurl.HTTPHEADER, thisArrHeader) ch.respondheader = cStringIO.StringIO() ch.setopt(pycurl.HEADERFUNCTION, ch.respondheader.write) ch.setopt(pycurl.CONNECTTIMEOUT, 30) ch.setopt(pycurl.TIMEOUT, 30) ch.setopt(pycurl.NOSIGNAL, 1) # ch.setopt( pycurl.VERBOSE, self.debug ) # c.setopt(pycurl.UPLOAD,1) # try: # c.setopt(pycurl.READFUNCTION, open(self.file_path, 'rb').read) # filesize = os.path.getsize(self.file_path) # c.setopt(pycurl.INFILESIZE, filesize) # # c.response = cStringIO.StringIO() # c.setopt(c.WRITEFUNCTION, c.response.write) # except: # c.close() # raise CBSError("Open file <"+self.file_path+"> error!") if (thisHttpMethod == PUT or thisHttpMethod == POST) and thisHttpBody != "": handle = cStringIO.StringIO() handle.write(thisHttpBody) handle.seek(0) size = len(thisHttpBody) ch.setopt(pycurl.UPLOAD, True) ch.setopt(pycurl.READFUNCTION, handle.read) ch.setopt(pycurl.INFILESIZE, size) ch.setopt(pycurl.CUSTOMREQUEST, thisHttpMethod) if thisHttpMethod == HEAD: # ch.setopt( pycurl.HEADER, True ) ch.setopt(pycurl.NOBODY, True) else: ch.response = cStringIO.StringIO() ch.setopt(ch.WRITEFUNCTION, ch.response.write) ch.perform() respond_http_status_code = ch.getinfo(pycurl.HTTP_CODE) respond_headers_array = ch.respondheader.getvalue() if thisHttpMethod == HEAD: respond_body = respond_headers_array else: respond_body = ch.response.getvalue() return Response(respond_body, respond_headers_array, respond_http_status_code) except pycurl.error, e: raise InternalException(e[1], e[0])
def init_default(self): try: Logger.syslogger = self.get_logger("system", self.workdir) Logger.operlogger = self.get_logger("operation", self.workdir) Logger.weblogger = self.get_logger("web", self.workdir) except Exception, e: traceback.print_exc() raise InternalException("init_default fail as[%s]" % (str(e)))
def init_with_config(self, config_path): try: logging.config.fileConfig(config_path) Logger.operlogger = logging.getLogger("operation") Logger.syslogger = logging.getLogger("system") Logger.weblogger = logging.getLogger("web") except Exception, e: traceback.print_exc() raise InternalException("init_with_config fail as[%s]" % (str(e)))
def load_private_key(self): workdir = os.path.dirname(os.path.abspath(__file__)) workdir = os.path.join(workdir,"ssl") filepath = os.path.join(workdir,"server.key") if not os.path.isfile(filepath): raise InternalException("private key not exist", CERT_NOT_EXIST_ERR) with open(filepath) as keyfile: self.key = keyfile.read()
def is_service_ready(self): if Clusterdb.instance().clu_is_exist(self.cluster_name): return self.client and self.client.test() \ and self.client.test_strategy_service(self.ip0) \ and (self.client.test_strategy_service(self.ip1) if self.ip1 else True) \ and (self.client.test_strategy_service(self.ip2) if self.ip2 else True) else: Log(1, 'The cluster[%s]lost' % (self.cluster_name)) raise InternalException("cluster deleted.", TASK_CANCEL_ERR)
def is_volume_exist(self): if StorageClassDB.instance().is_storage_class_exist( self.cluster_name, self.storage_class_name): return True else: Log( 1, 'The storage class[%s][%s] lost' % (self.cluster_name, self.storage_class_name)) raise InternalException("storage_class_name deleted.", TASK_CANCEL_ERR)
def get_cluster_id(self): if self.cluster_id: return self.cluster_id rlt = StoregeClusterDB.instance().get_cluster_info(self.cluster_name) if not rlt.success: Log(1, 'DeleteStorageNodeWork.get_cluster_id get_cluster_info[%s][%s]fail,as[%s]'%(self.cluster_name, self.ip, rlt.message)) raise InternalException("get_cluster_info[%s] fail,as[%s]."%(self.cluster_name, rlt.message), rlt.result) self.cluster_id = rlt.content.get('cluster_id') return self.cluster_id
def run(self): try: self.set_process() self.pre_work() return self.process() except Exception, e: PrintStack() msg = "Task do pre work fail,as[%s]" % (str(e)) Log(1, msg) self.log(msg) self.set_fail(msg, UNCAUGHT_EXCEPTION_ERR) raise InternalException(msg)
def get_logger(self, logger_name, log_home, **args): log_name = args.get('log_name', "%s.log" % (logger_name)) log_level = args.get('log_level', logging.DEBUG) log_size = args.get('log_size', 10240000) backupcount = args.get('backupcount', 10) log_path = os.path.join(log_home, log_name) if not os.path.isfile(log_path): try: f = open(log_path, "a") f.close() except Exception, e: traceback.print_exc() raise InternalException("init logging fail: " + str(e), FILE_OPERATE_ERR)
def upgrade(): current = SettingMgr.instance().get_version() if APP_VERSION == current: SysLog(3, 'upgrade.current is latest version [%s]' % (current)) return if current is None: Log(1, 'upgrade.get_version fail.') raise InternalException('upgrade.get_version fail.') file_name = 'upgrade' + current handler = GetDynamicClass('UpgradeHandler', file_name, 'upgrade') if handler: u = handler() return u.upgrade() else: SysLog(3, 'no support this version[%s] upgrade' % (current)) return Result(current)
def is_volume_exist(self): if PVDB.instance().is_volume_exist(self.cluster_name, self.pv_name): return True else: Log(1, 'The volume[%s][%s] lost'%(self.cluster_name, self.pv_name)) raise InternalException("pv deleted.", TASK_CANCEL_ERR)
def is_service_ready(self): if StoregeClusterDB.instance().is_cluster_exist(self.cluster_name): return True else: Log(1, 'The cluster[%s]lost' % (self.cluster_name)) raise InternalException("cluster deleted.", TASK_CANCEL_ERR)
def getSystemLog(self): if not os.path.exists(self.configure["system_log"]): raise InternalException("operation log file does not exist", FILE_OPERATE_ERR) logs = open(self.configure["system_log"]).readlines() return logs
def is_service_ready(self): if StorageNodeDB.instance().is_node_exist(self.cluster_name, self.ip): return True else: Log(1, 'The host[%s][%s] lost'%(self.cluster_name, self.ip)) raise InternalException("host deleted.", TASK_CANCEL_ERR)