def clearGroup(self):
        remoteGroupManagersConf = self.cHelper.getGroupMConf()
        remoteGroupManagerConf = remoteGroupManagersConf.get(self.groupName)
        if remoteGroupManagerConf == None:
            print "group do not exist!"
            print "706errorKEY"
            return False
        groupConsumerLoaded = remoteGroupManagerConf.get("consumersLoaded")
        for consumer in groupConsumerLoaded:
            consumerLocation = consumer["consumerLocation"]
            localDeviceMap = consumer["localDeviceMap"]
            arg = [consumerLocation,localDeviceMap]
            releaseExtraStorage.run(arg)
        # removeVGCmd = "vgremove "+self.groupName+"VG"
        removeVGCmd = "vgremove "+self.vgName
        self.executeCmd(removeVGCmd)
        for device in self.devicesList:
            # TODO add storage provider mapping to groupManager localdisk 
            # removePVCmd = "pvremove "+device.deviceName
            # self.executeCmd(removePVCmd)
            device.iscsiLogout()
            cmdStopProvider = "ssh -t root@"+device.deviceLocation+" \"python "+self.path+"main.py stopProvider "+device.deviceName+" "+self.groupName+"\""
            self.executeCmd(cmdStopProvider)

        # update information center
        
        gmConf = self.cHelper.getGroupMConf()
        currentTid = gmConf[self.groupName]["currentTid"]
        print "&&&&&&&&&&&&&& currentTid &&&&&&&&&&&& " + str(currentTid)
        sConf = staticConfig()
        gmConf[self.groupName] = {}
        gmConf[self.groupName]["currentTid"] = (currentTid-500)/200*200+500
        print "&&&&&&&&&&&&&& new currentTid &&&&&&&&&&& " + str(gmConf[self.groupName]["currentTid"])
        gmConf[self.groupName]["gmIP"] = sConf.getGroupMIP()
        gmConf[self.groupName]["devicesLoaded"] = []
        gmConf[self.groupName]["consumersLoaded"] = []
        self.cHelper.setGroupMConf(gmConf)
 def run(self, stepCode):
     print "Enter Test Procedure!\nstepCode:"+stepCode
     stepCode = int(stepCode)
     if stepCode <= 1:
         print "\n\n########################"
         print "createGroup 1"
         print "########################\n\n"
         createGroup.run(self.cgARG1)
         createGroup.run(self.cgARG2)
         self.viewResult()
     if stepCode <= 2:
         print "\n\n########################"
         print "addStorageConsumer 2"
         print "########################\n\n"
         addStorageConsumer.run(self.ascARG1)
         #addStorageConsumer.run(self.ascARG2)
         self.viewResult()
     if stepCode <= 3:
         print "\n\n########################"
         print "addDeviceToGroup 3"
         print "########################\n\n"
         addDeviceToGroup.run(self.adtgARG1)
         addDeviceToGroup.run(self.adtgARG2)
         #addDeviceToGroup.run(self.adtgARG3)
         addDeviceToGroup.run(self.adtgARG4)
         self.viewResult()
     if stepCode <= 4:
         print "\n\n########################"
         print "requestExtraStorage 4"
         print "########################\n\n"
         startT = time.time()
         requestExtraStorage.run(self.rqesARG1)
         firstT = time.time()-startT
         requestExtraStorage.run(self.rqesARG2)
         secondT = time.time()-firstT-startT
         requestExtraStorage.run(self.rqesARG3)
         thirdT = time.time()-secondT-firstT-startT
         requestExtraStorage.run(self.rqesARG4)
         fourthT = time.time()-thirdT-secondT-firstT-startT
         print startT
         print firstT
         print secondT
         print thirdT
         print fourthT
         #requestExtraStorage.run(self.rqesARG5)
         self.viewResult()
     if stepCode <= 5:
         print "\n\n########################"
         print "releaseExtraStorage 5"
         print "########################\n\n"
         for i in xrange(1):
             rlesARGcl = raw_input("consumerLocation:")
             rlesARGldm = raw_input("localDeviceMap:")
             releaseExtraStorage.run([rlesARGcl,rlesARGldm])
             self.viewResult()
     if stepCode <= 6:
         print "\n\n########################"
         print "bookStorage 6"
         print "#########################\n\n"
         timeManager.run("1434835582")
         print time.ctime(1434835582)
         bookStorage.run(self.bookStorageARG1)
         bookStorage.run(self.bookStorageARG2)
         self.viewResult()
         timeManager.run("1434838982")
         print time.ctime(1434838982)
     if stepCode <= 7:
         print "\n\n########################"
         print "requestBookedStorage 7"
         print "#########################\n\n"
         requestBookedStorage.run(self.rqbsARG1)
         requestBookedStorage.run(self.rqbsARG2)
         self.viewResult()
