Compare commits

..

2 Commits
master ... tmp

Author SHA1 Message Date
ec52f6f858 add getsestuff related stuff 2025-04-29 11:16:42 +02:00
d9877c1712 keep gesestuff in servicemanager
- still need to copy to
  /afs/psi.ch/project/sinq/common/stow/markus/bin/
2025-04-29 09:28:36 +02:00
17 changed files with 111 additions and 386 deletions

View File

@ -3,5 +3,3 @@
A manager for starting, stopping and listing services/servers like frappy, nicos and sea. A manager for starting, stopping and listing services/servers like frappy, nicos and sea.
Several instances of nicos, frappy, sea and seweb might run on the same machine. Several instances of nicos, frappy, sea and seweb might run on the same machine.
contains also getsestuff, the script to manage the repos on all instruments

View File

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# ***************************************************************************** # *****************************************************************************
# #
# This program is free software; you can redistribute it and/or modify it under # This program is free software; you can redistribute it and/or modify it under
@ -26,19 +27,28 @@ this code is currently used:
- from a script allowing to start/stop/list (and more) multiple frappy and nicos servers - from a script allowing to start/stop/list (and more) multiple frappy and nicos servers
""" """
import sys
from servicemanager.base import ServiceManager, ServiceDown, UsageError, get_config from servicemanager.base import ServiceManager, ServiceDown, UsageError, get_config
from servicemanager.nicosman import NicosManager from servicemanager.nicosman import NicosManager
from servicemanager.seaman import SeaManager from servicemanager.seaman import SeaManager
from servicemanager.frappyman import FrappyManager, Reconnect, Keep from servicemanager.frappyman import FrappyManager, Reconnect, Keep
from servicemanager.single import SewebManager, FeederManager
#from servicemanager.racks import RackConfig
#rack = RackConfig() class SewebManager(ServiceManager):
group = 'seweb'
services = ('',)
USAGE = """
Usage:
all = NicosManager, FrappyManager, SeaManager, SewebManager, FeederManager seaweb list [instance]
KINDS = 'action', 'ins', 'service' seaweb start <instance>
seaweb restart <instance>
seaweb stop <instance>
%s
"""
all = NicosManager, FrappyManager, SeaManager, SewebManager
def run(group, arglist): def run(group, arglist):
@ -58,7 +68,7 @@ def run(group, arglist):
'service': lambda arg: arg in serv.services, 'service': lambda arg: arg in serv.services,
} }
for kind in KINDS: for kind in 'action', 'ins', 'service':
if arglist and arg_is[kind](arglist[0]): # arg is of expected kind if arglist and arg_is[kind](arglist[0]): # arg is of expected kind
args[kind] = arglist.pop(0) args[kind] = arglist.pop(0)
continue continue
@ -81,11 +91,8 @@ def run(group, arglist):
args.setdefault('ins', serv.main_ins) args.setdefault('ins', serv.main_ins)
if guessed_args: if guessed_args:
args.setdefault('action', 'gui') args.setdefault('action', 'gui')
guessed = [group] + [args.get(k, '') for k in KINDS] + extra print('do you mean:\n %s %s %s %s %s' %
while not guessed[-1]: (group, args.get('action', ''), args.get('ins', ''), args.get('service', ''), ' '.join(extra)))
guessed.pop()
guessed = ' '.join(a or '?' for a in guessed)
print(f'do you mean:\n {guessed}')
else: else:
try: try:
serv.action(args['action'], *serv.treat_args(args, extra + arglist)) serv.action(args['action'], *serv.treat_args(args, extra + arglist))
@ -94,4 +101,5 @@ def run(group, arglist):
except UsageError as e: except UsageError as e:
serv.do_help() serv.do_help()
print('ERROR:', str(e)) print('ERROR:', str(e))
sys.exit(1)

View File

@ -1,12 +0,0 @@
import os
instruments = ['amor', 'camea', 'dmc', 'eiger', 'focus', 'hrpt', 'boa', 'sans', 'tasp', 'zebra'] # morpheus
def influx_setup(instrument=None):
ins_list = instruments if instrument is None else [instrument]
for ins in ins_list:
print(ins)
os.system(f'ssh {ins} influx setup --username l_samenv --password LIN3601se '
'--token lZpLL_FrPw3cgkeuOWo_DcwjFZerrVTa1QQk6bcjSOYgjQ8W0eyvsp4Z4FULvsz2XEWTZZ8OPengYfLaWU-gdA== '
f'--org linse --bucket sehistory --force --name {ins}')

38
base.py
View File

