blob: f214691b1b9d53d523ed4c0303955bffbb47472a [file] [log] [blame]
TamasBakaid38feb62019-02-28 09:06:19 +00001#!/usr/bin/env python3
2import argparse
3import sys
TamasBakaid38feb62019-02-28 09:06:19 +00004import subprocess
5import ipaddress
6import time
Bartek Grzybowski8200d2a2020-05-05 03:50:38 -07007from requests import get
8from requests.exceptions import MissingSchema, InvalidSchema, InvalidURL, ConnectionError, ConnectTimeout
9
10def validate_url(url):
11 '''Helper function to perform --urlves input param validation'''
12 try:
13 get(url, timeout=0.001)
14 except (MissingSchema, InvalidSchema, InvalidURL):
15 raise argparse.ArgumentTypeError(f'{url} is not a valid URL')
16 except (ConnectionError, ConnectTimeout):
17 pass
18 return url
19
20def validate_ip(ip):
21 '''Helper function to validate input param is a vaild IP address'''
22 try:
23 ip_valid = ipaddress.ip_address(ip)
24 except ValueError:
25 raise argparse.ArgumentTypeError(f'{ip} is not a valid IP address')
26 else:
27 return ip_valid
TamasBakaid38feb62019-02-28 09:06:19 +000028
29parser = argparse.ArgumentParser()
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -070030parser.add_argument('--bootstrap', help='Bootstrap the system', type=int)
31parser.add_argument('--trigger', help='Trigger one single VES event from each simulator', type=int)
32parser.add_argument('--triggerstart', help='Trigger only a subset of the simulators (note --triggerend)', type=int)
33parser.add_argument('--triggerend', help='Last instance to trigger', type=int)
34parser.add_argument('--urlves', help='URL of the VES collector', type=validate_url)
35parser.add_argument('--ipfileserver', help='Visible IP of the file server (SFTP/FTPS) to be included in the VES event',
36 type=validate_ip)
37parser.add_argument('--typefileserver', help='Type of the file server (SFTP/FTPS) to be included in the VES event',
38 type=str, choices=['sftp', 'ftps'])
39parser.add_argument('--ipstart', help='IP address range beginning', type=validate_ip)
Bartek Grzybowskic29b9462020-04-30 07:11:59 -070040parser.add_argument('--clean', action='store_true', help='Clean work-dirs')
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -070041parser.add_argument('--start', help='Start instances', type=int)
42parser.add_argument('--status', help='Status', type=int)
43parser.add_argument('--stop', help='Stop instances', type=int)
TamasBakaid38feb62019-02-28 09:06:19 +000044
45args = parser.parse_args()
46
TamasBakaibab325b2019-03-26 09:20:16 +000047if args.bootstrap and args.ipstart and args.urlves:
TamasBakaid38feb62019-02-28 09:06:19 +000048 print("Bootstrap:")
49
TamasBakaib59bffb2019-03-22 09:52:03 +000050 start_port=2000
TamasBakaie781c7c2019-07-11 11:26:20 +000051 ftps_pasv_port_start=8000
52 ftps_pasv_port_num_of_ports=10
Bartek Grzybowski3d3d3c22020-03-05 10:28:03 +010053
TamasBakaie781c7c2019-07-11 11:26:20 +000054 ftps_pasv_port_end=ftps_pasv_port_start + ftps_pasv_port_num_of_ports
Bartek Grzybowski3d3d3c22020-03-05 10:28:03 +010055
TamasBakaib59bffb2019-03-22 09:52:03 +000056
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -070057 for i in range(args.bootstrap):
TamasBakaid38feb62019-02-28 09:06:19 +000058 print("PNF simulator instance: " + str(i) + ".")
59
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -070060 ip_subnet = args.ipstart + int(0 + (i * 16))
TamasBakaid38feb62019-02-28 09:06:19 +000061 print("\tIp Subnet:" + str(ip_subnet))
62 # The IP ranges are in distance of 16 compared to each other.
63 # This is matching the /28 subnet mask used in the dockerfile inside.
64
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -070065 ip_gw = args.ipstart + int(1 + (i * 16))
TamasBakaid38feb62019-02-28 09:06:19 +000066 print("\tIP Gateway:" + str(ip_gw))
67
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -070068 IpPnfSim = args.ipstart + int(2 + (i * 16))
TamasBakaid38feb62019-02-28 09:06:19 +000069 print("\tIp Pnf SIM:" + str(IpPnfSim))
70
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -070071 IpFileServer = str(args.ipfileserver)
RehanRaza4b50bdd2019-05-03 14:28:39 +000072 TypeFileServer = args.typefileserver
TamasBakaib59bffb2019-03-22 09:52:03 +000073
TamasBakaib59bffb2019-03-22 09:52:03 +000074 PortSftp=start_port +1
Bartek Grzybowski3d3d3c22020-03-05 10:28:03 +010075 PortFtps=start_port +2
TamasBakaib59bffb2019-03-22 09:52:03 +000076 start_port +=2
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -070077 UrlFtps = str(args.ipstart + int(3 + (i * 16)))
TamasBakaibab325b2019-03-26 09:20:16 +000078 print("\tUrl Ftps: " + str(UrlFtps))
Bartek Grzybowski3d3d3c22020-03-05 10:28:03 +010079
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -070080 UrlSftp = str(args.ipstart + int(4 + (i * 16)))
TamasBakaibab325b2019-03-26 09:20:16 +000081 print("\tUrl Sftp: " + str(UrlSftp))
TamasBakaid38feb62019-02-28 09:06:19 +000082
83 foldername = "pnf-sim-lw-" + str(i)
84 completed = subprocess.run('mkdir ' + foldername, shell=True)
85 print('\tCreating folder:', completed.stdout)
86 completed = subprocess.run(
87 'cp -r pnf-sim-lightweight/* ' +
88 foldername,
89 shell=True)
90 print('\tCloning folder:', completed.stdout)
91
TamasBakaibab325b2019-03-26 09:20:16 +000092 composercmd = "./simulator.sh compose " + \
93 str(ip_gw) + " " + \
94 str(ip_subnet) + " " + \
95 str(i) + " " + \
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -070096 args.urlves + " " + \
TamasBakaibab325b2019-03-26 09:20:16 +000097 str(IpPnfSim) + " " + \
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -070098 IpFileServer + " " + \
99 TypeFileServer + " " + \
TamasBakaibab325b2019-03-26 09:20:16 +0000100 str(PortSftp) + " " + \
101 str(PortFtps) + " " + \
102 str(UrlFtps) + " " + \
TamasBakaie781c7c2019-07-11 11:26:20 +0000103 str(UrlSftp) + " " + \
104 str(ftps_pasv_port_start) + " " + \
105 str(ftps_pasv_port_end)
TamasBakaid38feb62019-02-28 09:06:19 +0000106
107 completed = subprocess.run(
108 'set -x; cd ' +
109 foldername +
110 '; ' +
111 composercmd,
112 shell=True)
113 print('Cloning:', completed.stdout)
Bartek Grzybowski3d3d3c22020-03-05 10:28:03 +0100114
TamasBakaie781c7c2019-07-11 11:26:20 +0000115 ftps_pasv_port_start += ftps_pasv_port_num_of_ports + 1
116 ftps_pasv_port_end += ftps_pasv_port_num_of_ports +1
TamasBakaid38feb62019-02-28 09:06:19 +0000117
TamasBakaibab325b2019-03-26 09:20:16 +0000118 completed = subprocess.run('set -x; cd pnf-sim-lightweight; ./simulator.sh build ', shell=True)
119 print("Build docker image: ", completed.stdout)
120
TamasBakaid38feb62019-02-28 09:06:19 +0000121 sys.exit()
122
123if args.clean:
124 completed = subprocess.run('rm -rf ./pnf-sim-lw-*', shell=True)
125 print('Deleting:', completed.stdout)
126 sys.exit()
127
128if args.start:
129
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -0700130 for i in range(args.start):
TamasBakaid38feb62019-02-28 09:06:19 +0000131 foldername = "pnf-sim-lw-" + str(i)
132
133 completed = subprocess.run(
134 'set -x ; cd ' +
135 foldername +
136 "; bash -x ./simulator.sh start",
137 shell=True)
138 print('Starting:', completed.stdout)
139
140 time.sleep(5)
141
142if args.status:
143
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -0700144 for i in range(args.status):
TamasBakaid38feb62019-02-28 09:06:19 +0000145 foldername = "pnf-sim-lw-" + str(i)
146
147 completed = subprocess.run(
148 'cd ' +
149 foldername +
150 "; ./simulator.sh status",
151 shell=True)
152 print('Status:', completed.stdout)
153
154if args.stop:
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -0700155 for i in range(args.stop):
TamasBakaid38feb62019-02-28 09:06:19 +0000156 foldername = "pnf-sim-lw-" + str(i)
157
158 completed = subprocess.run(
159 'cd ' +
160 foldername +
RehanRaza48f92042019-03-20 08:12:55 +0000161 "; ./simulator.sh stop " + str(i),
TamasBakaid38feb62019-02-28 09:06:19 +0000162 shell=True)
163 print('Stopping:', completed.stdout)
164
TamasBakaib59bffb2019-03-22 09:52:03 +0000165
166if args.trigger:
167 print("Triggering VES sending:")
168
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -0700169 for i in range(args.trigger):
TamasBakaib59bffb2019-03-22 09:52:03 +0000170 foldername = "pnf-sim-lw-" + str(i)
171
172 completed = subprocess.run(
173 'cd ' +
174 foldername +
175 "; ./simulator.sh trigger-simulator",
176 shell=True)
177 print('Status:', completed.stdout)
Bartek Grzybowski3d3d3c22020-03-05 10:28:03 +0100178
TamasBakai34a6f112019-04-15 08:38:51 +0000179if args.triggerstart and args.triggerend:
180 print("Triggering VES sending by a range of simulators:")
Bartek Grzybowski3d3d3c22020-03-05 10:28:03 +0100181
Bartek Grzybowski32fb5ab2020-05-05 03:54:01 -0700182 for i in range(args.triggerstart, args.triggerend+1):
TamasBakai34a6f112019-04-15 08:38:51 +0000183 foldername = "pnf-sim-lw-" + str(i)
184 print("Instance being processed:" + str(i))
Bartek Grzybowski3d3d3c22020-03-05 10:28:03 +0100185
TamasBakai34a6f112019-04-15 08:38:51 +0000186 completed = subprocess.run(
187 'cd ' +
188 foldername +
189 "; ./simulator.sh trigger-simulator",
190 shell=True)
191 print('Status:', completed.stdout)
TamasBakaid38feb62019-02-28 09:06:19 +0000192else:
193 print("No instruction was defined")
Bartek Grzybowskic29b9462020-04-30 07:11:59 -0700194 parser.print_usage()
TamasBakaid38feb62019-02-28 09:06:19 +0000195 sys.exit()