summaryrefslogtreecommitdiffstats
path: root/scripts/master_daemon.py
diff options
context:
space:
mode:
authorYaroslav Brustinov <ybrustin@cisco.com>2016-05-28 05:08:16 +0300
committerYaroslav Brustinov <ybrustin@cisco.com>2016-05-28 05:08:16 +0300
commitb0b3908b1b3742b84b48298fb8f7524e422bb28d (patch)
treeab2074e8e8f443613ee1c12968ab163972418911 /scripts/master_daemon.py
parent4607fb5588bbb7dc0a708ffd5f97fe99bee98dd2 (diff)
master_daemon & other daemons updates
Diffstat (limited to 'scripts/master_daemon.py')
-rwxr-xr-xscripts/master_daemon.py239
1 files changed, 95 insertions, 144 deletions
diff --git a/scripts/master_daemon.py b/scripts/master_daemon.py
index a950da02..0b1b7363 100755
--- a/scripts/master_daemon.py
+++ b/scripts/master_daemon.py
@@ -1,20 +1,22 @@
#!/usr/bin/python
-import os, sys, getpass
-import tempfile
-import argparse
-import socket
+import os
+import sys
+import getpass
+import shutil
+import multiprocessing
+import logging
+from collections import OrderedDict
+from argparse import *
from time import time, sleep
-import subprocess, shlex, shutil, multiprocessing
from glob import glob
-import logging
-logging.basicConfig(level = logging.FATAL) # keep quiet
-sys.path.append(os.path.join('external_libs', 'jsonrpclib-pelix-0.2.5'))
+sys.path.append(os.path.join('automation', 'trex_control_plane', 'server'))
+import outer_packages
+from singleton_daemon import SingletonDaemon, register_socket, run_command
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
-
-sys.path.append(os.path.join('external_libs', 'termstyle'))
import termstyle
+logging.basicConfig(level = logging.FATAL) # keep quiet
### Server functions ###
@@ -27,44 +29,6 @@ def add(a, b): # for sanity checks
def get_trex_path():
return args.trex_dir
-def is_trex_daemon_running():
- ret_code, stdout, stderr = run_command('ps -u root --format comm')
- if ret_code:
- raise Exception('Failed to list running processes, stderr: %s' % stderr)
- if 'trex_daemon_ser' in stdout: # name is cut
- return True
- return False
-
-def restart_trex_daemon():
- if is_trex_daemon_running:
- stop_trex_daemon()
- start_trex_daemon()
- return True
-
-def stop_trex_daemon():
- if not is_trex_daemon_running():
- return False
- return_code, stdout, stderr = run_command('%s stop' % trex_daemon_path)
- if return_code:
- raise Exception('Could not stop trex_daemon_server, %s' % [return_code, stdout, stderr])
- for i in range(50):
- if not is_trex_daemon_running():
- return True
- sleep(0.1)
- raise Exception('Could not stop trex_daemon_server')
-
-def start_trex_daemon():
- if is_trex_daemon_running():
- return False
- return_code, stdout, stderr = run_command('%s start' % trex_daemon_path)
- if return_code:
- raise Exception('Could not run trex_daemon_server, err: %s' % stderr)
- for i in range(50):
- if is_trex_daemon_running():
- return True
- sleep(0.1)
- raise Exception('Could not run trex_daemon_server')
-
def update_trex(package_path = 'http://trex-tgn.cisco.com/trex/release/latest'):
if not args.allow_update:
raise Exception('Updading server not allowed')
@@ -76,7 +40,7 @@ def update_trex(package_path = 'http://trex-tgn.cisco.com/trex/release/latest'):
file_name = os.path.basename(package_path)
ret_code, stdout, stderr = run_command('rsync -Lc %s %s' % (package_path, os.path.join(tmp_dir, file_name)), timeout = 300)
if ret_code:
- raise Exception('Could not get requested package.\nStdout: %s\nStderr: %s' % (stdout, stderr))
+ raise Exception('Could not get requested package. Result: %s' % [ret_code, stdout, stderr])
# clean old unpacked dirs
unpacked_dirs = glob(os.path.join(tmp_dir, 'v[0-9].[0-9][0-9]'))
for unpacked_dir in unpacked_dirs:
@@ -110,98 +74,43 @@ def fail(msg):
print(msg)
sys.exit(-1)
-def run_command(command, timeout = 15, cwd = None):
- if timeout:
- command = 'timeout %s %s' % (timeout, command)
- if not cwd:
- cwd = args.trex_dir
- # pipes might stuck, even with timeout
- with tempfile.TemporaryFile() as stdout_file, tempfile.TemporaryFile() as stderr_file:
- proc = subprocess.Popen(shlex.split(command), stdout = stdout_file, stderr = stderr_file, cwd = cwd)
- proc.wait()
- stdout_file.seek(0)
- stderr_file.seek(0)
- return (proc.returncode, stdout_file.read().decode(errors = 'replace'), stderr_file.read().decode(errors = 'replace'))
-
-def show_master_daemon_status():
- if get_master_daemon_pid():
- print(termstyle.red('Master daemon is running'))
- else:
- print(termstyle.red('Master daemon is NOT running'))
def start_master_daemon():
- if get_master_daemon_pid():
- print(termstyle.red('Master daemon is already running'))
- return
- server = multiprocessing.Process(target = start_master_daemon_func)
- server.daemon = True
- server.start()
+ if master_daemon.is_running():
+ raise Exception('Master daemon is already running')
+ proc = multiprocessing.Process(target = start_master_daemon_func)
+ proc.daemon = True
+ proc.start()
for i in range(50):
- if get_master_daemon_pid():
+ if master_daemon.is_running():
print(termstyle.green('Master daemon is started'))
os._exit(0)
sleep(0.1)
fail(termstyle.red('Master daemon failed to run'))
-def restart_master_daemon():
- if get_master_daemon_pid():
- kill_master_daemon()
- start_master_daemon()
def start_master_daemon_func():
- server = SimpleJSONRPCServer(('0.0.0.0', args.daemon_port))
- print('Started master daemon (port %s)' % args.daemon_port)
+ register_socket(master_daemon.tag)
+ server = SimpleJSONRPCServer(('0.0.0.0', master_daemon.port))
+ print('Started master daemon (port %s)' % master_daemon.port)
server.register_function(add)
server.register_function(check_connectivity)
server.register_function(get_trex_path)
- server.register_function(is_trex_daemon_running)
- server.register_function(restart_trex_daemon)
- server.register_function(start_trex_daemon)
- server.register_function(stop_trex_daemon)
server.register_function(update_trex)
+ # trex_daemon_server
+ server.register_function(trex_daemon_server.is_running, 'is_trex_daemon_running')
+ server.register_function(trex_daemon_server.restart, 'restart_trex_daemon')
+ server.register_function(trex_daemon_server.start, 'start_trex_daemon')
+ server.register_function(trex_daemon_server.stop, 'stop_trex_daemon')
+ # stl rpc proxy
+ server.register_function(stl_rpc_proxy.is_running, 'is_stl_rpc_proxy_running')
+ server.register_function(stl_rpc_proxy.restart, 'restart_stl_rpc_proxy')
+ server.register_function(stl_rpc_proxy.start, 'start_stl_rpc_proxy')
+ server.register_function(stl_rpc_proxy.stop, 'stop_stl_rpc_proxy')
+ server.register_function(server.funcs.keys, 'get_methods') # should be last
server.serve_forever()
-def get_master_daemon_pid():
- return_code, stdout, stderr = run_command('netstat -tlnp')
- if return_code:
- fail('Failed to determine which program holds port %s, netstat error: %s' % (args.daemon_port, stderr))
- for line in stdout.splitlines():
- if '0.0.0.0:%s' % args.daemon_port in line:
- line_arr = line.split()
- if len(line_arr) != 7:
- fail('Could not parse netstat line to determine which process holds port %s: %s'(args.daemon_port, line))
- if '/' not in line_arr[6]:
- fail('Expecting pid/program name in netstat line of using port %s, got: %s'(args.daemon_port, line_arr[6]))
- pid, program = line_arr[6].split('/')
- if 'python' not in program and 'master_daemon' not in program:
- fail('Some other program holds port %s, not our daemon: %s. Please verify.' % (args.daemon_port, program))
- return pid
- return None
-
-def kill_master_daemon():
- pid = get_master_daemon_pid()
- if not pid:
- print(termstyle.red('Master daemon is NOT running'))
- return True
- return_code, stdout, stderr = run_command('kill %s' % pid) # usual kill
- if return_code:
- fail('Failed to kill master daemon, error: %s' % stderr)
- for i in range(50):
- if not get_master_daemon_pid():
- print(termstyle.green('Master daemon is killed'))
- return True
- sleep(0.1)
- return_code, stdout, stderr = run_command('kill -9 %s' % pid) # unconditional kill
- if return_code:
- fail('Failed to kill trex_daemon, error: %s' % stderr)
- for i in range(50):
- if not get_master_daemon_pid():
- print(termstyle.green('Master daemon is killed'))
- return True
- sleep(0.1)
- fail('Failed to kill master daemon, even with -9. Please review manually.') # should not happen
-
# returns True if given path is under current dir or /tmp
def _check_path_under_current_or_temp(path):
if not os.path.relpath(path, '/tmp').startswith(os.pardir):
@@ -210,35 +119,58 @@ def _check_path_under_current_or_temp(path):
return True
return False
+
### Main ###
if getpass.getuser() != 'root':
fail('Please run this program as root/with sudo')
-actions_help = '''Specify action command to be applied on master daemon.
- (*) start : start the master daemon.
- (*) show : prompt the status of master daemon process (running / not running).
- (*) stop : exit the master daemon process.
- (*) restart : stop, then start again the master daemon process
- '''
-action_funcs = {'start': start_master_daemon,
- 'show': show_master_daemon_status,
- 'stop': kill_master_daemon,
- 'restart': restart_master_daemon,
- }
-
-parser = argparse.ArgumentParser(description = 'Runs master daemon that can start/stop TRex daemon or update TRex version.')
-parser.add_argument('-p', '--daemon-port', type=int, default = 8091, dest='daemon_port',
- help = 'Select port on which the master_daemon runs.\nDefault is 8091.', action = 'store')
+daemon_actions = OrderedDict([('start', 'start the daemon'),
+ ('stop', 'exit the daemon process'),
+ ('show', 'prompt the status of daemon process (running / not running)'),
+ ('restart', 'stop, then start again the daemon process')])
+
+actions_help = 'Specify action command to be applied on master daemon.\n' +\
+ '\n'.join([' (*) %-11s: %s' % (key, val) for key, val in daemon_actions.items()])
+
+daemons = {}.fromkeys(['master_daemon', 'trex_daemon_server', 'stl_rpc_proxy'])
+
+# show -p --master_port METAVAR instead of -p METAVAR --master_port METAVAR
+class MyFormatter(RawTextHelpFormatter):
+ def _format_action_invocation(self, action):
+ if not action.option_strings or action.nargs == 0:
+ return super(MyFormatter, self)._format_action_invocation(action)
+ default = action.dest.upper()
+ args_string = self._format_args(action, default)
+ return ', '.join(action.option_strings) + ' ' + args_string
+
+parser = ArgumentParser(description = 'Runs master daemon that can start/stop TRex daemon or update TRex version.',
+ formatter_class = MyFormatter)
+parser.add_argument('-p', '--master-port', type=int, default = 8091, dest='master_port',
+ help = 'Select port to which the Master daemon will listen.\nDefault is 8091.', action = 'store')
+parser.add_argument('--trex-daemon-port', type=int, default = 8090, dest='trex_daemon_port',
+ help = 'Select port to which the TRex daemon server will listen.\nDefault is 8090.', action = 'store')
+parser.add_argument('--stl-rpc-proxy-port', type=int, default = 8095, dest='stl_rpc_proxy_port',
+ help = 'Select port to which the Stateless RPC proxy will listen.\nDefault is 8095.', action = 'store')
parser.add_argument('-d', '--trex-dir', type=str, default = os.getcwd(), dest='trex_dir',
help = 'Path of TRex, default is current dir', action = 'store')
parser.add_argument('--allow-update', default = False, dest='allow_update', action = 'store_true',
help = "Allow update of TRex via RPC command. WARNING: It's security hole! Use on your risk!")
-parser.add_argument('action', choices=action_funcs.keys(),
- action='store', help=actions_help)
-parser.usage = None
+parser.add_argument('action', choices = daemon_actions,
+ action = 'store', help = actions_help)
+parser.add_argument('--type', '--daemon-type', '--daemon_type', choices = daemons.keys(), dest = 'daemon_type',
+ action = 'store', help = 'Specify daemon type to start/stop etc.\nDefault is master_daemon.')
+
args = parser.parse_args()
+args.trex_dir = os.path.normpath(args.trex_dir)
+args.daemon_type = args.daemon_type or 'master_daemon'
+
+stl_rpc_proxy_dir = os.path.join(args.trex_dir, 'automation', 'trex_control_plane', 'stl', 'examples')
+stl_rpc_proxy = SingletonDaemon('Stateless RPC proxy', 'trex_stl_rpc_proxy', args.stl_rpc_proxy_port, './rpc_proxy_server.py', stl_rpc_proxy_dir)
+trex_daemon_server = SingletonDaemon('TRex daemon server', 'trex_daemon_server', args.trex_daemon_port, './trex_daemon_server start', args.trex_dir)
+master_daemon = SingletonDaemon('Master daemon', 'trex_master_daemon', args.master_port, start_master_daemon) # add ourself for easier check if running, kill etc.
+daemons_by_name = {}
tmp_dir = '/tmp/trex-tmp'
if not _check_path_under_current_or_temp(args.trex_dir):
@@ -246,6 +178,7 @@ if not _check_path_under_current_or_temp(args.trex_dir):
if os.path.isfile(args.trex_dir):
raise Exception('Given path is a file')
if not os.path.exists(args.trex_dir):
+ print('Path %s does not exist, creating new assuming TRex will be unpacked there.' % args.trex_dir)
os.makedirs(args.trex_dir)
os.chmod(args.trex_dir, 0o777)
elif args.allow_update:
@@ -254,7 +187,25 @@ elif args.allow_update:
if not os.path.exists(tmp_dir):
os.makedirs(tmp_dir)
-trex_daemon_path = os.path.join(args.trex_dir, 'trex_daemon_server')
-action_funcs[args.action]()
+if args.daemon_type not in daemons.keys(): # not supposed to happen
+ raise Exception('Error in daemon type , should be one of following: %s' % daemon.keys())
+daemon = vars().get(args.daemon_type)
+if not daemon:
+ raise Exception('Daemon %s does not exist' % args.daemon_type)
+if args.action != 'show':
+ func = getattr(daemon, args.action)
+ if not func:
+ raise Exception('%s does not have function %s' % (daemon.name, args.action))
+ try:
+ func()
+ except Exception as e:
+ print(termstyle.red(e))
+ sys.exit(1)
+
+# prints running status
+if daemon.is_running():
+ print(termstyle.green('%s is running' % daemon.name))
+else:
+ print(termstyle.red('%s is NOT running' % daemon.name))