extended the job function and added completion

started to implement LinspectorInterface and passed it to Frontend and LishCommander as general Linspector accessor
This commit is contained in:
Rafael.Timmerberg 2013-10-17 23:36:57 +02:00
commit 240dc8b04a
5 changed files with 85 additions and 27 deletions

View file

@ -19,6 +19,7 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
__version__ = "0.1.7" __version__ = "0.1.7"
__default_config__ = "./examples/minimal.json" __default_config__ = "./examples/minimal.json"
@ -28,6 +29,7 @@ import logging.handlers
import os import os
import os.path as path import os.path as path
from linspector.core.interface import LinspectorInterface
from linspector.config.parser import FullConfigParser from linspector.config.parser import FullConfigParser
from linspector.core.job import Job from linspector.core.job import Job
from linspector.core.scheduler import Scheduler from linspector.core.scheduler import Scheduler
@ -128,7 +130,8 @@ def main():
take care of signals etc. to sthutdown the threads when stopping linspector. 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") log.debug("shutting down scheduler")
scheduler.shutdown(wait=True) scheduler.shutdown(wait=True)

View file

@ -20,6 +20,27 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
class Interface(): class LinspectorInterface(object):
def __init__(self): def __init__(self, jobs, scheduler, linspectorConfig):
pass 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()

View file

@ -23,6 +23,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
class Frontend(): class Frontend(object):
def __init__(self, **kwargs): def __init__(self, linspectorInterface):
return self.interface = linspectorInterface

View file

@ -31,12 +31,12 @@ __version__ = "0.1.1"
class LishFrontend(Frontend): class LishFrontend(Frontend):
def __init__(self, **kwargs): def __init__(self, linpsectorInterface):
super(LishFrontend, self)
#print(kwargs) #print(kwargs)
#self.jobs = kwargs["jobs"] #self.jobs = kwargs["jobs"]
commander = LishCommander(kwargs) commander = LishCommander(linpsectorInterface)
run = True run = True
while run: while run:
try: try:
@ -55,8 +55,8 @@ class CommandBase(Cmd, object):
super(CommandBase, self).__init__() super(CommandBase, self).__init__()
self._needs_update = False self._needs_update = False
def get_completion(self, args, text, showOnZeroText=True): def get_completion(self, args, text):
if len(text) == 0 and showOnZeroText: if len(text) == 0:
return args return args
else: else:
return [x for x in args if x.startswith(text)] 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") print("gives you control over a member of the hostgroup")
class LishCommander(Exit, ShellCommander, LogCommander): class LishCommander(Exit, ShellCommander, LogCommander):
def __init__(self, kwargs): def __init__(self, linspectorInterface):
super(LishCommander, self).__init__() super(LishCommander, self).__init__()
self.prompt = "<Lish>: " self.prompt = "<Lish>: "
self._linConf = kwargs["linspectorConfig"] self.interface = linspectorInterface
self._jobs = kwargs["jobs"]
self._scheduler = kwargs["scheduler"]
self._hostgroupArgs = ["list", "select"]
def do_hostgroup(self, text): def do_hostgroup(self, text):
args = shsplit(text) args = shsplit(text)
if args[0] == "list": if args[0] == "list":
print("current active Hostgroups:\n") 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() print l.get_name()
space = 4 * " " space = 4 * " "
for hg in l.get_hostgroups(): for hg in l.get_hostgroups():
@ -168,7 +168,7 @@ class LishCommander(Exit, ShellCommander, LogCommander):
def complete_hostgroup(self, text, line, begidx, endidx): def complete_hostgroup(self, text, line, begidx, endidx):
if begidx == 10: 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): def help_hostgroup(self):
print "usage:\n\t" + \ print "usage:\n\t" + \
@ -183,23 +183,53 @@ class LishCommander(Exit, ShellCommander, LogCommander):
def help_python(self): def help_python(self):
print "executes python using 'exec'." 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): def do_job(self, text):
if text == "disable": text = shsplit(text)
pass job_hex = text[0]
elif text == "enable": job = self.interface.find_job_by_hex_string(job_hex)
pass
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: else:
print "invalid or missing parameter\n" print "invalid or missing parameter\n"
self.help_job() 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): def help_job(self):
print "Job helper functions:\n\t" + \ print '''usage:
"disable <ID>:\t\tdisable a job\n\t" + \ <ID> prints extended information about this job
"enable <ID>:\t\tenable a job\n\t" <ID> enable: enables a job
<ID> disable: disables a job
'''
def do_jobs(self, text): def do_jobs(self, text):
if text == "list": if text == "list":
for job in self._jobs: for job in self.interface.jobs:
print job.pretty_string() print job.pretty_string()
else: else:
print "invalid or missing parameter\n" print "invalid or missing parameter\n"

View file

@ -54,6 +54,9 @@ class Service(object):
if KEY_PERIODS in kwargs: if KEY_PERIODS in kwargs:
self.add_periods(kwargs[KEY_PERIODS]) self.add_periods(kwargs[KEY_PERIODS])
def get_type(self):
return str(self.__class__)
def add_arguments(self, args): def add_arguments(self, args):
for key, val in args.items(): for key, val in args.items():
self._args[key] = val self._args[key] = val
@ -126,6 +129,7 @@ class Service(object):
def execute(self, jobInfo): def execute(self, jobInfo):
pass pass