Esempio n. 1
0
    def getFabricRelationMachines(self, fabric_id):
        res = {}
        res['status'] = 'success'
        conn = fabric_engine.connect()
        try:
            res['status'] = 'success'
            res['msg'] = 'ok'
            res['data'] = {}
            have_stmt = select([MachineTB]).where(FabricRelationTB.c.fabric_id == fabric_id) \
            .where( FabricRelationTB.c.machine_id == MachineTB.c.id )
            proxy = conn.execute(have_stmt).fetchall()
            res['data']['have'] = [dict(x) for x in proxy]

            fabric_info = self.getFabricInfo(fabric_id)
            res['data']['fabric_info'] = fabric_info

            not_have = select( [MachineTB] ).where( MachineRelationTB.c.machine_id == MachineTB.c.id ) \
                .where( MachineRelationTB.c.project_id == fabric_info['project_id'] ) \
                .where( ~MachineRelationTB.c.machine_id.in_(
                    select([FabricRelationTB.c.machine_id]).where( FabricRelationTB.c.fabric_id == fabric_id )
            ) )
            proxy = conn.execute( not_have ).fetchall()
            res['data']['not_have'] = [dict(x) for x in proxy]

        except Exception as e:
            res['status'] = 'exception'
            res['msg'] = str(e)
        finally:
            conn.close()

        return res
Esempio n. 2
0
    def getRelationMachines(self, project_id=None):
        res = {}
        res['status'] = 'error'
        conn = fabric_engine.connect()
        try:
            res['data'] = {}
            res['data']['have'] = []
            res['data']['not_have'] = []
            stm_have = select([MachineTB]).where(and_(
                MachineRelationTB.c.machine_id == MachineTB.c.id,
                MachineRelationTB.c.project_id == bindparam("project_id"),
            ))
            have = conn.execute(stm_have, project_id=project_id).fetchall()
            res['data']['have'] = [dict(x) for x in have]

            stm_no_have = select([MachineTB]).where(
                ~MachineTB.c.id.in_(
                    select([MachineRelationTB.c.machine_id]).where(
                        MachineRelationTB.c.project_id == bindparam("project_id")
                    )
                )
            ).where( MachineTB.c.is_valid == 'yes' )
            not_have = conn.execute(stm_no_have, project_id=project_id).fetchall()
            res['data']['not_have'] = [dict(y) for y in not_have]
            res['status'] = 'success'

        except Exception as e:
            res['status'] = 'exception'
            res['msg'] = str(e)
        finally:
            conn.close()

        return res
Esempio n. 3
0
    def delMachines(self, machine=None):
        """ delete machine"""
        res = {}
        res['status'] = 'error'

        # step.1 被删除的机器不能存在待发布状态
        if MachineModel().haveNotDeployMachine(machine_id=machine,
                                               status='not_deploy'):
            res['msg'] = 'in_not_deploy_status'
            return res
        # step.2
        if MachineModel().machineIsInProject(machine_id=machine):
            res['msg'] = 'in_project_relation'
            return res

        if machine:
            conn = fabric_engine.connect()
            try:
                stmt = MachineTB.update().values(is_valid='no').where(
                    MachineTB.c.id == machine)
                conn.execute(stmt)
                res['status'] = 'success'
                res['msg'] = 'ok'
            except Exception as e:
                res['status'] = 'exception'
                res['msg'] = str(e)
            finally:
                conn.close()
        else:
            res['status'] = 'error'
            res['msg'] = 'not_machines'

        return res
