Exemplo n.º 1
0
    def process_action(self, message):
        ""

        try:

            # 1 返回信息
            routing_key = message.delivery_info['routing_key']
            exchange = message.delivery_info['exchange']
            msg = message.body

            action = routing_key.split('.', 1)[1]

            self.logger.info('action: %s ;message: %s' % (action, str(msg)))

            js = JsonParser()
            json_msg = js.decode(msg)

            # 2 json格式报错,日志记录
            if json_msg == -1:
                self.logger.error('josn format error!')
                return

            bs = BusinessAction(action, json_msg)
            self.work.add_job(
                bs.job,
                None,
                None,
                None,
            )

        except Exception, e:
            log = str(e)
            self.logger.error('process_action : ' + log)
Exemplo n.º 2
0
    def __init__(self, data):
        ""

        # 需要处理的数据
        self.data = data
        self.db = DbTool()
        self.js = JsonParser()

        self.process()
Exemplo n.º 3
0
 def send_msg(self,routing_key,message):
     ""
     
     self.logger.info(str(message))
      
     js = JsonParser()
     message = js.encode(message)
     
     mq = RabbitMq(self.mq_addr)
     mq.exchange_declare(exchange_name=self.task_exchange,mq_type=self.task_type)
     mq.mq_send(msg=message,exchange_name=self.task_exchange,routing_key=routing_key)
     mq.mq_close()
Exemplo n.º 4
0
    def send_msg(self):
        ""

        routing_key = 'mon_rst.vm.monitor.stat'

        message = self.infos

        self.logger.debug(str(message))
        js = JsonParser()
        message = js.encode(message)

        mq = RabbitMq(self.mq_addr)
        mq.exchange_declare(exchange_name=self.monitor_exchange,
                            mq_type=self.monitor_type)
        mq.mq_send(msg=message,
                   exchange_name=self.monitor_exchange,
                   routing_key=routing_key)
        mq.mq_close()
Exemplo n.º 5
0
class Action():
    ""

    def __init__(self, data):
        ""

        # 需要处理的数据
        self.data = data
        self.db = DbTool()
        self.js = JsonParser()

        self.process()

    def process(self):
        ""

        action = self.data['action']

        if action == "create":
            self.appCreate()
        elif action == "update":
            self.appUpdate()
        elif action == "start":
            self.appStart()
        elif action == "stop":
            self.appStop()
        elif action == "drop":
            self.appDrop()

    # app创建
    def appCreate(self):
        ""

        # 1 解析数据
        taskId = self.data['taskId']
        command = self.data['command']

        appId = str(uuid.uuid1()).replace('-', '')

        status = "creating"
        # 2 更新appinfo表

        if 'appFileId' not in command:
            command['appFileId'] = ""
        if 'appEnv' not in command:
            command['appEnv'] = ""
        if 'userEnv' not in command:
            command['userEnv'] = ""
        if 'domain' not in command:
            command['domain'] = ""

        if 'user' not in command:
            command['user'] = ""

        status = "deploy"
        appinfo_sql = "INSERT INTO %s  VALUES ('%s','%s','%s',%d,%d,%d,'%s','%s','%s','%s','%s',%d,'%s','%s')" % (
            appinfo, appId, command['serviceId'], command['appName'],
            command['cpu'], command['mem'], command['disk'],
            command['appFileId'], command['appEnv'], command['userEnv'],
            command['domain'], taskId, 0, command['user'], status)
        self.db.execute(appinfo_sql)

        # 3 更新instanceinfo表
        size = int(command['unit'])
        for i in range(0, size):
            instanceId = str(uuid.uuid1()).replace('-', '')
            instanceinfo_sql = "INSERT INTO %s  (instanceId,app_id,status,createTime) VALUES ('%s','%s','%s',now())" % (
                instanceinfos, instanceId, appId, status)
            self.db.execute(instanceinfo_sql)

            # 更新taskinfo表
            taskId = str(uuid.uuid1()).replace('-', '')
            taskType = 'appDeploy'
            taskStatus = '10'
            dealwith = 'open'
            content = str(self.js.encode(command))
            instanceinfo_sql = "INSERT INTO %s  VALUES ('%s','%s','%s','%s','%s',now(),'%s',now())" % (
                taskinfo, taskId, taskType, content, taskStatus, dealwith,
                instanceId)
            self.db.execute(instanceinfo_sql)

    # app更新
    def appUpdate(self):
        ""

        print "update"

    # app启动
    def appStart(self):
        ""

        command = self.data['command']
        app = command['appId']
        info = instanceinfo.objects.filter(app=app)

        status = 'starting'

        # app状态修改
        app = Appinfo.objects.get(appId=app)
        app.status = status
        app.save()

        for instance in info:

            instanceId = instance.instanceId

            # 更新表
            instance.status = status
            instance.save()

            # 插入taskinfo表
            taskId = str(uuid.uuid1()).replace('-', '')
            taskType = 'appStart'
            taskStatus = '10'
            dealwith = 'open'
            info = {}
            info['instanceId'] = instance.instanceId
            content = str(self.js.encode(info))
            instanceinfo_sql = "INSERT INTO %s  VALUES ('%s','%s','%s','%s','%s',now(),'%s',now())" % (
                taskinfo, taskId, taskType, content, taskStatus, dealwith,
                instanceId)
            self.db.execute(instanceinfo_sql)

    # app停止
    def appStop(self):
        ""

        command = self.data['command']
        app = command['appId']
        info = instanceinfo.objects.filter(app=app)

        status = 'stoping'

        # app状态修改
        app = Appinfo.objects.get(appId=app)
        app.status = status
        app.save()

        for instance in info:

            instanceId = instance.instanceId

            # 更新表
            instance.status = status
            instance.save()

            # 插入taskinfo表
            taskId = str(uuid.uuid1()).replace('-', '')
            taskType = 'appStop'
            taskStatus = '10'
            dealwith = 'open'
            info = {}
            info['instanceId'] = instance.instanceId
            content = str(self.js.encode(info))
            instanceinfo_sql = "INSERT INTO %s  VALUES ('%s','%s','%s','%s','%s',now(),'%s',now())" % (
                taskinfo, taskId, taskType, content, taskStatus, dealwith,
                instanceId)
            self.db.execute(instanceinfo_sql)

    # app 删除
    def appDrop(self):
        ""
        command = self.data['command']
        app = command['appId']
        info = instanceinfo.objects.filter(app=app)

        status = 'droping'

        # app状态修改
        app = Appinfo.objects.get(appId=app)
        app.status = status
        app.save()

        for instance in info:

            instanceId = instance.instanceId

            # 更新表
            instance.status = status
            instance.save()

            # 插入taskinfo表
            taskId = str(uuid.uuid1()).replace('-', '')
            taskType = 'appDrop'
            taskStatus = '10'
            dealwith = 'open'
            info = {}
            info['instanceId'] = instance.instanceId
            content = str(self.js.encode(info))
            instanceinfo_sql = "INSERT INTO %s  VALUES ('%s','%s','%s','%s','%s',now(),'%s',now())" % (
                taskinfo, taskId, taskType, content, taskStatus, dealwith,
                instanceId)
            self.db.execute(instanceinfo_sql)
