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)
class RabbitRuntime(ComponentBase, RuntimeComponent): def __init__(self, *args, **kargs): ComponentBase.__init__(self, TYPE, *args, **kargs) self.tracereader = TraceReader(self.tracedir, IN_TRACE) def start(self): pkgsinstalled = self.tracereader.packages_installed() if (len(pkgsinstalled) == 0): msg = "Can not start %s since it was not installed" % (TYPE) raise StartException(msg) if (self.status().find('start') == -1): self._run_cmd(START_CMD) return None def status(self): pkgsinstalled = self.tracereader.packages_installed() if (len(pkgsinstalled) == 0): msg = "Can not check the status of %s since it was not installed" % ( TYPE) raise StatusException(msg) (sysout, stderr) = execute(*STATUS_CMD, run_as_root=True) return sysout.strip().lower() def _run_cmd(self, cmd): if (self.distro == UBUNTU11): with TemporaryFile() as f: execute(*cmd, run_as_root=True, stdout_fh=f, stderr_fh=f) else: execute(*cmd, run_as_root=True) def restart(self): pkgsinstalled = self.tracereader.packages_installed() if (len(pkgsinstalled) == 0): msg = "Can not check the status of %s since it was not installed" % ( TYPE) raise RestartException(msg) self._run_cmd(RESTART_CMD) return None def stop(self): pkgsinstalled = self.tracereader.packages_installed() if (len(pkgsinstalled) == 0): msg = "Can not stop %s since it was not installed" % (TYPE) raise StopException(msg) if (self.status().find('stop') == -1): self._run_cmd(STOP_CMD) return None
class RabbitRuntime(ComponentBase, RuntimeComponent): def __init__(self, *args, **kargs): ComponentBase.__init__(self, TYPE, *args, **kargs) self.tracereader = TraceReader(self.tracedir, IN_TRACE) def start(self): pkgsinstalled = self.tracereader.packages_installed() if(len(pkgsinstalled) == 0): msg = "Can not start %s since it was not installed" % (TYPE) raise StartException(msg) if(self.status().find('start') == -1): self._run_cmd(START_CMD) return None def status(self): pkgsinstalled = self.tracereader.packages_installed() if(len(pkgsinstalled) == 0): msg = "Can not check the status of %s since it was not installed" % (TYPE) raise StatusException(msg) (sysout, stderr) = execute(*STATUS_CMD, run_as_root=True) return sysout.strip().lower() def _run_cmd(self, cmd): if(self.distro == UBUNTU11): with TemporaryFile() as f: execute(*cmd, run_as_root=True, stdout_fh=f, stderr_fh=f) else: execute(*cmd, run_as_root=True) def restart(self): pkgsinstalled = self.tracereader.packages_installed() if(len(pkgsinstalled) == 0): msg = "Can not check the status of %s since it was not installed" % (TYPE) raise RestartException(msg) self._run_cmd(RESTART_CMD) return None def stop(self): pkgsinstalled = self.tracereader.packages_installed() if(len(pkgsinstalled) == 0): msg = "Can not stop %s since it was not installed" % (TYPE) raise StopException(msg) if(self.status().find('stop') == -1): self._run_cmd(STOP_CMD) return None
class PkgUninstallComponent(ComponentBase, UninstallComponent): def __init__(self, component_name, *args, **kargs): ComponentBase.__init__(self, component_name, *args, **kargs) self.tracereader = TraceReader(self.tracedir, IN_TRACE) def unconfigure(self): self._unconfigure_files() def _unconfigure_files(self): cfgfiles = self.tracereader.files_configured() if (len(cfgfiles)): LOG.info("Removing %s configuration files" % (len(cfgfiles))) for fn in cfgfiles: if (len(fn)): unlink(fn) LOG.info("Removed %s" % (fn)) def uninstall(self): self._uninstall_pkgs() self._uninstall_touched_files() self._uninstall_dirs() def _uninstall_pkgs(self): pkgsfull = self.tracereader.packages_installed() if (len(pkgsfull)): LOG.info("Potentially removing %s packages" % (len(pkgsfull))) self.packager.remove_batch(pkgsfull) def _uninstall_touched_files(self): filestouched = self.tracereader.files_touched() if (len(filestouched)): LOG.info("Removing %s touched files" % (len(filestouched))) for fn in filestouched: if (len(fn)): unlink(fn) LOG.info("Removed %s" % (fn)) def _uninstall_dirs(self): dirsmade = self.tracereader.dirs_made() if (len(dirsmade)): LOG.info("Removing %s created directories" % (len(dirsmade))) for dirname in dirsmade: deldir(dirname) LOG.info("Removed %s" % (dirname))
class PkgUninstallComponent(ComponentBase, UninstallComponent): def __init__(self, component_name, *args, **kargs): ComponentBase.__init__(self, component_name, *args, **kargs) self.tracereader = TraceReader(self.tracedir, IN_TRACE) def unconfigure(self): self._unconfigure_files() def _unconfigure_files(self): cfgfiles = self.tracereader.files_configured() if(len(cfgfiles)): LOG.info("Removing %s configuration files" % (len(cfgfiles))) for fn in cfgfiles: if(len(fn)): unlink(fn) LOG.info("Removed %s" % (fn)) def uninstall(self): self._uninstall_pkgs() self._uninstall_touched_files() self._uninstall_dirs() def _uninstall_pkgs(self): pkgsfull = self.tracereader.packages_installed() if(len(pkgsfull)): LOG.info("Potentially removing %s packages" % (len(pkgsfull))) self.packager.remove_batch(pkgsfull) def _uninstall_touched_files(self): filestouched = self.tracereader.files_touched() if(len(filestouched)): LOG.info("Removing %s touched files" % (len(filestouched))) for fn in filestouched: if(len(fn)): unlink(fn) LOG.info("Removed %s" % (fn)) def _uninstall_dirs(self): dirsmade = self.tracereader.dirs_made() if(len(dirsmade)): LOG.info("Removing %s created directories" % (len(dirsmade))) for dirname in dirsmade: deldir(dirname) LOG.info("Removed %s" % (dirname))
class DBRuntime(ComponentBase, RuntimeComponent): def __init__(self, *args, **kargs): ComponentBase.__init__(self, TYPE, *args, **kargs) self.tracereader = TraceReader(self.tracedir, IN_TRACE) def _gettypeactions(self, act, exception_cls): pkgsinstalled = self.tracereader.packages_installed() if(len(pkgsinstalled) == 0): msg = "Can not %s %s since it was not installed" % (act, TYPE) raise exception_cls(msg) #figure out how to do it dbtype = self.cfg.get("db", "type") typeactions = DB_ACTIONS.get(dbtype) if(typeactions == None or not typeactions.get(act)): msg = BASE_ERROR % (act, dbtype) raise NotImplementedError(msg) return typeactions.get(act) def start(self): if(self.status().find('start') == -1): startcmd = self._gettypeactions('start', StartException) execute(*startcmd, run_as_root=True) return None def stop(self): if(self.status().find('stop') == -1): stopcmd = self._gettypeactions('stop', StopException) execute(*stopcmd, run_as_root=True) return None def restart(self): restartcmd = self._gettypeactions('restart', RestartException) execute(*restartcmd, run_as_root=True) return None def status(self): statuscmd = self._gettypeactions('status', StatusException) (sysout, stderr) = execute(*statuscmd, run_as_root=True) return sysout.strip()
class DBRuntime(ComponentBase, RuntimeComponent): def __init__(self, *args, **kargs): ComponentBase.__init__(self, TYPE, *args, **kargs) self.tracereader = TraceReader(self.tracedir, IN_TRACE) def _gettypeactions(self, act, exception_cls): pkgsinstalled = self.tracereader.packages_installed() if (len(pkgsinstalled) == 0): msg = "Can not %s %s since it was not installed" % (act, TYPE) raise exception_cls(msg) #figure out how to do it dbtype = self.cfg.get("db", "type") typeactions = DB_ACTIONS.get(dbtype) if (typeactions == None or not typeactions.get(act)): msg = BASE_ERROR % (act, dbtype) raise NotImplementedError(msg) return typeactions.get(act) def start(self): if (self.status().find('start') == -1): startcmd = self._gettypeactions('start', StartException) execute(*startcmd, run_as_root=True) return None def stop(self): if (self.status().find('stop') == -1): stopcmd = self._gettypeactions('stop', StopException) execute(*stopcmd, run_as_root=True) return None def restart(self): restartcmd = self._gettypeactions('restart', RestartException) execute(*restartcmd, run_as_root=True) return None def status(self): statuscmd = self._gettypeactions('status', StatusException) (sysout, stderr) = execute(*statuscmd, run_as_root=True) return sysout.strip()
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)
def __init__(self, component_name, *args, **kargs): ComponentBase.__init__(self, component_name, *args, **kargs) self.tracereader = TraceReader(self.tracedir, IN_TRACE)
def __init__(self, *args, **kargs): ComponentBase.__init__(self, TYPE, *args, **kargs) self.tracereader = TraceReader(self.tracedir, IN_TRACE)
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)