Esempio n. 4
0
    def addMachineToFabric(self, fabric_id, machine_id):
        res = {}
        res['status'] = 'error'
        conn = fabric_engine.connect()
        try:
            fabric_info = self.getFabricInfo(fabric_id)
            if MachineModel().isInProjectByMachine(fabric_info['project_id'], machine_id):
                if not MachineModel().isInFabricByMachine(fabric_id, machine_id):
                    inst = FabricRelationTB.insert().values(
                        id = str(uuid.uuid1()) ,
                        fabric_id = fabric_id ,
                        project_id = fabric_info['project_id'],
                        machine_id = machine_id ,
                        status = 'not_deploy' ,
                        create_time = int(time.time())
                    )
                    conn.execute(inst)
                    # update total num
                    upstmt = FabricTB.update().values( total = fabric_info['total'] + 1 ).where( FabricTB.c.id == fabric_id )
                    conn.execute(upstmt)
                    res['status'] = 'success'
                    res['msg'] = 'ok'
                else:
                    res['status'] = 'error'
                    res['msg'] = 'already_in_fabric'
            else :
                res['status'] = 'error'
                res['msg'] = 'not_in_project'
        except Exception as e:
            res['status'] = 'exception'
            res['msg'] = str(e)
        finally:
            conn.close()

        return res
Esempio n. 5
0
    def editMachine(self, id, name, account, password, desc, ip, auth_type):
        res = {}
        res['status'] = 'error'
        if auth_type == 'password':
            if not account:
                res['msg'] = 'outof_account'
                return res
            if not password:
                res['msg'] = 'outof_password'
                return res
        if auth_type == 'key':
            password = None

        conn = fabric_engine.connect()
        try:
            upstmt = MachineTB.update().values(
                name=name,
                account=account,
                password=password,
                desc=desc,
                ip=ip,
                auth_type=auth_type).where(MachineTB.c.id == id)
            conn.execute(upstmt)
            res['status'] = 'success'
            res['msg'] = 'ok'
        except Exception as e:
            res['status'] = 'exception'
            res['msg'] = str(e)
        finally:
            conn.close()

        return res
Esempio n. 6
0
    def addRelation(self, project_id, machine_id = []):
        res = {}
        res['status'] = 'error'
        conn = fabric_engine.connect()
        try:
            data = []
            for x in machine_id:
                tmp = {}
                tmp['_id'] = str( uuid.uuid1() )
                tmp['_project_id'] = project_id
                tmp['_machine_id'] = x
                tmp['_create_time'] = int( time.time() )
                data.append(tmp)

            stmt = MachineRelationTB.insert().values( id=bindparam('_id'), project_id=bindparam('_project_id'), machine_id=bindparam('_machine_id'), create_time=bindparam('_create_time')  )
            conn.execute(stmt, data)
            res['status'] = 'success'
            res['msg'] = 'ok'
        except Exception as e:
            res['status'] = 'exception'
            res['msg'] = str(e)
        finally:
            conn.close()

        return res
Esempio n. 7
0
 def _getProjectInfo(self, project_id):
     """ get info by project id"""
     conn = fabric_engine.connect()
     result = conn.execute( select([ProjectTB]).where( ProjectTB.c.id == project_id ) )
     if result.rowcount != 0:
         res = dict( result.fetchone() )
     else :
         res = {}
     conn.close()
     return res
Esempio n. 8
0
    def _setMachinesStatus(self, fabric_id, status):
        conn = fabric_engine.connect()
        try:
            upstmt = FabricRelationTB.update().values(status=status).where(
                FabricRelationTB.c.fabric_id == fabric_id)
            conn.execute(upstmt)
            res = True
        except Exception as e:
            res = False
        finally:
            conn.close()

        return res
Esempio n. 9
0
 def updateFabricRelationInfo(self, relation_id, status, error, fabric_time,
                              finish_time):
     conn = fabric_engine.connect()
     try:
         stmt = FabricRelationTB.update().values(status=status,
                                                 error=error,
                                                 fabric_time=fabric_time,
                                                 finish_time=finish_time)
         stmt = stmt.where(FabricRelationTB.c.id == relation_id)
         conn.execute(stmt)
     except Exception as e:
         pass
     finally:
         conn.close()
Esempio n. 10
0
 def setFabricRelationStatus(self, fabric_id):
     conn = fabric_engine.connect()
     try:
         stmt = FabricRelationTB.update().values(status = 'cancel').where( FabricRelationTB.c.fabric_id == fabric_id )
         proxy = conn.execute(stmt)
         if proxy.rowcount > 0 :
             res = True
         else :
             res = False
     except Exception as e:
         res = True
     finally:
         conn.close()
     return res
