コード例 #1
0
    def export_enforcePolicies(self, granularity, name, useNewRes=True):
        """ Enforce all the policies. If `useNewRes` is False, use cached results only (where available).
    """
        try:
            gLogger.info(
                "ResourceManagementHandler.enforcePolicies: Attempting to enforce policies for %s %s"
                % (granularity, name))
            try:
                reason = serviceType = resourceType = None

                res = rsDB.getStuffToCheck(granularity, name=name)[0]
                status = res[1]
                formerStatus = res[2]
                siteType = res[3]
                tokenOwner = res[len(res) - 1]
                if granularity == 'Resource':
                    resourceType = res[4]
                elif granularity == 'Service':
                    serviceType = res[4]

                from DIRAC.ResourceStatusSystem.PolicySystem.PEP import PEP
                pep = PEP(VOExtension, granularity, name, status, formerStatus,
                          reason, siteType, serviceType, resourceType,
                          tokenOwner, useNewRes)
                pep.enforce(rsDBIn=rsDB, rmDBIn=rmDB)

            except RSSDBException, x:
                gLogger.error(whoRaised(x))
            except RSSException, x:
                gLogger.error(whoRaised(x))
コード例 #2
0
  def export_enforcePolicies(self, granularity, name, useNewRes = True):
    """ Enforce all the policies. If `useNewRes` is False, use cached results only (where available).
    """
    try:
      gLogger.info("ResourceManagementHandler.enforcePolicies: Attempting to enforce policies for %s %s" % (granularity, name))
      try:
        reason = serviceType = resourceType = None 

        res = rsDB.getStuffToCheck(granularity, name = name)[0]
        status = res[1]
        formerStatus = res[2]
        siteType = res[3]
        tokenOwner = res[len(res)-1]
        if granularity == 'Resource':
          resourceType = res[4]
        elif granularity == 'Service':
          serviceType = res[4]
        
        from DIRAC.ResourceStatusSystem.PolicySystem.PEP import PEP
        pep = PEP(VOExtension, granularity, name, status, formerStatus, reason, siteType, 
                  serviceType, resourceType, tokenOwner, useNewRes)
        pep.enforce(rsDBIn = rsDB, rmDBIn = rmDB)
        
      except RSSDBException, x:
        gLogger.error(whoRaised(x))
      except RSSException, x:
        gLogger.error(whoRaised(x))
コード例 #3
0
ファイル: Test_PolicySystem.py プロジェクト: DIRACGrid/DIRAC
  def test_enforce(self):

    pep = PEP( {'ResourceStatusClient':self.RSMock, 'ResourceManagementClient': self.RMMock, 'SiteStatus': self.RMMock} )
    pep.pdp = self.mockPDP
    res = pep.enforce( None )
    self.assertTrue(res['OK'])

    decisionParams = {}
    res = pep.enforce( decisionParams )
    self.assertTrue(res['OK'])

    decisionParams = {'element':'Site', 'name': 'Site1'}
    decParamsPDP = dict(decisionParams)
    decParamsPDP['active'] = 'active'
    self.mockPDP.takeDecision.return_value = {'OK':True,
                                              'Value':{'policyCombinedResult': {'PolicyType':['', ''],
                                                                                'PolicyAction':[( 'aa', 'bb' )],
                                                                                'Status':'S',
                                                                                'Reason':'testReason'},
                                                       'singlePolicyResults': [{'Status': 'Active',
                                                                                'PolicyName': 'SAM_CE_Policy',
                                                                                'Reason': 'SAM:ok'},
                                                                               {'Status': 'Banned',
                                                                                'PolicyName': 'DT_Policy_Scheduled',
                                                                                'Reason': 'DT:OUTAGE in 1 hours',
                                                                                'EndDate': '2010-02-16 15:00:00'}],
                                                       'decisionParams':decParamsPDP}}
    res = pep.enforce( decisionParams )
    self.assertTrue(res['OK'])

    decisionParams = {'element':'Resource', 'name': 'StorageElement', 'statusType': 'ReadAccess'}
    res = pep.enforce( decisionParams )
    self.assertTrue(res['OK'])
コード例 #4
0
ファイル: Test_PolicySystem.py プロジェクト: pmusset/DIRAC
    def test_enforce(self):

        pep = PEP({
            'ResourceStatusClient': self.RSMock,
            'ResourceManagementClient': self.RMMock,
            'SiteStatus': self.RMMock
        })
        pep.pdp = self.mockPDP
        res = pep.enforce(None)
        self.assertTrue(res['OK'])

        decisionParams = {}
        res = pep.enforce(decisionParams)
        self.assertTrue(res['OK'])

        decisionParams = {'element': 'Site', 'name': 'Site1'}
        decParamsPDP = dict(decisionParams)
        decParamsPDP['active'] = 'active'
        self.mockPDP.takeDecision.return_value = {
            'OK': True,
            'Value': {
                'policyCombinedResult': {
                    'PolicyType': ['', ''],
                    'PolicyAction': [('aa', 'bb')],
                    'Status': 'S',
                    'Reason': 'testReason'
                },
                'singlePolicyResults': [{
                    'Status': 'Active',
                    'PolicyName': 'SAM_CE_Policy',
                    'Reason': 'SAM:ok'
                }, {
                    'Status': 'Banned',
                    'PolicyName': 'DT_Policy_Scheduled',
                    'Reason': 'DT:OUTAGE in 1 hours',
                    'EndDate': '2010-02-16 15:00:00'
                }],
                'decisionParams':
                decParamsPDP
            }
        }
        res = pep.enforce(decisionParams)
        self.assertTrue(res['OK'])

        decisionParams = {
            'element': 'Resource',
            'name': 'StorageElement',
            'statusType': 'ReadAccess'
        }
        res = pep.enforce(decisionParams)
        self.assertTrue(res['OK'])
