servicemanager/seaman.py
Markus Zolliker 7f2ba5766c copy the seastatus before first start after boot time
after a crash the previous seastatus is not read correctly
for debugging reasons we should check if the status file is
invalid
2023-11-14 11:40:56 +01:00

201 lines
7.4 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 time
import termios
import subprocess
import psutil
import os
import re
from os.path import join, exists
from servicemanager.base import ServiceManager, ServiceDown, UsageError
CFGLINE = re.compile(r'(?:device makeitem (name|stick_name|confirmed) "(.*)" ""'
r'|addon_list makeitem (.*) (?:"permanent"|"volatile"))')
def run_command(cmd, wait=False):
if wait:
old = termios.tcgetattr(sys.stdin)
proc = subprocess.Popen(cmd.split())
try:
proc.wait()
except KeyboardInterrupt:
proc.terminate()
finally:
# in case cmd changed tty attributes
termios.tcsetattr(sys.stdin, termios.TCSAFLUSH, old)
print('')
else:
subprocess.Popen(cmd.split(), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
class SeaManager(ServiceManager):
group = 'sea'
services = ('sea', 'graph')
USAGE = """
Usage:
sea gui <instance>
sea <instance> # the same as sea gui <instance>
sea cli <instance> # the same as old seacmd
sea start <instance> [service] * # the same as old 'monit start sea'
sea restart <instance> [service] *
sea stop <instance> [service] *
sea list [instance] *
[service] is empty or one of sea, graph
%s
* wildcards allowed, using '.' to replace 0 or more arbitrary characters in <instance>
"""
def do_cli(self, ins):
if self.wildcard(ins):
raise UsageError('wildcards not allowed in sea cli')
try:
self.check_running(ins, 'sea')
except ServiceDown as e:
self.do_help()
print(str(e))
except KeyError: # running on an other machine?
self.do_help()
run_command('six -sea %s' % ins, wait=True)
def do_gui(self, ins='', *args):
if ins and self.wildcard(ins):
raise UsageError('wildcards not allowed in sea gui')
if ins:
args = (ins,) + args
if '-q' not in args:
try:
self.check_running(ins, 'sea')
except ServiceDown as e:
self.do_help()
print(str(e))
return
except KeyError: # running on an other machine?
self.do_help()
run_command('SeaClient %s' % ' '.join(args))
print('starting sea gui %s' % ' '.join(args))
time.sleep(5)
def prepare_start(self, ins, service, *args):
start_dir, env = super().prepare_start(ins, service)
# load newest version of SeaServer if needed
os.chdir(start_dir)
sea_server_src = os.environ.get(
'SEA_SERVER_SRC', '/afs/psi.ch/user/z/zolliker/public/git.rhel7/sics/SeaServer')
if sea_server_src and exists(sea_server_src):
if os.system('diff %s SeaServer' % sea_server_src):
print('reload SeaServer')
try:
os.rename('SeaServer', 'SeaServer0')
except Exception:
pass
os.system('cp %s ./' % sea_server_src)
if service == 'sea':
# debugging: copy status file in case of a reboot
seastatus = self.get_sea_status(ins)
if seastatus:
boot_time = time.strftime("%Y-%m-%dT%H-%M-%S", time.localtime(psutil.boot_time()))
dst = seastatus.replace('.tcl', '') + '.' + boot_time
if not exists(dst):
os.system(f'cp {seastatus} {dst}')
return start_dir, env
def get_sea_status_file(self, ins):
searoot = self.env[ins].get('SEA_ROOT', '')
seastatus = join(searoot, ins, 'status', 'seastatus.tcl')
if exists(seastatus):
return seastatus
seastatus = join(searoot, 'status', 'seastatus.tcl')
if exists(seastatus):
return seastatus
return None
def get_cfg(self, ins, service, addconfirmed=False):
"""return cfg info about running programs, if relevant
return samenv name
"""
if service != 'sea': # ignore when service == 'graph'
return ''
if 'sea' not in self.get_procs().get(ins, ()):
return ''
try:
seastatus = self.get_sea_status(ins)
if not seastatus:
return '?'
result = ['', '']
confirmed = ''
with open(seastatus, 'r', encoding='utf-8') as f:
for line in f:
match = CFGLINE.match(line)
if match:
key, dev, addon = match.groups()
if addon:
if addon != result[1]: # skip stick appearing also in addon_list
result.append(addon)
elif key == 'name':
result[0] = dev
elif key == 'stick_name':
result[1] = dev
elif key == 'confirmed':
confirmed = dev
if not result[-1]:
result.pop()
if addconfirmed:
result.insert(0, confirmed)
return '/'.join(result)
except Exception as e:
return repr(e)
def treat_args(self, argdict, unknown=(), extra=()):
extra = list(extra)
for arg in unknown:
if arg == '-q' and arg not in extra:
extra.append(arg)
continue
if argdict.get('ins'):
raise UsageError('superflous argument: %s' % arg)
insts = set()
filename = os.environ.get('InstrumentHostList')
if filename:
with open(filename) as fil:
for line in fil:
inst = ''
sea = False
for item in line.split():
key, _, value = item.partition('=')
if key == 'instr':
inst = value
elif key == 'sea':
sea = True
if inst and sea:
insts.add(inst)
if arg in insts:
argdict['ins'] = arg
else:
raise UsageError('unknown argument: %s' % arg)
return [argdict.pop('ins', '')] + extra