Exemplo n.º 6
0
        # 1 检测
        self.check(vm_name)

        # 2 删除vm和取消定义
        self.vm_tools.vm_delete(vm_name)

        # 3 删除lvm
        try:
            self.storageManage.remove_lvm(vm_name, True)
        except Exception, e:
            raise CustomException("Error: remove lvm failed!")

        # 4 删除网络
        vm_info = self.info_vm(vm_name)
        js = JsonParser()
        if vm_info is not None:
            tmps = js.decode(vm_info['interfaces'].replace('\'', '\"'))
            for tmp in tmps:
                ip = tmp['ip']
                br = tmp['br']
                self.net.fixedIpRemove(br, ip)

        # 5 删除代理端口
        proxy_infos = self.proxy_list(vm_name=vm_name)
        for proxy_info in proxy_infos:
            self.proxy_del(vm_name=proxy_info["vmName"],
                           ip=proxy_info["ip"],
                           port=proxy_info["port"])

        # 6 判断是否有外加的磁盘,删除
Exemplo n.º 7
0
class Action() :
    ""
    
    
    def __init__(self,data) :
        ""
        
        # 需要处理的数据
        self.data = data
        self.db = DbTool()
        self.js = JsonParser()
        
        
        self.process()
        
    
    
    def process(self):
        ""
        
        action = self.data['action']
        
        if action == "create":
            self.vmCreate()
        elif action == "update":
            self.vmUpdate()
        elif action == "start":
            self.vmStart()
        elif action == "proxy":
            self.vmProxy()
        elif action == "destroy":
            self.vmDestroy()
        elif action == "drop":
            self.vmDrop()

    # vm创建
    def vmCreate(self):
        ""
        
        taskId = self.data['taskId']
        command = self.data['command']
        
        # 参数
        
        # 别名
        vmAlias = command['vmAlias']
        # 随机
        vmName = str(uuid.uuid1()).replace('-','')
        command['vmName'] = vmName
        
        cpu = command['cpu']
        mem = command['mem']
        size = command['size']
        imageId = command['imageId']
        
        remark = ''
        if 'remark' in command:
            remark = command['remark']
        
        vmId = str(uuid.uuid1()).replace('-','')
        status = "creating"
        
        # vminfo表
        vminfo_sql = "INSERT INTO %s (vmId,vmName,vmAlias,cpu,mem,size,image_id,status,tokenId,remark,createTime) VALUES ('%s','%s','%s',%d,%d,%d,'%s','%s','%s','%s',now())"%(vm_info,vmId,vmName,vmAlias,cpu,mem,size,imageId,status,taskId,remark)
        self.db.execute(vminfo_sql)
        
        # 磁盘表
        if 'attachDisks' in command:
            attachDisks = command['attachDisks']
            for disk in attachDisks:
                target = disk['hardpoints']
                size = disk['size']
                diskId = str(uuid.uuid1()).replace('-','')
                remark = ''
                if 'remark' in disk:
                    remark = disk['remark']
                disk_sql = "INSERT INTO %s (diskId,vm_id,target,size,remark,createTime) VALUES ('%s','%s','%s',%d,'%s',now())"%(vm_disk,diskId,vmId,target,size,remark)
                self.db.execute(disk_sql)
        
        # 添加任务
        taskId = str(uuid.uuid1()).replace('-','')
        taskType = 'vmCreate'
        taskStatus = '10'
        dealwith = 'open'
        content = str(self.js.encode(command))
        instanceinfo_sql = "INSERT INTO %s  VALUES ('%s','%s','%s','%s','%s',now(),'%s',now())"%(task_info,taskId,taskType,content,taskStatus,dealwith,vmId)
        self.db.execute(instanceinfo_sql)
        
    
    # vm更改
    def vmUpdate(self):
        ""
        
        pass
    
    
    # vm删除
    def vmDrop(self):
        ""
        
        command = self.data['command']
        vmId = command['vmId']
        
        info = vminfo.objects.get(vmId=vmId)
        
        status = 'dropping'
        
        # vm状态修改
        info.status=status
        info.save()
        
        # 插入taskinfo表
        taskId = str(uuid.uuid1()).replace('-','')
        taskType = 'vmDrop'
        taskStatus = '10'
        dealwith = 'open'
        info={}
        info['vmId'] = vmId
        content = str(self.js.encode(info))
        instanceinfo_sql = "INSERT INTO %s  VALUES ('%s','%s','%s','%s','%s',now(),'%s',now())"%(task_info,taskId,taskType,content,taskStatus,dealwith,vmId)
        self.db.execute(instanceinfo_sql)
        
        
    
    # vm启动
    def vmStart(self):
        ""
        
        command = self.data['command']
        vmId = command['vmId']
        
        info = vminfo.objects.get(vmId=vmId)
        
        status = 'starting'
        
        # vm状态修改
        info.status=status
        info.save()
        
        # 插入taskinfo表
        taskId = str(uuid.uuid1()).replace('-','')
        taskType = 'vmStart'
        taskStatus = '10'
        dealwith = 'open'
        info={}
        info['vmId'] = vmId
        content = str(self.js.encode(info))
        instanceinfo_sql = "INSERT INTO %s  VALUES ('%s','%s','%s','%s','%s',now(),'%s',now())"%(task_info,taskId,taskType,content,taskStatus,dealwith,vmId)
        self.db.execute(instanceinfo_sql)
        
    
    # vm关闭
    def vmProxy(self):
        ""
        
        command = self.data['command']
        vmId = command['vmId']
        port = command['port']
        proxyType =  command['type']
        
        info = vminfo.objects.get(vmId=vmId)
        
        ip = info.ip