コード例 #5
0
    def _executeCheck(self, toBeChecked):
        """
    Create instance of a PEP, instantiated popping a service from lists.
    """

        while True:

            try:

                toBeChecked = self.ServicesToBeChecked.get()

                granularity = toBeChecked[0]
                serviceName = toBeChecked[1]
                status = toBeChecked[2]
                formerStatus = toBeChecked[3]
                siteType = toBeChecked[4]
                serviceType = toBeChecked[5]
                tokenOwner = toBeChecked[6]

                # Ignore all elements with token != RS_SVC
                if tokenOwner != 'RS_SVC':
                    continue

                gLogger.info("Checking Service %s, with status %s" %
                             (serviceName, status))

                newPEP = PEP(self.VOExtension,
                             granularity=granularity,
                             name=serviceName,
                             status=status,
                             formerStatus=formerStatus,
                             siteType=siteType,
                             serviceType=serviceType,
                             tokenOwner=tokenOwner)

                newPEP.enforce(rsDBIn=self.rsDB,
                               rmDBIn=self.rmDB,
                               setupIn=self.setup,
                               ncIn=self.nc,
                               daIn=self.diracAdmin,
                               csAPIIn=self.csAPI)

                # remove from InCheck list
                self.ServiceNamesInCheck.remove(toBeChecked[1])

            except Exception:
                gLogger.exception('SeSInspector._executeCheck')
                try:
                    self.ServiceNamesInCheck.remove(serviceName)
                except IndexError:
                    pass
コード例 #6
0
    def _executeCheck(self, arg):
        """ 
    Create instance of a PEP, instantiated popping a resource from lists.
    """

        while True:

            try:

                toBeChecked = self.StorageElementToBeChecked.get()

                granularity = toBeChecked[0]
                storageElementName = toBeChecked[1]
                status = toBeChecked[2]
                formerStatus = toBeChecked[3]
                siteType = toBeChecked[4]
                tokenOwner = toBeChecked[5]

                gLogger.info("Checking StorageElement %s, with status %s" %
                             (storageElementName, status))

                newPEP = PEP(self.VOExtension,
                             granularity=granularity,
                             name=storageElementName,
                             status=status,
                             formerStatus=formerStatus,
                             siteType=siteType,
                             tokenOwner=tokenOwner)

                newPEP.enforce(rsDBIn=self.rsDB,
                               rmDBIn=self.rmDB,
                               setupIn=self.setup,
                               ncIn=self.nc,
                               daIn=self.diracAdmin,
                               csAPIIn=self.csAPI)

                # remove from InCheck list
                self.StorageElementInCheck.remove(toBeChecked[1])

            except Exception:
                gLogger.exception('StElInspector._executeCheck')
                try:
                    self.StorageElementInCheck.remove(storageElementName)
                except IndexError:
                    pass


#############################################################################
コード例 #7
0
ファイル: StElInspectorAgent.py プロジェクト: roiser/DIRAC
    def _executeCheck(self, arg):
        """ 
    Create instance of a PEP, instantiated popping a resource from lists.
    """

        while True:

            try:

                toBeChecked = self.StorageElementToBeChecked.get()

                granularity = toBeChecked[0]
                storageElementName = toBeChecked[1]
                status = toBeChecked[2]
                formerStatus = toBeChecked[3]
                siteType = toBeChecked[4]
                tokenOwner = toBeChecked[5]

                gLogger.info("Checking StorageElement %s, with status %s" % (storageElementName, status))

                newPEP = PEP(
                    self.VOExtension,
                    granularity=granularity,
                    name=storageElementName,
                    status=status,
                    formerStatus=formerStatus,
                    siteType=siteType,
                    tokenOwner=tokenOwner,
                )

                newPEP.enforce(
                    rsDBIn=self.rsDB,
                    rmDBIn=self.rmDB,
                    setupIn=self.setup,
                    ncIn=self.nc,
                    daIn=self.diracAdmin,
                    csAPIIn=self.csAPI,
                )

                # remove from InCheck list
                self.StorageElementInCheck.remove(toBeChecked[1])

            except Exception:
                gLogger.exception("StElInspector._executeCheck")
                try:
                    self.StorageElementInCheck.remove(storageElementName)
                except IndexError:
                    pass
コード例 #8
0
  def _execute( self ):
    """
      Method run by each of the thread that is in the ThreadPool.
      It enters a loop until there are no sites on the queue.

      On each iteration, it evaluates the policies for such site
      and enforces the necessary actions. If there are no more sites in the
      queue, the loop is finished.
    """

    pep = PEP( clients = self.clients )

    while True:

      try:
        site = self.sitesToBeChecked.get_nowait()
      except Queue.Empty:
        return S_OK()

      resEnforce = pep.enforce( site )
      if not resEnforce['OK']:
        self.log.error( 'Failed policy enforcement', resEnforce['Message'] )
        self.sitesToBeChecked.task_done()
        continue

      # Used together with join !
      self.sitesToBeChecked.task_done()
