Esempio n. 1
0
    def __init__(self):
        super(IRService, self).__init__()

        #Config in IRUtil
        self._backend = IRUtil.getBackend()
        self._address = IRUtil.getAddress()
        self._fgirimgstore = IRUtil.getFgirimgstore()
        self._fgserverdir = IRUtil.getFgserverdir()
        #Setup log        
        self._log = fgLog.fgLog(IRUtil.getLogFile(), IRUtil.getLogLevel(), "Img Repo Server", True)

        if (self._backend == "mongodb"):
            self.metaStore = ImgMetaStoreMongo(self._address, self._fgserverdir, self._log)
            self.imgStore = ImgStoreMongo(self._address, self._fgserverdir, self._log)
            self.userStore = IRUserStoreMongo(self._address, self._fgserverdir, self._log)
        elif(self._backend == "mysql"):
            self.metaStore = ImgMetaStoreMysql(self._address, self._fgserverdir, self._log)
            self.imgStore = ImgStoreMysql(self._address, self._fgserverdir, self._log)
            self.userStore = IRUserStoreMysql(self._address, self._fgserverdir, self._log)
        elif(self._backend == "swiftmysql"):
            self.metaStore = ImgMetaStoreSwiftMysql(self._address, self._fgserverdir, self._log)
            self.imgStore = ImgStoreSwiftMysql(self._address, IRUtil.getAddressS(), self._fgserverdir, self._log)
            self.userStore = IRUserStoreSwiftMysql(self._address, self._fgserverdir, self._log)
        elif(self._backend == "swiftmongo"):
            self.metaStore = ImgMetaStoreSwiftMongo(self._address, self._fgserverdir, self._log)
            self.imgStore = ImgStoreSwiftMongo(self._address, IRUtil.getAddressS(), self._fgserverdir, self._log)
            self.userStore = IRUserStoreSwiftMongo(self._address, self._fgserverdir, self._log)
        elif(self._backend == "cumulusmysql"):
            self.metaStore = ImgMetaStoreCumulusMysql(self._address, self._fgserverdir, self._log)
            self.imgStore = ImgStoreCumulusMysql(self._address, IRUtil.getAddressS(), self._fgserverdir, self._log)
            self.userStore = IRUserStoreCumulusMysql(self._address, self._fgserverdir, self._log)
        elif(self._backend == "cumulusmongo"):
            self.metaStore = ImgMetaStoreCumulusMongo(self._address, self._fgserverdir, self._log)
            self.imgStore = ImgStoreCumulusMongo(self._address, IRUtil.getAddressS(), self._fgserverdir, self._log)
            self.userStore = IRUserStoreCumulusMongo(self._address, self._fgserverdir, self._log)
        else:
            self.metaStore = ImgMetaStoreFS()
            self.imgStore = ImgStoreFS()
            self.userStore = IRUserStoreFS()
