Rework input parameters handling 82/107382/4
authorBartek Grzybowski <b.grzybowski@partner.samsung.com>
Fri, 8 May 2020 11:39:22 +0000 (04:39 -0700)
committerMarco Platania <platania@research.att.com>
Mon, 11 May 2020 19:25:09 +0000 (19:25 +0000)
Main input options were turned into subcommands
for improved CLI user experience.

Input parameters handling and validation logic was
moved out of the code to argparse native facilities.

Updated the doc around script usage details.

Change-Id: Ifaa2a0e49f2a0b5b677020307dc74942c422bbe1
Issue-ID: INT-1577
Signed-off-by: Bartek Grzybowski <b.grzybowski@partner.samsung.com>
test/mocks/mass-pnf-sim/README.md
test/mocks/mass-pnf-sim/clean.sh
test/mocks/mass-pnf-sim/mass-pnf-sim.py

index f997f56..0e6ac03 100644 (file)
@@ -18,7 +18,7 @@ For debug purposes, you can use your own IP address as VES collector, use "ip" c
 Example:
 
 ```
-./mass-pnf-sim.py  --bootstrap 2 --urlves http://10.148.95.??:10000/eventListener/v7 --ipfileserver 10.148.95.??? --typefileserver sftp --ipstart 10.11.0.16
+./mass-pnf-sim.py bootstrap --count 2 --urlves http://10.148.95.??:10000/eventListener/v7 --ipfileserver 10.148.95.??? --typefileserver sftp --ipstart 10.11.0.16
 ```
 
 Note that the file creator is started at a time of the bootstrapping.
@@ -35,13 +35,13 @@ sudo nc -vv -l -k -p 10000
 Define the amount of simulators to be launched
 
 ```
-./mass-pnf-sim.py  --start 2
+./mass-pnf-sim.py start --count 2
 ```
 
 ### Trigger
 
 ```
-./mass-pnf-sim.py  --trigger 2
+./mass-pnf-sim.py trigger --count 2
 ```
 
 ### Trigger only a subset of the simulators
@@ -49,20 +49,20 @@ Define the amount of simulators to be launched
 The following command will trigger 0,1,2,3:
 
 ```
-./mass-pnf-sim.py  --triggerstart 0 --triggerend 3
+./mass-pnf-sim.py trigger-custom --triggerstart 0 --triggerend 3
 ```
 
 The following command will trigger 4 and 5:
 
 ```
-./mass-pnf-sim.py  --triggerstart 4 --triggerend 5
+./mass-pnf-sim.py trigger-custom --triggerstart 4 --triggerend 5
 ```
 
 ### Stop and clean
 
 ```
-./mass-pnf-sim.py  --stop 2
-./mass-pnf-sim.py  --clean
+./mass-pnf-sim.py stop --count 2
+./mass-pnf-sim.py clean
 ```
 
 ### Verbose printout from Python
index 7ba25e4..c557d3e 100755 (executable)
@@ -4,5 +4,4 @@ killall ROP_file_creator.sh sleep
 
 docker stop $(docker ps -aq); docker rm $(docker ps -aq)
 
-./mass-pnf-sim.py --clean
-
+./mass-pnf-sim.py clean
index 7358eb5..aa371e0 100755 (executable)
@@ -36,22 +36,37 @@ else:
     logging.basicConfig(level=logging.INFO, format='[%(levelname)s] %(message)s')
 
 parser = argparse.ArgumentParser()
-parser.add_argument('--bootstrap', help='Bootstrap the system', type=int, metavar='COUNT')
-parser.add_argument('--trigger', help='Trigger one single VES event from each simulator', type=int,
-                    metavar='COUNT')
-parser.add_argument('--triggerstart', help='Trigger only a subset of the simulators (note --triggerend)', type=int,
-                    metavar='COUNT_START')
-parser.add_argument('--triggerend', help='Last instance to trigger', type=int, metavar='COUNT_END')
-parser.add_argument('--urlves', help='URL of the VES collector', type=validate_url, metavar='URL')
-parser.add_argument('--ipfileserver', help='Visible IP of the file server (SFTP/FTPS) to be included in the VES event',
-                    type=validate_ip, metavar='IP')
-parser.add_argument('--typefileserver', help='Type of the file server (SFTP/FTPS) to be included in the VES event',
-                    type=str, choices=['sftp', 'ftps'])
-parser.add_argument('--ipstart', help='IP address range beginning', type=validate_ip, metavar='IP')
-parser.add_argument('--clean', action='store_true', help='Clean work-dirs')
-parser.add_argument('--start', help='Start instances', type=int, metavar='COUNT')
-parser.add_argument('--status', help='Status', type=int, metavar='COUNT')
-parser.add_argument('--stop', help='Stop instances', type=int, metavar='COUNT')
+subparsers = parser.add_subparsers(title='Subcommands', dest='subcommand')
+# Bootstrap command parser
+parser_bootstrap = subparsers.add_parser('bootstrap', help='Bootstrap the system')
+parser_bootstrap.add_argument('--count', help='Instance count to bootstrap', type=int, metavar='INT', default=1)
+parser_bootstrap.add_argument('--urlves', help='URL of the VES collector', type=validate_url, metavar='URL', required=True)
+parser_bootstrap.add_argument('--ipfileserver', help='Visible IP of the file server (SFTP/FTPS) to be included in the VES event',
+                              type=validate_ip, metavar='IP', required=True)
+parser_bootstrap.add_argument('--typefileserver', help='Type of the file server (SFTP/FTPS) to be included in the VES event',
+                              type=str, choices=['sftp', 'ftps'], required=True)
+parser_bootstrap.add_argument('--ipstart', help='IP address range beginning', type=validate_ip, metavar='IP', required=True)
+# Start command parser
+parser_start = subparsers.add_parser('start', help='Start instances')
+parser_start.add_argument('--count', help='Instance count to start', type=int, metavar='INT', default=1)
+# Stop command parser
+parser_stop = subparsers.add_parser('stop', help='Stop instances')
+parser_stop.add_argument('--count', help='Instance count to stop', type=int, metavar='INT', default=1)
+# Trigger command parser
+parser_trigger = subparsers.add_parser('trigger', help='Trigger one single VES event from each simulator')
+parser_trigger.add_argument('--count', help='Instance count to trigger', type=int, metavar='INT', default=1)
+# Trigger-custom command parser
+parser_triggerstart = subparsers.add_parser('trigger-custom', help='Trigger one single VES event from specific simulators')
+parser_triggerstart.add_argument('--triggerstart', help='First simulator id to trigger', type=int,
+                                 metavar='INT', required=True)
+parser_triggerstart.add_argument('--triggerend', help='Last simulator id to trigger', type=int,
+                                 metavar='INT', required=True)
+# Status command parser
+parser_status = subparsers.add_parser('status', help='Status')
+parser_status.add_argument('--count', help='Instance count to show status for', type=int, metavar='INT', default=1)
+# Clean command parser
+parser_clean = subparsers.add_parser('clean', help='Clean work-dirs')
+# General options parser
 parser.add_argument('--verbose', help='Verbosity level', choices=['info', 'debug'],
                     type=str, default='debug')
 
@@ -60,7 +75,11 @@ args = parser.parse_args()
 logger = logging.getLogger(__name__)
 logger.setLevel(getattr(logging, args.verbose.upper()))
 
-if args.bootstrap and args.ipstart and args.urlves:
+if args.subcommand is None:
+    parser.print_usage()
+    sys.exit(0)
+
+if args.subcommand == 'bootstrap' :
     logger.info("Bootstrapping PNF instances")
 
     start_port = 2000
@@ -69,7 +88,7 @@ if args.bootstrap and args.ipstart and args.urlves:
 
     ftps_pasv_port_end = ftps_pasv_port_start + ftps_pasv_port_num_of_ports
 
-    for i in range(args.bootstrap):
+    for i in range(args.count):
         logger.info(f"PNF simulator instance: {i}")
 
         # The IP ranges are in distance of 16 compared to each other.
@@ -138,16 +157,13 @@ if args.bootstrap and args.ipstart and args.urlves:
     completed = subprocess.run('set -x; cd pnf-sim-lightweight; ./simulator.sh build ', shell=True)
     logger.info(f"Build docker image: {completed.stdout}")
 
-    sys.exit()
-
-if args.clean:
+if args.subcommand == 'clean':
     completed = subprocess.run('rm -rf ./pnf-sim-lw-*', shell=True)
     logger.info(f'Deleting: {completed.stdout}')
-    sys.exit()
 
-if args.start:
+if args.subcommand == 'start':
 
-    for i in range(args.start):
+    for i in range(args.count):
         foldername = f"pnf-sim-lw-{i}"
 
         completed = subprocess.run(
@@ -156,12 +172,11 @@ if args.start:
             "; bash -x ./simulator.sh start",
             shell=True)
         logger.info(f'Starting: {completed.stdout}')
-
         time.sleep(5)
 
-if args.status:
+if args.subcommand == 'status':
 
-    for i in range(args.status):
+    for i in range(args.count):
         foldername = f"pnf-sim-lw-{i}"
 
         completed = subprocess.run(
@@ -171,8 +186,8 @@ if args.status:
             shell=True)
         logger.info(f'Status: {completed.stdout}')
 
-if args.stop:
-    for i in range(args.stop):
+if args.subcommand == 'stop':
+    for i in range(args.count):
         foldername = f"pnf-sim-lw-{i}"
 
         completed = subprocess.run(
@@ -183,10 +198,10 @@ if args.stop:
         logger.info(f'Stopping: {completed.stdout}')
 
 
-if args.trigger:
+if args.subcommand == 'trigger':
     logger.info("Triggering VES sending:")
 
-    for i in range(args.trigger):
+    for i in range(args.count):
         foldername = f"pnf-sim-lw-{i}"
 
         completed = subprocess.run(
@@ -196,7 +211,7 @@ if args.trigger:
             shell=True)
         logger.info(f'Status: {completed.stdout}')
 
-if args.triggerstart and args.triggerend:
+if args.subcommand == 'trigger-custom':
     logger.info("Triggering VES sending by a range of simulators:")
 
     for i in range(args.triggerstart, args.triggerend+1):
@@ -209,7 +224,3 @@ if args.triggerstart and args.triggerend:
             "; ./simulator.sh trigger-simulator",
             shell=True)
         logger.info(f'Status: {completed.stdout}')
-else:
-    logger.warning("No instruction was defined")
-    parser.print_usage()
-    sys.exit()