コード例 #9
0
ファイル: SiteInspectorAgent.py プロジェクト: thom991/DIRAC
    def _execute(self):
        """
      Method run by each of the thread that is in the ThreadPool.
      It enters a loop until there are no sites on the queue.

      On each iteration, it evaluates the policies for such site
      and enforces the necessary actions. If there are no more sites in the
      queue, the loop is finished.
    """

        pep = PEP(clients=self.clients)

        while True:

            try:
                site = self.sitesToBeChecked.get_nowait()
            except Queue.Empty:
                return S_OK()

            resEnforce = pep.enforce(site)
            if not resEnforce['OK']:
                self.log.error('Failed policy enforcement',
                               resEnforce['Message'])
                self.sitesToBeChecked.task_done()
                continue

            # Used together with join !
            self.sitesToBeChecked.task_done()
コード例 #10
0
    def _execute(self, site):
        """
        Method run by each of the thread that is in the ThreadPool.
        It evaluates the policies for such site and enforces the necessary actions.
        """

        pep = PEP(clients=self.clients)

        self.log.verbose(
            "%s ( VO=%s / status=%s / statusType=%s ) being processed" %
            (site["name"], site["vO"], site["status"], site["statusType"]))

        try:
            res = pep.enforce(site)
        except Exception:
            self.log.exception("Exception during enforcement")
            res = S_ERROR("Exception during enforcement")
        if not res["OK"]:
            self.log.error("Failed policy enforcement", res["Message"])
            return res

        resEnforce = res["Value"]

        oldStatus = resEnforce["decisionParams"]["status"]
        statusType = resEnforce["decisionParams"]["statusType"]
        newStatus = resEnforce["policyCombinedResult"]["Status"]
        reason = resEnforce["policyCombinedResult"]["Reason"]

        if oldStatus != newStatus:
            self.log.info(
                "%s (%s) is now %s ( %s ), before %s" %
                (site["name"], statusType, newStatus, reason, oldStatus))
コード例 #11
0
    def _executeCheck(self, _arg):
        '''
      Method executed by the threads in the pool. Picks one element from the
      common queue, and enforces policies on that element.
    '''
        # Init the APIs beforehand, and reuse them.
        __APIs__ = [
            'ResourceStatusClient', 'ResourceManagementClient',
            'GGUSTicketsClient'
        ]
        clients = knownAPIs.initAPIs(__APIs__, {})

        pep = PEP(clients=clients)

        while True:

            toBeChecked = self.sitesToBeChecked.get()

            pepDict = {
                'granularity': toBeChecked[0],
                'name': toBeChecked[1],
                'statusType': toBeChecked[2],
                'status': toBeChecked[3],
                'formerStatus': toBeChecked[4],
                'siteType': toBeChecked[5],
                'tokenOwner': toBeChecked[6]
            }

            try:

                self.log.info( "Checking Site %s, with type/status: %s/%s" % \
                              ( pepDict['name'], pepDict['statusType'], pepDict['status'] ) )

                pepRes = pep.enforce(**pepDict)
                if pepRes.has_key('PolicyCombinedResult') and pepRes[
                        'PolicyCombinedResult'].has_key('Status'):
                    pepStatus = pepRes['PolicyCombinedResult']['Status']
                    if pepStatus != pepDict['status']:
                        self.log.info('Updated Site %s (%s) from %s to %s' %
                                      (pepDict['name'], pepDict['statusType'],
                                       pepDict['status'], pepStatus))

                # remove from InCheck list
                self.siteNamesInCheck.remove(
                    (pepDict['name'], pepDict['statusType']))

            except Exception:
                self.log.exception( "SSInspector._executeCheck Checking Site %s, with type/status: %s/%s" % \
                              ( pepDict['name'], pepDict['statusType'], pepDict['status'] ) )
                try:
                    self.siteNamesInCheck.remove(
                        (pepDict['name'], pepDict['statusType']))
                except IndexError:
                    pass


################################################################################
#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF
コード例 #12
0
ファイル: Test_PolicySystem.py プロジェクト: DIRACGrid/DIRAC
    def test_enforce(self):

        pep = PEP(
            {"ResourceStatusClient": self.RSMock, "ResourceManagementClient": self.RMMock, "SiteStatus": self.RMMock}
        )
        pep.pdp = self.mockPDP
        res = pep.enforce(None)
        self.assertTrue(res["OK"])

        decisionParams = {}
        res = pep.enforce(decisionParams)
        self.assertTrue(res["OK"])

        decisionParams = {"element": "Site", "name": "Site1"}
        decParamsPDP = dict(decisionParams)
        decParamsPDP["active"] = "active"
        self.mockPDP.takeDecision.return_value = {
            "OK": True,
            "Value": {
                "policyCombinedResult": {
                    "PolicyType": ["", ""],
                    "PolicyAction": [("aa", "bb")],
                    "Status": "S",
                    "Reason": "testReason",
                },
                "singlePolicyResults": [
                    {"Status": "Active", "PolicyName": "SAM_CE_Policy", "Reason": "SAM:ok"},
                    {
                        "Status": "Banned",
                        "PolicyName": "DT_Policy_Scheduled",
                        "Reason": "DT:OUTAGE in 1 hours",
                        "EndDate": "2010-02-16 15:00:00",
                    },
                ],
                "decisionParams": decParamsPDP,
            },
        }
        res = pep.enforce(decisionParams)
        self.assertTrue(res["OK"])

        decisionParams = {"element": "Resource", "name": "StorageElement", "statusType": "ReadAccess"}
        res = pep.enforce(decisionParams)
        self.assertTrue(res["OK"])
