servicemanager/frappyman.py
Markus Zolliker 97af531a5c explicitly define SERV path item in SECOP_CONFDIR
when an item in SECOP_CONFDIR ends with SERV, only the directory
for the service is lookup up and not its parent
2021-04-28 09:18:12 +02:00

118 lines
4.1 KiB
Python

# -*- coding: utf-8 -*-
# *****************************************************************************
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Module authors:
# Markus Zolliker <markus.zolliker@psi.ch>
#
# *****************************************************************************
import sys
import os
from glob import glob
from os.path import join, isdir, basename
from configparser import ConfigParser
from servicemanager.base import ServiceManager, ServiceDown, UsageError
class FrappyManager(ServiceManager):
group = 'frappy'
services = ('main', 'stick', 'addons')
USAGE = """
Usage:
frappy list [<instance>]
frappy start <instance> <service> <cfgfiles>
frappy restart <instance> [<service>] [<cfgfiles>]
frappy stop <instance> [<service>]
frappy listcfg <instance> [<service>] # list available cfg files
<service> is one of main, stick, addons
%s
"""
def config_dirs(self, ins, service):
cfgpaths = []
for cfgpath in self.env[ins].get('SECOP_CONFDIR', '').split(os.pathsep):
if cfgpath.endswith('SERV'):
cfgpaths.append(cfgpath[:-4] + service)
else:
scfg = join(cfgpath, service)
if isdir(scfg):
cfgpaths.append(scfg)
cfgpaths.append(cfgpath)
return cfgpaths
def prepare_start(self, ins, service):
start_dir, env = super().prepare_start(ins, service)
his = env.get('FRAPPY_HISTORY')
if his:
env['FRAPPY_HISTORY'] = his.replace('_SERVICE', '_' + service)
cfgpaths = self.config_dirs(ins, service)
if cfgpaths:
env['SECOP_CONFDIR'] = os.pathsep.join(cfgpaths)
print(env['SECOP_CONFDIR'])
return start_dir, env
def do_gui(self, ins='', service='main'):
start_dir, env = self.prepare_start(ins, service)
sys.path.insert(0, start_dir)
try:
self.check_running(ins, service)
except ServiceDown as e:
raise UsageError('frappy %s %s is not running' % (ins, service))
return
except KeyError:
if not ins:
raise UsageError('missing instance')
raise UsageError('unknown instance %s' % ins)
print('starting frappy gui %s' % ins)
import mlzlog
from secop.gui.qt import QApplication
from secop.gui.mainwindow import MainWindow
mlzlog.initLogging('gui', 'info')
app = QApplication([])
win = MainWindow(['localhost:%d' % self.info[ins][service]])
win.show()
return app.exec_()
def do_listcfg(self, ins='', service='main'):
for cfgdir in self.config_dirs(ins, service):
table = []
for cfgfile in glob(join(cfgdir, '*.cfg')):
desc = ''
try:
parser = ConfigParser()
parser.read(cfgfile)
for s in parser.sections():
if s == 'NODE' or s.startswith('node '):
desc = parser[s].get('description', '').split('\n')[0]
break
except Exception:
pass
table.append((basename(cfgfile)[:-4], desc))
if table:
print('\n--- %s:\n' % cfgdir)
lcol = max(len(c) for c, _ in table)
for cfg, desc in table:
print('%s %s' % (cfg.ljust(lcol), desc))