예제 #1
0
파일: VolService.py 프로젝트: thoulen/afspy
 def setExtVolAttr(self, vid, dict):
     """
     set Extended Volume Attributes by dict
     """
     thisExtVolAttr=ExtVolAttr(vid=vid)
     thisExtVolAttr.setByDict(dict)
     volCache = self.DbSession.query(ExtVolAttr).filter(ExtVolAttr.vid == vid).first()
     if volCache:
         volCache.copyObj(thisExtVolAttr)
         self.DbSession.flush()
     else:
         volCache=self.DbSession.merge(thisExtVolAttr)  
         self.DbSession.flush()
     self.DbSession.commit()  
     return thisExtVolAttr
예제 #2
0
    def update_volume_mappings(self) :
        """
        (Re-)scan the entire cell
        update all ExtVolAttr to current Project definitions
        """
        # cycle through all Projects, collect volumes matching their regex in a dict
        RWVols = {}
        vname_vid_mapping = {}
        Projects = self.get_project_list()
        for prj in Projects :
            self.Logger.debug("Updating Project %s" % prj.name)
            if len(prj.volname_regex) > 0 :
                regEXSQL = 'AND ( name REGEXP ("%s")' % prj.volname_regex[0]
                if len (prj.volname_regex) > 1 :
                    for i in range(1,len(prj.volname_regex)) :
                        regEXSQL += 'OR name REGEXP ("%s") ' %  prj.volname_regex[1]
                rawSQL = 'SELECT vid, name FROM tbl_volume WHERE type="RW" %s );'  % regEXSQL
                for vid, vname in self.DBManager.execute_raw(rawSQL).fetchall() :
                    if vname in prj.excluded_volnames : continue
                    vname_vid_mapping[vname] = vid
                    if RWVols.has_key(vname) :
                        RWVols[vname].append(prj.db_id)
                    else :
                        RWVols[vname] = [prj.db_id,]
            # additional volumes 
            for _vname in prj.additional_volnames :
                if len(_vname) == 0 : continue
                res = self.DBManager.execute_raw('SELECT vid, name FROM tbl_volume WHERE type="RW" and name="%s"' % _vname ).fetchone()
                if res == None : raise ProjectServiceError('Project %s corrupted. additional Volume "%s" does not exist.' % (prj.name, _vname))
                vid, vname = res
                vname_vid_mapping[vname] = vid
                if RWVols.has_key(vname) :
                    if prj.db_id in RWVols[vname] :
                        self.Logger.warning("Project %s: Volume %s already caught by regexes." % (prj.name,vname))
                    RWVols[vname].append(prj.db_id)
                else :
                    RWVols[vname] = [prj.db_id,]

        # create dict of Projects, prjid is key
        Prjs={} 
        for p in Projects :
            Prjs[p.db_id] = p
        # clean list of nested projects for one volume. 

        # a volume should have only leaf projects
        for vname in RWVols :
            if len(RWVols[vname]) <= 1 : continue
            self.Logger.debug("clean up nested projects for RWVols[%s]=%s" % (vname, RWVols[vname]))
            prj_hierarchy = self.get_projects_by_volume_name(vname)
            
            #for prjid in RWVols[v] :
            #    if Prjs[prjid].NestingLevel == 0 : continue
            #    if Prjs[prjid].NestingLevel > lowestNest :
            #        self.Logger.debug("Removing prj with id=%s" % prjid)
            #        removals.append(prjid)
            #for r in removals :
            #    RWVols[v].remove(r)
    

        for vname in RWVols :
            vid = vname_vid_mapping[vname]
            self.Logger.debug("processing v=%s"  % vid)
            if vid == None : 
                self.Logger.warn("got a None in vols=%s" % RWVols)
                continue
            self.Logger.debug("processing v=%s"  % RWVols[vname])
            project_ids = RWVols[vname]
            self.Logger.debug("project_ids=%s"  % project_ids)
            this_ext_volume_attributes = self.DBManager.get_from_cache(ExtVolAttr, vid=vid, fresh_only=False, must_be_unique=True)
            if this_ext_volume_attributes != None :
                this_ext_volume_attributes.project_ids = project_ids 
            else :
                this_ext_volume_attributes = ExtVolAttr() 
                this_ext_volume_attributes.vid = vid
                this_ext_volume_attributes.project_ids = project_ids
                this_ext_volume_attributes.num_min_ro = 2
                this_ext_volume_attributes.owner = "N/A"
                this_ext_volume_attributes.pinned_on_server = 0
            this_ext_volume_attributes.update_db_repr()
            self.DBManager.set_into_cache(ExtVolAttr, this_ext_volume_attributes, vid=vid )
        for p in Projects :
            self.update_server_spread(p)
        return