Esempio n. 11
0
    def _getProjectInfo(self, project_id):
        conn = fabric_engine.connect()
        try:
            stmt = select([ProjectTB]).where(ProjectTB.c.id == project_id)
            proxy = conn.execute(stmt)
            if proxy.rowcount > 0:
                res = dict(proxy.fetchone())
            else:
                res = {}
        except Exception as e:
            res = {}
        finally:
            conn.close()

        return res
Esempio n. 12
0
    def machineIsInProject(self, machine_id):
        res = False
        conn = fabric_engine.connect()
        try:
            stmt = select([
                MachineRelationTB.c.id
            ]).where(MachineRelationTB.c.machine_id == machine_id).limit(1)
            proxy = conn.execute(stmt)
            if proxy.rowcount > 0:
                res = True
        except Exception as e:
            res = False
        finally:
            conn.close()

        return res
Esempio n. 13
0
    def getMachinesWaitingDeploy(self):
        conn = fabric_engine.connect()
        try:
            stmt = select([
                FabricRelationTB.c.id, MachineTB.c.ip, MachineTB.c.account,
                MachineTB.c.password, MachineTB.c.auth_type
            ]).where(MachineTB.c.id == FabricRelationTB.c.machine_id).where(
                FabricRelationTB.c.fabric_id == (self.fabric)['id'])
            proxy = conn.execute(stmt).fetchall()
            res = [dict(x) for x in proxy]
        except Exception as e:
            res = {}
        finally:
            conn.close()

        return res
Esempio n. 14
0
    def isInFabricByMachine(self, fabric_id, machine_id):
        conn = fabric_engine.connect()
        try:
            stmt = select([FabricRelationTB.c.id]).where(FabricRelationTB.c.fabric_id == fabric_id) \
                .where(FabricRelationTB.c.machine_id == machine_id)
            proxy = conn.execute(stmt)
            if proxy.rowcount > 0:
                res = True
            else:
                res = False
        except Exception as e:
            res = False
        finally:
            conn.close()

        return res
Esempio n. 15
0
    def _getMachineByFabric(self, fabric_id):
        conn = fabric_engine.connect()
        try:
            print("验证是否有需要发布的机器存在")
            stmt = select( [FabricRelationTB.c.id] ).where( FabricRelationTB.c.fabric_id == fabric_id ).limit(1)
            proxy = conn.execute(stmt)
            if proxy.rowcount > 0 :
                res = True
            else :
                res = False
        except Exception as e:
            res = False
        finally:
            conn.close()

        return res
Esempio n. 16
0
    def updateProjectState(self, project_id, status, error=None):
        res = False
        conn = fabric_engine.connect()
        try:
            stmt = ProjectTB.update().values(
                status=status, error=error).where(ProjectTB.c.id == project_id)
            resPro = conn.execute(stmt)
            if int(resPro.rowcount) > 0:
                res = True
            else:
                res = False
        except Exception as e:
            pass
        finally:
            conn.close()

        return res
Esempio n. 17
0
    def haveNotDeployMachine(self, machine_id, status='not_deploy'):
        res = False
        conn = fabric_engine.connect()
        try:
            stmt = select([
                FabricRelationTB.c.id
            ]).where(FabricRelationTB.c.machine_id == machine_id).where(
                FabricRelationTB.c.status == status).limit(1)
            proxy = conn.execute(stmt)
            if proxy.rowcount > 0:
                res = True
        except Exception as e:
            res = False
        finally:
            conn.close()

        return res
Esempio n. 18
0
    def getInfoByProjectId(self, project_id):
        """ get info by project id"""
        res = {}
        conn = fabric_engine.connect()
        try:
            result = conn.execute(
                select([ProjectTB]).where(ProjectTB.c.id == project_id))
            if result.rowcount != 0:
                res = dict(result.fetchone())
            else:
                pass
        except Exception as e:
            pass
        finally:
            conn.close()

        return res
Esempio n. 19
0
    def getFabricInfo(self, fabric_id):
        res = {}
        conn = fabric_engine.connect()
        try:
            stmt = select([FabricTB]).where( FabricTB.c.id == fabric_id ).limit(1)
            proxy = conn.execute(stmt)
            if proxy.rowcount > 0:
                res = dict(proxy.fetchone())
            else:
                res = {}

        except Exception as e:
            res = {}
        finally:
            conn.close()

        return res
Esempio n. 20
0
    def tagExists(self, project_id, tag, fabric_id=None):
        conn = fabric_engine.connect()
        try:
            stmt = select([FabricTB.c.tag]).where(FabricTB.c.project_id == project_id).where( FabricTB.c.type == 'new' ).where(FabricTB.c.tag == tag)
            if fabric_id:
                stmt = stmt.where( ~FabricTB.c.id == fabric_id )
            stmt = stmt.limit(1)
            proxy = conn.execute(stmt)
            if proxy.rowcount > 0 :
                res = True
            else :
                res = False
        except Exception as e:
            res = False
        finally:
            conn.close()

        return res
Esempio n. 21
0
    def allMachinesByFabric(self, fabric_id):
        res = {}
        res['status'] = 'error'
        conn = fabric_engine.connect()
        try:
            stmt = select([FabricRelationTB, MachineTB.c.name]).order_by(asc(FabricRelationTB.c.create_time)).where(FabricRelationTB.c.machine_id == MachineTB.c.id).where(FabricRelationTB.c.fabric_id == fabric_id)
            proxy = conn.execute(stmt).fetchall()
            res['status'] = 'success'
            res['msg'] = 'ok'
            res['data'] = [dict(x) for x in proxy ]

        except Exception as e:
            res['status'] = 'exception'
            res['msg'] = 'ok'
        finally:
            conn.close()

        return res
Esempio n. 22
0
    def delRelation(self, project, machine):
        res = {}
        res['status'] = 'error'
        conn = fabric_engine.connect()
        try:
            for x in machine:
                stmt = MachineRelationTB.delete().where( MachineRelationTB.c.project_id == project ).where( MachineRelationTB.c.machine_id == x )
                conn.execute( stmt )

            res['status'] = 'success'
            res['msg'] = 'ok'

        except Exception as e :
            res['status'] = 'exception'
            res['msg'] = str(e)
        finally:
            conn.close()

        return res
Esempio n. 23
0
    def getMachines(self, *args, **kwargs):
        res = {}
        res['status'] = 'error'
        conn = fabric_engine.connect()
        try:
            stm = select([MachineTB
                          ]).where(MachineTB.c.is_valid == 'yes').order_by(
                              desc(MachineTB.c.create_time))
            proxy = conn.execute(stm)
            machines = proxy.fetchall() if proxy.rowcount > 0 else []
            res['data'] = [dict(x) for x in machines]
            res['status'] = 'success'
            res['msg'] = 'ok'
        except Exception as e:
            res['status'] = 'exception'
            res['msg'] = str(e)
        finally:
            conn.close()

        return res
Esempio n. 24
0
    def getMachineByProject(self, project_id):
        res = {}
        res['status'] = 'error'
        conn = fabric_engine.connect()
        try:
            stmt = select([MachineTB]).order_by(
                desc(MachineRelationTB.c.create_time)).where(
                    MachineTB.c.id == MachineRelationTB.c.machine_id).where(
                        MachineRelationTB.c.project_id == project_id)
            proxy = conn.execute(stmt).fetchall()
            res['data'] = [dict(x) for x in proxy]
            res['status'] = 'success'
            res['msg'] = 'ok'
        except Exception as e:
            res['status'] = 'exception'
            res['msg'] = str(e)
        finally:
            conn.close()

        return res
Esempio n. 25
0
    def editFabric(self, id, type, project, desc, tag):
        res = {}
        res['status'] = 'error'
        if not id:
            res['msg'] = 'not_id'
            return res
        if type not in ['new','tag'] :
            res['msg'] = 'type_error'
            return res
        if not project:
            res['msg'] = 'not_project'
            return res
        if not tag:
            res['msg'] = 'not_tag'
            return res

        if type == 'new' and self.tagExists(project, tag, id):
            res['msg'] = 'tag_already_exists'
            return res

        if type == 'tag' and not self.tagExists(project, tag):
            res['msg'] = 'tag_not_exists'
            return res

        conn = fabric_engine.connect()
        try:
            upstmt = FabricTB.update().values(
                type = type,
                project_id = project,
                desc = desc,
                tag = tag,
            ).where( FabricTB.c.id == id )
            proxy = conn.execute(upstmt)
            res['status'] = 'success'
        except Exception as e:
            res['status'] = 'exception'
            res['msg'] = str(e)
        finally:
            conn.close()

        return res
