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