TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | import argparse |
| 3 | import sys |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 4 | import subprocess |
| 5 | import ipaddress |
| 6 | import time |
Bartek Grzybowski | 8200d2a | 2020-05-05 03:50:38 -0700 | [diff] [blame] | 7 | from requests import get |
| 8 | from requests.exceptions import MissingSchema, InvalidSchema, InvalidURL, ConnectionError, ConnectTimeout |
| 9 | |
| 10 | def validate_url(url): |
| 11 | '''Helper function to perform --urlves input param validation''' |
| 12 | try: |
| 13 | get(url, timeout=0.001) |
| 14 | except (MissingSchema, InvalidSchema, InvalidURL): |
| 15 | raise argparse.ArgumentTypeError(f'{url} is not a valid URL') |
| 16 | except (ConnectionError, ConnectTimeout): |
| 17 | pass |
| 18 | return url |
| 19 | |
| 20 | def validate_ip(ip): |
| 21 | '''Helper function to validate input param is a vaild IP address''' |
| 22 | try: |
| 23 | ip_valid = ipaddress.ip_address(ip) |
| 24 | except ValueError: |
| 25 | raise argparse.ArgumentTypeError(f'{ip} is not a valid IP address') |
| 26 | else: |
| 27 | return ip_valid |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 28 | |
| 29 | parser = argparse.ArgumentParser() |
Bartek Grzybowski | 32fb5ab | 2020-05-05 03:54:01 -0700 | [diff] [blame^] | 30 | parser.add_argument('--bootstrap', help='Bootstrap the system', type=int) |
| 31 | parser.add_argument('--trigger', help='Trigger one single VES event from each simulator', type=int) |
| 32 | parser.add_argument('--triggerstart', help='Trigger only a subset of the simulators (note --triggerend)', type=int) |
| 33 | parser.add_argument('--triggerend', help='Last instance to trigger', type=int) |
| 34 | parser.add_argument('--urlves', help='URL of the VES collector', type=validate_url) |
| 35 | parser.add_argument('--ipfileserver', help='Visible IP of the file server (SFTP/FTPS) to be included in the VES event', |
| 36 | type=validate_ip) |
| 37 | parser.add_argument('--typefileserver', help='Type of the file server (SFTP/FTPS) to be included in the VES event', |
| 38 | type=str, choices=['sftp', 'ftps']) |
| 39 | parser.add_argument('--ipstart', help='IP address range beginning', type=validate_ip) |
Bartek Grzybowski | c29b946 | 2020-04-30 07:11:59 -0700 | [diff] [blame] | 40 | parser.add_argument('--clean', action='store_true', help='Clean work-dirs') |
Bartek Grzybowski | 32fb5ab | 2020-05-05 03:54:01 -0700 | [diff] [blame^] | 41 | parser.add_argument('--start', help='Start instances', type=int) |
| 42 | parser.add_argument('--status', help='Status', type=int) |
| 43 | parser.add_argument('--stop', help='Stop instances', type=int) |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 44 | |
| 45 | args = parser.parse_args() |
| 46 | |
TamasBakai | bab325b | 2019-03-26 09:20:16 +0000 | [diff] [blame] | 47 | if args.bootstrap and args.ipstart and args.urlves: |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 48 | print("Bootstrap:") |
| 49 | |
TamasBakai | b59bffb | 2019-03-22 09:52:03 +0000 | [diff] [blame] | 50 | start_port=2000 |
TamasBakai | e781c7c | 2019-07-11 11:26:20 +0000 | [diff] [blame] | 51 | ftps_pasv_port_start=8000 |
| 52 | ftps_pasv_port_num_of_ports=10 |
Bartek Grzybowski | 3d3d3c2 | 2020-03-05 10:28:03 +0100 | [diff] [blame] | 53 | |
TamasBakai | e781c7c | 2019-07-11 11:26:20 +0000 | [diff] [blame] | 54 | ftps_pasv_port_end=ftps_pasv_port_start + ftps_pasv_port_num_of_ports |
Bartek Grzybowski | 3d3d3c2 | 2020-03-05 10:28:03 +0100 | [diff] [blame] | 55 | |
TamasBakai | b59bffb | 2019-03-22 09:52:03 +0000 | [diff] [blame] | 56 | |
Bartek Grzybowski | 32fb5ab | 2020-05-05 03:54:01 -0700 | [diff] [blame^] | 57 | for i in range(args.bootstrap): |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 58 | print("PNF simulator instance: " + str(i) + ".") |
| 59 | |
Bartek Grzybowski | 32fb5ab | 2020-05-05 03:54:01 -0700 | [diff] [blame^] | 60 | ip_subnet = args.ipstart + int(0 + (i * 16)) |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 61 | print("\tIp Subnet:" + str(ip_subnet)) |
| 62 | # The IP ranges are in distance of 16 compared to each other. |
| 63 | # This is matching the /28 subnet mask used in the dockerfile inside. |
| 64 | |
Bartek Grzybowski | 32fb5ab | 2020-05-05 03:54:01 -0700 | [diff] [blame^] | 65 | ip_gw = args.ipstart + int(1 + (i * 16)) |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 66 | print("\tIP Gateway:" + str(ip_gw)) |
| 67 | |
Bartek Grzybowski | 32fb5ab | 2020-05-05 03:54:01 -0700 | [diff] [blame^] | 68 | IpPnfSim = args.ipstart + int(2 + (i * 16)) |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 69 | print("\tIp Pnf SIM:" + str(IpPnfSim)) |
| 70 | |
Bartek Grzybowski | 32fb5ab | 2020-05-05 03:54:01 -0700 | [diff] [blame^] | 71 | IpFileServer = str(args.ipfileserver) |
RehanRaza | 4b50bdd | 2019-05-03 14:28:39 +0000 | [diff] [blame] | 72 | TypeFileServer = args.typefileserver |
TamasBakai | b59bffb | 2019-03-22 09:52:03 +0000 | [diff] [blame] | 73 | |
TamasBakai | b59bffb | 2019-03-22 09:52:03 +0000 | [diff] [blame] | 74 | PortSftp=start_port +1 |
Bartek Grzybowski | 3d3d3c2 | 2020-03-05 10:28:03 +0100 | [diff] [blame] | 75 | PortFtps=start_port +2 |
TamasBakai | b59bffb | 2019-03-22 09:52:03 +0000 | [diff] [blame] | 76 | start_port +=2 |
Bartek Grzybowski | 32fb5ab | 2020-05-05 03:54:01 -0700 | [diff] [blame^] | 77 | UrlFtps = str(args.ipstart + int(3 + (i * 16))) |
TamasBakai | bab325b | 2019-03-26 09:20:16 +0000 | [diff] [blame] | 78 | print("\tUrl Ftps: " + str(UrlFtps)) |
Bartek Grzybowski | 3d3d3c2 | 2020-03-05 10:28:03 +0100 | [diff] [blame] | 79 | |
Bartek Grzybowski | 32fb5ab | 2020-05-05 03:54:01 -0700 | [diff] [blame^] | 80 | UrlSftp = str(args.ipstart + int(4 + (i * 16))) |
TamasBakai | bab325b | 2019-03-26 09:20:16 +0000 | [diff] [blame] | 81 | print("\tUrl Sftp: " + str(UrlSftp)) |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 82 | |
| 83 | foldername = "pnf-sim-lw-" + str(i) |
| 84 | completed = subprocess.run('mkdir ' + foldername, shell=True) |
| 85 | print('\tCreating folder:', completed.stdout) |
| 86 | completed = subprocess.run( |
| 87 | 'cp -r pnf-sim-lightweight/* ' + |
| 88 | foldername, |
| 89 | shell=True) |
| 90 | print('\tCloning folder:', completed.stdout) |
| 91 | |
TamasBakai | bab325b | 2019-03-26 09:20:16 +0000 | [diff] [blame] | 92 | composercmd = "./simulator.sh compose " + \ |
| 93 | str(ip_gw) + " " + \ |
| 94 | str(ip_subnet) + " " + \ |
| 95 | str(i) + " " + \ |
Bartek Grzybowski | 32fb5ab | 2020-05-05 03:54:01 -0700 | [diff] [blame^] | 96 | args.urlves + " " + \ |
TamasBakai | bab325b | 2019-03-26 09:20:16 +0000 | [diff] [blame] | 97 | str(IpPnfSim) + " " + \ |
Bartek Grzybowski | 32fb5ab | 2020-05-05 03:54:01 -0700 | [diff] [blame^] | 98 | IpFileServer + " " + \ |
| 99 | TypeFileServer + " " + \ |
TamasBakai | bab325b | 2019-03-26 09:20:16 +0000 | [diff] [blame] | 100 | str(PortSftp) + " " + \ |
| 101 | str(PortFtps) + " " + \ |
| 102 | str(UrlFtps) + " " + \ |
TamasBakai | e781c7c | 2019-07-11 11:26:20 +0000 | [diff] [blame] | 103 | str(UrlSftp) + " " + \ |
| 104 | str(ftps_pasv_port_start) + " " + \ |
| 105 | str(ftps_pasv_port_end) |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 106 | |
| 107 | completed = subprocess.run( |
| 108 | 'set -x; cd ' + |
| 109 | foldername + |
| 110 | '; ' + |
| 111 | composercmd, |
| 112 | shell=True) |
| 113 | print('Cloning:', completed.stdout) |
Bartek Grzybowski | 3d3d3c2 | 2020-03-05 10:28:03 +0100 | [diff] [blame] | 114 | |
TamasBakai | e781c7c | 2019-07-11 11:26:20 +0000 | [diff] [blame] | 115 | ftps_pasv_port_start += ftps_pasv_port_num_of_ports + 1 |
| 116 | ftps_pasv_port_end += ftps_pasv_port_num_of_ports +1 |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 117 | |
TamasBakai | bab325b | 2019-03-26 09:20:16 +0000 | [diff] [blame] | 118 | completed = subprocess.run('set -x; cd pnf-sim-lightweight; ./simulator.sh build ', shell=True) |
| 119 | print("Build docker image: ", completed.stdout) |
| 120 | |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 121 | sys.exit() |
| 122 | |
| 123 | if args.clean: |
| 124 | completed = subprocess.run('rm -rf ./pnf-sim-lw-*', shell=True) |
| 125 | print('Deleting:', completed.stdout) |
| 126 | sys.exit() |
| 127 | |
| 128 | if args.start: |
| 129 | |
Bartek Grzybowski | 32fb5ab | 2020-05-05 03:54:01 -0700 | [diff] [blame^] | 130 | for i in range(args.start): |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 131 | foldername = "pnf-sim-lw-" + str(i) |
| 132 | |
| 133 | completed = subprocess.run( |
| 134 | 'set -x ; cd ' + |
| 135 | foldername + |
| 136 | "; bash -x ./simulator.sh start", |
| 137 | shell=True) |
| 138 | print('Starting:', completed.stdout) |
| 139 | |
| 140 | time.sleep(5) |
| 141 | |
| 142 | if args.status: |
| 143 | |
Bartek Grzybowski | 32fb5ab | 2020-05-05 03:54:01 -0700 | [diff] [blame^] | 144 | for i in range(args.status): |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 145 | foldername = "pnf-sim-lw-" + str(i) |
| 146 | |
| 147 | completed = subprocess.run( |
| 148 | 'cd ' + |
| 149 | foldername + |
| 150 | "; ./simulator.sh status", |
| 151 | shell=True) |
| 152 | print('Status:', completed.stdout) |
| 153 | |
| 154 | if args.stop: |
Bartek Grzybowski | 32fb5ab | 2020-05-05 03:54:01 -0700 | [diff] [blame^] | 155 | for i in range(args.stop): |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 156 | foldername = "pnf-sim-lw-" + str(i) |
| 157 | |
| 158 | completed = subprocess.run( |
| 159 | 'cd ' + |
| 160 | foldername + |
RehanRaza | 48f9204 | 2019-03-20 08:12:55 +0000 | [diff] [blame] | 161 | "; ./simulator.sh stop " + str(i), |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 162 | shell=True) |
| 163 | print('Stopping:', completed.stdout) |
| 164 | |
TamasBakai | b59bffb | 2019-03-22 09:52:03 +0000 | [diff] [blame] | 165 | |
| 166 | if args.trigger: |
| 167 | print("Triggering VES sending:") |
| 168 | |
Bartek Grzybowski | 32fb5ab | 2020-05-05 03:54:01 -0700 | [diff] [blame^] | 169 | for i in range(args.trigger): |
TamasBakai | b59bffb | 2019-03-22 09:52:03 +0000 | [diff] [blame] | 170 | foldername = "pnf-sim-lw-" + str(i) |
| 171 | |
| 172 | completed = subprocess.run( |
| 173 | 'cd ' + |
| 174 | foldername + |
| 175 | "; ./simulator.sh trigger-simulator", |
| 176 | shell=True) |
| 177 | print('Status:', completed.stdout) |
Bartek Grzybowski | 3d3d3c2 | 2020-03-05 10:28:03 +0100 | [diff] [blame] | 178 | |
TamasBakai | 34a6f11 | 2019-04-15 08:38:51 +0000 | [diff] [blame] | 179 | if args.triggerstart and args.triggerend: |
| 180 | print("Triggering VES sending by a range of simulators:") |
Bartek Grzybowski | 3d3d3c2 | 2020-03-05 10:28:03 +0100 | [diff] [blame] | 181 | |
Bartek Grzybowski | 32fb5ab | 2020-05-05 03:54:01 -0700 | [diff] [blame^] | 182 | for i in range(args.triggerstart, args.triggerend+1): |
TamasBakai | 34a6f11 | 2019-04-15 08:38:51 +0000 | [diff] [blame] | 183 | foldername = "pnf-sim-lw-" + str(i) |
| 184 | print("Instance being processed:" + str(i)) |
Bartek Grzybowski | 3d3d3c2 | 2020-03-05 10:28:03 +0100 | [diff] [blame] | 185 | |
TamasBakai | 34a6f11 | 2019-04-15 08:38:51 +0000 | [diff] [blame] | 186 | completed = subprocess.run( |
| 187 | 'cd ' + |
| 188 | foldername + |
| 189 | "; ./simulator.sh trigger-simulator", |
| 190 | shell=True) |
| 191 | print('Status:', completed.stdout) |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 192 | else: |
| 193 | print("No instruction was defined") |
Bartek Grzybowski | c29b946 | 2020-04-30 07:11:59 -0700 | [diff] [blame] | 194 | parser.print_usage() |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 195 | sys.exit() |