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