@ -24,7 +24,7 @@
import sys import sys
import json import json
import os import os
from os.path import expanduser, basename, exists from os.path import expanduser, basename
import subprocess import subprocess
import time import time
import re import re
@ -84,7 +84,6 @@ class ServiceManager:
revcmd = {} revcmd = {}
USAGE = None USAGE = None
main_ins = None main_ins = None
single_ins = None
def __init__(self): def __init__(self):
self.env = {} self.env = {}
@ -147,18 +146,12 @@ class ServiceManager:
nr = section.get(self.group) nr = section.get(self.group)
if nr is not None: if nr is not None:
nr = '%02d' % int(nr) nr = '%02d' % int(nr)
self.commands[ins] = command.replace('~', expanduser('~'))
services = self.get_services(section) services = self.get_services(section)
env = {k: get_subs(section, k, ins, nr) for k in section if k.isupper()} env = {k: get_subs(section, k, ins, nr) for k in section if k.isupper()}
result[ins] = services result[ins] = services
self.env[ins] = env self.env[ins] = env
cmd = command.replace('~', expanduser('~'))
if cmd.startswith('PY '):
cmd = env.get('PY', 'python3') + cmd[2:]
self.commands[ins] = cmd
self.info = result self.info = result
if len(self.info) == 1:
self.single_ins = list(self.info)[0]
return result
#def get_cmdpats(self, groups): #def get_cmdpats(self, groups):
# return self.cmdpats # return self.cmdpats
@ -214,7 +207,7 @@ class ServiceManager:
match = cmdpat.match(cmd) match = cmdpat.match(cmd)
if match: if match:
gdict = match.groupdict() gdict = match.groupdict()
ins = gdict.get('ins', self.main_ins) or self.single_ins ins = gdict.get('ins', self.main_ins)
serv = gdict.get('serv', '') serv = gdict.get('serv', '')
if cfginfo is not None and 'cfg' in gdict: if cfginfo is not None and 'cfg' in gdict:
cfginfo[ins, serv] = gdict['cfg'] cfginfo[ins, serv] = gdict['cfg']
@ -226,7 +219,7 @@ class ServiceManager:
or None, when no wildcard character in ins or None, when no wildcard character in ins
""" """
if not ins or ins == 'all': if ins is None or ins == 'all':
return list(self.info) return list(self.info)
pat = re.sub(r'(\.|\*)', '.*', ins) pat = re.sub(r'(\.|\*)', '.*', ins)
if pat == ins: if pat == ins:
@ -286,9 +279,9 @@ class ServiceManager:
return done return done
def do_stop(self, ins, service=None, *args): def do_stop(self, ins, service=None, *args):
if not ins:
raise UsageError(f'need instrument or "all" to stop all')
self.get_info() self.get_info()
if ins is None:
raise ValueError('use stop all if you really want to stop all')
ins_list = self.wildcard(ins) ins_list = self.wildcard(ins)
if ins_list is not None: if ins_list is not None:
return ins_list return ins_list
@ -305,8 +298,6 @@ class ServiceManager:
return env.get('%s_ROOT' % gr, ''), env return env.get('%s_ROOT' % gr, ''), env
def do_start(self, ins, service=None, cfg='', restart=False, wait=False, logger=None, opts=''): def do_start(self, ins, service=None, cfg='', restart=False, wait=False, logger=None, opts=''):
if not ins:
raise UsageError(f'need instrument or "all" to start all')
ins_list = self.wildcard(ins) ins_list = self.wildcard(ins)
if ins_list is not None: if ins_list is not None:
return ins_list return ins_list
@ -325,10 +316,10 @@ class ServiceManager:
return return
try: try:
service_ports = self.get_ins_info(ins) service_ports = self.get_ins_info(ins)
except (KeyError, ValueError): except ValueError:
raise UsageError('do not know %r' % ins) raise ValueError('do not know %r' % ins)
if ins in self.remote_hosts: if ins in self.remote_hosts:
raise UsageError('can not start, %s is running on a remote host' % self.group) raise ValueError('can not start, %s is running on a remote host' % self.group)
services = list(service_ports) if service is None else [service] services = list(service_ports) if service is None else [service]
if restart: if restart:
self.stop(ins, service) self.stop(ins, service)
@ -345,7 +336,6 @@ class ServiceManager:
services = to_start services = to_start
for service_i in services: for service_i in services:
port = service_ports[service_i] port = service_ports[service_i]
# TODO: remove unused pkg
cmd = self.commands[ins] % dict(ins=ins, serv=service_i, port=port, cfg=cfg, pkg=self.pkg or ins) cmd = self.commands[ins] % dict(ins=ins, serv=service_i, port=port, cfg=cfg, pkg=self.pkg or ins)
if opts: if opts:
cmd = f'{cmd} {opts}' cmd = f'{cmd} {opts}'
@ -363,9 +353,6 @@ class ServiceManager:
os.chdir(start_dir) os.chdir(start_dir)
if start_dir not in sys.path: if start_dir not in sys.path:
sys.path.insert(0, start_dir) sys.path.insert(0, start_dir)
nicosenv = '/home/nicos/nicos/nicosenv/bin/'
if exists(nicosenv):
env['PATH'] = f"{nicosenv}:{env['PATH']}"
if wait: if wait:
proc = subprocess.Popen(cmd.split(), env=env) proc = subprocess.Popen(cmd.split(), env=env)
for _ in range(3): for _ in range(3):
@ -412,8 +399,8 @@ class ServiceManager:
os.chdir(wd) os.chdir(wd)
def do_restart(self, ins, service=None, cfg=None, logger=None): def do_restart(self, ins, service=None, cfg=None, logger=None):
if not ins: if ins is None:
raise UsageError("need instrument or 'all' or wildcard") raise UsageError("need instance or 'all' or wildcard")
ins_list = self.wildcard(ins) ins_list = self.wildcard(ins)
if ins_list is not None: if ins_list is not None:
if cfg is not None: if cfg is not None:
@ -424,7 +411,7 @@ class ServiceManager:
def do_run(self, ins, service=None, cfg=None, opts=''): def do_run(self, ins, service=None, cfg=None, opts=''):
"""for tests: run and wait""" """for tests: run and wait"""
if self.wildcard(ins) is not None: if self.wildcard(ins) is not None:
raise UsageError('need instrument and service for "%s run"' % self.group) raise UsageError('no wildcards allowed with %s run' % self.group)
if not service: if not service:
try: try:
service, = self.services service, = self.services
@ -434,7 +421,6 @@ class ServiceManager:
def do_list(self, ins=None, *args): def do_list(self, ins=None, *args):
"""info about running services""" """info about running services"""
self.get_info()
show_unused = ins == 'all' show_unused = ins == 'all'
if show_unused: if show_unused:
ins = None ins = None

