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