Esempio n. 26
0
    def getMachineInfo(self, machine_id):
        res = {}
        res['status'] = 'error'
        conn = fabric_engine.connect()
        try:
            res['status'] = 'success'
            res['msg'] = 'ok'
            stmt = select([MachineTB
                           ]).where(MachineTB.c.id == machine_id).limit(1)
            proxy = conn.execute(stmt)
            if proxy.rowcount > 0:
                res['data'] = dict(proxy.fetchone())
            else:
                res['data'] = None
        except Exception as e:
            res['status'] = 'exception'
            res['msg'] = str(e)
        finally:
            conn.close()

        return res
Esempio n. 27
0
    def getTagByProject(self, project_id=None):
        res = {}
        res['status'] = 'error'
        conn = fabric_engine.connect()
        try:
            stmt = select([FabricTB.c.tag]).where(FabricTB.c.type == 'new').where( FabricTB.c.status =='end' ) \
            .where( FabricTB.c.total == FabricTB.c.success_num ) \
            .order_by(desc(FabricTB.c.create_time)).limit(10)
            if project_id:
                stmt = stmt.where( FabricTB.c.project_id == project_id )
            proxy = conn.execute(stmt).fetchall()
            res['data'] = [ dict(x) for x in proxy ]
            res['status'] = 'success'
            res['msg'] = 'ok'
        except Exception as e:
            res['status'] = 'exception'
            res['msg'] = str(e)
        finally:
            conn.close()

        return res
Esempio n. 28
0
    def delMachineToFabric(self, fabric_id, machine_id):
        res = {}
        res['status'] = 'error'
        conn = fabric_engine.connect()
        try:
            fabric_info = self.getFabricInfo(fabric_id)
            dele = FabricRelationTB.delete().where( FabricRelationTB.c.fabric_id == fabric_id ).where( FabricRelationTB.c.machine_id == machine_id )
            conn.execute(dele)

            upstmt = FabricTB.update().values(total = fabric_info['total'] - 1).where( FabricTB.c.id == fabric_id )
            conn.execute(upstmt)

            res['status'] = 'success'
            res['msg'] = 'ok'
        except Exception as e:
            res['status'] = 'exception'
            res['msg'] = str(e)
        finally:
            conn.close()

        return res
Esempio n. 29
0
    def tryAgain(self, project_id):
        res = {}
        res['status'] = 'error'
        conn = fabric_engine.connect()
        try:
            stmt = ProjectTB.update().values(status='cloneing').where(ProjectTB.c.id == project_id)
            resPro = conn.execute(stmt)
            if int(resPro.rowcount) > 0:
                self._sendCloneMessage(project_id)

                res['status'] = 'success'
                res['msg'] = 'ok'
            else:
                res['msg'] = 'not_project'
        except Exception as e:
            res['status'] = 'exception'
            res['msg'] = str(e)
        finally:
            conn.close()

        return res
Esempio n. 30
0
    def getFabrics(self, status=None):
        res = {}
        res['status'] = 'error'
        conn = fabric_engine.connect()
        try:
            stmt = select([FabricTB, ProjectTB.c.name.label('project_name')]).order_by(desc(FabricTB.c.create_time)).where( FabricTB.c.project_id == ProjectTB.c.id )
            if status:
                stmt = stmt.where( FabricTB.c.status == status )

            stmt = stmt.limit(10)
            proxy = conn.execute(stmt).fetchall()
            res['data']= [dict(x) for x in proxy]
            res['status'] = 'success'
            res['msg'] = 'ok'
        except Exception as e:
            res['status'] = 'exception'
            res['msg'] = str(e)
        finally:
            conn.close()

        return res