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( |
| 17 | '--ipves', |
| 18 | help='IP of the VES collector', |
| 19 | ) |
| 20 | |
| 21 | parser.add_argument( |
| 22 | '--ipstart', |
| 23 | help='IP address range beginning', |
| 24 | ) |
| 25 | |
| 26 | parser.add_argument( |
| 27 | '--clean', |
| 28 | action='store_true', |
| 29 | help='Cleaning work-dirs', |
| 30 | ) |
| 31 | |
| 32 | parser.add_argument( |
| 33 | '--start', |
| 34 | help='Starting instances', |
| 35 | ) |
| 36 | |
| 37 | parser.add_argument( |
| 38 | '--status', |
| 39 | help='Status', |
| 40 | ) |
| 41 | |
| 42 | parser.add_argument( |
| 43 | '--stop', |
| 44 | help='Stopping instances', |
| 45 | ) |
| 46 | |
| 47 | args = parser.parse_args() |
| 48 | |
| 49 | if args.bootstrap and args.ipstart and args.ipves: |
| 50 | print("Bootstrap:") |
| 51 | |
| 52 | for i in range(int(args.bootstrap)): |
| 53 | print("PNF simulator instance: " + str(i) + ".") |
| 54 | |
| 55 | ip_subnet = ipaddress.ip_address(args.ipstart) + int(0 + (i * 16)) |
| 56 | print("\tIp Subnet:" + str(ip_subnet)) |
| 57 | # The IP ranges are in distance of 16 compared to each other. |
| 58 | # This is matching the /28 subnet mask used in the dockerfile inside. |
| 59 | |
| 60 | ip_gw = ipaddress.ip_address(args.ipstart) + int(1 + (i * 16)) |
| 61 | print("\tIP Gateway:" + str(ip_gw)) |
| 62 | |
| 63 | IpPnfSim = ipaddress.ip_address(args.ipstart) + int(2 + (i * 16)) |
| 64 | print("\tIp Pnf SIM:" + str(IpPnfSim)) |
| 65 | |
| 66 | IpFtps = ipaddress.ip_address(args.ipstart) + int(3 + (i * 16)) |
| 67 | print("\tIp Ftps: " + str(IpFtps)) |
| 68 | |
| 69 | IpSftp = ipaddress.ip_address(args.ipstart) + int(4 + (i * 16)) |
| 70 | print("\tIp Sftp:" + str(IpSftp)) |
| 71 | |
| 72 | foldername = "pnf-sim-lw-" + str(i) |
| 73 | completed = subprocess.run('mkdir ' + foldername, shell=True) |
| 74 | print('\tCreating folder:', completed.stdout) |
| 75 | completed = subprocess.run( |
| 76 | 'cp -r pnf-sim-lightweight/* ' + |
| 77 | foldername, |
| 78 | shell=True) |
| 79 | print('\tCloning folder:', completed.stdout) |
| 80 | |
| 81 | composercmd = "./simulator.sh compose " +\ |
| 82 | str(ip_gw) + " " +\ |
| 83 | str(ip_subnet) + " " +\ |
| 84 | str(i) + " " +\ |
| 85 | str(args.ipves) + " " +\ |
| 86 | str(IpPnfSim) + " " +\ |
| 87 | str(IpFtps) + " " +\ |
| 88 | str(IpSftp) |
| 89 | |
| 90 | completed = subprocess.run( |
| 91 | 'set -x; cd ' + |
| 92 | foldername + |
| 93 | '; ' + |
| 94 | composercmd, |
| 95 | shell=True) |
| 96 | print('Cloning:', completed.stdout) |
| 97 | |
| 98 | sys.exit() |
| 99 | |
| 100 | if args.clean: |
| 101 | completed = subprocess.run('rm -rf ./pnf-sim-lw-*', shell=True) |
| 102 | print('Deleting:', completed.stdout) |
| 103 | sys.exit() |
| 104 | |
| 105 | if args.start: |
| 106 | |
| 107 | for i in range(int(args.start)): |
| 108 | foldername = "pnf-sim-lw-" + str(i) |
| 109 | |
| 110 | completed = subprocess.run( |
| 111 | 'set -x ; cd ' + |
| 112 | foldername + |
| 113 | "; bash -x ./simulator.sh start", |
| 114 | shell=True) |
| 115 | print('Starting:', completed.stdout) |
| 116 | |
| 117 | time.sleep(5) |
| 118 | |
| 119 | if args.status: |
| 120 | |
| 121 | for i in range(int(args.status)): |
| 122 | foldername = "pnf-sim-lw-" + str(i) |
| 123 | |
| 124 | completed = subprocess.run( |
| 125 | 'cd ' + |
| 126 | foldername + |
| 127 | "; ./simulator.sh status", |
| 128 | shell=True) |
| 129 | print('Status:', completed.stdout) |
| 130 | |
| 131 | if args.stop: |
| 132 | for i in range(int(args.stop)): |
| 133 | foldername = "pnf-sim-lw-" + str(i) |
| 134 | |
| 135 | completed = subprocess.run( |
| 136 | 'cd ' + |
| 137 | foldername + |
| 138 | "; ./simulator.sh stop", |
| 139 | shell=True) |
| 140 | print('Stopping:', completed.stdout) |
| 141 | |
| 142 | else: |
| 143 | print("No instruction was defined") |
| 144 | sys.exit() |