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