-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart-PHANTOM.py
executable file
·359 lines (259 loc) · 11.8 KB
/
start-PHANTOM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env python3
from os import listdir, walk
from os.path import isfile, join, exists, dirname
import argparse, sys, subprocess, socket
import settings, repository
token_file = "token.txt"
parser = argparse.ArgumentParser(description='Tool to support the execution of an application on PHANTOM Framework')
parser.add_argument('-u', '--noUpload', dest='noUpload', action='store_true' , help='Do not (re)upload the application to the repository. (Application should be already in repository)')
parser.add_argument('-d', '--onlyDesc', dest='descriptionOnly', action='store_true' , help='Only re-uploads the description files to the repository)')
parser.add_argument('-i', '--skipInputs', dest='noInputs', action='store_true' , help='Do not (re)upload the application inputs to the repository. (Inputs should be already in repository)')
parser.add_argument('-c', '--clean', dest='clean', action='store_true' , help='Clean all the data in repositories and temporary cache on PHANTOM tools. Automatically update PHANTOM_FILES (-p)')
parser.add_argument('-m', '--ipmarket', dest='ipMarket', action='store_true' , help='Uploads the IP Core Market place to the repository')
parser.add_argument('-p', '--phantomfiles', dest='phFiles', action='store_true' , help='Uploads the PHANTOM files (PHANTOM API and Monitoring API)')
parser.add_argument('--Tpe', dest='MBTpe', action='store_true' , help='Runs only the MBT Performance estimation tool')
parser.add_argument('--Tte', dest='MBTte', action='store_true' , help='Runs only the MBT Test Execution tool')
def getIP():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(('10.255.255.255', 1))
IP = s.getsockname()[0]
except:
IP = '127.0.0.1'
finally:
s.close()
return IP
def externalizeIP(ip):
if ip == "localhost":
return getIP()
else:
return ip
config_decoder = {
"user": ["#USER#", settings.user],
"pwd" : ["#PWD#", settings.password],
"repo_ip" : ["#REPO_IP#", externalizeIP(settings.repository_ip)],
"repo_port" : ["#REPO_PORT#", settings.repository_port],
"appman_ip" : ["#APPMAN_IP#",settings.app_manager_ip],
"appman_port" : ["#APPMAN_PORT#", settings.app_manager_port],
"exeman_ip" : ["#EXE_IP#", settings.exe_manager_ip],
"exeman_port" : ["#EXE_PORT#", settings.exe_manager_port],
"mon_ip" : ["#MON_IP#", settings.monitoring_ip],
"mon_port" : ["#MON_PORT#", settings.monitoring_port],
"res_ip" : ["#RES_IP#", settings.resource_ip],
"res_port" : ["#RES_PORT#", settings.resource_port],
"app_name" : ["#APPNAME#", settings.app_name],
"token" : ["#TOKEN#", "No_Token"],
"PT_mode" : ["#PT_MODE#",settings.PT_mode],
"CompPath" : ["#COMPNETPATH#",settings.CompNetPath],
"CompName" : ["#COMPNETNAME#",settings.CompNetName],
"PlatPath" : ["#PLATDESPATH#",settings.PlatDesPath],
"Platname" : ["#PLATDESNAME#",settings.PlatDesName],
"DM_mode" : ["#DM_MODE#",settings.DM_mode],
# "dep_plan" : ["#DEP_PLAN#", settings.deployment_plan],
# "mbt_test_comp" : ["#TEST_COMP#",settings.tested_comp_name],
# "mbt_output_test" : ["#OUTOUT_TEST#", settings.output_est_name]
}
def main():
args = parser.parse_args()
if args.clean:
process=subprocess.Popen(['bash','/home/demo/Desktop/phantom-tools/User-tools/management-scripts/clean-installation.sh'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,cwd="./management-scripts")
stdoutdata,stderrdata=process.communicate(input=b'y\n')
process.wait()
print(stdoutdata)
print(stderrdata)
#authentication verification
auth_token = getToken()
config_decoder["token"]=["#TOKEN#", auth_token]
if args.MBTpe: #runs only MBT Performance estimation
generateConfigFile('MBTpe',settings.MBT_pe_path, "PerformanceEstimator.py", ["mbt_test_comp","mbt_output_test"])
newTerminal(settings.MBT_pe_path,'python3 PerformanceEstimator.py ' + settings.CompNetName, 'MBT Perf Est')
elif args.MBTte: #runs only mBT Test Execution
generateConfigFile('MBTte',settings.MBT_te_path, "MyExample.cfg", ["user", "pwd", "repo_ip", "repo_port", "appman_port", "exeman_port", "res_port", "mon_port", "app_name", "dep_plan", "token"])
newTerminal(settings.MBT_te_path,'ttcn3_start ./mbttest MyExample.cfg', 'MBT Test Exec')
else: #runs other options
if args.ipMarket:
#Downloads Ipcores
print("Downloading Marketplace...")
code = subprocess.call(['git','clone',settings.ipMarket_path,settings.ip_folder])
if code == 0:
print("Uploading IP Cores...")
uploadMarket(settings.ip_folder, auth_token,"")
print("Cleaning local marketplace")
code = subprocess.call(['rm','-rf',settings.ip_folder])
else:
print("Unable to download the IPCore Marketplace")
if not args.noUpload:
#upload source code
if settings.root_path != '' and (not args.descriptionOnly):
uploadRootFiles(settings.root_path, auth_token)
print("Uploading")
if settings.src_path != '' and (not args.descriptionOnly):
print("Uploading source code...")
uploadAllFiles(settings.src_path, auth_token, "src")
#upload descriptions
if settings.desc_path != '':
print("Uploading description files...")
uploadAllFiles(settings.desc_path, auth_token, "description")
#upload PHANTOM_FILES
if (args.phFiles or args.clean) and settings.phantom_path != '':
print("Uploading PHANTOM files")
uploadPHANTOM_FILES(settings.phantom_path, auth_token,"")
if not args.noInputs:
#upload inputs
if settings.inputs_path != '':
print("Uploading description files...")
uploadAllFiles(settings.inputs_path, auth_token, "inputs")
#register app in application manager
repository.websocketUpdateStatus(settings.app_name, "development", "finished", auth_token)
#configure and start MOM
generateConfigFile('MOM',settings.MOM_path, "configuration.xml", ["user", "pwd", "repo_ip", "repo_port", "appman_port", "exeman_port", "app_name", "CompName", "Platname"])
newTerminal(settings.MOM_path,'/usr/bin/java -jar GA_MOM.jar --online', 'MOM')
#configure and start PT
generateConfigFile('PT',settings.PT_path, "config.properties", ["user", "token", "repo_ip", "repo_port", "appman_ip","appman_port", "mon_ip","mon_port", "app_name", "PT_mode", "CompPath", "CompName", "PlatPath", "Platname", "exeman_ip", "exeman_port"])
newTerminal(settings.PT_path,'/usr/bin/java -jar ParallelisationToolset_lite.jar', 'PT')
#configure and start IPCore-GEN
# generateConfigFile("IPGEN",settings.IP_path, "settings.py", ["user", "pwd", "repo_ip", "repo_port", "appman_ip","appman_port", "CompName"])
# newTerminal(settings.IP_path,'/usr/bin/python3 ipcore-generator.py subscribe ' + settings.app_name, 'IPCore-Gen')
#configure and start DM
generateConfigFile('DM',settings.DM_path, "config.properties", ["user", "token", "repo_ip", "repo_port", "appman_ip","appman_port", "mon_ip","mon_port", "app_name", "DM_mode","exeman_ip", "exeman_port", "CompPath", "CompName", "PlatPath", "Platname", "exeman_ip", "exeman_port","res_ip","res_port"])
newTerminal(settings.DM_path,'/usr/bin/java -jar DeploymentManager.jar','DM')
def getToken():
try: #attempts to read token
f=open(token_file,"r")
print("Token file found")
token=f.read()
if repository.isTokenValid(token) is True:
return token
else:
print("Token is invalid!!!")
token = getNewToken()
except: #token not found
print("Token file not found. Attempting login")
token = getNewToken()
if token == -1:
sys.exit(1)
else:
print("Authentication successfull. Saving token...")
f = open(token_file,"w")
f.write(str(token))
f.close()
print("Token saved!")
return token
def getNewToken():
token=repository.authenticate(settings.user,settings.password)
if token == 1:
print("Attempting to create a new user") # should we really do this?
if repository.register(settings.user,settings.password) == 0:
print("User registered with success. Starting authentication")
token=repository.authenticate(settings.user,settings.password)
if token == 1:
return -1
else:
print("Error during registering!!!!")
return -1
return token
def listFiles(path_dir):
"""
Identifies all the files under a specific directory. Navegates across sub-directories.
Returns a dictionary with key: dir path; value: list of files in dir
"""
allfiles = []
for root, dirs, files in walk(path_dir):
for name in files:
allfiles.append(join(root,name))
return allfiles
def uploadRootFiles(pathdir, token):
rootFiles = {"Makefile","cla.in"}
fnames = listdir(pathdir)
for f in fnames:
filepath=join(pathdir, f)
if isfile(filepath) and f in rootFiles:
repository.uploadFile(filepath, token, relativePath(pathdir, "", filepath), settings.app_name,"development")
def uploadAllFiles(path_dir, token, repo_folder):
files = listFiles(path_dir)
for filePath in files:
repository.uploadFile(filePath, token, relativePath(path_dir, repo_folder, filePath), settings.app_name,"development")
def uploadPHANTOM_FILES(path_dir, token, repo_folder):
files = listFiles(path_dir)
for filePath in files:
source = "DM" if ("/DM/" in filePath) else "Monitoring"
# repository.uploadFile(filePath, token, relativePath(path_dir, repo_folder, filePath), "PHANTOM_FILES","DM" if ("/DM/" in filePath) else "Monitoring")
repository.uploadFile(filePath, token, relativePath(path_dir + '/' + source + '/', repo_folder, filePath), "PHANTOM_FILES",source)
def uploadMarket(path_dir, token, repo_folder):
files = listFiles(path_dir)
for filePath in files:
repository.uploadFile(filePath, token, relativePath(path_dir, repo_folder, filePath), "IPCore-Marketplace","DM")
def relativePath(root, repo_folder,fullPath):
path = fullPath.replace(root,'')
remote_path = str(repo_folder) + dirname(path)
if remote_path != '' and remote_path[-1] == '/':
return remote_path[:-1]
else:
return remote_path
def generateConfigFile(toolID,dir_path,configFileName,listToConfigure):
dir_path = enforce_trailing_slash(dir_path)
print("templates/" + toolID + "-configuration-template.txt")
try:
f_template = open("templates/" + toolID + "-configuration-template.txt", "r")
template = f_template.read()
except:
print("Template file not found")
sys.exit(1)
for config in listToConfigure:
[pattern, value] = config_decoder[config]
template = template.replace(pattern,str(value))
f_template = open(dir_path + configFileName, "w")
f_template.write(template)
f_template.close()
print("")
def generateRemoteConfigFile(dir_path,configFileName,listToConfigure):
dir_path = enforce_trailing_slash(dir_path)
try:
f_template = open(dir_path + "/configuration-template.txt", "r")
template = f_template.read()
except:
print("Template file not found")
sys.exit(1)
for config in listToConfigure:
[pattern, value] = config_decoder[config]
if value == "localhost":
value = getIP()
template = template.replace(pattern,str(value))
f_template = open(dir_path + configFileName, "w")
f_template.write(template)
f_template.close()
print("")
def newTerminal(workdir,command,title):
# return subprocess.Popen(['gnome-terminal' ,'--profile=NoClosing', '--working-directory=' + workdir, '-x'] + command)
return subprocess.Popen(['xterm' ,'-s','-si','-sk','-sb','-hold','-fa', 'Monospace', '-fs','11','-T',title, '-e' ,command],cwd=workdir)
# Utils
def enforce_trailing_slash(path):
'''
Returns path, with the '/' character appended if it did not already end with it
'''
if path[-1] != '/':
return path + '/'
else:
return path
def externalizeIP(ip):
if ip == "localhost":
return getIP()
else:
return ip
def getIP():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(('10.255.255.255', 1))
IP = s.getsockname()[0]
except:
IP = '127.0.0.1'
finally:
s.close()
return IP
if __name__ == "__main__":
main()