Ejemplo n.º 1
0
 def __prevent_duplicate_runs(self):
     if self.action == 'start' and self.pidfile.is_locked():
         print termstyle.green("Server daemon is already running")
         exit(1)
     elif self.action == 'stop' and not self.pidfile.is_locked():
         print termstyle.green("Server daemon is not running")
         exit(1)
Ejemplo n.º 2
0
    def do_push_files(self, filepaths):
        """Pushes a custom file to be stored locally on TRex server.\
        \nPush multiple files by specifying their path separated by ' ' (space)."""
        try:
            filepaths = filepaths.split(' ')
            print termstyle.green("*** Starting pushing files ({trex_files}) to TRex. ***".format(
                trex_files=', '.join(filepaths))
            )
            ret_val = self.trex.push_files(filepaths)
            if ret_val:
                print termstyle.green("*** End of TRex push_files method (success) ***")
            else:
                print termstyle.magenta("*** End of TRex push_files method (failed) ***")

        except IOError as inst:
            print termstyle.magenta(inst)
Ejemplo n.º 3
0
 def __prompt_init_msg(self):
     if self.action == 'start':
         print termstyle.green("Starting daemon server...")
     elif self.action == 'stop':
         print termstyle.green("Stopping daemon server...")
Ejemplo n.º 4
0
class InteractiveStatelessTRex(cmd.Cmd):

    intro = termstyle.green(
        "\nInteractive shell to play with Cisco's TRex stateless API.\
                             \nType help to view available pre-defined scenarios\n(c) All rights reserved.\n"
    )
    prompt = '> '

    def __init__(self, trex_host, trex_port, virtual, verbose):
        cmd.Cmd.__init__(self)

        self.verbose = verbose
        self.virtual = virtual
        self.trex = CTRexStatelessClient(trex_host, trex_port, self.virtual)
        self.DEFAULT_RUN_PARAMS = dict(m=1.5,
                                       nc=True,
                                       p=True,
                                       d=100,
                                       f='avl/sfr_delay_10_1g.yaml',
                                       l=1000)
        self.run_params = dict(self.DEFAULT_RUN_PARAMS)

    def do_transmit(self, line):
        """Transmits a request over using a given link to server.\
        \nuse: transmit [method_name] [method_params]"""
        if line == "":
            print "\nUsage: [method name] [param dict as string]\n"
            print "Example: rpc test_add {'x': 12, 'y': 17}\n"
            return

        args = line.split(' ', 1)  # args will have max length of 2
        method_name = args[0]
        params = None
        bad_parse = False

        try:
            params = ast.literal_eval(args[1])
            if not isinstance(params, dict):
                bad_parse = True
        except ValueError as e1:
            bad_parse = True
        except SyntaxError as e2:
            bad_parse = True

        if bad_parse:
            print "\nValue should be a valid dict: '{0}'".format(args[1])
            print "\nUsage: [method name] [param dict as string]\n"
            print "Example: rpc test_add {'x': 12, 'y': 17}\n"
            return

        response = self.trex.transmit(method_name, params)
        if not self.virtual:
            # expect response
            rc, msg = response
            if rc:
                print "\nServer Response:\n\n" + json.dumps(msg) + "\n"
            else:
                print "\n*** " + msg + "\n"

    def do_push_files(self, filepaths):
        """Pushes a custom file to be stored locally on TRex server.\
        \nPush multiple files by specifying their path separated by ' ' (space)."""
        try:
            filepaths = filepaths.split(' ')
            print termstyle.green(
                "*** Starting pushing files ({trex_files}) to TRex. ***".
                format(trex_files=', '.join(filepaths)))
            ret_val = self.trex.push_files(filepaths)
            if ret_val:
                print termstyle.green(
                    "*** End of TRex push_files method (success) ***")
            else:
                print termstyle.magenta(
                    "*** End of TRex push_files method (failed) ***")

        except IOError as inst:
            print termstyle.magenta(inst)