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