コード例 #13
0
  def _executeCheck( self ):
    """
    Create instance of a PEP, instantiated popping a resource from lists.
    """


    while True:

      try:

        toBeChecked  = self.ResourcesToBeChecked.get()

        granularity  = toBeChecked[ 0 ]
        resourceName = toBeChecked[ 1 ]
        status       = toBeChecked[ 2 ]
        formerStatus = toBeChecked[ 3 ]
        siteType     = toBeChecked[ 4 ]
        resourceType = toBeChecked[ 5 ]
        tokenOwner   = toBeChecked[ 6 ]

        # Ignore all elements with token != RS_SVC
        if tokenOwner != 'RS_SVC':
          continue

        gLogger.info( "Checking Resource %s, with status %s" % ( resourceName, status ) )

        newPEP = PEP( self.VOExtension, granularity = granularity, name = resourceName,
                      status = status, formerStatus = formerStatus, siteType = siteType,
                      resourceType = resourceType, tokenOwner = tokenOwner )

        newPEP.enforce( rsDBIn = self.rsDB, rmDBIn = self.rmDB, setupIn = self.setup,
                        ncIn = self.nc, daIn = self.diracAdmin, csAPIIn = self.csAPI )

        # remove from InCheck list
        self.ResourceNamesInCheck.remove( toBeChecked[ 1 ] )

      except Exception:
        gLogger.exception( 'RSInspector._executeCheck' )
        try:
          self.ResourceNamesInCheck.remove( resourceName )
        except IndexError:
          pass
コード例 #14
0
  def _execute( self, threadNumber ):
    '''
      Method run by the thread pool. It enters a loop until there are no elements
      on the queue. On each iteration, it evaluates the policies for such element
      and enforces the necessary actions. If there are no more elements in the
      queue, the loop is finished.
    '''

    tHeader = '%sJob%s' % ( '* '*30, threadNumber )
    
    self.log.info( '%s UP' % tHeader )
    
    pep = PEP( clients = self.clients )
    
    while True:
    
      try:
        element = self.elementsToBeChecked.get_nowait()
      except Queue.Empty:
        self.log.info( '%s DOWN' % tHeader )
        return S_OK()
      
      self.log.info( '%s ( %s / %s ) being processed' % ( element[ 'name' ], 
                                                          element[ 'status' ],
                                                          element[ 'statusType' ] ) )
      
      resEnforce = pep.enforce( element )
      if not resEnforce[ 'OK' ]:
        self.log.error( resEnforce[ 'Message' ] )
        self.elementsToBeChecked.task_done()
        continue
      
      resEnforce = resEnforce[ 'Value' ]  
      
      oldStatus  = resEnforce[ 'decissionParams' ][ 'status' ]
      statusType = resEnforce[ 'decissionParams' ][ 'statusType' ]
      newStatus  = resEnforce[ 'policyCombinedResult' ][ 'Status' ]
      reason     = resEnforce[ 'policyCombinedResult' ][ 'Reason' ]
      
      if oldStatus != newStatus:
        self.log.info( '%s (%s) is now %s ( %s ), before %s' % ( element[ 'name' ], 
                                                                 statusType,
                                                                 newStatus, 
                                                                 reason, 
                                                                 oldStatus ) )
        
      # Used together with join !
      self.elementsToBeChecked.task_done()   

    self.log.info( '%s DOWN' % tHeader )

    return S_OK()
コード例 #15
0
    def _execute(self, threadNumber):
        '''
      Method run by the thread pool. It enters a loop until there are no elements
      on the queue. On each iteration, it evaluates the policies for such element
      and enforces the necessary actions. If there are no more elements in the
      queue, the loop is finished.
    '''

        tHeader = '%sJob%s' % ('* ' * 30, threadNumber)

        self.log.info('%s UP' % tHeader)

        pep = PEP(clients=self.clients)

        while True:

            try:
                element = self.elementsToBeChecked.get_nowait()
            except Queue.Empty:
                self.log.info('%s DOWN' % tHeader)
                return S_OK()

            self.log.info(
                '%s ( %s / %s ) being processed' %
                (element['name'], element['status'], element['statusType']))

            resEnforce = pep.enforce(element)
            if not resEnforce['OK']:
                self.log.error(resEnforce['Message'])
                self.elementsToBeChecked.task_done()
                continue

            resEnforce = resEnforce['Value']

            oldStatus = resEnforce['decissionParams']['status']
            statusType = resEnforce['decissionParams']['statusType']
            newStatus = resEnforce['policyCombinedResult']['Status']
            reason = resEnforce['policyCombinedResult']['Reason']

            if oldStatus != newStatus:
                self.log.info('%s (%s) is now %s ( %s ), before %s' %
                              (element['name'], statusType, newStatus, reason,
                               oldStatus))

            # Used together with join !
            self.elementsToBeChecked.task_done()

        self.log.info('%s DOWN' % tHeader)

        return S_OK()
