def execute(self, context): """Execute method of the detection prototype Args: context (dict): Command Context Note: This method normally will *never* return. As it is a prototype. So it should continue into infinity Returns: bool: True always unless failures occur """ if context.get('foreground'): # set foreground if in context self.ioc.getLogger().foreground = True Detector = Detect(self.ioc) if 'loop' in context: # scan only a certain amount of times scan_count = 0 while scan_count < int(context.get('loop')): if not Detector.detectSource(): self.ioc.getLogger().warning("Detection Process Failed", notify=False) scan_count += 1 else: try: while True: if not Detector.detectSource(): self.ioc.getLogger().warning( "Detection Process Failed", notify=False) continue except KeyboardInterrupt: # graceful close for scanning self.ioc.getLogger().trace( "Keyboard interrupt in detect detected", trace=True) return True # ensure we clean up after ourselves if context.get('foreground'): self.ioc.getLogger().foreground = False return True
def test_scheduleScheduling(self): d = Detect() p = PrototypeConfig(d.ioc) source = { 'key': 'var', 'ver': 'key', 'greg': 'old', 'created': '2017-11-24' } configuration = { 'name': 'demoConfig', 'job': 'otherThing', 'exe_env': 'general', 'source': 'Google', 'logic': { 'DateRange': [{ 'field': 'created', 'format': '%Y-%m-%d', 'min': '2017-11-23', 'max': '2017-11-25' }], 'Regex': [{ 'field': 'key', 'pattern': '.*', 'variable': True, 'variable_name': 'field' }, { 'field': 'ver', 'pattern': '.*' }], 'Exists': [{ 'field': 'greg', 'variable': True, 'variable_name': 'greg' }] } } p.load(True, [configuration]) sourceId = d.ioc.getCollection('SourceData').insert_one({ 'grease_data': { 'sourcing': { 'server': ObjectId(d.ioc.getConfig().NodeIdentity) }, 'detection': { 'server': ObjectId(d.ioc.getConfig().NodeIdentity), 'detectionStart': None, 'detectionEnd': None, 'detection': {} }, 'scheduling': { 'schedulingServer': None, 'schedulingStart': None, 'schedulingEnd': None }, 'execution': { 'server': None, 'assignmentTime': None, 'executionStart': None, 'executionEnd': None, 'context': {}, 'executionSuccess': False, 'commandSuccess': False, 'failures': 0, 'retryTime': datetime.datetime.utcnow() } }, 'source': str('test').encode('utf-8'), 'configuration': configuration.get('name'), 'data': source, 'createTime': datetime.datetime.utcnow(), 'expiry': Deduplication.generate_max_expiry_time(1) }).inserted_id scheduleServer = d.ioc.getCollection('JobServer').insert_one({ 'jobs': 0, 'os': platform.system().lower(), 'roles': ["general"], 'prototypes': ["schedule"], 'active': True, 'activationTime': datetime.datetime.utcnow() }).inserted_id self.assertTrue(d.detectSource()) self.assertFalse(d.getScheduledSource()) self.assertTrue( len( d.ioc.getCollection('SourceData').find_one( { 'grease_data.scheduling.server': ObjectId(scheduleServer), 'grease_data.scheduling.start': None, 'grease_data.scheduling.end': None }, sort=[('createTime', pymongo.DESCENDING)]))) d.ioc.getCollection('JobServer').delete_one( {'_id': ObjectId(scheduleServer)}) d.ioc.getCollection('SourceData').delete_one( {'_id': ObjectId(sourceId)})
def test_scheduling_fail(self): d = Detect() p = PrototypeConfig(d.ioc) s = Scheduler(d.ioc) source = { 'key': 'var', 'ver': 'key', 'greg': 'old', 'created': '2017-11-24' } configuration = { 'name': 'demoConfig', 'job': 'otherThing', 'exe_env': 'minix', 'source': 'Google', 'logic': { 'DateRange': [ { 'field': 'created', 'format': '%Y-%m-%d', 'min': '2017-11-23', 'max': '2017-11-25' } ], 'Regex': [ { 'field': 'key', 'pattern': '.*', 'variable': True, 'variable_name': 'field' }, { 'field': 'ver', 'pattern': '.*' } ], 'Exists': [ { 'field': 'greg', 'variable': True, 'variable_name': 'greg' } ] } } p.load(True, [configuration]) sourceId = d.ioc.getCollection('SourceData').insert_one({ 'grease_data': { 'sourcing': { 'server': ObjectId(d.ioc.getConfig().NodeIdentity) }, 'detection': { 'server': ObjectId(d.ioc.getConfig().NodeIdentity), 'detectionStart': None, 'detectionEnd': None, 'detection': {} }, 'scheduling': { 'schedulingServer': None, 'schedulingStart': None, 'schedulingEnd': None }, 'execution': { 'server': None, 'assignmentTime': None, 'executionStart': None, 'executionEnd': None, 'context': {}, 'executionSuccess': False, 'commandSuccess': False, 'failures': 0, 'retryTime': datetime.datetime.utcnow() } }, 'source': str('test').encode('utf-8'), 'configuration': configuration.get('name'), 'data': source, 'createTime': datetime.datetime.utcnow(), 'expiry': Deduplication.generate_max_expiry_time(1) }).inserted_id d.ioc.getCollection('JobServer').update_one( {'_id': ObjectId(d.ioc.getConfig().NodeIdentity)}, { '$set': { 'prototypes': ["scan", "detect", "schedule"] } } ) self.assertTrue(d.detectSource()) self.assertFalse(d.getScheduledSource()) self.assertTrue(s.getDetectedSource()) self.assertFalse(s.scheduleExecution()) self.assertTrue(d.ioc.getCollection('JobServer').update_one( {'_id': ObjectId(d.ioc.getConfig().NodeIdentity)}, { '$set': { 'prototypes': [] } } )) d.ioc.getCollection('SourceData').delete_one({'_id': ObjectId(sourceId)})