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/>.
"""
__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)

View file

@ -20,6 +20,27 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
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()

View file

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

View file

@ -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 = "<Lish>: "
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 <ID>:\t\tdisable a job\n\t" + \
"enable <ID>:\t\tenable a job\n\t"
print '''usage:
<ID> prints extended information about this job
<ID> enable: enables a job
<ID> 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"

View file

@ -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