From 240dc8b04aa83f147ea7fa1554c49c82315c1fc0 Mon Sep 17 00:00:00 2001 From: "Rafael.Timmerberg" Date: Thu, 17 Oct 2013 23:36:57 +0200 Subject: [PATCH] extended the job function and added completion started to implement LinspectorInterface and passed it to Frontend and LishCommander as general Linspector accessor --- bin/linspector | 5 ++- linspector/core/interface.py | 27 ++++++++++-- linspector/frontends/frontend.py | 6 +-- linspector/frontends/lish.py | 70 +++++++++++++++++++++++--------- linspector/services/service.py | 4 ++ 5 files changed, 85 insertions(+), 27 deletions(-) diff --git a/bin/linspector b/bin/linspector index 17cb128..089b33d 100755 --- a/bin/linspector +++ b/bin/linspector @@ -19,6 +19,7 @@ You should have received a copy of the GNU Affero General Public License along with this program. If not, see . """ + __version__ = "0.1.7" __default_config__ = "./examples/minimal.json" @@ -28,6 +29,7 @@ import logging.handlers import os import os.path as path +from linspector.core.interface import LinspectorInterface from linspector.config.parser import FullConfigParser from linspector.core.job import Job from linspector.core.scheduler import Scheduler @@ -128,7 +130,8 @@ def main(): take care of signals etc. to sthutdown the threads when stopping linspector. ''' - frontend = LishFrontend(jobs=jobs, scheduler=scheduler, linspectorConfig=linConf) + interface = LinspectorInterface(jobs, scheduler, linConf) + LishFrontend(interface) log.debug("shutting down scheduler") scheduler.shutdown(wait=True) diff --git a/linspector/core/interface.py b/linspector/core/interface.py index c2457d6..8cf3bff 100644 --- a/linspector/core/interface.py +++ b/linspector/core/interface.py @@ -20,6 +20,27 @@ along with this program. If not, see . """ -class Interface(): - def __init__(self): - pass +class LinspectorInterface(object): + def __init__(self, jobs, scheduler, linspectorConfig): + self.jobs = jobs + self._scheduler = scheduler + self._config = linspectorConfig + self._recompute_job_hex_strings() + + def _recompute_job_hex_strings(self): + self.jobHex = [] + for job in self.jobs: + self.jobHex.append(job.hex_string()) + + def get_job_hex_strings(self): + return self.jobHex + + def find_job_by_hex_string(self, hexString): + for job in self.jobs: + if job.hex_string() == hexString: + return job + + def get_enabled_layouts(self): + self._config.get_enabled_layouts() + + diff --git a/linspector/frontends/frontend.py b/linspector/frontends/frontend.py index 42d58e0..162cf51 100644 --- a/linspector/frontends/frontend.py +++ b/linspector/frontends/frontend.py @@ -23,6 +23,6 @@ along with this program. If not, see . """ -class Frontend(): - def __init__(self, **kwargs): - return \ No newline at end of file +class Frontend(object): + def __init__(self, linspectorInterface): + self.interface = linspectorInterface diff --git a/linspector/frontends/lish.py b/linspector/frontends/lish.py index ee425c0..70385d1 100644 --- a/linspector/frontends/lish.py +++ b/linspector/frontends/lish.py @@ -31,12 +31,12 @@ __version__ = "0.1.1" class LishFrontend(Frontend): - def __init__(self, **kwargs): - + def __init__(self, linpsectorInterface): + super(LishFrontend, self) #print(kwargs) #self.jobs = kwargs["jobs"] - commander = LishCommander(kwargs) + commander = LishCommander(linpsectorInterface) run = True while run: try: @@ -55,8 +55,8 @@ class CommandBase(Cmd, object): super(CommandBase, self).__init__() self._needs_update = False - def get_completion(self, args, text, showOnZeroText=True): - if len(text) == 0 and showOnZeroText: + def get_completion(self, args, text): + if len(text) == 0: return args else: return [x for x in args if x.startswith(text)] @@ -127,25 +127,25 @@ class HostgroupCommander(Exit, object): print("gives you control over a member of the hostgroup") + + class LishCommander(Exit, ShellCommander, LogCommander): - def __init__(self, kwargs): + def __init__(self, linspectorInterface): super(LishCommander, self).__init__() self.prompt = ": " - self._linConf = kwargs["linspectorConfig"] - self._jobs = kwargs["jobs"] - self._scheduler = kwargs["scheduler"] + self.interface = linspectorInterface + - self._hostgroupArgs = ["list", "select"] def do_hostgroup(self, text): args = shsplit(text) if args[0] == "list": print("current active Hostgroups:\n") - for l in self._linConf.get_enabled_layouts(): + for l in self.interface.get_enabled_layouts(): print l.get_name() space = 4 * " " for hg in l.get_hostgroups(): @@ -168,7 +168,7 @@ class LishCommander(Exit, ShellCommander, LogCommander): def complete_hostgroup(self, text, line, begidx, endidx): if begidx == 10: - return [x for x in self._hostgroupArgs if x.startswith(text)] if len(text) > 0 else self._hostgroupArgs + return self.get_completion(["list", "select"], text) def help_hostgroup(self): print "usage:\n\t" + \ @@ -183,23 +183,53 @@ class LishCommander(Exit, ShellCommander, LogCommander): def help_python(self): print "executes python using 'exec'." + def print_jobs_infos(self, job): + print "Job: " + job.hex_string() + "\n" + \ + "Host: " + job.host + "\n" + \ + "Service: " + job.service.get_type() + "\n" + \ + "Members: " + str([member.name for member in job.members]) + + + def do_job(self, text): - if text == "disable": - pass - elif text == "enable": - pass + text = shsplit(text) + job_hex = text[0] + job = self.interface.find_job_by_hex_string(job_hex) + + if job is None: + print "first parameter must be a job hex id! get a List of all ids by typing 'jobs list'\n" + self.help_job() + + if len(text) == 1: + self.print_jobs_infos(job) + return + + cmd = text[1] + if cmd in ["enable", "disable"]: + if "n" in cmd: + job.enable() + else: + job.disable() else: print "invalid or missing parameter\n" self.help_job() + def complete_job(self, text, line, begidx, endidx): + if begidx == 4: + return self.get_completion(self.interface.get_job_hex_strings(), text) + elif begidx == 13: + return self.get_completion(["enable", "disable"], text) + def help_job(self): - print "Job helper functions:\n\t" + \ - "disable :\t\tdisable a job\n\t" + \ - "enable :\t\tenable a job\n\t" + print '''usage: + prints extended information about this job + enable: enables a job + disable: disables a job + ''' def do_jobs(self, text): if text == "list": - for job in self._jobs: + for job in self.interface.jobs: print job.pretty_string() else: print "invalid or missing parameter\n" diff --git a/linspector/services/service.py b/linspector/services/service.py index af4b220..e9a9b99 100644 --- a/linspector/services/service.py +++ b/linspector/services/service.py @@ -54,6 +54,9 @@ class Service(object): if KEY_PERIODS in kwargs: self.add_periods(kwargs[KEY_PERIODS]) + def get_type(self): + return str(self.__class__) + def add_arguments(self, args): for key, val in args.items(): self._args[key] = val @@ -126,6 +129,7 @@ class Service(object): + def execute(self, jobInfo): pass