[WIP] introduce get_server_state

propose_cfgs will be obsolete
This commit is contained in:
zolliker 2023-10-17 11:01:33 +02:00
parent 7ac25158eb
commit 7cc025bef4

View File

@ -65,6 +65,7 @@ class Namespace(dict):
SEAEXT = {'main': '.config', 'stick': '.stick'}
# TODO: put inline
def get_service(key):
return key if key in ('main', 'stick') else 'addons'
@ -169,6 +170,112 @@ def make_proposed(givencfgs, ourcfgs, seacfgs, sea_info, strict=False, giventitl
return result, [' '.join(v.ljust(w) for w, v in zip(wid, row)) for row in rows]
def summarize_server_state(givencfgs, ourcfgs, seacfgs, sea_info, strict=False):
"""get a guess for the configuration from information about running services
:param givencfgs: dict <service> of given configuration (from nicos cache)
:param outcfgs: dict <service> of running configuration (frappy)
:param seacfgs: dict <service> of running configuration (sea)
:param sea_info: dict <frappycfg> of <seacfg> with info about sea configs
:param strict: when True return empty cfg result on error
:return: tuple (<error>, <proposed>, (<frappyitems>, <seaitems>), (<givenitems>, <remarks>) where:
<error>: proposed config not sure
<proposed>: dict <service> of proposed cfg
<frappyitems>: dict of items runnning in frappy servers (addons are separated)
<seaitems>: dict of items running on the sea server
<givenitems>: dict of given items (addons are separated)
<remarks>: dict of actions / remarks
"""
givencfgs = dict(givencfgs)
addons = givencfgs.pop('addons', '')
for addon in addons.split(','):
addon = addon.strip()
if addon:
givencfgs[addon] = addon
frappycfgs = dict(ourcfgs)
addons = givencfgs.pop('addons', '')
for addon in addons.split(','):
addon = addon.strip()
if addon:
frappycfgs[addon] = addon
seacfgfiles = [(c or '') + SEAEXT.get(s, '.addon')
for c, s in zip_longest(seacfgs, FrappyManager.services)]
seacfgset = set(seacfgfiles)
inverted_sea_info = {k: [] for k in seacfgfiles}
for cfg, seacfg in sea_info.items():
if seacfg in seacfgset:
inverted_sea_info[seacfg].append(cfg)
error = False
result = {}
addons = set()
seacfgs = {}
remarks = {}
for seacfg, seacfgfile, key in zip_longest(seacfgs, seacfgfiles, ('main', 'stick')):
if not seacfg:
continue
available = inverted_sea_info[seacfgfile]
if available:
proposed = available[0] if len(available) == 1 else None
if not proposed:
running = None
for item in available:
if item == givencfgs.get(key or item):
proposed = item
if item == frappycfgs.get(key or item):
running = item
if running and not proposed:
proposed = running
if proposed:
pkey = key or proposed
given = givencfgs.get(pkey)
running = frappycfgs.get(pkey)
if running == proposed:
remarks[pkey] = '' if given == running else 'reconnect'
else:
remarks[pkey] = 'restart'
seacfgs[pkey] = seacfg
if key:
result[key] = proposed
else:
addons.add(proposed)
else:
for item in available:
remarks[item] = 'ambiguous'
seacfgs[item] = seacfg
error = True
else:
pkey = key or seacfg
seacfg[pkey] = seacfg
remarks[pkey] = 'missing frappy config'
error = True
restart = set()
for key, running in frappycfgs.items():
if running:
service = get_service(key)
if service == 'addons':
addons.add(running)
if not seacfgs.get(key):
if sea_info.get(running):
restart.add(service)
remarks[key] = 'restart to start sea'
elif givencfgs.get(key) != running:
remarks[key] = 'reconnect'
elif remarks.get(key) is None:
remarks[key] = ''
if addons:
result['addons'] = ','.join(addons)
if not error:
for service, cfg in ourcfgs.items():
if cfg:
prop = result.get(service, '')
if prop == cfg and service not in restart:
result[service] = True
return error, result, (ourcfgs, seacfgs), (givencfgs, remarks)
class FrappyManager(ServiceManager):
group = 'frappy'
services = ('main', 'stick', 'addons')
@ -429,5 +536,28 @@ class FrappyManager(ServiceManager):
seacfgs.append('')
sea_info = {}
self.all_cfg(ins, None, sea_info=sea_info)
return self.make_proposed(givencfgs, ourcfgs, seacfgs, sea_info, strict)
return make_proposed(givencfgs, ourcfgs, seacfgs, sea_info, strict)
def get_server_state(self, ins, givencfgs):
"""get proposed configuration and an overview of the running servers
:param ins: the instance to be checked for
:param givencfgs: a dict <service> of cfg given by the ECS
:return: tuple (<error>, <proposed>, (<frappyitems>, <seaitems>), (<givenitems>, <remarks>) where:
<error>: proposed config not sure
<proposed>: dict <service> of proposed cfg
<frappyitems>: dict of items runnning in frappy servers (addons are separated)
<seaitems>: dict of items running on the sea server
<givenitems>: dict of given items (addons are separated)
<remarks>: dict of actions / remarks
"""
ourcfgs = self.get_cfg(ins, None)
sea = SeaManager()
seacfgs = sea.get_cfg(ins, 'sea').split('/')
if len(seacfgs) < 2:
seacfgs.append('')
sea_info = {}
self.all_cfg(ins, None, sea_info=sea_info)
return summarize_server_state(givencfgs, ourcfgs, seacfgs, sea_info)