Esempio n. 1
0
    def test_servicePSCcomparison(self):
        """ Assure ourselves that profiles compare as we expect..
"""
        hostProfile = PelotonSettings(ram=2048, hostname='kylie', cpus=1)
        goodSvcProfiles = [PelotonSettings(mincpus=1),
                           PelotonSettings(),
                           PelotonSettings(hostname='kylie'),
                           PelotonSettings(hostname='s:kylie'),
                           PelotonSettings(hostname='r:k.l.*$'),
                           PelotonSettings(hostname='f:k?l*'),
                           PelotonSettings(minram=2048),
                           PelotonSettings(maxram=2048),
                           PelotonSettings(minram=2048, maxram=2048),
                           PelotonSettings(minram=1024, maxram=4096)]
                           
        badSvcProfiles = [PelotonSettings(mincpus=2),
                           PelotonSettings(hostname='billie'),
                           PelotonSettings(hostname='s:billie'),
                           PelotonSettings(hostname='r:bi.l.*$'),
                           PelotonSettings(hostname='f:bi?l*'),
                           PelotonSettings(minram=4096),
                           PelotonSettings(maxram=1024),
                           PelotonSettings(minram=1024, maxram=2000)]
        
        sc = ServicePSCComparator()
        self.assertRaises(NotImplementedError, sc.gt, goodSvcProfiles[0], hostProfile)
        for p in goodSvcProfiles:
            self.assertTrue(sc.eq(p, hostProfile))
            
        for p in badSvcProfiles:
            self.assertFalse(sc.eq(p, hostProfile))
Esempio n. 2
0
class ServiceLoader(AbstractEventHandler):
    """ The loader is the front-end to the launch sequencer and the component
which listens for and responds to requests from other PSCs to start a service. 

If this node issues a request to launch a service it locates the service, loads 
the profiles and instantiates the launch sequencer.

If this node is receiving a request to start a service it deals with checking
that it could, in principle, run the service and messaging with the 
requestor to determine whether it should or not. If yes, it instructs
a worker (or workers) to startup via the kernel.
"""
    def __init__(self, kernel):
        self.kernel = kernel
        self.logger = kernel.logger
        self.dispatcher = kernel.dispatcher
        self.spComparator = ServicePSCComparator()
        self.dispatcher.register('psc.service.loader', self, 'domain_control')

        self.callBackChannel = 'psc.service.loader.%s%s' % (kernel.guid, crypto.makeCookie(10))
        self.dispatcher.register(self.callBackChannel, self, 'domain_control')
        
    def launchService(self, serviceName, runconfig=None):
        """ Start the process of launching a service which will require
the following steps:
    
    1. Locate the profile, extract the psclimits and launch parameters
    3. Create a service launch sequencer with the profile and start it
 """
        self.logger.info("Mapping launch request for service %s" % serviceName)
        # 1. locate and load the profile
        pqcn = "%s.%s.%s" % (serviceName.lower(), serviceName.lower(), serviceName)
        cls = getClassFromString(pqcn, reload=True)
        self.__service = cls(serviceName, None, None)
        self.__service.loadConfig(self.kernel.settings.servicepath, runconfig)
        # 2. Start the sequencer
        ServiceLaunchSequencer(self.kernel, serviceName, self.__service.profile).start()

    def eventReceived(self, msg, exchange='', key='', ctag=''):
        """ Handle events related to the starting of services as requested
by another node. """
        if msg['action'] == 'requestForLaunch':
            msg['serviceProfile'] = eval(msg['serviceProfile'])
            # store service profile in the library
            self.kernel.serviceLibrary.setProfile(msg['serviceProfile'])
            
            self.logger.debug("Request to consider ability to launch service: %s (%s) as %s" % (msg['serviceProfile']['name'], msg['serviceProfile']['version'], msg['serviceProfile']['publishedName']))
            if self.spComparator.eq(msg['serviceProfile']['psclimits'], self.kernel.profile, optimistic=True):
                self.logger.debug("Profile OK - optimistic")
                self.dispatcher.fireEvent(msg['callback'],
                                          'domain_control',
                                          action='SERVICE_PSC_MATCH',
                                          callback=self.callBackChannel)
                
            else:
                self.dispatcher.fireEvent(msg['callback'],
                                          'domain_control',
                                          action='SERVICE_PSC_NOMATCH')
                
        elif msg['action'] == 'startService':
            self.logger.debug("Instructed to start service %s as %s" % (msg['serviceName'], msg['publishedName']))
            self.kernel.startServiceGroup(msg['serviceName'],msg['publishedName'])