blob: a73057e3de7530ec8a63fc045d6f537c38fe30e0 [file] [log] [blame]
TamasBakaid38feb62019-02-28 09:06:19 +00001#!/usr/bin/env python3
2import argparse
3import sys
TamasBakaid38feb62019-02-28 09:06:19 +00004import subprocess
5import ipaddress
6import time
Bartek Grzybowski1f762342020-05-05 08:54:37 -07007import logging
Bartek Grzybowski8200d2a2020-05-05 03:50:38 -07008from requests import get
9from requests.exceptions import MissingSchema, InvalidSchema, InvalidURL, ConnectionError, ConnectTimeout
10
11def validate_url(url):
12 '''Helper function to perform --urlves input param validation'''
Bartek Grzybowski1f762342020-05-05 08:54:37 -070013 logger = logging.getLogger("urllib3")
14 logger.setLevel(logging.WARNING)
Bartek Grzybowski8200d2a2020-05-05 03:50:38 -070015 try:
16 get(url, timeout=0.001)
17 except (MissingSchema, InvalidSchema, InvalidURL):
18 raise argparse.ArgumentTypeError(f'{url} is not a valid URL')
19 except (ConnectionError, ConnectTimeout):
20 pass
21 return url
22
23def validate_ip(ip):
24 '''Helper function to validate input param is a vaild IP address'''
25 try:
26 ip_valid = ipaddress.ip_address(ip)
27 except ValueError:
28 raise argparse.ArgumentTypeError(f'{ip} is not a valid IP address')
29 else:
30 return ip_valid
TamasBakaid38feb62019-02-28 09:06:19 +000031
Bartek Grzybowski879d5672020-05-06 03:37:23 -070032if sys.stdout.isatty():
33 logging.basicConfig(level=logging.INFO, format='\033[92m[%(levelname)s]\033[0m %(message)s')
34else:
35 logging.basicConfig(level=logging.INFO, format='[%(levelname)s] %(message)s')
36
TamasBakaid38feb62019-02-28 09:06:19 +000037parser = argparse.ArgumentParser()
Bartek Grzybowski70c116f2020-05-05 04:27:25 -070038parser.add_argument('--bootstrap', help='Bootstrap the system', type=int, metavar='COUNT')
39parser.add_argument('--trigger', help='Trigger one single VES event from each simulator', type=int,
40 metavar='COUNT')
41parser.add_argument('--triggerstart', help='Trigger only a subset of the simulators (note --triggerend)', type=int,
42 metavar='COUNT_START')
43parser.add_argument('--triggerend', help='Last instance to trigger', type=int, metavar='COUNT_END')
44parser.add_argument('--urlves', help='URL of the VES collector', type=validate_url, metavar='URL')
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -070045parser.add_argument('--ipfileserver', help='Visible IP of the file server (SFTP/FTPS) to be included in the VES event',
Bartek Grzybowski70c116f2020-05-05 04:27:25 -070046 type=validate_ip, metavar='IP')
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -070047parser.add_argument('--typefileserver', help='Type of the file server (SFTP/FTPS) to be included in the VES event',
48 type=str, choices=['sftp', 'ftps'])
Bartek Grzybowski70c116f2020-05-05 04:27:25 -070049parser.add_argument('--ipstart', help='IP address range beginning', type=validate_ip, metavar='IP')
Bartek Grzybowskic29b9462020-04-30 07:11:59 -070050parser.add_argument('--clean', action='store_true', help='Clean work-dirs')
Bartek Grzybowski70c116f2020-05-05 04:27:25 -070051parser.add_argument('--start', help='Start instances', type=int, metavar='COUNT')
52parser.add_argument('--status', help='Status', type=int, metavar='COUNT')
53parser.add_argument('--stop', help='Stop instances', type=int, metavar='COUNT')
Bartek Grzybowski1f762342020-05-05 08:54:37 -070054parser.add_argument('--verbose', help='Verbosity level', choices=['info', 'debug'],
55 type=str, default='debug')
TamasBakaid38feb62019-02-28 09:06:19 +000056
57args = parser.parse_args()
Bartek Grzybowski879d5672020-05-06 03:37:23 -070058
Bartek Grzybowski1f762342020-05-05 08:54:37 -070059logger = logging.getLogger(__name__)
60logger.setLevel(getattr(logging, args.verbose.upper()))
TamasBakaid38feb62019-02-28 09:06:19 +000061
TamasBakaibab325b2019-03-26 09:20:16 +000062if args.bootstrap and args.ipstart and args.urlves:
Bartek Grzybowski1f762342020-05-05 08:54:37 -070063 logger.info("Bootstrap:")
TamasBakaid38feb62019-02-28 09:06:19 +000064
Bartek Grzybowski8ec08162020-05-06 04:42:18 -070065 start_port = 2000
66 ftps_pasv_port_start = 8000
67 ftps_pasv_port_num_of_ports = 10
Bartek Grzybowski3d3d3c22020-03-05 10:28:03 +010068
Bartek Grzybowski8ec08162020-05-06 04:42:18 -070069 ftps_pasv_port_end = ftps_pasv_port_start + ftps_pasv_port_num_of_ports
Bartek Grzybowski3d3d3c22020-03-05 10:28:03 +010070
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -070071 for i in range(args.bootstrap):
Bartek Grzybowskic9514c72020-05-06 05:56:20 -070072 logger.info(f"PNF simulator instance: {i}")
TamasBakaid38feb62019-02-28 09:06:19 +000073
TamasBakaid38feb62019-02-28 09:06:19 +000074 # The IP ranges are in distance of 16 compared to each other.
75 # This is matching the /28 subnet mask used in the dockerfile inside.
Bartek Grzybowskic9514c72020-05-06 05:56:20 -070076 ip_offset = i * 16
TamasBakaid38feb62019-02-28 09:06:19 +000077
Bartek Grzybowskic9514c72020-05-06 05:56:20 -070078 ip_subnet = args.ipstart + ip_offset
79 logger.debug(f"\tIp Subnet: {ip_subnet}")
TamasBakaid38feb62019-02-28 09:06:19 +000080
Bartek Grzybowskic9514c72020-05-06 05:56:20 -070081 ip_gw = args.ipstart + 1 + ip_offset
82 logger.debug(f"\tIP Gateway: {ip_gw}")
83
84 ip_PnfSim = args.ipstart + 2 + ip_offset
85 logger.debug(f"\tIp Pnf SIM: {ip_PnfSim}")
TamasBakaid38feb62019-02-28 09:06:19 +000086
Bartek Grzybowski8ec08162020-05-06 04:42:18 -070087 PortSftp = start_port + 1
88 PortFtps = start_port + 2
89 start_port += 2
Bartek Grzybowski3d3d3c22020-03-05 10:28:03 +010090
Bartek Grzybowskic9514c72020-05-06 05:56:20 -070091 ip_ftps = args.ipstart + 3 + ip_offset
92 logger.debug(f"\tUrl Ftps: {ip_ftps}")
TamasBakaid38feb62019-02-28 09:06:19 +000093
Bartek Grzybowskic9514c72020-05-06 05:56:20 -070094 ip_sftp = args.ipstart + 4 + ip_offset
95 logger.debug(f"\tUrl Sftp: {ip_sftp}")
96
97 foldername = f"pnf-sim-lw-{i}"
TamasBakaid38feb62019-02-28 09:06:19 +000098 completed = subprocess.run('mkdir ' + foldername, shell=True)
Bartek Grzybowski1f762342020-05-05 08:54:37 -070099 logger.info(f'\tCreating folder: {completed.stdout}')
TamasBakaid38feb62019-02-28 09:06:19 +0000100 completed = subprocess.run(
101 'cp -r pnf-sim-lightweight/* ' +
102 foldername,
103 shell=True)
Bartek Grzybowski1f762342020-05-05 08:54:37 -0700104 logger.info(f'\tCloning folder: {completed.stdout}')
TamasBakaid38feb62019-02-28 09:06:19 +0000105
TamasBakaibab325b2019-03-26 09:20:16 +0000106 composercmd = "./simulator.sh compose " + \
107 str(ip_gw) + " " + \
108 str(ip_subnet) + " " + \
109 str(i) + " " + \
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -0700110 args.urlves + " " + \
Bartek Grzybowskic9514c72020-05-06 05:56:20 -0700111 str(ip_PnfSim) + " " + \
Bartek Grzybowski8ec08162020-05-06 04:42:18 -0700112 str(args.ipfileserver) + " " + \
113 args.typefileserver + " " + \
TamasBakaibab325b2019-03-26 09:20:16 +0000114 str(PortSftp) + " " + \
115 str(PortFtps) + " " + \
Bartek Grzybowskic9514c72020-05-06 05:56:20 -0700116 str(ip_ftps) + " " + \
117 str(ip_sftp) + " " + \
TamasBakaie781c7c2019-07-11 11:26:20 +0000118 str(ftps_pasv_port_start) + " " + \
119 str(ftps_pasv_port_end)
TamasBakaid38feb62019-02-28 09:06:19 +0000120
121 completed = subprocess.run(
122 'set -x; cd ' +
123 foldername +
124 '; ' +
125 composercmd,
126 shell=True)
Bartek Grzybowski1f762342020-05-05 08:54:37 -0700127 logger.info(f'Cloning: {completed.stdout}')
Bartek Grzybowski3d3d3c22020-03-05 10:28:03 +0100128
TamasBakaie781c7c2019-07-11 11:26:20 +0000129 ftps_pasv_port_start += ftps_pasv_port_num_of_ports + 1
Bartek Grzybowski8ec08162020-05-06 04:42:18 -0700130 ftps_pasv_port_end += ftps_pasv_port_num_of_ports + 1
TamasBakaid38feb62019-02-28 09:06:19 +0000131
TamasBakaibab325b2019-03-26 09:20:16 +0000132 completed = subprocess.run('set -x; cd pnf-sim-lightweight; ./simulator.sh build ', shell=True)
Bartek Grzybowski1f762342020-05-05 08:54:37 -0700133 logger.info(f"Build docker image: {completed.stdout}")
TamasBakaibab325b2019-03-26 09:20:16 +0000134
TamasBakaid38feb62019-02-28 09:06:19 +0000135 sys.exit()
136
137if args.clean:
138 completed = subprocess.run('rm -rf ./pnf-sim-lw-*', shell=True)
Bartek Grzybowski1f762342020-05-05 08:54:37 -0700139 logger.info(f'Deleting: {completed.stdout}')
TamasBakaid38feb62019-02-28 09:06:19 +0000140 sys.exit()
141
142if args.start:
143
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -0700144 for i in range(args.start):
Bartek Grzybowskic9514c72020-05-06 05:56:20 -0700145 foldername = f"pnf-sim-lw-{i}"
TamasBakaid38feb62019-02-28 09:06:19 +0000146
147 completed = subprocess.run(
148 'set -x ; cd ' +
149 foldername +
150 "; bash -x ./simulator.sh start",
151 shell=True)
Bartek Grzybowski1f762342020-05-05 08:54:37 -0700152 logger.info(f'Starting: {completed.stdout}')
TamasBakaid38feb62019-02-28 09:06:19 +0000153
154 time.sleep(5)
155
156if args.status:
157
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -0700158 for i in range(args.status):
Bartek Grzybowskic9514c72020-05-06 05:56:20 -0700159 foldername = f"pnf-sim-lw-{i}"
TamasBakaid38feb62019-02-28 09:06:19 +0000160
161 completed = subprocess.run(
162 'cd ' +
163 foldername +
164 "; ./simulator.sh status",
165 shell=True)
Bartek Grzybowski1f762342020-05-05 08:54:37 -0700166 logger.info(f'Status: {completed.stdout}')
TamasBakaid38feb62019-02-28 09:06:19 +0000167
168if args.stop:
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -0700169 for i in range(args.stop):
Bartek Grzybowskic9514c72020-05-06 05:56:20 -0700170 foldername = f"pnf-sim-lw-{i}"
TamasBakaid38feb62019-02-28 09:06:19 +0000171
172 completed = subprocess.run(
173 'cd ' +
174 foldername +
Bartek Grzybowskic9514c72020-05-06 05:56:20 -0700175 f"; ./simulator.sh stop {i}",
TamasBakaid38feb62019-02-28 09:06:19 +0000176 shell=True)
Bartek Grzybowski1f762342020-05-05 08:54:37 -0700177 logger.info(f'Stopping: {completed.stdout}')
TamasBakaid38feb62019-02-28 09:06:19 +0000178
TamasBakaib59bffb2019-03-22 09:52:03 +0000179
180if args.trigger:
Bartek Grzybowski1f762342020-05-05 08:54:37 -0700181 logger.info("Triggering VES sending:")
TamasBakaib59bffb2019-03-22 09:52:03 +0000182
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -0700183 for i in range(args.trigger):
Bartek Grzybowskic9514c72020-05-06 05:56:20 -0700184 foldername = f"pnf-sim-lw-{i}"
TamasBakaib59bffb2019-03-22 09:52:03 +0000185
186 completed = subprocess.run(
187 'cd ' +
188 foldername +
189 "; ./simulator.sh trigger-simulator",
190 shell=True)
Bartek Grzybowski1f762342020-05-05 08:54:37 -0700191 logger.info(f'Status: {completed.stdout}')
Bartek Grzybowski3d3d3c22020-03-05 10:28:03 +0100192
TamasBakai34a6f112019-04-15 08:38:51 +0000193if args.triggerstart and args.triggerend:
Bartek Grzybowski1f762342020-05-05 08:54:37 -0700194 logger.info("Triggering VES sending by a range of simulators:")
Bartek Grzybowski3d3d3c22020-03-05 10:28:03 +0100195
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -0700196 for i in range(args.triggerstart, args.triggerend+1):
Bartek Grzybowskic9514c72020-05-06 05:56:20 -0700197 foldername = f"pnf-sim-lw-{i}"
198 logger.info(f"Instance being processed: {i}")
Bartek Grzybowski3d3d3c22020-03-05 10:28:03 +0100199
TamasBakai34a6f112019-04-15 08:38:51 +0000200 completed = subprocess.run(
201 'cd ' +
202 foldername +
203 "; ./simulator.sh trigger-simulator",
204 shell=True)
Bartek Grzybowski1f762342020-05-05 08:54:37 -0700205 logger.info(f'Status: {completed.stdout}')
TamasBakaid38feb62019-02-28 09:06:19 +0000206else:
Bartek Grzybowski1f762342020-05-05 08:54:37 -0700207 logger.warning("No instruction was defined")
Bartek Grzybowskic29b9462020-04-30 07:11:59 -0700208 parser.print_usage()
TamasBakaid38feb62019-02-28 09:06:19 +0000209 sys.exit()