コード例 #16
0
ファイル: RSInspectorAgent.py プロジェクト: bmb/DIRAC
  def _executeCheck( self, _arg ):
    '''
      Method executed by the threads in the pool. Picks one element from the
      common queue, and enforces policies on that element.
    '''
    # Init the APIs beforehand, and reuse them.
    __APIs__ = [ 'ResourceStatusClient', 'ResourceManagementClient' ]
    clients = knownAPIs.initAPIs( __APIs__, {} )

    pep = PEP( clients = clients )

    while True:

      toBeChecked  = self.resourcesToBeChecked.get()

      pepDict = { 'granularity'  : toBeChecked[ 0 ],
                  'name'         : toBeChecked[ 1 ],
                  'statusType'   : toBeChecked[ 2 ],
                  'status'       : toBeChecked[ 3 ],
                  'formerStatus' : toBeChecked[ 4 ],
                  'siteType'     : toBeChecked[ 5 ],
                  'resourceType' : toBeChecked[ 6 ],
                  'tokenOwner'   : toBeChecked[ 7 ] }

      try:

        self.log.info( "Checking Resource %s, with type/status: %s/%s" % \
                      ( pepDict['name'], pepDict['statusType'], pepDict['status'] ) )

        pepRes =  pep.enforce( **pepDict )
        if pepRes.has_key( 'PolicyCombinedResult' ) and pepRes[ 'PolicyCombinedResult' ].has_key( 'Status' ):
          pepStatus = pepRes[ 'PolicyCombinedResult' ][ 'Status' ]
          if pepStatus != pepDict[ 'status' ]:
            self.log.info( 'Updated Site %s (%s) from %s to %s' %
                          ( pepDict['name'], pepDict['statusType'], pepDict['status'], pepStatus ))

        # remove from InCheck list
        self.resourceNamesInCheck.remove( ( pepDict[ 'name' ], pepDict[ 'statusType' ] ) )

      except Exception:
        self.log.exception( "RSInspector._executeCheck Checking Resource %s, with type/status: %s/%s" % \
                      ( pepDict['name'], pepDict['statusType'], pepDict['status'] ) )
        try:
          self.resourceNamesInCheck.remove( ( pepDict[ 'name' ], pepDict[ 'statusType' ] ) )
        except IndexError:
          pass

################################################################################
#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF
コード例 #17
0
    def _execute(self):
        """
      Method run by the thread pool. It enters a loop until there are no elements
      on the queue. On each iteration, it evaluates the policies for such element
      and enforces the necessary actions. If there are no more elements in the
      queue, the loop is finished.
    """

        pep = PEP(clients=self.clients)

        while True:

            try:
                element = self.elementsToBeChecked.get_nowait()
            except Queue.Empty:
                return S_OK()

            self.log.verbose(
                '%s ( %s / %s ) being processed' %
                (element['name'], element['status'], element['statusType']))

            try:
                resEnforce = pep.enforce(element)
            except Exception as e:
                self.log.exception('Exception during enforcement')
                resEnforce = S_ERROR('Exception during enforcement')
            if not resEnforce['OK']:
                self.log.error('Failed policy enforcement',
                               resEnforce['Message'])
                self.elementsToBeChecked.task_done()
                continue

            resEnforce = resEnforce['Value']

            oldStatus = resEnforce['decisionParams']['status']
            statusType = resEnforce['decisionParams']['statusType']
            newStatus = resEnforce['policyCombinedResult']['Status']
            reason = resEnforce['policyCombinedResult']['Reason']

            if oldStatus != newStatus:
                self.log.info('%s (%s) is now %s ( %s ), before %s' %
                              (element['name'], statusType, newStatus, reason,
                               oldStatus))

            # Used together with join !
            self.elementsToBeChecked.task_done()
コード例 #18
0
ファイル: SeSInspectorAgent.py プロジェクト: zenglzh/DIRAC
  def _executeCheck(self):
    '''
      Method executed by the threads in the pool. Picks one element from the
      common queue, and enforces policies on that element.
    '''
    # Init the APIs beforehand, and reuse them.
    __APIs__ = [ 'ResourceStatusClient', 'ResourceManagementClient' ]
    clients = knownAPIs.initAPIs( __APIs__, {} )

    pep = PEP( clients = clients )

    while True:
      toBeChecked = self.queue.get()

      pepDict = { 'granularity'  : toBeChecked[ 0 ],
                  'name'         : toBeChecked[ 1 ],
                  'statusType'   : toBeChecked[ 2 ],
                  'status'       : toBeChecked[ 3 ],
                  'formerStatus' : toBeChecked[ 4 ],
                  'siteType'     : toBeChecked[ 5 ],
                  'serviceType'  : toBeChecked[ 6 ],
                  'tokenOwner'   : toBeChecked[ 7 ]}

      try:
        self.log.info( "Checking Service %s, with type/status: %s/%s" %
                      ( pepDict['name'], pepDict['statusType'], pepDict['status'] ) )

        pepRes = pep.enforce( **pepDict )
        if pepRes.has_key( 'PolicyCombinedResult' ) and pepRes[ 'PolicyCombinedResult' ].has_key( 'Status' ):
          pepStatus = pepRes[ 'PolicyCombinedResult' ][ 'Status' ]
          if pepStatus != pepDict[ 'status' ]:
            self.log.info( 'Updated %s %s from %s/%s to %s/%s' %
                          ( pepDict["granularity"],
                            pepDict['name'],
                            pepDict['statusType'], pepDict['status'],
                            pepDict['statusType'], pepStatus ))

      except Exception:
        self.log.exception( "SeSInspector._executeCheck Checking Service %s, with type/status: %s/%s" %
                           ( pepDict['name'], pepDict['statusType'], pepDict['status'] ) )
コード例 #19
0
ファイル: Test_PolicySystem.py プロジェクト: remenska/DIRAC
  def test_enforce(self):

    for policyType in PolicyTypes:
      for granularity in ValidRes:
        for status in ValidStatus:
          for oldStatus in ValidStatus:
            if status == oldStatus:
              continue
            for newPolicyType in PolicyTypes:
              if policyType == newPolicyType:
                continue
#              for newGranularity in ValidRes:
              for siteType in ValidSiteType:
                for serviceType in ValidServiceType:
                  for resourceType in ValidResourceType:
                    for user in ("RS_SVC", "Federico"):
                      for setup in ("LHCb-Production", "LHCb-Development", "LHCb-Certification"):

                        self.mock_pdp.takeDecision.return_value = {'PolicyCombinedResult': [{'PolicyType':[policyType, newPolicyType],
                                                                                             'Action':True, 'Status':status,
                                                                                             'Reason':'testReason'}],
                                                                   'SinglePolicyResults': [{'Status': 'Active',
                                                                                            'PolicyName': 'SAM_CE_Policy',
                                                                                            'Reason': 'SAM:ok',
                                                                                            'SAT': True},
                                                                                            {'Status': 'Banned',
                                                                                             'PolicyName': 'DT_Policy_Scheduled',
                                                                                             'Reason': 'DT:OUTAGE in 1 hours',
                                                                                             'EndDate': '2010-02-16 15:00:00',
                                                                                             'SAT': True}] }
          #                pep = PEP(granularity, 'XX', status, oldStatus, 'XX', 'T1', 'Computing', 'CE', {'PolicyType':newPolicyType, 'Granularity':newGranularity})


                        pep = PEP(self.VO, granularity, 'XX', status, oldStatus, 'XX', siteType,
                                  serviceType, resourceType, user)


        #                self.mock_pdp.takeDecision.return_value = {'PolicyCombinedResult': [{'PolicyType':[policyType, newPolicyType],
        #                                                                                     'Action':True, 'Status':status,
        #                                                                                     'Reason':'testReason'},
        #                                                                                     {'PolicyType':[policyType, newPolicyType],
        #                                                                                      'Action':True, 'Status':status,
        #                                                                                      'Reason':'testReason'}],
        #                                                           'SinglePolicyResults': [{'Status': 'Active',
        #                                                                                    'PolicyName': 'SAM_CE_Policy',
        #                                                                                    'Reason': 'SAM:ok',
        #                                                                                    'SAT': True},
        #                                                                                    {'Status': 'Banned',
        #                                                                                     'PolicyName': 'DT_Policy_Scheduled',
        #                                                                                     'Reason': 'DT:OUTAGE in 1 hours',
        #                                                                                     'EndDate': '2010-02-16 15:00:00',
        #                                                                                     'SAT': True}] }
          #                pep = PEP(granularity, 'XX', status, oldStatus, 'XX', 'T1', 'Computing', 'CE', {'PolicyType':newPolicyType, 'Granularity':newGranularity})
        #                pep = PEP(granularity, 'XX', status, oldStatus, 'XX', 'T1', 'Computing', 'CE', user)
                        self.mock_rsDB.getMonitoredsHistory.return_value = ('Active', 'Reason', '2010-04-09 09:54:52')
                        res = pep.enforce(pdpIn = self.mock_pdp, rsDBIn = self.mock_rsDB, rmDBIn = self.mock_rmDB, ncIn = self.mock_nc,
                                          setupIn = setup, daIn = self.mock_da, csAPIIn = self.mock_csAPI)
                        self.assertEqual(res, None)
                        self.mock_pdp.takeDecision.return_value = {'PolicyCombinedResult': [{'PolicyType':[policyType, newPolicyType],
                                                                                             'Action':False, 'Reason':'testReason'}],
                                                                   'SinglePolicyResults': [{'Status': 'Active',
                                                                                            'PolicyName': 'SAM_CE_Policy',
                                                                                            'Reason': 'SAM:ok',
                                                                                            'SAT': True},
                                                                                            {'Status': 'Banned',
                                                                                             'PolicyName': 'DT_Policy_Scheduled',
                                                                                             'Reason': 'DT:OUTAGE in 1 hours',
                                                                                             'EndDate': '2010-02-16 15:00:00',
                                                                                             'SAT': True}] }
                        res = pep.enforce(pdpIn = self.mock_pdp, rsDBIn = self.mock_rsDB, rmDBIn = self.mock_rmDB, ncIn = self.mock_nc,
                                          setupIn = setup, daIn = self.mock_da, csAPIIn = self.mock_csAPI)
                        self.assertEqual(res, None)
