Some cosmetic changes to runsics.py

This commit is contained in:
Ferdi Franceschini
2013-05-20 09:07:49 +10:00
parent 36445471bf
commit 041735a969

View File

@ -17,21 +17,23 @@ sys.stdout = os.fdopen(sys.stdout.fileno(), 'w',0)
# TODO These numbers should be available on the system to any application # TODO These numbers should be available on the system to any application
server_port = { server_port = {
'sics': {'telnet': 60001, 'interrupt': 60002, 'server': 60003, 'quieck': 60004}, 'sics': {'telnet': 60001, 'interrupt': 60002, 'server': 60003,
'scriptval': {'telnet': 60011, 'interrupt': 60012, 'server': 60013, 'quieck': 60014} 'quieck': 60004},
'scriptval': {'telnet': 60011, 'interrupt': 60012, 'server': 60013,
'quieck': 60014}
} }
inst_test_sockoffset = { inst_test_sockoffset = {
'wombat': 0, 'wombat' : 0,
'echidna': 100, 'echidna' : 100,
'kowari': 200, 'kowari' : 200,
'quokka': 300, 'quokka' : 300,
'platypus': 400, 'platypus' : 400,
'taipan': 500, 'taipan' : 500,
'pelican': 600, 'pelican' : 600,
'dingo': 700, 'dingo' : 700,
'emu': 800, 'emu' : 800,
'kookaburra': 900, 'kookaburra' : 900,
'bilby': 1000 'bilby' : 1000
} }
deflt_dir = '/usr/local/sics/server' deflt_dir = '/usr/local/sics/server'
@ -68,14 +70,17 @@ def start_cmd(server, args):
SIMENV = sicsenv['scriptval'] SIMENV = sicsenv['scriptval']
else: else:
SIMENV = sicsenv[args.test] SIMENV = sicsenv[args.test]
start_str = 'sudo -u %s %s %s/SICServer -d %s' % (args.user, SIMENV, args.dir, args.config) start_str = 'sudo -u %s %s %s/SICServer -d %s' % (args.user, SIMENV,
args.dir, args.config)
# print "DEBUG: start_str:%s" % (start_str) # print "DEBUG: start_str:%s" % (start_str)
# Suppress output if launching the script validator # Suppress output if launching the script validator
if (server == 'scriptval'): if (server == 'scriptval'):
with open(os.devnull) as fp: with open(os.devnull) as fp:
subprocess.call(shlex.split(start_str), preexec_fn=sics_preexec, cwd=args.dir, stderr=fp, stdout=fp) subprocess.call(shlex.split(start_str), preexec_fn=sics_preexec,
cwd=args.dir, stderr=fp, stdout=fp)
else: else:
subprocess.call(shlex.split(start_str), preexec_fn=sics_preexec, cwd=args.dir) subprocess.call(shlex.split(start_str), preexec_fn=sics_preexec,
cwd=args.dir)
status_cmd(server, args) status_cmd(server, args)
def stop_cmd(server, args): def stop_cmd(server, args):
@ -86,7 +91,8 @@ def stop_cmd(server, args):
int_port = server_port[server]['interrupt'] int_port = server_port[server]['interrupt']
else: else:
int_port = server_port[server]['interrupt'] + inst_test_sockoffset[args.sockoffset] int_port = server_port[server]['interrupt'] + inst_test_sockoffset[args.sockoffset]
print "STOP %s(%d) by sending '%s' to socket %d" % (server, pid, INTMSG, int_port) print "STOP %s(%d) by sending '%s' to socket %d" % (server, pid,
INTMSG, int_port)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto( INTMSG + '\n\r', ('localhost', int_port) ) sock.sendto( INTMSG + '\n\r', ('localhost', int_port) )
sock.close() sock.close()
@ -105,13 +111,15 @@ def stop_cmd(server, args):
if status_fn(server, args) != (0,0): if status_fn(server, args) != (0,0):
print 'Failed to stop %s' % server print 'Failed to stop %s' % server
print "Fragging PID %d with default KILL" % (pid) print "Fragging PID %d with default KILL" % (pid)
subprocess.call(shlex.split('sudo -u %s /bin/kill %d' % (sics_killer, pid))) subprocess.call(shlex.split('sudo -u %s /bin/kill %d' %
(sics_killer, pid)))
else: else:
return return
if status_fn(server, args) != (0,0): if status_fn(server, args) != (0,0):
print 'Failed again!' print 'Failed again!'
print "Terminating PID %d with EXTREME PREJUDICE (-15)" % (pid) print "Terminating PID %d with EXTREME PREJUDICE (-15)" % (pid)
subprocess.call(shlex.split('sudo -u %s /bin/kill -15 %d' % sics_killer, (pid))) subprocess.call(shlex.split('sudo -u %s /bin/kill -15 %d' %
sics_killer, (pid)))
else: else:
return return
if status_cmd(server, args) != (0,0): if status_cmd(server, args) != (0,0):
@ -125,7 +133,9 @@ def status_fn(server, args):
else: else:
sock = server_port[server]['server'] + inst_test_sockoffset[args.sockoffset] sock = server_port[server]['server'] + inst_test_sockoffset[args.sockoffset]
status_str = 'sudo -u %s netstat -nltp' % sics_checker status_str = 'sudo -u %s netstat -nltp' % sics_checker
netstat_str, err = subprocess.Popen(shlex.split(status_str), stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() netstat_str, err = subprocess.Popen(shlex.split(status_str),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
# ffr: We can replace the previous Popen().communicate() line with the following two lines if Python is upgraded to V2.7 # ffr: We can replace the previous Popen().communicate() line with the following two lines if Python is upgraded to V2.7
# with open(os.devnull) as fp: # with open(os.devnull) as fp:
# netstat_str = subprocess.check_output(shlex.split(status_str), stderr=fp) # netstat_str = subprocess.check_output(shlex.split(status_str), stderr=fp)
@ -146,27 +156,64 @@ def status_cmd(server, args):
print '%s not running' % server print '%s not running' % server
return (sock, pid) return (sock, pid)
args_adv_grp_help = """
Options providing more control. Usually only useful for SICS
developers and testers. If you are running multiple instances
of SICS on a single host you can set the TEST_SICS environment
variable to one of the --test values to get nice defaults.
"""
args_config_help = 'Run SICS with the given configuration file. Default=%s'
args_dir_help = 'Run SICS from the given directory. Default=%s'
args_test_help = 'Run SICS in the given mode. Default=%s'
args_user_help = 'Run SICS as the given user. Default=%s'
args_sockoffset_help = """
Offset the sockets to allow multiple SICS instances on a single
host. Default=%s
"""
args_sicsonly_help = 'Just run SICS without the script validator'
args_scriptval_help = """
Launch script validator as well as SICServer. Use this to
override the default action when the TEST_SICS environmant
variable is set.
"""
args_dev_help = """
Launch SICS only in current directory listening on socket %s
and with simulation = fullsim
"""
def main(**kwargs): def main(**kwargs):
global sics_killer, sics_checker global sics_killer, sics_checker
runsics_cmd = {'start': start_cmd, 'stop': stop_cmd, 'status': status_cmd} runsics_cmd = {'start': start_cmd, 'stop': stop_cmd, 'status': status_cmd}
# Setup defaults. Use nice defaults in test environments. # Setup defaults. Use nice defaults in test environments.
# Parse arguments # Parse arguments
parser = argparse.ArgumentParser( description='SICS Server startup script.' ) parser = argparse.ArgumentParser( description='SICS Server startup script.')
cmd_grp = parser.add_argument_group('Commands', 'Runsics commands') cmd_grp = parser.add_argument_group('Commands', 'Runsics commands')
adv_grp = parser.add_argument_group('Advanced', 'Options providing more control. Usually only useful for SICS developers and testers. If you are running multiple instances of SICS on a single host you can set the TEST_SICS environment variable to one of the --test values to get nice defaults.') adv_grp = parser.add_argument_group('Advanced', args_adv_grp_help)
# Mandatory arguments # Mandatory arguments
cmd_grp.add_argument('cmd', choices=['start', 'stop', 'status']) cmd_grp.add_argument('cmd', choices=['start', 'stop', 'status'])
# Advanced arguments # Advanced arguments
adv_grp.add_argument('-c', '--config', help='Run SICS with the given configuration file. Default=%s' % inst_config, default = inst_config ) adv_grp.add_argument('-c', '--config', help= args_config_help % inst_config,
adv_grp.add_argument('-d', '--dir', help='Run SICS from the given directory. Default=%s' % deflt_dir, default = deflt_dir) default = inst_config )
adv_grp.add_argument('-t', '--test', choices=['fullsim', 'fakedev', 'scriptval'], help='Run SICS in the given mode. Default=%s' % deflt_test, default=deflt_test) adv_grp.add_argument('-d', '--dir', help=args_dir_help % deflt_dir,
adv_grp.add_argument('-u', '--user', help='Run SICS as the given user. Default=%s' % deflt_user, default = deflt_user ) default = deflt_dir)
adv_grp.add_argument('--sockoffset', help='Offset the sockets to allow multiple SICS instances on a single host. Default=%s' % deflt_sockoffset, choices=inst_test_sockoffset.keys(), default = deflt_sockoffset ) adv_grp.add_argument('-t', '--test',
choices=['fullsim', 'fakedev', 'scriptval'],
help=args_dir_help % deflt_test, default=deflt_test)
adv_grp.add_argument('-u', '--user', help= args_user_help % deflt_user,
default = deflt_user )
adv_grp.add_argument('--sockoffset',
help= args_sockoffset_help % deflt_sockoffset,
choices=inst_test_sockoffset.keys(),
default = deflt_sockoffset )
exc_grp = parser.add_mutually_exclusive_group() exc_grp = parser.add_mutually_exclusive_group()
exc_grp.add_argument('--sicsonly', help='Just run SICS without the script validator', action='store_true', default=False) exc_grp.add_argument('--sicsonly', help= args_sicsonly_help,
exc_grp.add_argument('--scriptval', help='Launch script validator as well as SICServer. Use this to override the default action when the TEST_SICS environmant variable is set.', action='store_true', default=False) action='store_true', default=False)
exc_grp.add_argument('--dev', help='Launch SICS only in current directory listening on socket %s and with simulation = fullsim' % (server_port['sics']['server'] + inst_test_sockoffset[inst_name]), action='store_true') exc_grp.add_argument('--scriptval', help= args_scriptval_help,
action='store_true', default=False)
exc_grp.add_argument('--dev',
help= args_dir_help % (server_port['sics']['server'] +
inst_test_sockoffset[inst_name]),
action='store_true')
args = parser.parse_args() args = parser.parse_args()
# By default don't launch script validator in development environments. # By default don't launch script validator in development environments.
if args.dev: if args.dev: