From 0f1538c156642d7dca7aee5ba9b0b79785446278 Mon Sep 17 00:00:00 2001 From: Markus Zolliker Date: Mon, 16 Oct 2023 11:16:18 +0200 Subject: [PATCH] [WIP] frappy() is now a device --- commands.py | 55 --------------------------------------- devices.py | 75 +++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 53 insertions(+), 77 deletions(-) diff --git a/commands.py b/commands.py index c3c62f6..b0d0260 100644 --- a/commands.py +++ b/commands.py @@ -36,61 +36,6 @@ def set_se_list(): fc.set_envlist() -@usercommand -@helparglist('main [, stick [, addons]]') -def frappy(*args, main=None, stick=None, addons=None, force=False): - """(re)start frappy server(s) with given configs and load setup if needed - - - without argument: list running frappy servers, restart failed frappy servers - - frappy(''): if available, the standard stick is added too - - frappy(''): the stick is removed too - - addons are not changed when not given - - frappy(main='') # main cfg is changed, but stick is kept - - frappy('restart') # restart all frappy servers - - frappy(stick='restart') # restart stick frappy server - """ - fc = get_frappy_config() - if not fc: - return - stickarg = stick - confirmed = SeaManager().get_cfg(config.instrument, 'sea', True).split('/', 1)[0] - if args: - if main is not None: - raise TypeError('got multiple values for main') - main = args[0] - if len(args) == 1: # special case: main given as single argument - if main == 'restart': - stick = 'restart' - addons = 'restart' - elif stick is None: # auto stick - if main == '': - stick = '' # remove stick with main - else: - stickcfg = main + 'stick' - if FrappyManager().is_cfg(config.instrument, 'stick', stickcfg): - # if a default stick is available, start this also - stick = stickcfg - else: - stick = '' # remove stick when main has changed - else: - if stick is not None: - raise TypeError('got multiple values for stick') - stick, *alist = args[1:] - if alist: - if addons is not None: - raise TypeError('got multiple values for addons') - addons = ','.join(alist) - elif main is None and stick is None and addons is None: # bare frappy() command - fc.show_config(fc.check_services()) - return - if confirmed and confirmed != main and main not in (None, 'restart') and not force: - session.log.warning('%r is plugged to the cryostat control rack', confirmed) - cmd = all_info({'main': main, 'stick': stickarg, 'addons': addons}, '')[:-1] + ', force=True)' - session.log.warning(f'if you are sure, use: %s', cmd) - raise TypeError('refuse to override plugged device') - fc.show_config(fc.start_services(main, stick, addons)) - - @usercommand def frappy_main(*args): raise NameError('frappy_main() is no longer avaiable, use frappy() instead') diff --git a/devices.py b/devices.py index 75f9119..39e0579 100644 --- a/devices.py +++ b/devices.py @@ -37,7 +37,7 @@ from nicos.services.daemon.script import RequestError, ScriptRequest from nicos.utils.comparestrings import compare from nicos.devices.secop.devices import get_attaching_devices from nicos.commands.basic import AddSetup, CreateAllDevices, CreateDevice -from servicemanager import FrappyManager +from servicemanager import FrappyManager, SeaManager SERVICES = FrappyManager.services @@ -264,6 +264,55 @@ class FrappyConfig(Device): self._cache.put(self, 'config', all_cfg) return all_cfg + def __call__(self, *args, main=None, stick=None, addons=None, force=False): + """(re)start frappy server(s) with given configs and load setup if needed + + - without argument: list running frappy servers, restart failed frappy servers + - frappy(''): if available, the standard stick is added too + - frappy(''): the stick is removed too + - addons are not changed when not given + - frappy(main='') # main cfg is changed, but stick is kept + - frappy('restart') # restart all frappy servers + - frappy(stick='restart') # restart stick frappy server + """ + stickarg = stick + confirmed = SeaManager().get_cfg(config.instrument, 'sea', True).split('/', 1)[0] + if args: + if main is not None: + raise TypeError('got multiple values for main') + main = args[0] + if len(args) == 1: # special case: main given as single argument + if main == 'restart': + stick = 'restart' + addons = 'restart' + elif stick is None: # auto stick + if main == '': + stick = '' # remove stick with main + else: + stickcfg = main + 'stick' + if FrappyManager().is_cfg(config.instrument, 'stick', stickcfg): + # if a default stick is available, start this also + stick = stickcfg + else: + stick = '' # remove stick when main has changed + else: + if stick is not None: + raise TypeError('got multiple values for stick') + stick, *alist = args[1:] + if alist: + if addons is not None: + raise TypeError('got multiple values for addons') + addons = ','.join(alist) + elif main is None and stick is None and addons is None: # bare frappy() command + self.show_config(self.check_services()) + return + if confirmed and confirmed != main and main not in (None, 'restart') and not force: + session.log.warning('%r is plugged to the cryostat control rack', confirmed) + cmd = all_info({'main': main, 'stick': stickarg, 'addons': addons}, '')[:-1] + ', force=True)' + session.log.warning(f'if you are sure, use: %s', cmd) + raise TypeError('refuse to override plugged device') + self.show_config(self.start_services(main, stick, addons)) + def show_config(self, allcfg): guess_info = self.to_consider(allcfg) # remove 'frappy_changed()' commands in script queue @@ -290,7 +339,7 @@ class FrappyConfig(Device): if self._servers_loaded: return None if self._initial_config is None: - success = True + # we do this only once for all services fm = FrappyManager() running = fm.get_cfg(config.instrument, None) cache = self._getCache() @@ -309,25 +358,7 @@ class FrappyConfig(Device): cfgs['stick'] = running_stick else: cfgs.pop('stick', None) - for serv, guess in fm.guess_cfgs(config.instrument, cfgs).items(): - ok = guess.get('ok', []) - for proposed in guess.get('proposed', []): - if isinstance(proposed, str): # start only when ambiguous - ok.append(proposed) - else: - success = False - if ok: - cfgs[serv] = ','.join(ok) - if not success: - cfgs = {} - result = {} - for serv, cfg in cfgs.items(): - runcfg = running.get(serv) - if runcfg != cfg: - result[serv] = cfg - elif runcfg: - result[serv] = True # restart not needed as cfg has not changed - self._initial_config = result + self._initial_config = fm.propose_cfgs(config.instrument, cfgs)[0] return self._initial_config.get(service) def changed(self): @@ -485,7 +516,7 @@ class FrappyNode(SecNodeDevice, Moveable): if mode == SIMULATION or session.sessiontype == POLLER: super().doInit(mode) else: - fc = session.devices.get('frappy_config') + fc = session.devices.get('frappy') if fc: cfg = fc.initial_restart_cfg(self.service) if isinstance(cfg, str): # may also be None or True