View File

@ -1,8 +0,0 @@
#!/usr/bin/env python3
import sys
from os.path import expanduser
sys.path.append(expanduser('~'))
from servicemanager import run
run('feeder', sys.argv[1:])

View File

@ -1,4 +1,4 @@
#!/home/software/virtualenv/nicosenv/bin/python3 #!/home/nicos/nicos/nicosenv/bin/python3
import sys import sys
import time import time
from os import path from os import path
@ -15,8 +15,8 @@ Usage:
nicos cli start nicos command line client and connect without asking for password nicos cli start nicos command line client and connect without asking for password
""" """
# yr = time.strftime('%y') yr = time.strftime('%y')
connect = f'user:sinq@localhost' connect = f'user:{yr}lns1@localhost'
if sys.argv[-1] == 'cli': if sys.argv[-1] == 'cli':
from nicos.clients.cli import main from nicos.clients.cli import main

View File

@ -1,11 +0,0 @@
[Unit]
Description=sehistory feeder process
[Service]
Type=simple
WorkingDirectory=%h/sehistory
ExecStart=%h/sevenv/bin/python feeder.py -d %i
Restart=always
[Install]
WantedBy=default.target

View File

@ -1,7 +1,7 @@
[hrpt2] [hrpt2]
SEA_SEA_PORT = 8652 SEA_SEA_PORT = 8642
SEA_GRAPH_PORT = 8752 SEA_GRAPH_PORT = 8742
sea_command = ./SeaServer %(serv)s_%(ins)s.tcl sea_command = ./SeaServer %(serv)s_%(ins)s.tcl
sea = 2 sea = 2

View File

@ -1,11 +0,0 @@
[local]
uri=http://localhost:8086
bucket=sehistory
org=linse
token=lZpLL_FrPw3cgkeuOWo_DcwjFZerrVTa1QQk6bcjSOYgjQ8W0eyvsp4Z4FULvsz2XEWTZZ8OPengYfLaWU-gdA==
[central]
uri=http://linse-c:8086
bucket=sehistory
org=linse
token=ggo2cRoCIkgniXdUGi-s6oxXJPPDzgKEFHh6PXuBg1QQa3aBo36tGFP_6cf50FRNoDQgGcelh3xBM5QsVw4rHA==

View File

@ -10,7 +10,7 @@ FRAPPY_LOGDIR = ~/frappylog
FRAPPY_PIDDIR = ~/frappylog/pid FRAPPY_PIDDIR = ~/frappylog/pid
FRAPPY_SEA_DIR = ~/frappy/cfg/sea FRAPPY_SEA_DIR = ~/frappy/cfg/sea
FRAPPY_CALIB_PATH = ~/calcurves FRAPPY_CALIB_PATH = ~/calcurves
frappy_command = ~/sevenv/bin/python bin/frappy-server %(ins)s_%(serv)s -p=%(port)s -c=%(cfg)s frappy_command = bin/frappy-server %(ins)s_%(serv)s -p=%(port)s -c=%(cfg)s
SEA_SEA_PORT = 8641 SEA_SEA_PORT = 8641
SEA_GRAPH_PORT = 8741 SEA_GRAPH_PORT = 8741
@ -18,17 +18,12 @@ SEA_ROOT = ~/sea
sea_command = ./SeaServer %(serv)s.tcl sea_command = ./SeaServer %(serv)s.tcl
SEWEB_ROOT = ~/seweb SEWEB_ROOT = ~/seweb
SEWEB_PORT = 8642 SEWEB_PORT = 8941
seweb_command = ~/sevenv/bin/python secop-webserver %(port)s -i %(ins)s SEWEB_HISTORY = ~/seweb/history
seweb_command = python3 seweb.py frappy ins=%(ins)s port=%(port)s
FEEDER_ROOT = ~/sehistory PYTHONPATH = ~
FEEDER_PORT = 0
feeder_command = ~/sevenv/bin/python feeder.py -d %(serv)s
PYTHONPATH = ~:~/frappy
[MAIN] [MAIN]
frappy = 1 frappy = 1
sea = 1 sea = 1
seweb = 1
feeder = 1

