From 1f762345464de01b37efcfa009c40fe5fd4201aa Mon Sep 17 00:00:00 2001 From: Bartek Grzybowski Date: Tue, 5 May 2020 08:54:37 -0700 Subject: Make script verbosity adjustible Adds '--verbose' option and leverages 'logging' module facilities. Change-Id: I9b079c476949788678167afb1ed115ad4490d99c Issue-ID: INT-1577 Signed-off-by: Bartek Grzybowski --- test/mocks/mass-pnf-sim/mass-pnf-sim.py | 52 +++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 22 deletions(-) (limited to 'test/mocks/mass-pnf-sim') diff --git a/test/mocks/mass-pnf-sim/mass-pnf-sim.py b/test/mocks/mass-pnf-sim/mass-pnf-sim.py index 0ca29d007..c4fb0ded8 100755 --- a/test/mocks/mass-pnf-sim/mass-pnf-sim.py +++ b/test/mocks/mass-pnf-sim/mass-pnf-sim.py @@ -4,11 +4,16 @@ import sys import subprocess import ipaddress import time +import logging from requests import get from requests.exceptions import MissingSchema, InvalidSchema, InvalidURL, ConnectionError, ConnectTimeout +logging.basicConfig(level=logging.INFO, format='\033[92m[%(levelname)s]\033[0m %(message)s') + def validate_url(url): '''Helper function to perform --urlves input param validation''' + logger = logging.getLogger("urllib3") + logger.setLevel(logging.WARNING) try: get(url, timeout=0.001) except (MissingSchema, InvalidSchema, InvalidURL): @@ -43,11 +48,15 @@ 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') +parser.add_argument('--verbose', help='Verbosity level', choices=['info', 'debug'], + type=str, default='debug') args = parser.parse_args() +logger = logging.getLogger(__name__) +logger.setLevel(getattr(logging, args.verbose.upper())) if args.bootstrap and args.ipstart and args.urlves: - print("Bootstrap:") + logger.info("Bootstrap:") start_port=2000 ftps_pasv_port_start=8000 @@ -55,20 +64,19 @@ 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): - print("PNF simulator instance: " + str(i) + ".") + logger.info("PNF simulator instance: " + str(i) + ".") ip_subnet = args.ipstart + int(0 + (i * 16)) - print("\tIp Subnet:" + str(ip_subnet)) + logger.debug("\tIp Subnet:" + str(ip_subnet)) # The IP ranges are in distance of 16 compared to each other. # This is matching the /28 subnet mask used in the dockerfile inside. ip_gw = args.ipstart + int(1 + (i * 16)) - print("\tIP Gateway:" + str(ip_gw)) + logger.debug("\tIP Gateway:" + str(ip_gw)) IpPnfSim = args.ipstart + int(2 + (i * 16)) - print("\tIp Pnf SIM:" + str(IpPnfSim)) + logger.debug("\tIp Pnf SIM:" + str(IpPnfSim)) IpFileServer = str(args.ipfileserver) TypeFileServer = args.typefileserver @@ -77,19 +85,19 @@ if args.bootstrap and args.ipstart and args.urlves: PortFtps=start_port +2 start_port +=2 UrlFtps = str(args.ipstart + int(3 + (i * 16))) - print("\tUrl Ftps: " + str(UrlFtps)) + logger.debug("\tUrl Ftps: " + str(UrlFtps)) UrlSftp = str(args.ipstart + int(4 + (i * 16))) - print("\tUrl Sftp: " + str(UrlSftp)) + logger.debug("\tUrl Sftp: " + str(UrlSftp)) foldername = "pnf-sim-lw-" + str(i) completed = subprocess.run('mkdir ' + foldername, shell=True) - print('\tCreating folder:', completed.stdout) + logger.info(f'\tCreating folder: {completed.stdout}') completed = subprocess.run( 'cp -r pnf-sim-lightweight/* ' + foldername, shell=True) - print('\tCloning folder:', completed.stdout) + logger.info(f'\tCloning folder: {completed.stdout}') composercmd = "./simulator.sh compose " + \ str(ip_gw) + " " + \ @@ -112,19 +120,19 @@ if args.bootstrap and args.ipstart and args.urlves: '; ' + composercmd, shell=True) - print('Cloning:', completed.stdout) + logger.info(f'Cloning: {completed.stdout}') ftps_pasv_port_start += ftps_pasv_port_num_of_ports + 1 ftps_pasv_port_end += ftps_pasv_port_num_of_ports +1 completed = subprocess.run('set -x; cd pnf-sim-lightweight; ./simulator.sh build ', shell=True) - print("Build docker image: ", completed.stdout) + logger.info(f"Build docker image: {completed.stdout}") sys.exit() if args.clean: completed = subprocess.run('rm -rf ./pnf-sim-lw-*', shell=True) - print('Deleting:', completed.stdout) + logger.info(f'Deleting: {completed.stdout}') sys.exit() if args.start: @@ -137,7 +145,7 @@ if args.start: foldername + "; bash -x ./simulator.sh start", shell=True) - print('Starting:', completed.stdout) + logger.info(f'Starting: {completed.stdout}') time.sleep(5) @@ -151,7 +159,7 @@ if args.status: foldername + "; ./simulator.sh status", shell=True) - print('Status:', completed.stdout) + logger.info(f'Status: {completed.stdout}') if args.stop: for i in range(args.stop): @@ -162,11 +170,11 @@ if args.stop: foldername + "; ./simulator.sh stop " + str(i), shell=True) - print('Stopping:', completed.stdout) + logger.info(f'Stopping: {completed.stdout}') if args.trigger: - print("Triggering VES sending:") + logger.info("Triggering VES sending:") for i in range(args.trigger): foldername = "pnf-sim-lw-" + str(i) @@ -176,22 +184,22 @@ if args.trigger: foldername + "; ./simulator.sh trigger-simulator", shell=True) - print('Status:', completed.stdout) + logger.info(f'Status: {completed.stdout}') if args.triggerstart and args.triggerend: - print("Triggering VES sending by a range of simulators:") + logger.info("Triggering VES sending by a range of simulators:") for i in range(args.triggerstart, args.triggerend+1): foldername = "pnf-sim-lw-" + str(i) - print("Instance being processed:" + str(i)) + logger.info("Instance being processed:" + str(i)) completed = subprocess.run( 'cd ' + foldername + "; ./simulator.sh trigger-simulator", shell=True) - print('Status:', completed.stdout) + logger.info(f'Status: {completed.stdout}') else: - print("No instruction was defined") + logger.warning("No instruction was defined") parser.print_usage() sys.exit() -- cgit 1.2.3-korg