class ProgramRuntime(ComponentBase, RuntimeComponent): #this here determines how we start and stop and #what classes handle different running/stopping types STARTER_CLS_MAPPING = { Foreground.RUN_TYPE: ForegroundRunner, Screen.RUN_TYPE: ScreenRunner, } STOPPER_CLS_MAPPING = { Foreground.RUN_TYPE: ForegroundRunner, Screen.RUN_TYPE: ScreenRunner, } def __init__(self, component_name, *args, **kargs): ComponentBase.__init__(self, component_name, *args, **kargs) self.run_type = kargs.get("run_type", Foreground.RUN_TYPE) self.tracereader = TraceReader(self.tracedir, IN_TRACE) self.tracewriter = TraceWriter(self.tracedir, START_TRACE) self.starttracereader = TraceReader(self.tracedir, START_TRACE) self.check_installed_pkgs = kargs.get("check_installed_pkgs", True) def _getstartercls(self, start_mode): if (start_mode not in ProgramRuntime.STARTER_CLS_MAPPING): raise NotImplementedError("Can not yet start %s mode" % (start_mode)) return ProgramRuntime.STARTER_CLS_MAPPING.get(start_mode) def _getstoppercls(self, stop_mode): if (stop_mode not in ProgramRuntime.STOPPER_CLS_MAPPING): raise NotImplementedError("Can not yet stop %s mode" % (stop_mode)) return ProgramRuntime.STOPPER_CLS_MAPPING.get(stop_mode) def _was_installed(self): if (not self.check_installed_pkgs): return True if (len(self.tracereader.packages_installed())): return True return False def _get_apps_to_start(self): raise NotImplementedError() def _get_app_options(self, app): return None def _get_param_map(self, app=None): return { 'ROOT': self.appdir, } def start(self): #ensure it was installed if (not self._was_installed()): msg = "Can not start %s since it was not installed" % ( self.component_name) raise StartException(msg) #select how we are going to start it startercls = self._getstartercls() starter = startercls() #start all apps #this fns list will have info about what was started fns = list() apps = self._get_apps_to_start() for app in apps: #adjust the program options now that we have real locations params = self._get_param_map(app) program_opts = self._get_app_options(app) if (params and program_opts): adjusted_opts = list() for opt in program_opts: adjusted_opts.append(param_replace(opt, params)) program_opts = adjusted_opts LOG.info("Starting %s with options [%s]" % (app, ", ".join(program_opts))) #start it with the given settings fn = starter.start(app, app, *program_opts, app_dir=self.appdir, trace_dir=self.tracedir) if (fn and len(fn)): fns.append(fn) LOG.info("Started %s, details are in %s" % (app, fn)) #this trace is used to locate details about what to stop self.tracewriter.started_info(app, fn) else: LOG.info("Started %s" % (app)) return fns def stop(self): #ensure it was installed if (not self._was_installed()): msg = "Can not stop %s since it was not installed" % ( self.component_name) raise StopException(msg) #we can only stop what has a started trace start_traces = self.starttracereader.apps_started() killedam = 0 for mp in start_traces: #extract the apps name and where its trace is fn = mp.get('trace_fn') name = mp.get('name') #missing some key info, skip it if (fn == None or name == None): continue #figure out which class will stop it contents = parse_fn(fn) killcls = None for (cmd, action) in contents: if (cmd == Runner.RUN_TYPE): killcls = self._getstoppercls(action) break #did we find a class that can do it? if (killcls): #we can try to stop it LOG.info("Stopping %s" % (name)) #create an instance of the killer class and attempt to stop killer = killcls() killer.stop(name, trace_dir=self.tracedir) killedam += 1 #if we got rid of them all get rid of the trace if (killedam == len(start_traces)): fn = self.starttracereader.trace_fn LOG.info("Deleting trace file %s" % (fn)) unlink(fn)
class ProgramRuntime(ComponentBase, RuntimeComponent): #this here determines how we start and stop and #what classes handle different running/stopping types STARTER_CLS_MAPPING = { Foreground.RUN_TYPE: ForegroundRunner, Screen.RUN_TYPE: ScreenRunner, } STOPPER_CLS_MAPPING = { Foreground.RUN_TYPE: ForegroundRunner, Screen.RUN_TYPE: ScreenRunner, } def __init__(self, component_name, *args, **kargs): ComponentBase.__init__(self, component_name, *args, **kargs) self.run_type = kargs.get("run_type", Foreground.RUN_TYPE) self.tracereader = TraceReader(self.tracedir, IN_TRACE) self.tracewriter = TraceWriter(self.tracedir, START_TRACE) self.starttracereader = TraceReader(self.tracedir, START_TRACE) self.check_installed_pkgs = kargs.get("check_installed_pkgs", True) def _getstartercls(self, start_mode): if(start_mode not in ProgramRuntime.STARTER_CLS_MAPPING): raise NotImplementedError("Can not yet start %s mode" % (start_mode)) return ProgramRuntime.STARTER_CLS_MAPPING.get(start_mode) def _getstoppercls(self, stop_mode): if(stop_mode not in ProgramRuntime.STOPPER_CLS_MAPPING): raise NotImplementedError("Can not yet stop %s mode" % (stop_mode)) return ProgramRuntime.STOPPER_CLS_MAPPING.get(stop_mode) def _was_installed(self): if(not self.check_installed_pkgs): return True if(len(self.tracereader.packages_installed())): return True return False def _get_apps_to_start(self): raise NotImplementedError() def _get_app_options(self, app): return None def _get_param_map(self, app=None): return { 'ROOT': self.appdir, } def start(self): #ensure it was installed if(not self._was_installed()): msg = "Can not start %s since it was not installed" % (self.component_name) raise StartException(msg) #select how we are going to start it startercls = self._getstartercls() starter = startercls() #start all apps #this fns list will have info about what was started fns = list() apps = self._get_apps_to_start() for app in apps: #adjust the program options now that we have real locations params = self._get_param_map(app) program_opts = self._get_app_options(app) if(params and program_opts): adjusted_opts = list() for opt in program_opts: adjusted_opts.append(param_replace(opt, params)) program_opts = adjusted_opts LOG.info("Starting %s with options [%s]" % (app, ", ".join(program_opts))) #start it with the given settings fn = starter.start(app, app, *program_opts, app_dir=self.appdir, trace_dir=self.tracedir) if(fn and len(fn)): fns.append(fn) LOG.info("Started %s, details are in %s" % (app, fn)) #this trace is used to locate details about what to stop self.tracewriter.started_info(app, fn) else: LOG.info("Started %s" % (app)) return fns def stop(self): #ensure it was installed if(not self._was_installed()): msg = "Can not stop %s since it was not installed" % (self.component_name) raise StopException(msg) #we can only stop what has a started trace start_traces = self.starttracereader.apps_started() killedam = 0 for mp in start_traces: #extract the apps name and where its trace is fn = mp.get('trace_fn') name = mp.get('name') #missing some key info, skip it if(fn == None or name == None): continue #figure out which class will stop it contents = parse_fn(fn) killcls = None for (cmd, action) in contents: if(cmd == Runner.RUN_TYPE): killcls = self._getstoppercls(action) break #did we find a class that can do it? if(killcls): #we can try to stop it LOG.info("Stopping %s" % (name)) #create an instance of the killer class and attempt to stop killer = killcls() killer.stop(name, trace_dir=self.tracedir) killedam += 1 #if we got rid of them all get rid of the trace if(killedam == len(start_traces)): fn = self.starttracereader.trace_fn LOG.info("Deleting trace file %s" % (fn)) unlink(fn)