Esempio n. 2
0
class IRService(object):

    ############################################################
    # __init__
    ############################################################
    def __init__(self):
        super(IRService, self).__init__()

        #Config in IRUtil
        self._backend = IRUtil.getBackend()
        self._address = IRUtil.getAddress()
        self._fgirimgstore = IRUtil.getFgirimgstore()
        self._fgserverdir = IRUtil.getFgserverdir()
        #Setup log        
        self._log = fgLog.fgLog(IRUtil.getLogFile(), IRUtil.getLogLevel(), "Img Repo Server", True)

        if (self._backend == "mongodb"):
            self.metaStore = ImgMetaStoreMongo(self._address, self._fgserverdir, self._log)
            self.imgStore = ImgStoreMongo(self._address, self._fgserverdir, self._log)
            self.userStore = IRUserStoreMongo(self._address, self._fgserverdir, self._log)
        elif(self._backend == "mysql"):
            self.metaStore = ImgMetaStoreMysql(self._address, self._fgserverdir, self._log)
            self.imgStore = ImgStoreMysql(self._address, self._fgserverdir, self._log)
            self.userStore = IRUserStoreMysql(self._address, self._fgserverdir, self._log)
        elif(self._backend == "swiftmysql"):
            self.metaStore = ImgMetaStoreSwiftMysql(self._address, self._fgserverdir, self._log)
            self.imgStore = ImgStoreSwiftMysql(self._address, IRUtil.getAddressS(), self._fgserverdir, self._log)
            self.userStore = IRUserStoreSwiftMysql(self._address, self._fgserverdir, self._log)
        elif(self._backend == "swiftmongo"):
            self.metaStore = ImgMetaStoreSwiftMongo(self._address, self._fgserverdir, self._log)
            self.imgStore = ImgStoreSwiftMongo(self._address, IRUtil.getAddressS(), self._fgserverdir, self._log)
            self.userStore = IRUserStoreSwiftMongo(self._address, self._fgserverdir, self._log)
        elif(self._backend == "cumulusmysql"):
            self.metaStore = ImgMetaStoreCumulusMysql(self._address, self._fgserverdir, self._log)
            self.imgStore = ImgStoreCumulusMysql(self._address, IRUtil.getAddressS(), self._fgserverdir, self._log)
            self.userStore = IRUserStoreCumulusMysql(self._address, self._fgserverdir, self._log)
        elif(self._backend == "cumulusmongo"):
            self.metaStore = ImgMetaStoreCumulusMongo(self._address, self._fgserverdir, self._log)
            self.imgStore = ImgStoreCumulusMongo(self._address, IRUtil.getAddressS(), self._fgserverdir, self._log)
            self.userStore = IRUserStoreCumulusMongo(self._address, self._fgserverdir, self._log)
        else:
            self.metaStore = ImgMetaStoreFS()
            self.imgStore = ImgStoreFS()
            self.userStore = IRUserStoreFS()

    ############################################################
    # uploadValidator
    ############################################################
    def uploadValidator(self, userId, size):
        self._log.info("user:"******" command:uploadValidator args={size:" + str(size) + "}")
        return self.userStore.uploadValidator(userId, size)

    ############################################################
    # userAdd
    ############################################################
    def userAdd(self, userId, username):
        self._log.info("user:"******" command:userAdd args={userIdtoAdd:" + username + "}")
        user = IRUser(username)
        return self.userStore.userAdd(userId, user)

    ############################################################
    # userDel
    ############################################################
    def userDel(self, userId, userIdtoDel):
        self._log.info("user:"******" command:userDel args={userIdtoDel:" + userIdtoDel + "}")
        return self.userStore.userDel(userId, userIdtoDel)

    ############################################################
    # userList
    ############################################################
    def userList(self, userId):
        self._log.info("user:"******" command:userlist")
        return self.userStore.queryStore(userId, None)

    ############################################################
    # setUserRole
    ############################################################
    def setUserRole(self, userId, userIdtoModify, role):
        self._log.info("user:"******" command:setUserRole args={userIdtoModify:" + userIdtoModify + ", role:" + role + "}")
        if (role in IRUser.Role):
            return self.userStore.setRole(userId, userIdtoModify, role)
        else:
            self._log.error("Role " + role + " is not valid")
            print "Role not valid. Valid roles are " + str(IRUser.Role)
            return False

    ############################################################
    # setUserQuota
    ############################################################
    def setUserQuota(self, userId, userIdtoModify, quota):
        self._log.info("user:"******" command:setUserQuota args={userIdtoModify:" + userIdtoModify + ", quota:" + str(quota) + "}")
        return self.userStore.setQuota(userId, userIdtoModify, quota)

    ############################################################
    # setUserStatus
    ############################################################
    def setUserStatus(self, userId, userIdtoModify, status):
        self._log.info("user:"******" command:setUserStatus args={userIdtoModify:" + userIdtoModify + ", status:" + status + "}")
        if (status in IRUser.Status):
            return self.userStore.setUserStatus(userId, userIdtoModify, status)
        else:
            self._log.error("Status " + status + " is not valid")
            print "Status not valid. Status available: " + str(IRUser.Status)
            return False

    def auth(self, userId):
        # to be implemented when integrating with the security framework
        return IRUtil.auth(userId, None)

    def query(self, userId, queryString):
        self._log.info("user:"******" command:list args={queryString:" + queryString + "}")
        return self.metaStore.getItems(queryString)

    ############################################################
    # get
    ############################################################
    def get(self, userId, option, imgId):
        self._log.info("user:"******" command:get args={option:" + option + ", imgId:" + imgId + "}")
        if (option == "img"):
            return self.imgStore.getItem(imgId, userId)
        elif (option == "uri"):
            return self.imgStore.getItemUri(imgId, userId)

    ############################################################
    # put
    ############################################################
    def put(self, userId, imgId, fileName, attributeString, size):
        if (str(size) == 0) :
            print("Error size is 0 ")
        else :
            print("Logging log info for put action- user  " + userId + " len(size) " + str(size))
            self._log.info("user:"******" command:put args={imgId:" + imgId + ", metadata:" + attributeString + ",\
                       size:" + str(size) + "}")
        """
        Register the file in the database

        return imgId or 0 if something fails
        """

        status = False
        if type(fileName) == cherrypy._cpreqbody.Part :
            if (size > 0):
                aMeta = self._createImgMeta(userId, imgId, attributeString, False)
                aImg = ImgEntry(imgId, aMeta, fileName.filename, size)
                statusImg = self.imgStore.addRequestItem(aImg, fileName)

            if(statusImg):
                #put metadata into the image meta store

                #with MongoDB I put the metadata with the ImgEntry, so it skips meta add                    
                if(re.search("mongo", IRUtil.getBackend()) == None):
                    statusMeta = self.metaStore.addItem(aMeta)
                else:
                    statusMeta = True

                    if(statusMeta):
                    #Add size and #imgs to user
                        statusAcc = self.userStore.updateAccounting(userId, size, 1)

                        if (statusAcc):
                            status = True

                        else:
                            self._log.error("File size must be higher than 0")
        else:
            fileName = self._fgirimgstore + imgId
            if (os.path.isfile(fileName)):
                if (size > 0):
                    #parse attribute string and construct image metadata
                    aMeta = self._createImgMeta(userId, imgId, attributeString, False)
                    aImg = ImgEntry(imgId, aMeta, fileName, size)
                    statusImg = self.imgStore.addItem(aImg)

                    if(statusImg):
                        #put metadata into the image meta store

                        #with MongoDB I put the metadata with the ImgEntry, so it skips meta add                    
                        if(re.search("mongo", IRUtil.getBackend()) == None):
                            statusMeta = self.metaStore.addItem(aMeta)
                        else:
                            statusMeta = True

                        if(statusMeta):
                        #Add size and #imgs to user
                            statusAcc = self.userStore.updateAccounting(userId, size, 1)

                        if (statusAcc):
                            status = True

                        else:
                            self._log.error("File size must be higher than 0")

        if(status):
            return aImg._imgId
        else:
            return 0

    ############################################################
    # updateItem
    ############################################################
    def updateItem(self, userId, imgId, attributeString):
        self._log.info("user:"******" command:updateItem args={imgId:" + imgId + ",metadata:" + attributeString + "}")
        """
        Update Image Repository
        
        keywords:
        option: img - update only the Image file
                meta - update only the Metadata
                all - update Image file and Metadata
        """
        success = False
        self._log.debug(str(attributeString))
        aMeta = self._createImgMeta(userId, imgId, attributeString, True)
        self._log.debug(str(aMeta))
        success = self.metaStore.updateItem(userId, imgId, aMeta)

        return success

    ############################################################
    # remove
    ############################################################
    def remove(self, userId, imgId):
        self._log.info("user:"******" command:remove args={imgId:" + imgId + "}")
        size = [0] #Size is output parameter in the first call. 
        status = self.imgStore.removeItem(userId, imgId, size)
        if(status):
            status = self.userStore.updateAccounting(userId, -(size[0]), -1)
        return status

    ############################################################
    # histImg
    ############################################################
    def histImg(self, userId, imgId):
        self._log.info("user:"******" command:histImg args={imgId:" + imgId + "}")
        return self.imgStore.histImg(imgId)

    ############################################################
    # printHistImg
    ############################################################
    def printHistImg(self, imgs):
        output = {}
        output ['head'] = "    Image Id \t\t     Created Date \t Last Access \t    #Access \n"
        output ['head'] = string.expandtabs(output ['head'], 8)
        stradd = ""
        for i in range(len(output['head'])):
            stradd += "-"
        output ['head'] += stradd

        if(imgs != None):
            for key in imgs.keys():
                spaces = ""
                num = 24 - len(imgs[key]._imgId)
                if (num > 0):
                    for i in range(num):
                        spaces += " "

                output[key] = imgs[key]._imgId + spaces + "  " + str(imgs[key]._createdDate) + "  " + \
                        str(imgs[key]._lastAccess) + "    " + str(imgs[key]._accessCount) + "\n"

        return output


    ############################################################
    # histUser
    ############################################################
    def histUser(self, userId, userIdtoSearch):
        self._log.info("user:"******" command:histImg args={userIdtoSearch:" + userIdtoSearch + "}")
        output = {}
        output ['head'] = "User Id  Used Disk \t\t  Last Login  \t\t #Owned Images \n"
        output ['head'] = string.expandtabs(output ['head'], 8)
        stradd = ""
        for i in range(len(output['head'])):
            stradd += "-"
        output ['head'] += stradd

        if (userIdtoSearch == "None"):
            userIdtoSearch = None

        users = self.userStore.queryStore(userId, userIdtoSearch)

        if(users != None):
            for key in users.keys():
                spaces = ""
                num = 8 - len(users[key]._userId)
                if (num > 0):
                    for i in range(num):
                        spaces += " "


                output[key] = users[key]._userId + spaces + "   " + str(users[key]._fsUsed).split(".")[0] + " \t\t " + \
                        str(users[key]._lastLogin) + "   \t " + str(users[key]._ownedImgs).split(".")[0] + "\n"

        return output



    ############################################################
    # _createImgMeta
    ############################################################
    def _createImgMeta(self, userId, imgId, attributeString, update):  ##We assume that values are check in client side
        """
        Create a ImgMeta object from a list of attributes
        
        keywords
        update: if True no default values are added
        """
        args = [''] * 10
        attributes = attributeString.split("|")
        for item in attributes:
            attribute = item.strip()
            #print attribute
            tmp = attribute.split("=")
            if (len(tmp) == 2):
                key = string.lower(tmp[0])
                value = tmp[1]
                if key in ImgMeta.metaArgsIdx.keys():
                    if (key == "vmtype"):
                        value = string.lower(value)
                        if not (value in ImgMeta.VmType):
                            print "Wrong value for VmType, please use: " + str(ImgMeta.VmType)
                            break
                    elif (key == "imgtype"):
                        value = string.lower(value)
                        if not (value in ImgMeta.ImgType):
                            print "Wrong value for ImgType, please use: " + str(ImgMeta.ImgType)
                            break
                    elif(key == "permission"):
                        value = string.lower(value)
                        if not (value in ImgMeta.Permission):
                            print "Wrong value for Permission, please use: " + str(ImgMeta.Permission)
                            break
                    elif (key == "imgstatus"):
                        value = string.lower(value)
                        if not (value in ImgMeta.ImgStatus):
                            print "Wrong value for ImgStatus, please use: " + str(ImgMeta.ImgStatus)
                            break
                    args[ImgMeta.metaArgsIdx[key]] = value
        if not update:
            for x in range(len(args)):
                if args[x] == '':
                    args[x] = ImgMeta.argsDefault[x]
            #if x==4 or x==5 or x==8:
             #   args[x] = "'" + args[x] + "'"
            #print IRService.argsDefault[x], args[x]

        aMeta = ImgMeta(imgId, args[1], args[2], userId, args[4],
                        args[5], args[6], args[7], args[8], args[9])
        #print aMeta
        return aMeta