<s> """<STR_LIT>""" <EOL> import itertools <EOL> from grr . gui import api_call_handler_base <EOL> from grr . gui import api_value_renderers <EOL> from grr . lib import aff4 <EOL> from grr . lib import flow <EOL> from grr . lib . aff4_objects import cronjobs as aff4_cronjobs <EOL> from grr . lib . rdfvalues import grr_rdf <EOL> from grr . lib . rdfvalues import structs as rdf_structs <EOL> from grr . proto import api_pb2 <EOL> CATEGORY = "<STR_LIT>" <EOL> class ApiCronJob ( rdf_structs . RDFProtoStruct ) : <EOL> """<STR_LIT>""" <EOL> protobuf = api_pb2 . ApiCronJob <EOL> def GetArgsClass ( self ) : <EOL> if self . flow_name : <EOL> flow_cls = flow . GRRFlow . classes . get ( self . flow_name ) <EOL> if flow_cls is None : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> self . flow_name ) <EOL> return flow_cls . args_type <EOL> def GetCronJobState ( cron_job ) : <EOL> """<STR_LIT>""" <EOL> if cron_job . Get ( cron_job . Schema . DISABLED ) : <EOL> return ApiCronJob . State . DISABLED <EOL> else : <EOL> return ApiCronJob . State . ENABLED <EOL> def IsCronJobFailing ( cron_job ) : <EOL> """<STR_LIT>""" <EOL> statuses = itertools . islice ( <EOL> cron_job . GetValuesForAttribute ( cron_job . Schema . LAST_RUN_STATUS ) , <NUM_LIT:0> , <NUM_LIT:4> ) <EOL> failures_count = <NUM_LIT:0> <EOL> for status in statuses : <EOL> if status . status != grr_rdf . CronJobRunStatus . Status . OK : <EOL> failures_count += <NUM_LIT:1> <EOL> return failures_count >= <NUM_LIT:2> <EOL> def CronJobToApiCronJob ( cron_job ) : <EOL> """<STR_LIT>""" <EOL> cron_args = cron_job . Get ( cron_job . Schema . CRON_ARGS ) <EOL> api_cron_job = ApiCronJob ( urn = cron_job . urn , <EOL> description = cron_args . description , <EOL> flow_name = cron_args . flow_runner_args . flow_name , <EOL> flow_runner_args = cron_args . flow_runner_args , <EOL> periodicity = cron_args . periodicity , <EOL> lifetime = cron_args . lifetime , <EOL> allow_overruns = cron_args . allow_overruns , <EOL> state = GetCronJobState ( cron_job ) , <EOL> last_run_time = cron_job . Get ( <EOL> cron_job . Schema . LAST_RUN_TIME ) , <EOL> is_failing = IsCronJobFailing ( cron_job ) ) <EOL> try : <EOL> api_cron_job . flow_args = cron_args . flow_args <EOL> except ValueError : <EOL> pass <EOL> return api_cron_job <EOL> class ApiListCronJobsArgs ( rdf_structs . RDFProtoStruct ) : <EOL> protobuf = api_pb2 . ApiListCronJobsArgs <EOL> class ApiListCronJobsHandler ( api_call_handler_base . ApiCallHandler ) : <EOL> """<STR_LIT>""" <EOL> category = CATEGORY <EOL> args_type = ApiListCronJobsArgs <EOL> def Render ( self , args , token = None ) : <EOL> if not args . count : <EOL> stop = None <EOL> else : <EOL> stop = args . offset + args . count <EOL> cron_jobs_urns = list ( itertools . islice ( <EOL> aff4_cronjobs . CRON_MANAGER . ListJobs ( token = token ) , args . offset , stop ) ) <EOL> cron_jobs = aff4 . FACTORY . MultiOpen ( <EOL> cron_jobs_urns , aff4_type = "<STR_LIT>" , token = token , age = aff4 . ALL_TIMES ) <EOL> items = [ CronJobToApiCronJob ( cron_job ) for cron_job in cron_jobs ] <EOL> items . sort ( key = lambda item : item . urn ) <EOL> result = dict ( offset = args . offset , <EOL> count = len ( items ) , <EOL> items = api_value_renderers . RenderValue ( items ) ) <EOL> return result <EOL> class ApiCreateCronJobHandler ( api_call_handler_base . ApiCallHandler ) : <EOL> """<STR_LIT>""" <EOL> category = CATEGORY <EOL> args_type = ApiCronJob <EOL> privileged = True <EOL> def Render ( self , args , token = None ) : <EOL> args . flow_args . hunt_runner_args . hunt_name = "<STR_LIT>" <EOL> if args . flow_name != "<STR_LIT>" : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % args . flow_name ) <EOL> if not args . flow_runner_args . flow_name : <EOL> args . flow_runner_args . flow_name = args . flow_name <EOL> cron_args = aff4_cronjobs . CreateCronJobFlowArgs ( <EOL> description = args . description , <EOL> periodicity = args . periodicity , <EOL> flow_runner_args = args . flow_runner_args , <EOL> flow_args = args . flow_args , <EOL> allow_overruns = args . allow_overruns , <EOL> lifetime = args . lifetime ) <EOL> urn = aff4_cronjobs . CRON_MANAGER . ScheduleFlow ( <EOL> cron_args = cron_args , disabled = True , token = token ) <EOL> fd = aff4 . FACTORY . Open ( urn , aff4_type = "<STR_LIT>" , token = token , <EOL> age = aff4 . ALL_TIMES ) <EOL> api_cron_job = CronJobToApiCronJob ( fd ) <EOL> return api_value_renderers . RenderValue ( api_cron_job ) </s>