View File

@ -1,11 +0,0 @@
[Unit]
Description=SE web server
[Service]
Type=simple
WorkingDirectory=%h/seweb
ExecStart=%h/sevenv/bin/python secop-webserver 8642 -i %i
Restart=always
[Install]
WantedBy=default.target

View File

@ -1,7 +1,7 @@
[zebra2] [zebra2]
SEA_SEA_PORT = 8652 SEA_SEA_PORT = 8642
SEA_GRAPH_PORT = 8752 SEA_GRAPH_PORT = 8742
sea_command = ./SeaServer %(serv)s_%(ins)s.tcl sea_command = ./SeaServer %(serv)s_%(ins)s.tcl
sea = 2 sea = 2

View File

@ -37,12 +37,9 @@ STICK = 2
class Namespace(dict): class Namespace(dict):
def __init__(self, frappymanager, *args): def __init__(self):
self.fm = frappymanager
self.args = args
self['Node'] = self.node self['Node'] = self.node
self['Mod'] = self.mod self['Mod'] = self.mod
self['Include'] = self.include
for fun in 'Param', 'Command', 'Group': for fun in 'Param', 'Command', 'Group':
self[fun] = self.dummy self[fun] = self.dummy
self.init() self.init()
@ -62,12 +59,6 @@ class Namespace(dict):
def dummy(self, *args, **kwds): def dummy(self, *args, **kwds):
return None return None
def include(self, cfg):
local = {}
print('INCLUDE', self.args, cfg, self.fm.get_cfg_file(*self.args, cfg, True))
with open(self.fm.get_cfg_file(*self.args, cfg, True), encoding='utf-8') as f:
exec(f.read(), self, local)
__builtins__ = builtins __builtins__ = builtins
@ -123,19 +114,16 @@ class FrappyManager(ServiceManager):
cfgpaths = [] cfgpaths = []
cfgparser = ConfigParser() cfgparser = ConfigParser()
cfgparser.optionxform = str cfgparser.optionxform = str
env = self.env.get(ins, {}) cfgfile = self.env[ins].get('FRAPPY_CONFIG_FILE')
cfgfile = env.get('FRAPPY_CONFIG_FILE') confdir = self.env[ins].get('FRAPPY_CONFDIR')
confdir = env.get('FRAPPY_CONFDIR')
if cfgfile: if cfgfile:
cfgfile = cfgfile.replace('<SERV>', service) cfgfile = self.env[ins]['FRAPPY_CONFIG_FILE'].replace('<SERV>', service)
cfgparser.read(cfgfile) cfgparser.read(cfgfile)
try: try:
section = cfgparser['FRAPPY'] section = cfgparser['FRAPPY']
except KeyError: except KeyError:
raise ValueError('%s does not exist or has no FRAPPY section' % cfgfile) raise ValueError('%s does not exist or has no FRAPPY section' % cfgfile)
confdir = section.get('confdir', confdir) confdir = section.get('confdir', confdir)
if not confdir:
return []
for cfgpath in confdir.split(os.pathsep): for cfgpath in confdir.split(os.pathsep):
if cfgpath.endswith('<SERV>'): if cfgpath.endswith('<SERV>'):
cfgpaths.append(expanduser(cfgpath[:-6] + service)) cfgpaths.append(expanduser(cfgpath[:-6] + service))
@ -219,9 +207,6 @@ class FrappyManager(ServiceManager):
nodes = self.get_nodes(ins, service) nodes = self.get_nodes(ins, service)
from frappy.client.interactive import init, interact from frappy.client.interactive import init, interact
init(*nodes) init(*nodes)
try:
interact(appname=ins)
except TypeError: # older frappy client
interact() interact()
@staticmethod @staticmethod
@ -234,25 +219,19 @@ class FrappyManager(ServiceManager):
return namespace.description, local.get('sea_cfg', namespace.sea_cfg) return namespace.description, local.get('sea_cfg', namespace.sea_cfg)
def cfg_details(self, ins, service, cfg): def cfg_details(self, ins, service, cfg):
namespace = Namespace(self, ins, service) namespace = Namespace()
if cfgfile: for cfgdir in self.config_dirs(ins, service):
cfgfile = join(cfgdir, f'{cfg}_cfg.py')
if exists(cfgfile):
return self.get_cfg_details(namespace, cfgfile) return self.get_cfg_details(namespace, cfgfile)
raise FileNotFoundError(f'{cfg} not found') raise FileNotFoundError(f'{cfg} not found')
def get_cfg_file(self, ins, service, cfg, lazy=False):
filenames = [f'{cfg}_cfg.py']
if lazy:
filenames.extend([f'{cfg}.py', cfg])
for cfgdir in self.config_dirs(ins, service):
for filename in filenames:
cfgfile = join(cfgdir, filename)
if exists(cfgfile):
return cfgfile
print('NOT FOUND', cfg, self.config_dirs(ins, service), filenames)
return None
def is_cfg(self, ins, service, cfg): def is_cfg(self, ins, service, cfg):
return bool(self.get_cfg_file(ins, service, cfg)) try:
self.cfg_details(ins, service, cfg)
return True
except Exception:
return False
def all_cfg(self, ins, service, details=False): def all_cfg(self, ins, service, details=False):
"""get available cfg files """get available cfg files
@ -270,7 +249,7 @@ class FrappyManager(ServiceManager):
all_cfg = set() all_cfg = set()
if not ins: if not ins:
return {} return {}
namespace = Namespace(self, ins, service) namespace = Namespace()
if details: if details:
self.frappy2sea = f2s = {} self.frappy2sea = f2s = {}
self.sea2frappy = s2f = {} self.sea2frappy = s2f = {}
@ -341,7 +320,7 @@ class FrappyManager(ServiceManager):
argdict['service'] = cfg argdict['service'] = cfg
return super().treat_args(argdict, (), extra) return super().treat_args(argdict, (), extra)
if (',' in cfg or cfg.endswith('.cfg') or if (',' in cfg or cfg.endswith('.cfg') or
self.get_cfg_file(argdict.get('ins'), argdict.get('service'), cfg, True)): self.is_cfg(argdict.get('ins'), argdict.get('service'), cfg)):
return super().treat_args(argdict, (), [cfg] + extra) return super().treat_args(argdict, (), [cfg] + extra)
return super().treat_args(argdict, unknown, extra) return super().treat_args(argdict, unknown, extra)
@ -425,6 +404,7 @@ class FrappyManager(ServiceManager):
if proposed_addons: # and set(proposed_addons) != set(running_addons): if proposed_addons: # and set(proposed_addons) != set(running_addons):
proposed_cfg['addons'] = {','.join(proposed_addons)} proposed_cfg['addons'] = {','.join(proposed_addons)}
self._debug = {}
for service in FrappyManager.services: for service in FrappyManager.services:
given = givencfgs.get(service) given = givencfgs.get(service)
running = self.frappy_cfgs.get(service) running = self.frappy_cfgs.get(service)
@ -433,6 +413,8 @@ class FrappyManager(ServiceManager):
if running: if running:
self.state[f'frappy {service}'] = running self.state[f'frappy {service}'] = running
self._debug[service] = (seaconfig, available, running, running in self.frappy2sea)
if seaconfig and (available or running in self.frappy2sea): if seaconfig and (available or running in self.frappy2sea):
# we get here when the sea server is running and either at least one of: # we get here when the sea server is running and either at least one of:
# - the sea config is matching any frappy cfg # - the sea config is matching any frappy cfg

View File

@ -10,7 +10,7 @@ from subprocess import Popen, PIPE, check_output
from glob import glob from glob import glob
from socket import gethostname from socket import gethostname
instruments = ['camea', 'dmc', 'eiger', 'focus', 'hrpt', 'sans', 'tasp', 'zebra', 'boa', 'amor'] instruments = ['amor', 'camea', 'dmc', 'eiger', 'focus', 'hrpt', 'morpheus', 'sans', 'tasp', 'zebra'] # boa
stuffsrc = '/afs/psi.ch/project/sinq/common/lib/servicemanager/' stuffsrc = '/afs/psi.ch/project/sinq/common/lib/servicemanager/'
@ -41,7 +41,6 @@ if home.endswith('zolliker'):
if remote: if remote:
instrument = '' instrument = ''
nicosroot = '' nicosroot = ''
nicosenv = ''
else: else:
nicosroot = '/home/nicos/nicos' nicosroot = '/home/nicos/nicos'
if not exists(f'{nicosroot}/.git/config'): if not exists(f'{nicosroot}/.git/config'):
@ -93,15 +92,13 @@ def do(cmd):
def docopy(src, dst): def docopy(src, dst):
if not os.system(f'diff {dst} {src}'): if os.system(f'diff {dst} {src}'):
return False
if doit: if doit:
if not do(f'cp {src} {dst}'): if not do(f'cp {src} {dst}'):
do(f'mv {dst} {dst}0') do(f'mv {dst} {dst}0')
do(f'cp {src} {dst}') do(f'cp {src} {dst}')
else: else:
todo.add(action) todo.add(action)
return True
def dolink(dst, src): def dolink(dst, src):
@ -155,6 +152,19 @@ def check_repo(root, repo, url=None, branch=None):
print(gitconfig.get('remote.origin.url'), 'does not match', url) print(gitconfig.get('remote.origin.url'), 'does not match', url)
print(f'{join(root, repo)} exists already') print(f'{join(root, repo)} exists already')
return False return False
# if repo == 'sea':
# do('mkdir seagit')
# if sim:
# print('> cd seagit')
# else:
# chdir('seagit')
# do('git clone %s' % url)
# do(f'mv sea/.git ../sea/.git')
# if sim:
# print('> cd ..')
# else:
# chdir('..')
# do(f'rm -rf seagit')
else: else:
do('git clone %s' % url) do('git clone %s' % url)
pull = True pull = True
@ -270,35 +280,12 @@ def do_sshnicos():
do_ssh() do_ssh()
def do_sevenv():
"""install python packages needed for seweb etc."""
sevenv = join(home, 'sevenv')
if not exists(join(sevenv, 'bin')):
if exists(nicosenv):
# start from nicos env if possible
py = join(nicosenv, 'bin/python')
else:
py = 'python3'
do(f'{py} -m venv {sevenv}')
if exists(join(sevenv, 'bin')):
upgrade = True
for pkg in ['mlzlog', 'scipy', 'psutil', 'flask', 'gevent', 'influxdb_client']:
if not glob(f'{sevenv}/lib/python3*/site-packages/{pkg}'):
if doit:
if upgrade:
do(f'{sevenv}/bin/python3 -m pip install --upgrade pip')
upgrade = False
do(f'{sevenv}/bin/python3 -m pip install {pkg}')
else:
print(f'missing {pkg} in sevenv')
def do_frappy(): def do_frappy():
"""Frappy framework""" """Frappy framework"""
frappydir = join(home, 'frappy') frappydir = join(home, 'frappy')
if exists(frappydir) and not exists(join(frappydir, '.git')): if exists(frappydir) and not exists(join(frappydir, '.git')):
do('rm -rf %s' % frappydir) do('rm -rf %s' % frappydir)
if check_repo(home, 'frappy'): if check_repo(home, 'frappy', 'gitlab'):
do('git pull') do('git pull')
if exists(join(frappydir, '.git')): if exists(join(frappydir, '.git')):
sys.path.extend(glob(f'{nicosenv}/lib/*/site-packages')) sys.path.extend(glob(f'{nicosenv}/lib/*/site-packages'))
@ -323,35 +310,15 @@ def do_servicemanager():
do('git pull') do('git pull')
def do_feeder(): def do_sehistory():
"""history feeder""" """history feeder"""
if check_repo(home, 'sehistory', None, 'master'): if check_repo(home, 'sehistory', None, 'master'):
do('git pull') do('git pull')
configdir = join(home, '.config')
systemddir = join(configdir, 'systemd/user')
if not exists(systemddir):
do(f'mkdir -p {systemddir}')
docopy(join(home, 'servicemanager/cfg/sehistory'), join(configdir, 'sehistory'))
if docopy(join(home, 'servicemanager/cfg/feeder@.service'),
join(systemddir, 'feeder@.service')):
do('systemctl --user daemon-reload')
def do_seweb():
"""SE web client"""
if check_repo(home, 'seweb', None, 'master'):
do('git pull')
systemddir = join(home, '.config/systemd/user')
if not exists(systemddir):
do(f'mkdir -p {systemddir}')
if docopy(join(home, 'servicemanager/cfg/seweb@.service'),
join(systemddir, 'seweb@.service')):
do('systemctl --user daemon-reload')
def do_sea(): def do_sea():
"""SEA server scripts""" """SEA server scripts"""
if check_repo(home, 'sea'): if check_repo(home, 'sea', 'gitlab'):
do('git pull') do('git pull')
# do('git checkout master -- .gitignore') # do not know why this is needed # do('git checkout master -- .gitignore') # do not know why this is needed
# do('chmod -x tcl/config/json_racklist tcl/*.* tcl/*.tcl tcl/*/*.tcl') # do('chmod -x tcl/config/json_racklist tcl/*.* tcl/*.tcl tcl/*/*.tcl')
@ -363,12 +330,11 @@ def do_sea():
do('rm -rf tcl/calcurves') do('rm -rf tcl/calcurves')
do_calcurves() do_calcurves()
docopy('/afs/psi.ch/user/z/zolliker/public/git.rhel7/sics/SeaServer', join(home, 'sea', 'SeaServer')) docopy('/afs/psi.ch/user/z/zolliker/public/git.rhel7/sics/SeaServer', join(home, 'sea', 'SeaServer'))
dolink('/afs/psi.ch/project/sinq/rhel7/bin/SeaClient', join(home, 'bin', 'SeaClient'))
def do_calcurves(): def do_calcurves():
"""calibration curves""" """calibration curves"""
if check_repo(home, 'calcurves'): if check_repo(home, 'calcurves', 'gitlab'):
do('git pull') do('git pull')
dolink('~/calcurves', '~/sea/tcl/calcurves') dolink('~/calcurves', '~/sea/tcl/calcurves')
@ -376,7 +342,7 @@ def do_calcurves():
def do_frappysinq(): def do_frappysinq():
"""nicos_sinq/frappy_sinq setups and extensions""" """nicos_sinq/frappy_sinq setups and extensions"""
if exists(nicosroot): if exists(nicosroot):
if check_repo(join(nicosroot, 'nicos_sinq'), 'frappy_sinq'): if check_repo(join(nicosroot, 'nicos_sinq'), 'frappy_sinq', 'gitlab'):
do('git pull') do('git pull')
@ -493,7 +459,7 @@ def do_bin():
chdir(home) chdir(home)
if not exists('bin'): if not exists('bin'):
do('mkdir -p bin') do('mkdir -p bin')
pgms = ['frappy', 'sea', 'seweb', 'feeder'] pgms = ['frappy', 'sea']
if nicosroot: if nicosroot:
executable = f'{nicosenv}/bin/python3' executable = f'{nicosenv}/bin/python3'
else: else:
@ -543,6 +509,11 @@ def remove_line(file, content):
todo.add(action) todo.add(action)
print('remove above') print('remove above')
#def do_monit():
# """remove monit support for sea / graph"""
# remove_line('%s/monitconfig' % home, 'sea')
# remove_line('%s/monitconfig' % home, 'graph')
selected_instruments = None selected_instruments = None
action_arg = '' action_arg = ''
@ -567,7 +538,7 @@ for arg in sys.argv[1:]:
action_arg = arg action_arg = arg
nc_actions = ['frappysinq', 'nicosconf', 'nicosenv'] # 'sshnicos', 'nicos_pick' nc_actions = ['sshnicos', 'frappysinq', 'nicosconf', 'nicosenv'] # 'nicos_pick'
ncactionfuncs = {} ncactionfuncs = {}
with_su = False with_su = False
@ -579,8 +550,8 @@ else:
with_su = action_arg in nc_actions with_su = action_arg in nc_actions
for a in nc_actions: for a in nc_actions:
ncactionfuncs[a] = locals()['do_%s' % a] ncactionfuncs[a] = locals()['do_%s' % a]
actions = ['bin', 'scfg', 'frappy', 'servicemanager', 'sea', actions = ['ssh', 'bin', 'scfg', 'frappy', 'servicemanager', 'sea',
'calcurves', 'feeder', 'seweb', 'sevenv'] # 'ssh' 'calcurves', 'sehistory']
actionfuncs = {} actionfuncs = {}
for a in actions: for a in actions:
@ -591,8 +562,6 @@ for a in actions:
def print_help(): def print_help():
if not help:
return
if remote: if remote:
inst = "<instrument> " inst = "<instrument> "
else: else:
@ -624,12 +593,10 @@ if remote:
docmd(f'sshpass -e ssh {inst}@{inst} getsestuff {action_arg} nohelp') docmd(f'sshpass -e ssh {inst}@{inst} getsestuff {action_arg} nohelp')
sys.exit(0) sys.exit(0)
def su_action(action): def su_action(action):
os.environ['SSHPASS'] = scramble('P[d20+2:1eh') os.environ['SSHPASS'] = scramble('P[d20+2:1eh')
docmd(f'sshpass -e ssh nicos@localhost {sys.argv[0]} {action} nohelp') docmd(f'sshpass -e ssh nicos@localhost {sys.argv[0]} {action} nohelp')
if with_su: if with_su:
su_action(action_arg) su_action(action_arg)
sys.exit(0) sys.exit(0)
@ -658,7 +625,8 @@ if action_arg in ('', 'all', 'sim') and nc_actions:
print_help() print_help()
sys.exit(0) sys.exit(0)
print_help() if help:
if sim or not action_arg and todo: print_help()
if sim or not action_arg and todo:
print('needs updates: %s' % ' '.join(todo)) print('needs updates: %s' % ' '.join(todo))

View File

@ -84,7 +84,6 @@ class NicosManager(ServiceManager):
print('ambiguos package: %s' % ', '.join(instdir)) print('ambiguos package: %s' % ', '.join(instdir))
else: else:
env['NICOS_PACKAGE'] = basename(dirname(instdir[0])) env['NICOS_PACKAGE'] = basename(dirname(instdir[0]))
return self.info
def do_create(self, ins, *args): def do_create(self, ins, *args):
if ins == 'check': if ins == 'check':

View File

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# ***************************************************************************** # *****************************************************************************
# #
# This program is free software; you can redistribute it and/or modify it under # This program is free software; you can redistribute it and/or modify it under
@ -33,16 +34,6 @@ CFGLINE = re.compile(r'(?:device makeitem (name|stick_name|confirmed) "(.*)" ""'
r'|addon_list makeitem (.*) (?:"permanent"|"volatile"))') r'|addon_list makeitem (.*) (?:"permanent"|"volatile"))')
INIFILE_CONTENT = """
set instrument {ins_i}
set serverport {port}
set logbase {insdir}
{extraline}cd {seadir}tcl
exe echo 1
exe {service}init.tcl
"""
def run_command(cmd, wait=False): def run_command(cmd, wait=False):
if wait: if wait:
old = termios.tcgetattr(sys.stdin) old = termios.tcgetattr(sys.stdin)
@ -77,49 +68,7 @@ class SeaManager(ServiceManager):
%(legend)s %(legend)s
""" """
def do_create(self, ins, *args): def do_cli(self, ins):
if ins == 'check':
ins_list = self.wildcard(None)
else:
ins_list = self.wildcard(ins)
if ins_list is None:
ins_list = [ins]
self.get_info()
for ins_i in ins_list:
seadir = f"{expanduser('~')}/sea/"
if not exists(seadir):
print(f'get {seadir} from git first')
return
insdir = seadir if ins_i == self.main_ins else f'{seadir}{ins_i}/'
if ins != 'check':
os.makedirs(f'{insdir}/logger', exist_ok=True)
os.makedirs(f'{insdir}/log', exist_ok=True)
os.makedirs(f'{insdir}/status', exist_ok=True)
extraline = 'set connect_sea_to_sics 0\n'
for service in 'sea', 'graph':
port = self.info[ins_i][service]
if not port:
continue
sea_init = INIFILE_CONTENT.format(**locals()).strip()
extraline = ''
filename = f'{seadir}{service}_{ins_i}.tcl'
try:
with open(filename) as f:
content = f.read().strip()
except FileNotFoundError:
content = ''
if sea_init != content:
if ins == 'check':
if content:
print(filename, 'does not match')
else:
print(filename, 'does not exist')
else:
with open(filename, 'w') as f:
f.write(sea_init)
f.write('\n')
def do_cli(self, ins, service=None):
if self.wildcard(ins): if self.wildcard(ins):
raise UsageError('wildcards not allowed in sea cli') raise UsageError('wildcards not allowed in sea cli')
try: try:
@ -129,8 +78,7 @@ class SeaManager(ServiceManager):
print(str(e)) print(str(e))
except KeyError: # running on an other machine? except KeyError: # running on an other machine?
self.do_help() self.do_help()
service = service or 'sea' run_command('six -sea %s' % ins, wait=True)
run_command(f'six -{service} {ins}', wait=True)
def do_gui(self, ins='', *args): def do_gui(self, ins='', *args):
if ins and self.wildcard(ins): if ins and self.wildcard(ins):
@ -169,7 +117,7 @@ class SeaManager(ServiceManager):
seastatus = self.get_status_filename(ins) seastatus = self.get_status_filename(ins)
if seastatus: if seastatus:
boot_time = time.strftime("%Y-%m-%dT%H-%M-%S", time.localtime(psutil.boot_time())) boot_time = time.strftime("%Y-%m-%dT%H-%M-%S", time.localtime(psutil.boot_time()))
dst = seastatus.replace('.tcl', '') + '.before_boot_at' + boot_time dst = seastatus.replace('.tcl', '') + '.' + boot_time
if not exists(dst): if not exists(dst):
os.system(f'cp {seastatus} {dst}') os.system(f'cp {seastatus} {dst}')
return start_dir, env return start_dir, env
@ -248,8 +196,6 @@ class SeaManager(ServiceManager):
argdict['ins'] = arg argdict['ins'] = arg
else: else:
raise UsageError('unknown argument: %s' % arg) raise UsageError('unknown argument: %s' % arg)
else:
print('env variable "InstrumentHostList" is not defined')
result = [argdict.pop('ins', '')] result = [argdict.pop('ins', '')]
service = argdict.pop('service', '') service = argdict.pop('service', '')
if service: if service:

104
single.py
View File

@ -1,104 +0,0 @@
# *****************************************************************************
#
# 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 os
from os.path import expanduser
from configparser import ConfigParser
from servicemanager.base import ServiceManager
class SingleManager(ServiceManager):
"""service to be started with systemd, only one per computer
running as systemd (user level)
"""
def systemd_service(self, service):
"""return the name of the systemd service"""
raise NotImplementedError
def treat_args(self, argdict, unknown=(), extra=()):
if not argdict.get('ins'):
argdict['ins'] = self.single_ins
return super().treat_args(argdict, unknown, extra)
def systemd_action(self, service, action):
service_ports = self.get_ins_info(self.single_ins)
services = list(service_ports) if service is None else [service]
for service in services:
print(f'systemctl --user {action} {self.systemd_service(service)}')
os.system(f'systemctl --user {action} {self.systemd_service(service)}')
def do_start(self, ins, service=None, cfg='', restart=False, wait=False, logger=None, opts=''):
if wait:
super().do_start(ins, service, cfg, False, True, logger, opts)
return
if restart:
action = 'restart'
else:
action = 'start'
self.systemd_action(service, action)
def do_stop(self, ins, service=None, *args):
self.systemd_action(service, 'stop')
class FeederManager(SingleManager):
group = 'feeder'
services = ('central', 'local')
USAGE = """
Usage:
feeder start <instance> <service>
feeder restart <instance> <service>
feeder stop <instance> <service>
feeder list [<instance>]
%s
"""
def __init__(self):
parser = ConfigParser()
parser.read(expanduser('~/.config/sehistory'))
services = tuple(s for s in self.services if s in parser.sections())
self.services = services
super().__init__()
def systemd_service(self, service):
return f'{self.group}@{service}'
class SewebManager(SingleManager):
group = 'seweb'
services = ('',)
USAGE = """
Usage:
seweb start <instance>
seweb restart <instance>
seweb stop <instance>
seweb list [<instance>]
%s
"""
def systemd_service(self, service):
return f'{self.group}@{self.single_ins}'