diff options
Diffstat (limited to 'scripts/automation/trex_control_plane/console/trex_console.py')
-rwxr-xr-x | scripts/automation/trex_control_plane/console/trex_console.py | 78 |
1 files changed, 62 insertions, 16 deletions
diff --git a/scripts/automation/trex_control_plane/console/trex_console.py b/scripts/automation/trex_control_plane/console/trex_console.py index 9140977a..495e1c22 100755 --- a/scripts/automation/trex_control_plane/console/trex_console.py +++ b/scripts/automation/trex_control_plane/console/trex_console.py @@ -130,12 +130,17 @@ class TRexConsole(TRexGeneralCmd): ################### internal section ######################## + def verify_connected(f): @wraps(f) def wrap(*args): inst = args[0] + func_name = f.__name__ + if func_name.startswith("do_"): + func_name = func_name[3:] + if not inst.stateless_client.is_connected(): - print format_text("\nNot connected to server\n", 'bold') + print format_text("\n'{0}' cannot be executed on offline mode\n".format(func_name), 'bold') return ret = f(*args) @@ -143,9 +148,32 @@ class TRexConsole(TRexGeneralCmd): return wrap + # TODO: remove this ugly duplication + def verify_connected_and_rw (f): + @wraps(f) + def wrap(*args): + inst = args[0] + func_name = f.__name__ + if func_name.startswith("do_"): + func_name = func_name[3:] + + if not inst.stateless_client.is_connected(): + print format_text("\n'{0}' cannot be executed on offline mode\n".format(func_name), 'bold') + return + + if inst.stateless_client.is_read_only(): + print format_text("\n'{0}' cannot be executed on read only mode\n".format(func_name), 'bold') + return + + ret = f(*args) + return ret + + return wrap + + def get_console_identifier(self): return "{context}_{server}".format(context=self.__class__.__name__, - server=self.stateless_client.get_system_info()['hostname']) + server=self.stateless_client.get_server()) def register_main_console_methods(self): main_names = set(self.trex_console.get_names()).difference(set(dir(self.__class__))) @@ -156,11 +184,17 @@ class TRexConsole(TRexGeneralCmd): def postcmd(self, stop, line): - if self.stateless_client.is_connected(): - self.prompt = "TRex > " - else: - self.supported_rpc = None + if not self.stateless_client.is_connected(): self.prompt = "TRex (offline) > " + self.supported_rpc = None + return stop + + if self.stateless_client.is_read_only(): + self.prompt = "TRex (read only) > " + return stop + + + self.prompt = "TRex > " return stop @@ -287,7 +321,7 @@ class TRexConsole(TRexGeneralCmd): def do_connect (self, line): '''Connects to the server\n''' - rc = self.stateless_client.cmd_connect() + rc = self.stateless_client.cmd_connect_line(line) if rc.bad(): return @@ -314,7 +348,7 @@ class TRexConsole(TRexGeneralCmd): if (l > 2) and (s[l - 2] in file_flags): return TRexConsole.tree_autocomplete(s[l - 1]) - @verify_connected + @verify_connected_and_rw def do_start(self, line): '''Start selected traffic in specified port(s) on TRex\n''' @@ -325,7 +359,7 @@ class TRexConsole(TRexGeneralCmd): self.do_start("-h") ############# stop - @verify_connected + @verify_connected_and_rw def do_stop(self, line): '''stops port(s) transmitting traffic\n''' @@ -335,7 +369,7 @@ class TRexConsole(TRexGeneralCmd): self.do_stop("-h") ############# update - @verify_connected + @verify_connected_and_rw def do_update(self, line): '''update speed of port(s)currently transmitting traffic\n''' @@ -345,14 +379,14 @@ class TRexConsole(TRexGeneralCmd): self.do_update("-h") ############# pause - @verify_connected + @verify_connected_and_rw def do_pause(self, line): '''pause port(s) transmitting traffic\n''' self.stateless_client.cmd_pause_line(line) ############# resume - @verify_connected + @verify_connected_and_rw def do_resume(self, line): '''resume port(s) transmitting traffic\n''' @@ -361,7 +395,7 @@ class TRexConsole(TRexGeneralCmd): ########## reset - @verify_connected + @verify_connected_and_rw def do_reset (self, line): '''force stop all ports\n''' self.stateless_client.cmd_reset_line(line) @@ -375,6 +409,7 @@ class TRexConsole(TRexGeneralCmd): self.stateless_client.cmd_validate_line(line) + @verify_connected def do_stats(self, line): '''Fetch statistics from TRex server by port\n''' self.stateless_client.cmd_stats_line(line) @@ -383,6 +418,7 @@ class TRexConsole(TRexGeneralCmd): def help_stats(self): self.do_stats("-h") + @verify_connected def do_clear(self, line): '''Clear cached local statistics\n''' self.stateless_client.cmd_clear_line(line) @@ -529,9 +565,17 @@ def main(): # Stateless client connection stateless_client = CTRexStatelessClient(options.user, options.server, options.port, options.pub) - rc = stateless_client.cmd_connect() + + print "\nlogged as {0}".format(format_text(options.user, 'bold')) + rc = stateless_client.connect() + + # error can be either no able to connect or a read only if rc.bad(): - return + if not stateless_client.is_connected(): + rc.annotate() + else: + rc.annotate(show_status = False) + if options.batch: cont = stateless_client.run_script_file(options.batch[0]) @@ -544,7 +588,9 @@ def main(): console.cmdloop() except KeyboardInterrupt as e: print "\n\n*** Caught Ctrl + C... Exiting...\n\n" - return + + finally: + stateless_client.shutdown() if __name__ == '__main__': main() |