allow to use one central cfg file on NFS

This commit is contained in:
2026-02-17 14:24:40 +01:00
parent 886d25ccee
commit 2f17a8ca28

39
base.py
View File

@@ -24,7 +24,7 @@
import sys
import json
import os
from os.path import expanduser, basename, exists
from os.path import expanduser, basename, exists, isdir
import subprocess
import time
import re
@@ -69,8 +69,12 @@ def printTable(headers, items, printfunc, minlen=0, rjust=False):
def get_config():
parser = ConfigParser(interpolation=None)
parser.optionxform = str
parser.read(expanduser('~/servicemanager.cfg'))
return parser
cfgfiles = '/sq_sw/linse/servicemanager.cfg', expanduser('~/servicemanager.cfg')
for cfgfile in cfgfiles:
if isdir(cfgfile):
parser.read(cfgfile)
return parser
raise FileNotFoundError(f'can not find cfg file in {cfgfiles}')
class ServiceManager:
@@ -85,10 +89,10 @@ class ServiceManager:
USAGE = None
main_ins = None
single_ins = None
is_remote = False
def __init__(self):
self.env = {}
self.remote_hosts = {}
self.commands = {}
# self.revcmd = {}
self.info = {}
@@ -136,12 +140,12 @@ class ServiceManager:
value = value.replace('<INS>', ins)
return ':'.join(expanduser(p % dict(ins=ins)) for p in value.split(':'))
this_ins = os.environ.get('Instrument') or basename(expanduser('~'))
for ins in parser.sections():
section = dict(parser[ins])
if ins == 'MAIN':
ins = self.main_ins = os.environ.get('Instrument') or basename(expanduser('~'))
if 'REMOTE_HOST' in section:
self.remote_hosts[ins] = section['REMOTE_HOST']
if ins == this_ins:
self.main_ins = this_ins
self.is_remote = socket.gethostname().split('.')[0] != section.get('host')
command = section.get('%s_command' % self.group)
self.revcmd[command] = self.group
nr = section.get(self.group)
@@ -151,7 +155,7 @@ class ServiceManager:
env = {k: get_subs(section, k, ins, nr) for k in section if k.isupper()}
result[ins] = services
self.env[ins] = env
cmd = command.replace('~', expanduser('~'))
cmd = command.replace('~', expanduser('~'))
if cmd.startswith('PY '):
cmd = env.get('PY', 'python3') + cmd[2:]
self.commands[ins] = cmd
@@ -239,7 +243,7 @@ class ServiceManager:
if ins not in self.info:
raise KeyError("don't know %r" % ins)
sp_ins = ' ' + ins if ins != self.main_ins else ''
if ins in self.remote_hosts:
if self.is_remote:
return
if not self.get_procs().get(ins, {}).get(service):
startcmd = '%s start%s' % (self.group, sp_ins)
@@ -302,10 +306,15 @@ class ServiceManager:
if not ins:
raise UsageError('need instance')
env = self.env[ins]
startdir = env.get('%s_ROOT' % gr, '')
if startdir not in sys.path:
sys.path.insert(0, startdir)
return startdir, env
start_dir_path = env.get('%s_ROOT' % gr, '')
for start_dir in start_dir_path.split(':'):
if isdir(start_dir):
break
else:
raise FileNotFoundError(f'can not find any of {start_dir_path}')
if start_dir not in sys.path:
sys.path.insert(0, start_dir)
return start_dir, env
def do_start(self, ins, service=None, cfg='', restart=False, wait=False, logger=None, opts=''):
if not ins:
@@ -336,7 +345,7 @@ class ServiceManager:
service_ports = self.get_ins_info(ins)
except (KeyError, ValueError):
raise UsageError('do not know %r' % ins)
if ins in self.remote_hosts:
if self.is_remote:
raise UsageError('can not start, %s is running on a remote host' % self.group)
services = list(service_ports) if service is None else [service]
if restart: