def handleTshService(s_name, env, arg): calllist = s_name.split('.') modulepath = calllist[:-1] funname = calllist[-1] db = dbmysql.mysqldb() DEBUG("Function name %s" % funname) try: service = __import__('modules'+'.'+'.'.join(modulepath), fromlist=['from modules import',]) except Exception as e: ERROR('Import module failed. [%s]' % s_name) ERROR('Import module failed. [%s]' % e) ERROR('Import module failed. [%s]' % traceback.format_exc()) del db, env return buildResult(SYSTEM_ERR) if hasattr(service, funname): funobj = getattr(service, funname) else: ERROR('There is no %s in %s' % (funname, modulepath)) del db, env, service return buildResult(SYSTEM_ERR) del service try: funret = funobj(db, env, arg) DEBUG(funret) except Exception as arg: ERROR('Error in %s: [%s]' % (s_name, arg)) ERROR('Import module failed. [%s]' % traceback.format_exc()) del funobj, db, env return buildResult(SYSTEM_ERR) del funobj, db, env RetCode = funret.get('RetCode') if type(RetCode) != int: ERROR('Wrong formatstr in function return data %s: [%s],ret[%s]' % (s_name, arg, funret)) return buildResult(SYSTEM_ERR) retObj = buildResult(RetCode, funret.get('RetObj')) retObj["RetMsg"] = err_desc_en.get(RetCode, '') return retObj
def delete(self, table, obj=None, limit=1000000, cond='', dbname=DB_NAME): conn = self.connect(dbname) if (conn == None): return SQL_ERROR self.cur = conn.cursor() if obj != None: if 'id' in obj: cond = 'WHERE id=%(id)s' else: cond = 'WHERE %s' % ' AND '.join( ['%s=%%(%s)s' % (k, k) for k in list(obj.keys())]) cond += " LIMIT %d " % (limit) sql = 'DELETE FROM %s %s' % (table, cond) DEBUG(sql) if obj == None: obj = {} try: self.cur.execute(sql, obj) except mysql.OperationalError: conn.rollback() return SQL_ERROR conn.commit() return SQL_SUCCESS
def insert(self, table, obj, dbname=DB_NAME): conn = self.connect(dbname) if (conn == None): return SQL_ERROR self.cur = conn.cursor() columns = tablelist[dbname][table] columnlist = [k for k in list(obj.keys()) if k in columns] sql = 'INSERT INTO %s (%s) VALUES(%s)' % (table, ','.join( columnlist), ','.join(['%%(%s)s' % k for k in columnlist])) DEBUG(sql) try: self.cur.execute(sql, obj) except mysql.OperationalError: conn.rollback() return SQL_ERROR rowid = self.cur.lastrowid if rowid == None: conn.rollback() return SQL_ERROR conn.commit() return rowid
def post(self, *args, **kwargs): argObj = getArgObj(self.request) # import time # yield tornado.gen.Task(tornado.ioloop.IOLoop.instance().add_timeout, time.time() + 10) if (not argObj.get("api")): ERROR("not a valid api, no api exist") self.write(buildFailureReply(INVALID_PARAS)) self.finish() return (status, session) = self.checkSession(argObj) if (not status): ERROR("check session failed %s " % str(argObj)) self.write(buildFailureReply(NO_AUTH_SKEY)) self.finish() return (role, accountId) = self.getAccountInfo(session) argObj["paras"]["role"] = role # IF accountId Specified, just use it if not argObj["paras"].get("accountId"): argObj["paras"]["accountId"] = accountId if argObj["api"].split(".")[-1] == "APILogOut": argObj["centerSessionID"] = self.get_cookie("centerSessionID", "").replace("%22", "") DEBUG(argObj) retObj = doDispatching(argObj, session, API_PROTOS) self.write(buildReply(retObj)) self.finish()
def collectMsg(): DEBUG("### Collect msg thread start...") while True: for func in funcList: now = get_current_time() if (now - func["lastCollect"]) > func["CollectInterval"] * 1000: func["lastCollect"] = now tmp = func["func"]() if lock.acquire(): msg[func["type"]].append(tmp) if (len(msg[func["type"]]) > 5): msg[func["type"]] = msg[func["type"]][-5:] lock.release() if lock.acquire(): fd = open(MSG_FILE, "w") fd.write(transToStr(msg, indent=4)) fd.close() lock.release() sleep(5)
def update(self, table, obj=None, cond='', field='', dbname=DB_NAME): conn = self.connect(dbname) if (conn == None): return SQL_ERROR self.cur = conn.cursor() if obj != None: columns = tablelist[dbname][table] columnlist = [k for k in list(obj.keys()) if k in columns] field = 'SET %s' % ','.join( ['%s=%%(%s)s' % (k, k) for k in columnlist]) if cond == '': if 'id' in obj: cond = 'WHERE id=%(id)s' sql = 'UPDATE %s %s %s' % (table, field, cond) DEBUG(sql) if obj == None: obj = {} try: self.cur.execute(sql, obj) except mysql.OperationalError: conn.rollback() return SQL_ERROR conn.commit() return SQL_SUCCESS
def log_add(request, args): DEBUG("running in " + args['func'] + str(args)) ret = callService("client.log.log_add", request, args) if ret['RetCode'] == CMD_SUCCESS: resObj = buildSuccessData(ret['RetObj']) else: resObj = buildFailerData(ret['RetCode']) return resObj
def checkSession(self, argObj): apiName = argObj.get("api") if (apiName.split(".")[-1] in IGNORE_SESSION_APIS): DEBUG("User login API, no need check session") return (True, {}) sessionId = getSessionId(argObj) if (not sessionId): return (False, {}) DEBUG("got session id %s" % sessionId) sessionObj = getSessionObj(self.db, sessionId) if not sessionObj: return (False, {}) return (True, sessionObj)
def log_list(request, args): DEBUG("running in " + args['func'] + str(args)) RetObj = callService("log.log.log_list", request, args) if RetObj['RetCode'] == CMD_SUCCESS: resObj = buildSuccessData(RetObj['RetObj']) else: resObj = buildFailerData(RetObj['RetCode']) return resObj
def api_action(request): args = {} func = request.REQUEST.get("fc") argObj = request.REQUEST.get("argObj") DEBUG("fc" + func) DEBUG("argobj" + str(argObj)) if argObj is None: args["argObj"] = argObj else: exec('args["argObj"]=' + request.REQUEST.get("argObj")) args["func"] = func DEBUG("argobj" + str(args)) ret = apiDispatch(func, request, args) resObj = json.JSONEncoder().encode(ret) DEBUG("obj " + str(resObj)) return HttpResponse(resObj)
def logout(self): if self.state == ROBOT_STATE_OFFLINE: WARNING("robot %s already logout" % self.myId) return OCT_SUCCESS, None DEBUG(SystemConf.robots) if self.state == ROBOT_STATE_WAITINGSCAN or self.state == ROBOT_STATE_ONLINE: rob = SystemConf.robots.get(self.myId) if not rob: ERROR("rob thread %s not running" % self.myId) else: DEBUG("rob %s is gone to stop" % self.myId) rob.running_state = False del SystemConf.robots[self.myId] self.state = ROBOT_STATE_OFFLINE self.updateState() return OCT_SUCCESS, None
def delete(self): cond = "WHERE ID='%s'" % self.myId DEBUG("to delete session %s" % (self.myId)) ret = self.db.delete(TB_SESSION, cond=cond) if ret == -1: WARNING("delete session %s error for db operation" % self.myId) return DB_ERR return 0
def execute(self, table, sql, dbname=DB_NAME): conn = self.connect(dbname) if (conn == None): return SQL_ERROR self.cur = conn.cursor() DEBUG(sql) try: self.cur.execute(sql) except mysql.OperationalError: conn.rollback() return SQL_ERROR conn.commit() return SQL_SUCCESS
def add(self): userObj = { "ID": getUuid(), "Q_Robots": self.robots, "Q_Message": self.messageCapacity, "Q_Group": self.group, "Q_LastSync": get_current_time(), "Q_CreateTime": get_current_time() } ret = self.db.insert(TB_QUOTA, userObj) if (ret == -1): WARNING("add user %s error for db operation" % self.myId) return DB_ERR DEBUG(userObj) return OCT_SUCCESS
def add(self): robotObj = { "ID": getUuid(), "R_UId": self.uId, "R_AccountId": self.accountId, "R_Name": self.name, "R_CreateTime": get_current_time(), "R_LastSync": get_current_time(), } ret = self.db.insert(TB_ROBOT, robotObj) if (ret == -1): WARNING("add robot %s error for db operation" % self.name) return DB_ERR DEBUG(robotObj) return OCT_SUCCESS
def add(self): if not self.myId: self.myId = getUuid() dbObj = { "ID": self.myId, "A_Name": self.name, "A_Address": self.address, "A_CreateTime": get_current_time(), "A_LastSync": get_current_time(), } ret = self.db.insert(TB_AGENT, dbObj) if (ret == -1): WARNING("add agent %s error for db operation" % self.name) return DB_ERR DEBUG(dbObj) return OCT_SUCCESS
def add(self): userObj = { "ID": getUuid(), "U_Name": self.name, "U_Password": getEncPass(self.password), "U_Email": self.email, "U_PhoneNumber": self.phone, "U_CreateTime": get_current_time(), "U_LastSync": get_current_time(), } ret = self.db.insert(TB_ACCOUNT, userObj) if (ret == -1): WARNING("add user %s error for db operation" % self.name) return DB_ERR DEBUG(userObj) return OCT_SUCCESS
def add(self): self.myId = getUuid() obj = { "ID": self.myId, "P_Name": self.name, "P_Type": self.type, "P_TypeName": PRODUCT_TYPES[self.type], "P_Description": self.desc, "P_Info": transToStr(self.infoObj), "P_CreateTime": get_current_time(), "P_LastSync": get_current_time() } ret = self.db.insert(TB_PRODUCT, obj) if (ret == -1): WARNING("add product %s error for db operation" % self.name) return DB_ERR DEBUG(obj) return OCT_SUCCESS
def add(self): self.myId = getUuid() obj = { "ID": self.myId, "QR_Name": self.name, "QR_Type": self.type, "QR_Price": self.price, "QR_Points": self.points, "QR_Info": transToStr(self.info), "QR_Paras": transToStr(self.paras), "QR_CreateTime": get_current_time(), "QR_Description": self.desc, } ret = self.db.insert(TB_QUERYRESULT, obj) if (ret == -1): WARNING("add user %s error for db operation" % self.name) return DB_ERR DEBUG(obj) return OCT_SUCCESS
def sendMsg(): DEBUG("### Send msg thread start...") if not os.path.exists(CONFIG_FILE_PATH): ERROR("agent config file not exist") return obj = fileToObj(CONFIG_FILE_PATH) serverIp = obj.get("serverIp") if not serverIp: ERROR("can't find server ip in config file") return agentId = obj.get("agentId") if not agentId: agentId = getUuid() obj["agentId"] = agentId fd = open(CONFIG_FILE_PATH, "w") fd.write(transToStr(obj, indent=4)) fd.close() while True: if lock.acquire(): for func in funcList: msg_type = func["type"] if len(msg[msg_type]) < 1: continue now = get_current_time() if (now - func["lastSend"]) > func["SendInterval"] * 1000: func["lastSend"] = now api = "octlink.quantum.v1.sync.APISyncMsg" paras = { "agentId": agentId, "type": msg_type, "data": transToStr(msg[msg_type][-1]), "timeout": 0 } session_uuid = "00000000000000000000000000000000" (retCode, retObj) = api_call(serverIp, "9999", api, paras, session_key=session_uuid, async=False, https=False) if (retCode): ERROR("connect to server error") continue DEBUG("send msg OK!") lock.release() sleep(60)
def log_list(db, args): list = [] retobj = {} DEBUG("args " + str(args)) argObj = args['argObj'] if argObj is None: retobj['RetCode'] = NOT_ENOUGH_PARAS return retobj total = 0 page = 1 pagetotal = 1 pagelog = 5 page = argObj['page'] pagelog = argObj['pagelog'] order_by = argObj['order_by'] filter = argObj['filter'] filter_data = argObj['filter_data'] if order_by is None: cond = None else: cond = 'order by %s' % (order_by) if filter is None: cond = 'order by %s' % (order_by) elif filter == 'all': cond = 'order by %s' % (order_by) else: cond = 'WHERE %s is \'%s\' order by %s' % (filter, filter_data, order_by) ret = db.select('tb_log', cond=cond, field='count(*)') try: total = db.cur.fetchall()[0][0] except IndexError: pass pagetotal = (total + pagelog - 1) // pagelog offset = total - page * pagelog if offset > 0: ret = db.select('tb_log', cond=cond, offset=offset, limit=pagelog) else: ret = db.select('tb_log', cond=cond, offset=0, limit=pagelog + offset) for dur in db.cur: dbobj = dbsqlite3.row_to_dict("tb_log", dur) log = { 'id': dbobj['id'], 'caused_by': dbobj['caused_by'], 'appname': dbobj['appname'], 'soft_version': dbobj['soft_version'], 'os_version': dbobj['os_version'], 'machine': dbobj['machine'], 'up_time': dbobj['up_time'], 'other_message': dbobj['other_message'], 'md5': dbobj['md5'], 'logcontent': dbobj['logcontent'], 'frequency': dbobj['frequency'], } list.append(log) lists = {'pagetotal': pagetotal, 'total': total, 'list': list} retobj['RetObj'] = lists retobj['RetCode'] = CMD_SUCCESS return retobj
def log_add(db, args): DEBUG("ars" + str(args)) retobj = {} log = {} argObj = args['argObj'] if argObj is None: retobj['RetCode'] = NOT_ENOUGH_PARAS return retobj appname = argObj['appName'] soft_version = argObj['softVersion'] caused_by = 'Unknow' os_version = argObj['osVersion'] machine = argObj['machine'] up_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) other_message = argObj['otherMessage'] logcontent = argObj['logcontent'] #log_md5 = argObj['md5'] , #frequency = argObj['frequency'], if logcontent is not None: log_md5 = hashlib.md5(logcontent).hexdigest() pattern = re.compile(r'caused by:(?P<sign>.*)\n') caused_by = pattern.findall(logcontent)[0] cond = "WHERE appname='%s' and soft_version='%s' and md5='%s'" % ( appname, soft_version, log_md5) db.select('tb_log', cond=cond) for dur in db.cur: dbobj = dbsqlite3.row_to_dict("tb_log", dur) if (machine == dbobj['machine'])\ and (os_version == dbobj['os_version']): log['up_time'] = up_time log['frequency'] = dbobj['frequency'] + 1 cond = "WHERE id=%d" % (dbobj['id']) db.update("tb_log", log, cond=cond) retobj['RetCode'] = CMD_SUCCESS retobj['RetObj'] = None return retobj else: continue log = { 'appname': appname, 'soft_version': soft_version, 'caused_by': caused_by, 'os_version': os_version, 'machine': machine, 'up_time': up_time, 'other_message': other_message, 'logcontent': logcontent, 'md5': log_md5, 'frequency': 1, } db.insert("tb_log", log) retobj['RetCode'] = CMD_SUCCESS retobj['RetObj'] = None return retobj