コード例 #20
0
  def export_publisher(self, granularity, name, useNewRes = False):
    """ get a view
    
    :Parameters:
      `granularity`
        string - a ValidRes
    
      `name`
        string - name of the res

      `useNewRes`
        boolean. When set to true, will get new results, 
        otherwise it will get cached results (where available).
    """
    try:
      gLogger.info("ResourceManagementHandler.publisher: Attempting to get info for %s: %s" % (granularity, name))
      try:
        if useNewRes == True:
          from DIRAC.ResourceStatusSystem.PolicySystem.PEP import PEP
          gLogger.info("ResourceManagementHandler.publisher: Recalculating policies for %s: %s" % (granularity, name))
          if granularity in ('Site', 'Sites'):
            res = rsDB.getStuffToCheck(granularity, name = name)[0]
            status = res[1]
            formerStatus = res[2]
            siteType = res[3]
            tokenOwner = res[4]
            
            pep = PEP(VOExtension, granularity, name, status, formerStatus, None, siteType, 
                      None, None, tokenOwner, useNewRes)
            pep.enforce(rsDBIn = rsDB, rmDBIn = rmDB)

            res = rsDB.getMonitoredsList('Service', paramsList = ['ServiceName'], siteName = name)
            services = [x[0] for x in res]
            for s in services:
              res = rsDB.getStuffToCheck('Service', name = s)[0]
              status = res[1]
              formerStatus = res[2]
              siteType = res[3]
              serviceType = res[4]
              
              pep = PEP(VOExtension, 'Service', s, status, formerStatus, None, siteType, 
                        serviceType, None, tokenOwner, useNewRes)
              pep.enforce(rsDBIn = rsDB, rmDB = rmDB)
          else:
            reason = serviceType = resourceType = None 
  
            res = rsDB.getStuffToCheck(granularity, name = name)[0]
            status = res[1]
            formerStatus = res[2]
            siteType = res[3]
            tokenOwner = res[len(res)-1]
            if granularity == 'Resource':
              resourceType = res[4]
            elif granularity == 'Service':
              serviceType = res[4]
            
#            from DIRAC.ResourceStatusSystem.PolicySystem.PEP import PEP
            pep = PEP(VOExtension, granularity, name, status, formerStatus, reason, siteType, 
                      serviceType, resourceType, tokenOwner, useNewRes)
            pep.enforce(rsDBIn = rsDB, rmDBIn = rmDB)
            
        res = publisher.getInfo(granularity, name, useNewRes)
      except InvalidRes, x:
        errorStr = "Invalid granularity"
        gLogger.exception(whoRaised(x) + errorStr)
        return S_ERROR(errorStr)
      except RSSException, x:
        errorStr = "RSSException"
        gLogger.exception(whoRaised(x) + errorStr)
コード例 #21
0
    def export_publisher(self, granularity, name, useNewRes=False):
        """ get a view
    
    :Parameters:
      `granularity`
        string - a ValidRes
    
      `name`
        string - name of the res

      `useNewRes`
        boolean. When set to true, will get new results, 
        otherwise it will get cached results (where available).
    """
        try:
            gLogger.info(
                "ResourceManagementHandler.publisher: Attempting to get info for %s: %s"
                % (granularity, name))
            try:
                if useNewRes == True:
                    from DIRAC.ResourceStatusSystem.PolicySystem.PEP import PEP
                    gLogger.info(
                        "ResourceManagementHandler.publisher: Recalculating policies for %s: %s"
                        % (granularity, name))
                    if granularity in ('Site', 'Sites'):
                        res = rsDB.getStuffToCheck(granularity, name=name)[0]
                        status = res[1]
                        formerStatus = res[2]
                        siteType = res[3]
                        tokenOwner = res[4]

                        pep = PEP(VOExtension, granularity, name, status,
                                  formerStatus, None, siteType, None, None,
                                  tokenOwner, useNewRes)
                        pep.enforce(rsDBIn=rsDB, rmDBIn=rmDB)

                        res = rsDB.getMonitoredsList(
                            'Service',
                            paramsList=['ServiceName'],
                            siteName=name)
                        services = [x[0] for x in res]
                        for s in services:
                            res = rsDB.getStuffToCheck('Service', name=s)[0]
                            status = res[1]
                            formerStatus = res[2]
                            siteType = res[3]
                            serviceType = res[4]

                            pep = PEP(VOExtension, 'Service', s, status,
                                      formerStatus, None, siteType,
                                      serviceType, None, tokenOwner, useNewRes)
                            pep.enforce(rsDBIn=rsDB, rmDB=rmDB)
                    else:
                        reason = serviceType = resourceType = None

                        res = rsDB.getStuffToCheck(granularity, name=name)[0]
                        status = res[1]
                        formerStatus = res[2]
                        siteType = res[3]
                        tokenOwner = res[len(res) - 1]
                        if granularity == 'Resource':
                            resourceType = res[4]
                        elif granularity == 'Service':
                            serviceType = res[4]