def run(arg):
    cswitcher = codecSwitcher()
    logger = autoscaleLog(__file__)
    logger.writeLog(arg)
    sConf = staticConfig()
    path = sConf.getPath()
    infoCLocation = sConf.getInfoCLocation()
    cHelper = configHelper( infoCLocation.get("ipInfoC"), infoCLocation.get("portInfoC"))
    print arg
    userName = arg[0]
    consumerLocation = arg[1]
    stepSize = int(arg[2])
    tagList = cswitcher.getEncode(arg[3:])
    gchooser = groupChooser()
    groupList = gchooser.chooseGroup(tagList)
    if groupList == []:
        print "No storage resource available"
        print "failed1failed2failed"
        return False
    groupName = groupList[0]
    # currentTime = time.time()
    timeM = timeManager()
    currentTime = timeM.getTime()
    # print "currentTime"
    # print currentTime
    ctKey = str(currentTime/3600)
    # print "ctKey"
    # print ctKey
    userBooking = cHelper.getUserBookingTable()
    # print "userBooking GET"
    # print userBooking
    userBookingForUser = userBooking.get(userName)
    # print "userBookingForUser"
    # print userBookingForUser
    if userBookingForUser == None:
        print "User did not book any storage"
        print "failed1failed2failed"
        return False
    userBookingForUserForGroup = userBookingForUser.get(groupName)
    # print "userBookingForUserForGroup"
    # print userBookingForUserForGroup
    if userBookingForUserForGroup == None:
        print "User did not book storage for "+str(tagList)
        print "failed1failed2failed"
        return False
    bookedStorageSize = userBookingForUserForGroup.get(ctKey)
    # print "bookedStorageSize"
    # print bookedStorageSize
    if bookedStorageSize == None:
        print "User did not book storage for this period"
        print "failed1failed2failed"
        return False
    bookedStorageSize = int(bookedStorageSize)
    if bookedStorageSize < stepSize:
        print "User do not have enough booked storage space"
        print "failed1failed2failed"
        return False

    # check current available space, if not enough , release

    currentUsageInfo = getUsageInfo.run([])
    groupSize = currentUsageInfo.get(groupName).get('groupSize')
    usedSize = currentUsageInfo.get(groupName).get('usedSize')
    asize = groupSize - usedSize
    neededSize = 0
    if stepSize > asize:
        neededSize = stepSize - asize
    if neededSize > 0:
        releaseConsumerList = []
        releaseCandidates = cHelper.getReleaseCandidates()
        for deviceMapConsumerIP,size in releaseCandidates.items():
            neededSize = neededSize - size
            releaseConsumerList.append(deviceMapConsumerIP)
            if neededSize <= 0:
                break
        if neededSize > 0:
            print "Do not have enough space sorry"
            return False
        # release consumers
        for deviceMapConsumerIP in releaseConsumerList:
            deviceMap, consumerIP = deviceMapConsumerIP.split("@")
            releaseExtraStorage.run([consumerIP,deviceMap])

    requestStorageCmd = "ssh -t root@"+consumerLocation+" \"python "+path+"main.py requestStorage "+groupName+" "+str(stepSize)+" booked\""
    print requestStorageCmd
    out = executeCmd(requestStorageCmd)
    if out.find("706errorKEY") >= 0:
        print "failed1failed2failed"
        sys.exit(1)

    # userBookingForUserForGroup[ctKey] = bookedStorageSize - stepSize
    startKeyInt = int(ctKey)
    timeKeys = userBookingForUserForGroup.keys()
    timeKeysInt = []
    for timeKey in timeKeys:
        timeKeysInt.append(int(timeKey))
    timeKeysInt.sort()
    for i in xrange(len(timeKeysInt)):
        timeKeyInt = timeKeysInt[i]
        if timeKeyInt < startKeyInt:
            userBookingForUserForGroup.pop(cswitcher.getEncode(timeKeyInt))
        elif timeKeyInt == startKeyInt:
            timeKey = cswitcher.getEncode(timeKeyInt)
            timeKeyBookedSize = userBookingForUserForGroup[timeKey]
            userBookingForUserForGroup[timeKey] = timeKeyBookedSize - stepSize
            startKeyInt += 1
        else:
            break
    cHelper.setUserBookingTable(userBooking)
    print "get booked storage succeded"
    return True