#        vmName = info.vmName
#        
#        command['vmName'] = vmName
#        command['ip'] = ip
        
        netId = str(uuid.uuid1()).replace('-','')
        
        remark = ''
        if 'remark' in command:
                    remark = command['remark']
        # vm net 表
        disk_sql = "INSERT INTO %s (netId,ip,port,proxyType,vm_id,remark,createTime) VALUES ('%s','%s',%d,'%s','%s','%s',now())"%(vm_net,netId,ip,port,proxyType,vmId,remark)
        self.db.execute(disk_sql)
        
        # 任务表
        taskId = str(uuid.uuid1()).replace('-','')
        taskType = 'vmProxy'
        taskStatus = '10'
        dealwith = 'open'
        content = str(self.js.encode(command))
        instanceinfo_sql = "INSERT INTO %s  VALUES ('%s','%s','%s','%s','%s',now(),'%s',now())"%(task_info,taskId,taskType,content,taskStatus,dealwith,netId)
        self.db.execute(instanceinfo_sql)
        
        
    
    # vm关闭
    def vmDestroy(self):
        ""
        
        command = self.data['command']
        vmId = command['vmId']
        
        info = vminfo.objects.get(vmId=vmId)
        
        status = 'stopping'
        
        # vm状态修改
        info.status=status
        info.save()
        
        # 插入taskinfo表
        taskId = str(uuid.uuid1()).replace('-','')
        taskType = 'vmDestroy'
        taskStatus = '10'
        dealwith = 'open'
        info={}
        info['vmId'] = vmId
        content = str(self.js.encode(info))
        instanceinfo_sql = "INSERT INTO %s  VALUES ('%s','%s','%s','%s','%s',now(),'%s',now())"%(task_info,taskId,taskType,content,taskStatus,dealwith,vmId)
        self.db.execute(instanceinfo_sql)