#            from DIRAC.ResourceStatusSystem.PolicySystem.PEP import PEP
                        pep = PEP(VOExtension, granularity, name, status,
                                  formerStatus, reason, siteType, serviceType,
                                  resourceType, tokenOwner, useNewRes)
                        pep.enforce(rsDBIn=rsDB, rmDBIn=rmDB)

                res = publisher.getInfo(granularity, name, useNewRes)
            except InvalidRes, x:
                errorStr = "Invalid granularity"
                gLogger.exception(whoRaised(x) + errorStr)
                return S_ERROR(errorStr)
            except RSSException, x:
                errorStr = "RSSException"
                gLogger.exception(whoRaised(x) + errorStr)
コード例 #22
0
ファイル: Test_PolicySystem.py プロジェクト: vfalbor/DIRAC
  def test_enforce(self):

    for policyType in PolicyTypes:
      for granularity in ValidRes:
        for status in ValidStatus:
          for oldStatus in ValidStatus:
            if status == oldStatus:
              continue
            for newPolicyType in PolicyTypes:
              if policyType == newPolicyType:
                continue
#              for newGranularity in ValidRes:
              for siteType in ValidSiteType:
                for serviceType in ValidServiceType:
                  for resourceType in ValidResourceType:
                    for user in ("RS_SVC", "Federico"):
                      for setup in ("LHCb-Production", "LHCb-Development", "LHCb-Certification"):

                        self.mock_pdp.takeDecision.return_value = {'PolicyCombinedResult': [{'PolicyType':[policyType, newPolicyType],
                                                                                             'Action':True, 'Status':status,
                                                                                             'Reason':'testReason'}],
                                                                   'SinglePolicyResults': [{'Status': 'Active',
                                                                                            'PolicyName': 'SAM_CE_Policy',
                                                                                            'Reason': 'SAM:ok',
                                                                                            'SAT': True},
                                                                                            {'Status': 'Banned',
                                                                                             'PolicyName': 'DT_Policy_Scheduled',
                                                                                             'Reason': 'DT:OUTAGE in 1 hours',
                                                                                             'EndDate': '2010-02-16 15:00:00',
                                                                                             'SAT': True}] }
          #                pep = PEP(granularity, 'XX', status, oldStatus, 'XX', 'T1', 'Computing', 'CE', {'PolicyType':newPolicyType, 'Granularity':newGranularity})


                        pep = PEP(self.VO, granularity, 'XX', status, oldStatus, 'XX', siteType,
                                  serviceType, resourceType, user)


        #                self.mock_pdp.takeDecision.return_value = {'PolicyCombinedResult': [{'PolicyType':[policyType, newPolicyType],
        #                                                                                     'Action':True, 'Status':status,
        #                                                                                     'Reason':'testReason'},
        #                                                                                     {'PolicyType':[policyType, newPolicyType],
        #                                                                                      'Action':True, 'Status':status,
        #                                                                                      'Reason':'testReason'}],
        #                                                           'SinglePolicyResults': [{'Status': 'Active',
        #                                                                                    'PolicyName': 'SAM_CE_Policy',
        #                                                                                    'Reason': 'SAM:ok',
        #                                                                                    'SAT': True},
        #                                                                                    {'Status': 'Banned',
        #                                                                                     'PolicyName': 'DT_Policy_Scheduled',
        #                                                                                     'Reason': 'DT:OUTAGE in 1 hours',
        #                                                                                     'EndDate': '2010-02-16 15:00:00',
        #                                                                                     'SAT': True}] }
          #                pep = PEP(granularity, 'XX', status, oldStatus, 'XX', 'T1', 'Computing', 'CE', {'PolicyType':newPolicyType, 'Granularity':newGranularity})
        #                pep = PEP(granularity, 'XX', status, oldStatus, 'XX', 'T1', 'Computing', 'CE', user)
                        self.mock_rsDB.getMonitoredsHistory.return_value = ('Active', 'Reason', '2010-04-09 09:54:52')
                        res = pep.enforce(pdpIn = self.mock_pdp, rsDBIn = self.mock_rsDB, rmDBIn = self.mock_rmDB, ncIn = self.mock_nc,
                                          setupIn = setup, daIn = self.mock_da, csAPIIn = self.mock_csAPI)
                        self.assertEqual(res, None)
                        self.mock_pdp.takeDecision.return_value = {'PolicyCombinedResult': [{'PolicyType':[policyType, newPolicyType],
                                                                                             'Action':False, 'Reason':'testReason'}],
                                                                   'SinglePolicyResults': [{'Status': 'Active',
                                                                                            'PolicyName': 'SAM_CE_Policy',
                                                                                            'Reason': 'SAM:ok',
                                                                                            'SAT': True},
                                                                                            {'Status': 'Banned',
                                                                                             'PolicyName': 'DT_Policy_Scheduled',
                                                                                             'Reason': 'DT:OUTAGE in 1 hours',
                                                                                             'EndDate': '2010-02-16 15:00:00',
                                                                                             'SAT': True}] }
                        res = pep.enforce(pdpIn = self.mock_pdp, rsDBIn = self.mock_rsDB, rmDBIn = self.mock_rmDB, ncIn = self.mock_nc,
                                          setupIn = setup, daIn = self.mock_da, csAPIIn = self.mock_csAPI)
                        self.assertEqual(res, None)