diff --git a/__init__.py b/__init__.py index 79e43bf..523c0a5 100644 --- a/__init__.py +++ b/__init__.py @@ -129,10 +129,11 @@ class ServiceManager: result = OrderedDict() parser = ConfigParser(interpolation=None) parser.optionxform = str - parser.read(expanduser('servman.cfg')) + parser.read(expanduser('~/servman.cfg')) defaults = parser['DEFAULT'] self.commands = {} self.revcmd = {} + for ins in parser.sections(): section = dict(parser[ins]) command = section.get('%s_command' % self.group) @@ -140,26 +141,27 @@ class ServiceManager: if self.group in section: self.commands[ins] = command services = self.get_services(parser[ins]) - env = {k: expanduser(section.get(k)) for k in defaults if k.isupper()} + env = {k: expanduser(section.get(k) % dict(ins=ins)) + for k in defaults if k.isupper()} result[ins] = services self.env[ins] = env self.info = result - def get_cmdpats(self, groups): - return self.cmdpats + #def get_cmdpats(self, groups): + # return self.cmdpats def get_ins_info(self, ins): self.get_info() return self.info[ins] - def get_cfg(self, cmd): - """return info about running program, if relevant + #def get_cfg(self, service, cmd): + # """return info about running program, if relevant + # + # example for frappy: return cfg + # """ + # return '' - example for frappy: return cfg - """ - return '' - - def get_procs(self, groups=None): + def get_procs(self, groups=None, cfginfo=None): """return processes result is a dict[ins] of dict[service] of list of tuples (process, cfg) @@ -190,6 +192,8 @@ class ServiceManager: gdict = match.groupdict() ins = gdict['ins'] serv = gdict['serv'] + if cfginfo is not None and 'cfg' in gdict: + cfginfo[ins, serv] = gdict['cfg'] result.setdefault(ins, {}).setdefault(serv, []).append(p) return result @@ -335,7 +339,8 @@ class ServiceManager: def do_list(self, ins=None, *args): """info about running services""" - procs = self.get_procs(self.all) + cfginfo = {} + procs = self.get_procs(self.all, cfginfo) rows = [] merged = OrderedDict() show_unused = ins == 'all' @@ -361,15 +366,14 @@ class ServiceManager: if plist: if sm == self: show_ins = True - cfg = sm.get_cfg(' '.join(plist[0].info['cmdline'])) gs = '%s %s' % (group, serv) port = str(port or '') - run_info.append(('', gs, port, cfg)) + run_info.append(('', gs, port, cfginfo.get((ins_i, serv), ''))) if len(plist) > 1: rows.append(['', ' WARNING: multiple processes %s' % ', '.join(str(p.pid) for p, _ in plist)]) extra = sm.extra_info(ins_i) - if extra and run_info: + if extra and show_ins: run_info.append(['', extra]) if show_ins: rows.extend(run_info) diff --git a/bin/sea b/bin/sea index 481d60c..766f8bf 100755 --- a/bin/sea +++ b/bin/sea @@ -25,14 +25,19 @@ import os from os.path import join, abspath, split, dirname, expanduser import sys -nicos_root = os.environ.get('NICOS_ROOT', expanduser('~/nicos')) -pkg_dir = abspath(join(nicos_root, 'nicos_linse')) -sys.path.insert(1, join(pkg_dir, 'common')) +#nicos_root = os.environ.get('NICOS_ROOT', expanduser('~/nicos')) +#pkg_dir = abspath(join(nicos_root, 'nicos_linse')) +#sys.path.insert(1, join(pkg_dir, 'common')) -from clitools import FrappyManager, NicosManager, SeaManager, run +sys.path.insert(1, abspath(join(__file__, '../../..'))) -NicosManager(pkg_dir) -FrappyManager(pkg_dir) +from servman import run +from servman.frappy import FrappyManager +from servman.nicos import NicosManager +from servman.sea import SeaManager + +NicosManager() +FrappyManager() serv = SeaManager() USAGE = """ diff --git a/frappy.py b/frappy.py index 53a749f..e690724 100644 --- a/frappy.py +++ b/frappy.py @@ -20,16 +20,10 @@ # # ***************************************************************************** +import re from servman import ServiceManager class FrappyManager(ServiceManager): group = 'frappy' services = ('main', 'stick', 'addons') - - def get_cfg(self, cmd): - if cmd: - match = self.cmdpat.match(cmd) - if match: - return match.groupdict().get('cfg') - return '' diff --git a/nicos.py b/nicos.py index c64fc41..eea4345 100644 --- a/nicos.py +++ b/nicos.py @@ -233,6 +233,7 @@ class NicosManager(ServiceManager): env = self.prepare_start(ins)[1] os.environ.update(env) os.chdir(join(os.environ['NICOS_ROOT'], env['NICOS_PACKAGE'])) + sys.path.insert(0, os.environ['NICOS_ROOT']) def run_client(self, ins, main, app, **kwargs): serverhost = os.environ.get('NICOS_SERVER_HOST', 'localhost') @@ -248,8 +249,10 @@ class NicosManager(ServiceManager): def do_gui(self, ins): self.prepare_client(ins) + print(os.getcwd()) from nicos.clients.gui.main import main - print('starting nicos gui %s' % ins, expanduser('~/.config/nicos_%s' % ins)) - self.run_client(ins, main, 'nicos-gui', instance=ins) + userpath = expanduser('~/.config/nicos_%s' % ins) + print('starting nicos gui %s' % ins, userpath) + self.run_client(ins, main, 'nicos-gui', userpath=userpath) diff --git a/sea.py b/sea.py index 8f63003..c2674fa 100644 --- a/sea.py +++ b/sea.py @@ -48,15 +48,25 @@ class SeaManager(ServiceManager): services = ('sea', 'graph') def do_cli(self, ins): - self.check_running(ins, 'sea') - run_command('six -sea %s' % ins, wait=True) - - def do_gui(self, ins): try: self.check_running(ins, 'sea') except ServiceDown as e: print('%s, try to start...' % e) self.do_start(ins) time.sleep(1) # make sure caller did read the message + except KeyError as e: # running on an other machine? + pass + run_command('six -sea %s' % ins, wait=True) + + def do_gui(self, ins=''): + try: + self.check_running(ins, 'sea') + except ServiceDown as e: + print('%s, try to start...' % e) + self.do_start(ins) + time.sleep(1) # make sure caller did read the message + except KeyError as e: # running on an other machine? + pass run_command('SeaClient %s' % ins) print('starting sea gui %s' % ins) + time.sleep(5) diff --git a/sicsclient.py b/sicsclient.py new file mode 100644 index 0000000..d559e10 --- /dev/null +++ b/sicsclient.py @@ -0,0 +1,62 @@ +import socket + + +def raw_sics_client(hostport): + if ':' in hostport: + host, port = hostport.split(':') + hostport = (host, int(port)) + sock = socket.create_connection(hostport, timeout=3) + bbuf = b'' + while True: + try: + reply = sock.recv(8192) + # print(reply.decode('latin-1'), end='') + if not reply: + sock.close() + return + except socket.timeout: + sock.close() + return + bbuf += reply + blist = bbuf.split(b'\n') + bbuf = blist.pop() + for bline in blist: + request = yield bline.decode('latin-1') + if request is not None: + sock.sendall(request.encode('latin-1') + b'\n') + + +def check(sics, command, expected): + reply = sics.send(command) + if reply != expected: + raise ValueError('expected %r but got %r' % (expected, reply)) + + +def sics_coroutine(hostport, login): + sics = raw_sics_client(hostport) + check(sics, None, 'OK') + check(sics, login, 'Login OK') + request = yield + while True: + reply = sics.send('fulltransact %s' % request) + while not reply.startswith('TRANSACTIONSTART'): + reply = next(sics) + reply = next(sics) + result = [] + while not reply.startswith('TRANSACTIONFINISHED'): + result.append(reply) + reply = next(sics) + request = yield '\n'.join(result) + + +def sics_client(hostport, login='Spy 007'): + sics = sics_coroutine(hostport, login) + next(sics) # start generator + return sics + + +def sics_command(hostport, command, login='Spy 007'): + sics = sics_client(hostport, login) + result = sics.send(command) + sics.close() + return result