Soft Patch Panel
 help / color / mirror / Atom feed
* [spp] [PATCH 0/1] Add checking for spp_primary is launched
@ 2019-10-12  9:59 Yasufumi Ogawa
  2019-10-12  9:59 ` [spp] [PATCH 1/1] cli: wait " Yasufumi Ogawa
  0 siblings, 1 reply; 2+ messages in thread
From: Yasufumi Ogawa @ 2019-10-12  9:59 UTC (permalink / raw)
  To: spp, ferruh.yigit, yasufum.o

This patch is to add checking if spp_primary is ready for use in SPP
CLI. It is applied to both of the latest and stable-v18.08 repositories.

Yasufumi Ogawa (1):
  cli: wait for spp_primary is launched

 src/cli/shell.py | 55 ++++++++++++++++++++++++++++++++++++------------
 1 file changed, 42 insertions(+), 13 deletions(-)

-- 
2.17.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [spp] [PATCH 1/1] cli: wait for spp_primary is launched
  2019-10-12  9:59 [spp] [PATCH 0/1] Add checking for spp_primary is launched Yasufumi Ogawa
@ 2019-10-12  9:59 ` Yasufumi Ogawa
  0 siblings, 0 replies; 2+ messages in thread
From: Yasufumi Ogawa @ 2019-10-12  9:59 UTC (permalink / raw)
  To: spp, ferruh.yigit, yasufum.o

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-10-12  9:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-12  9:59 [spp] [PATCH 0/1] Add checking for spp_primary is launched Yasufumi Ogawa
2019-10-12  9:59 ` [spp] [PATCH 1/1] cli: wait " Yasufumi Ogawa

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).