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