class AppDControl: "" def __init__(self): "" self.logger = LoggerUtil().getLogger() self.conf = ConfigManage() self.mq = Mq() def __getDb(self): db_conf = self.conf.get_db_conf() db = MysqlTools(db_conf) return db def appControl(self, taskinfo): """ @summary: 软件控制任务 @param:taskinfo 任务内容 taskStatus : 10 : 等待启动 20 : 启动中 30 : 启动成功 31 : 启动失败 30 : 待关闭 40 : 关闭中 50 : 关闭成功 51 : 关闭失败 """ taskId = taskinfo['taskId'] taskStatus = taskinfo['taskStatus'] if taskStatus == '10': self.appStart(taskinfo) else: self.logger.warning('Not support taskinfo,taskId:' + taskId + ',taskStatus:' + taskStatus) #------appControl------# #任务一 def appStart(self, taskinfo): """ @summary: 软件启动 @param:taskinfo 任务内容 """ self.logger.debug('appStart: begin dealwith task') jsonParser = JsonParser() taskId = taskinfo['taskId'] content = taskinfo['content'] businessId = taskinfo['businessId'] #获取数据操作对象 db = self.__getDb() #读取应用实例信息表 dealwithInfoModule = DealwithInfoModule(db) instanceInfoModule = InstanceInfoModule(db) instanceInfo = instanceInfoModule.getById(businessId) if not instanceInfo or not instanceInfo['engineId']: #写任务信息表 self.logger.debug( 'appDeploy_can: update task status taskStatus=40 and dealwith=close' ) 'taskStatus=40,dealwith=close' tim_param = {} tim_param['taskId'] = taskId tim_param['taskStatus'] = '31' tim_param['dealwith'] = 'close' taskinfoModule = TaskinfoModule(db) res = taskinfoModule.update(tim_param) #写任务处理表(DealwithInfoModule) 启动申请 self.logger.debug( 'appDeploy_can: write deploy message to dealwithinfo table') message = {} dealwithId = UUIDUtils.getId() message['action'] = 'app.start.error' message['taskId'] = dealwithId message['content'] = {} message['instanceId'] = businessId message['errorCode'] = '2011' message['errorMessage'] = 'app instance not exist' message = jsonParser.encode(message) dim_param = {} dim_param['dealwithId'] = dealwithId dim_param['taskId'] = taskId dim_param['dealwithType'] = '31' dim_param['message'] = message dim_param['status'] = 'failure' dealwithInfoModule.insert(dim_param) return engineId = instanceInfo['engineId'] router_key = engineId + ".app.start.apply" action = ".app.start.apply" dealwithId = UUIDUtils.getId() message = {} message['action'] = action message['taskId'] = dealwithId message['content'] = {} message['content']['instanceId'] = businessId message = jsonParser.encode(message) #写任务处理表(DealwithInfoModule) 启动申请消息 dim_param = {} dim_param['dealwithId'] = dealwithId dim_param['taskId'] = taskId dim_param['dealwithType'] = '20' dim_param['message'] = message dim_param['status'] = 'success' dealwithInfoModule.insert(dim_param) #开启事物 self.logger.debug('appStart: begin change the task status') db.begin() try: #更新任务信息表 状态 self.logger.debug( 'appStart: update task status taskStatus=20 and dealwith=close' ) 'taskStatus=40,dealwith=close' tim_param = {} tim_param['taskId'] = taskId tim_param['taskStatus'] = '20' tim_param['dealwith'] = 'close' taskinfoModule = TaskinfoModule(db) res = taskinfoModule.update(tim_param) #发送广播消息 self.logger.debug('appStart: send app start request to task of mq') self.mq.send_task_message(router_key, message) db.end() self.logger.debug('appStart: send app start request was over') except Exception, e: db.end(option='rollback') self.logger.debug( 'appStart: process status error, error message: ' + str(e)) finally:
class VmCreate: "" def __init__(self): "" self.logger = LoggerUtil().getLogger() self.conf = ConfigManage() self.mq = Mq() def __getDb(self): db_conf = self.conf.get_db_conf() db = MysqlTools(db_conf) return db def vmCreate(self, taskinfo): """ @summary: 软件创建任务 @param:taskinfo 任务内容 taskStatus : 10 : 等待创建 20 : 创建询问广播 30 : 接收资源应答 40 : 创建申请 90 : 创建成功 91 : 创建失败 """ taskId = taskinfo['taskId'] taskStatus = taskinfo['taskStatus'] if taskStatus == '10': self.vmCreate_10(taskinfo) else: self.logger.warning('Not support taskinfo,taskId:' + taskId + ',taskStatus:' + taskStatus) #------vmdeploy------# #任务一 def vmCreate_10(self, taskinfo): """ @summary: 待创建任务 @param:taskinfo 任务内容 """ #发送ask广播action=bdct.vm.deploy.ask #更改taskStatus=20等待资源,dealwith=close self.logger.debug('vmCreate_10: begin dealwith task') jsonParser = JsonParser() taskId = taskinfo['taskId'] content = taskinfo['content'] businessId = taskinfo['businessId'] contentObj = jsonParser.decode(content) cpu = contentObj['cpu'] mem = contentObj['mem'] disk = contentObj['size'] imageId = contentObj['imageId'] #获取数据操作对象 db = self.__getDb() router_key = 'bdct.vm.create.ask' action = 'vm.create.ask' dealwithId = UUIDUtils.getId() # 查询vm类型 vmImage = VmImageModule(db) ImageInfo = vmImage.getByImageId(imageId) vmType = ImageInfo['vtype'] #创建询问广播消息 message = {} message['action'] = action message['taskId'] = dealwithId message['content'] = {} message['content']['cpu'] = cpu message['content']['mem'] = mem message['content']['disk'] = disk message['content']['vmType'] = vmType message = jsonParser.encode(message) #写任务处理表库(DealwithInfoModule) self.logger.debug( 'vmCreate_10: write send ask message to dealwithInfo table') dim_param = {} dim_param['dealwithId'] = dealwithId dim_param['taskId'] = taskId dim_param['dealwithType'] = '20' dim_param['message'] = message dim_param['status'] = 'success' dealwithInfoModule = DealwithInfoModule(db) dealwithInfoModule.insert(dim_param) self.logger.debug('vmCreate_10: begin change task status') #开启事物 db.begin() try: #修改任务信息表(TaskinfoModule) self.logger.debug('vmCreate_10: update task status') tim_param = {} tim_param['taskId'] = taskId tim_param['taskStatus'] = '20' tim_param['dealwith'] = 'close' taskinfoModule = TaskinfoModule(db) res = taskinfoModule.update(tim_param) #发送广播消息 self.logger.debug( 'vmCreate_10: send ask message to the bdct of mq') self.mq.send_bdct_message(router_key, message) db.end() self.logger.debug('vmCreate_10: process task is over') except Exception, e: db.end(option='rollback') self.logger.error( 'vmCreate_10: process task error, error message: ' + str(e)) finally:
class AppDrop: "" def __init__(self) : "" self.logger = LoggerUtil().getLogger() self.conf = ConfigManage() self.mq = Mq() def __getDb(self) : db_conf = self.conf.get_db_conf() db = MysqlTools(db_conf) return db def appDrop(self, taskinfo): """ taskStatus : 10 : 等待删除 20 : 删除MQ消息发送 90 : 删除成功 91 : 删除失败 """ taskId = taskinfo['taskId'] taskStatus = taskinfo['taskStatus'] if taskStatus == '10': self.appDrop_10(taskinfo) else : self.logger.warning('Not support taskinfo,taskId:'+taskId+',taskStatus:'+taskStatus) #------appdrop------# #任务一 def appDrop_10(self, taskinfo): "" self.logger.debug('appDrop_10: begin drop app ') jsonParser = JsonParser() instanceId = taskinfo['businessId'] taskId = taskinfo['taskId'] #获取数据操作对象 db = self.__getDb() # 查询instance表 instanceInfoModule = InstanceInfoModule(db) instanceInfo = instanceInfoModule.getById(instanceId) # 获取engineid engineId = instanceInfo['engineId'] # 发现消息 action = 'app.drop.apply' router_key = engineId + ".app.drop.apply" dealwithId = UUIDUtils.getId() # 删除应用消息 message = {} message['action'] = action message['taskId'] = dealwithId message['content'] = {} message['content']['instanceId'] = instanceId message = jsonParser.encode(message) #写任务处理表库(DealwithInfoModule) self.logger.debug('appDrop_10: write drop message to dealwithInfo table') dim_param = {} dim_param['dealwithId'] = dealwithId dim_param['taskId'] = taskId dim_param['dealwithType'] = '20' dim_param['message'] = message dim_param['status'] = 'success' dealwithInfoModule = DealwithInfoModule(db) dealwithInfoModule.insert(dim_param) #开启事物 db.begin() try : #修改任务信息表(TaskinfoModule) self.logger.debug('appDrop_10: update task status') tim_param = {} tim_param['taskId'] = taskId tim_param['taskStatus'] = '20' tim_param['dealwith'] = 'close' taskinfoModule = TaskinfoModule(db) res = taskinfoModule.update(tim_param) #发送消息 self.logger.debug('appDrop_10: send drop app request to task of mq') self.mq.send_task_message(router_key, message) db.end() self.logger.debug('appDrop_10: process task is over') except Exception,e: db.end(option='rollback') self.logger.error('appDrop_10: process task error, error message: '+str(e)) finally :
class AppDeploy: "" def __init__(self): "" self.logger = LoggerUtil().getLogger() self.conf = ConfigManage() self.mq = Mq() def __getDb(self): db_conf = self.conf.get_db_conf() db = MysqlTools(db_conf) return db def appDeploy(self, taskinfo): """ @summary: 软件部署任务 @param:taskinfo 任务内容 taskStatus : 10 : 等待部署 20 : 部署询问广播 30 : 接收资源应答 40 : 部署申请 90 : 部署成功 91 : 部署失败 """ taskId = taskinfo['taskId'] taskStatus = taskinfo['taskStatus'] if taskStatus == '10': self.appDeploy_10(taskinfo) else: self.logger.warning('Not support taskinfo,taskId:' + taskId + ',taskStatus:' + taskStatus) #------appdeploy------# #任务一 def appDeploy_10(self, taskinfo): """ @summary: 待部署任务 @param:taskinfo 任务内容 """ #发送ask广播action=bdct.app.deploy.ask #更改taskStatus=20等待资源,dealwith=close self.logger.debug('appDeploy_10: begin dealwith task') jsonParser = JsonParser() taskId = taskinfo['taskId'] content = taskinfo['content'] businessId = taskinfo['businessId'] contentObj = jsonParser.decode(content) cpu = contentObj['cpu'] mem = contentObj['mem'] disk = contentObj['disk'] serviceId = contentObj['serviceId'] #获取数据操作对象 db = self.__getDb() router_key = 'bdct.app.deploy.ask' action = 'app.deploy.ask' dealwithId = UUIDUtils.getId() #部署询问广播消息 message = {} message['action'] = action message['taskId'] = dealwithId message['content'] = {} message['content']['cpu'] = cpu message['content']['mem'] = mem message['content']['disk'] = disk message['content']['serviceId'] = serviceId message = jsonParser.encode(message) #写任务处理表库(DealwithInfoModule) self.logger.debug( 'appDeploy_10: write send ask message to dealwithInfo table') 'dealwithId,taskId,dealwithType,message,status' dim_param = {} dim_param['dealwithId'] = dealwithId dim_param['taskId'] = taskId dim_param['dealwithType'] = '20' dim_param['message'] = message dim_param['status'] = 'success' print "1999" dealwithInfoModule = DealwithInfoModule(db) dealwithInfoModule.insert(dim_param) self.logger.debug('appDeploy_10: begin change task status') #开启事物 db.begin() try: #修改任务信息表(TaskinfoModule) self.logger.debug('appDeploy_10: update task status') 'taskStatus=20,dealwith=close' tim_param = {} tim_param['taskId'] = taskId tim_param['taskStatus'] = '20' tim_param['dealwith'] = 'close' taskinfoModule = TaskinfoModule(db) res = taskinfoModule.update(tim_param) #修改应用实例表(InstanceInfoModule) self.logger.debug('appDeploy_10: update instance status') 'status=deploy' iim_param = {} iim_param['instanceId'] = businessId iim_param['status'] = 'deploy' instanceInfoModule = InstanceInfoModule(db) instanceInfoModule.update(iim_param) #发送广播消息 self.logger.debug( 'appDeploy_10: send ask message to the bdct of mq') self.mq.send_bdct_message(router_key, message) db.end() self.logger.debug('appDeploy_10: process task is over') except Exception, e: db.end(option='rollback') self.logger.error( 'appDeploy_10: process task error, error message: ' + str(e)) finally: