Soft Patch Panel
 help / color / mirror / Atom feed
From: Yasufumi Ogawa <yasufum.o@gmail.com>
To: spp@dpdk.org, ferruh.yigit@intel.com, yasufum.o@gmail.com
Subject: [spp] [PATCH 1/1] cli: wait for spp_primary is launched
Date: Sat, 12 Oct 2019 18:59:24 +0900	[thread overview]
Message-ID: <20191012095924.30592-2-yasufum.o@gmail.com> (raw)
In-Reply-To: <20191012095924.30592-1-yasufum.o@gmail.com>

This update is to add checking if spp_primary is ready for use. Interval
time and timeout for checking are defined in Shell class as following.

    WAIT_PRI_INTERVAL = 0.5  # sec
    WAIT_PRI_TIMEOUT = 20    # sec

This update is also including refactor for each of `print` method to use
`format` method.

Signed-off-by: Yasufumi Ogawa <yasufum.o@gmail.com>
---
 src/cli/shell.py | 55 ++++++++++++++++++++++++++++++++++++------------
 1 file changed, 42 insertions(+), 13 deletions(-)

diff --git a/src/cli/shell.py b/src/cli/shell.py
index 925c231..7fda64d 100644
--- a/src/cli/shell.py
+++ b/src/cli/shell.py
@@ -18,12 +18,16 @@ from .shell_lib import common
 from . import spp_common
 from .spp_common import logger
 import subprocess
+import time
 import yaml
 
 
 class Shell(cmd.Cmd, object):
     """SPP command prompt."""
 
+    WAIT_PRI_INTERVAL = 0.5  # sec
+    WAIT_PRI_TIMEOUT = 20    # sec
+
     def __init__(self, spp_cli_objs, config, use_cache=False):
 
         # Load default config, can be changed via `config` command
@@ -78,6 +82,31 @@ class Shell(cmd.Cmd, object):
         common.set_current_server_addr(
                 self.spp_ctl_cli.ip_addr, self.spp_ctl_cli.port)
 
+        # Wait for launching spp_primary.
+        print('Waiting for spp_primary is ready .', end='', flush=True)
+        wait_cnt = self.WAIT_PRI_TIMEOUT / self.WAIT_PRI_INTERVAL
+        cnt = 0
+        is_pri_ready = False
+        while(is_pri_ready is False) and (cnt < wait_cnt):
+            res = self.spp_ctl_cli.get('processes')
+            if res is not None:
+                if res.status_code == 200:
+                    pri_obj = None
+                    try:
+                        proc_objs = res.json()
+                        for proc_obj in proc_objs:
+                            if proc_obj['type'] == 'primary':
+                                pri_obj = proc_obj
+                    except KeyError as e:
+                        print('Error: {} is not defined!'.format(e))
+
+                    if pri_obj is not None:
+                        is_pri_ready = True
+            time.sleep(self.WAIT_PRI_INTERVAL)
+            print('.', end='', flush=True)
+            cnt += 1
+        print(' OK!')
+
     def init_spp_procs(self):
         """Initialize delegators of SPP processes.
 
@@ -129,7 +158,7 @@ class Shell(cmd.Cmd, object):
         """
 
         if common.is_comment_line(line):
-            print("%s" % line.strip())
+            print("{}".format(line.strip()))
 
         else:
             super(Shell, self).default(line)
@@ -146,7 +175,7 @@ class Shell(cmd.Cmd, object):
         """Display information about connected clients."""
 
         print('- spp-ctl:')
-        print('  - address: %s:%s' % (self.spp_ctl_cli.ip_addr,
+        print('  - address: {}:{}'.format(self.spp_ctl_cli.ip_addr,
                                       self.spp_ctl_cli.port))
         res = self.spp_ctl_cli.get('processes')
         if res is not None:
@@ -177,8 +206,8 @@ class Shell(cmd.Cmd, object):
                     cnt = 1
                     for pt in ['nfv', 'vf', 'mirror', 'pcap']:
                         for obj in sec_obj[pt]:
-                            print('    %d: %s:%s' %
-                                  (cnt, obj['type'], obj['client-id']))
+                            print('    {}: {}:{}'.format(
+                                    cnt, obj['type'], obj['client-id']))
                             cnt += 1
                 except KeyError as e:
                     print('Error: {} is not defined!'.format(e))
@@ -312,7 +341,7 @@ class Shell(cmd.Cmd, object):
             if self._is_sec_registered('nfv', int(cmds[0])):
                 self.secondaries['nfv'][int(cmds[0])].run(cmds[1])
         else:
-            print('Invalid command: %s' % tmparg)
+            print('Invalid command: {}'.format(tmparg))
 
     def help_nfv(self):
         """Print help message of nfv command."""
@@ -373,7 +402,7 @@ class Shell(cmd.Cmd, object):
             if self._is_sec_registered('vf', int(cmds[0])):
                 self.secondaries['vf'][int(cmds[0])].run(cmds[1])
         else:
-            print('Invalid command: %s' % tmparg)
+            print('Invalid command: {}'.format(tmparg))
 
     def help_vf(self):
         """Print help message of vf command."""
@@ -430,7 +459,7 @@ class Shell(cmd.Cmd, object):
             if self._is_sec_registered('mirror', int(cmds[0])):
                 self.secondaries['mirror'][int(cmds[0])].run(cmds[1])
         else:
-            print('Invalid command: %s' % tmparg)
+            print('Invalid command: {}'.format(tmparg))
 
     def help_mirror(self):
         """Print help message of mirror command."""
@@ -839,7 +868,7 @@ class Shell(cmd.Cmd, object):
                 print("No subgraph.")
             else:
                 for label, subg in self.spp_topo.subgraphs.items():
-                    print('label: %s\tsubgraph: "%s"' % (label, subg))
+                    print('label: {}\tsubgraph: "{}"'.format(label, subg))
         else:  # add or del
             tokens = args_cleaned.split(' ')
             # Add subgraph
@@ -853,18 +882,18 @@ class Shell(cmd.Cmd, object):
 
                     # TODO(yasufum) add validation for subgraph
                     self.spp_topo.subgraphs[label] = subg
-                    print("Add subgraph '%s'" % label)
+                    print("Add subgraph '{}'".format(label))
                 else:
-                    print("Invalid syntax '%s'!" % args_cleaned)
+                    print("Invalid syntax '{}'!".format(args_cleaned))
             # Delete subgraph
             elif ((tokens[0] == 'del') or
                     (tokens[0] == 'delete') or
                     (tokens[0] == 'remove')):
                 del(self.spp_topo.subgraphs[tokens[1]])
-                print("Delete subgraph '%s'" % tokens[1])
+                print("Delete subgraph '{}'".format(tokens[1]))
 
             else:
-                print("Ivalid subcommand '%s'!" % tokens[0])
+                print("Ivalid subcommand '{}'!".format(tokens[0]))
 
     def help_topo_subgraph(self):
         """Print help message of topo_subgraph command."""
@@ -919,7 +948,7 @@ class Shell(cmd.Cmd, object):
         do_cmd = '%s.%s' % (mod_name, method_name)
         exec('Shell.%s = %s' % (method_name, do_cmd))
 
-        print("Module '%s' loaded." % mod_name)
+        print("Module '{}' loaded.".format(mod_name))
 
     def help_load_cmd(self):
         """Print help message of load_cmd command."""
-- 
2.17.1


      reply	other threads:[~2019-10-12  9:59 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-12  9:59 [spp] [PATCH 0/1] Add checking " Yasufumi Ogawa
2019-10-12  9:59 ` Yasufumi Ogawa [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191012095924.30592-2-yasufum.o@gmail.com \
    --to=yasufum.o@gmail.com \
    --cc=ferruh.yigit@intel.com \
    --cc=spp@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).