Trisurf Monte Carlo simulator
Samo Penic
2016-07-08 c7f52eada2cb8c1695afac6c7beed8fdda669288
commit | author | age
7169f1 1 import argparse
SP 2 import paramiko
3 from . import Remote
61d2e7 4 from . import trisurf
7169f1 5 import socket
d6583e 6 import os,sys
9f5ff5 7 import tabulate
a3f6b7 8 import subprocess,re
dfa2df 9 import psutil
7169f1 10 #import http.server
SP 11 #import socketserver
61d2e7 12 if sys.version_info>=(3,0):
SP 13     from urllib.parse import urlparse
14     from . import WebTrisurf
15 else:
16     from urlparse import urlparse
c89a71 17     from vtk import *
7169f1 18 #import io
5f4aea 19
SP 20
7169f1 21
SP 22 #Color definitions for terminal
23 class bcolors:
24     HEADER = '\033[95m'
25     OKBLUE = '\033[94m'
26     OKGREEN = '\033[92m'
27     WARNING = '\033[93m'
28     FAIL = '\033[91m'
29     ENDC = '\033[0m'
30     BOLD = '\033[1m'
31     UNDERLINE = '\033[4m'
32
33 #parses Command Line Arguments and returns the list of parsed values
34 def ParseCLIArguments(arguments):
35     parser = argparse.ArgumentParser(description='Manages (start, stop, status) multiple simulation processes of trisurf according to the configuration file.')
36     parser.add_argument('proc_no', metavar='PROC_NO', nargs='*',
37                 help='process number at host. If hostname is not specified, localhost is assumed. If no processes are specified all processes on all hosts are assumed.')
38     action_group=parser.add_mutually_exclusive_group(required=True)
39     action_group.add_argument('-c','--comment',nargs=1, help='append comment to current comment')
40     action_group.add_argument('--delete-comment', help='delete comment',action='store_true')
41     action_group.add_argument('-k','--kill','--stop','--suspend', help='stop/kill the process', action='store_true')
42     action_group.add_argument('-r','--run','--start','--continue', help='start/continue process', action='store_true')
43     action_group.add_argument('-s','--status',help='print status of the processes',action='store_true')
44     action_group.add_argument('-v','--version', help='print version information and exit', action='store_true')
45     action_group.add_argument('--web-server', type=int,metavar="PORT", nargs=1, help='EXPERIMENTAL: starts web server and never exist.')
c89a71 46     action_group.add_argument('-p','--preview',help='preview last VTU shape',action='store_true')
7169f1 47     parser.add_argument('--force', help='if dangerous operation (killing all the processes) is requested, this flag is required to execute the operation. Otherwise, the request will be ignored.', action="store_true")
SP 48     parser.add_argument('-H', '--host', nargs=1, help='specifies which host is itended for the operation. Defauts to localhost for all operations except --status and --version, where all configured hosts are assumed.')
49     parser.add_argument('--html', help='Generate HTML output', action="store_true")
50     parser.add_argument('-n', nargs='+', metavar='PROC_NO', type=int, help='OBSOLETE. Specifies process numbers.')
51     parser.add_argument('-R','--raw',help='print status and the rest of the information in raw format', action="store_true")
52     parser.add_argument('-x','--local-only',help='do not attempt to contact remote hosts. Run all operations only on local machine',action='store_true')
53     args = parser.parse_args(arguments)
54     return args
55
56
57 #gets version of trisurf currently running
a3f6b7 58 def getTrisurfVersion():
SP 59     p = subprocess.Popen('trisurf --version', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
60     lines=p.stdout.readlines()
61     version=re.findall(r'[0-9a-f]{7}(?:-dirty)?', lines[0].decode('ascii'))
62     p.wait()
63     if(len(version)):
64         return version[0]
65     else:
66         return "unknown version"
67
9f5ff5 68
7169f1 69
SP 70 def copyConfigAndConnect(hosts):
71     print("Connecting to remote hosts and copying config files, tapes and snapshots")
72     for host in hosts:
73         if(host['name'] !=socket.gethostname()): #if I am not the computer named in host name
74             try:
75                 username=host['username']
76             except:
77                 username=os.getusername() #default username is current user user's name
78             try:
79                 port=host['port']
80             except:
81                 port=22 #default ssh port
82             rm=Remote.Connection(hostname=host['address'],username=username, port=port)
83             rm.connect()
84             rm.send_file(__file__,'remote_control.py')
85             for run in host['runs']:
86                 try:
87                     rm.send_file(run.tapeFile,run.tapeFile)
88                 except:
89                     pass
90                 try:
91                     rm.send_file(run.snapshotFile,run.snapshotFile)
92                 except:
93                     pass
94             host['_conn']= rm
95     # we are connected to all hosts...
96     return hosts
97
98
99
100 def getTargetRunIdxList(args):
101     target_runs=list(map(int,args['proc_no']))
102     if len(target_runs)==0:
103         #check if obsolete -n flags have numbers
104         target_runs=args['n']
105         if target_runs==None:
106             return None
107     target_runs=list(set(target_runs))
108     return target_runs
109
110
111
112 def status_processes(args,host):
113     target_runs=getTargetRunIdxList(args)
114     if target_runs==None:
115         target_runs=list(range(1,len(host['runs'])+1))
116     report=[]
117     for i in target_runs:
118         line=host['runs'][i-1].getStatistics()
119         line.insert(0,i)
120         report.append(line)
121     if(args['raw']):
122         print(report)
123     else:
124         if(args['html']):
125             tablefmt='html'
9f5ff5 126         else:
7169f1 127             tablefmt='fancy_grid'
SP 128         print(tabulate.tabulate(report,headers=["Run no.", "Run start time", "ETA", "Status", "PID", "Path", "Comment"], tablefmt=tablefmt))
129     return
130
131 def run_processes(args,host):
132     target_runs=getTargetRunIdxList(args)
133     if target_runs==None:
134         target_runs=list(range(1,len(host['runs'])+1))
135     for i in target_runs:
136         host['runs'][i-1].start()
137     return
138
139 def kill_processes(args,host):
140     target_runs=getTargetRunIdxList(args)
141     if target_runs==None:
142         if args['force']==True:
143             target_runs=list(range(1,len(host['runs'])+1))
144         else:
145             print("Not stopping all processes on the host. Run with --force flag if you are really sure to stop all simulations")
146             return
147     for i in target_runs:
148         host['runs'][i-1].stop()
149     return
150
151 def comment_processes(args,host):
152     target_runs=getTargetRunIdxList(args)
153     if target_runs==None:
154         target_runs=list(range(1,len(host['runs'])+1))
155     for i in target_runs:
156         host['runs'][i-1].writeComment(args['comment'][0],'a')
157     print("Comment added")
158     return
159
160 def delete_comments(args,host):
161     target_runs=getTargetRunIdxList(args)
162     if target_runs==None:
163         if args['force']==True:
164             target_runs=list(range(1,len(host['runs'])+1))
165         else:
166             print("Not deleting comments on all posts on the host. Run with --force flag if you are really sure to delete all comments")
167             return
168     for i in target_runs:
169         host['runs'][i-1].writeComment("")
170     print("Comment deleted")
171     return
172
173
174 def start_web_server(args,host):
175     print('Server listening on port {}'.format(args['web_server'][0]))
61d2e7 176     if sys.version_info>=(3,0):
SP 177         WebTrisurf.WebServer(port=args['web_server'][0])
178     else:
179         print("Cannot start WebServer in python 2.7")
7169f1 180     exit(0)
SP 181
182 def perform_action(args,host):
183     #find which flags have been used and act upon them. -r -s -k -v -c --delete-comment are mutually exclusive, so only one of them is active
184     if args['run']:
185         run_processes(args,host)
186     elif args['kill']:
187         kill_processes(args,host)
188     elif args['status']:
189         status_processes(args,host)
190     elif args['comment']!= None:
191         comment_processes(args,host)
192     elif args['delete_comment']:
193         delete_comments(args,host)
194     elif args['web_server']!=None:
195         start_web_server(args,host)
c89a71 196     elif args['preview']:
SP 197         preview_vtu(args,host)
7169f1 198     else: #version requested
SP 199         print(getTrisurfVersion())
200     return
201
202
c89a71 203
SP 204 def preview_vtu(args,host):
205     #only for localhost at the moment
d6583e 206     Dir=trisurf.Directory(maindir=host['runs'][0].maindir,simdir=host['runs'][0].subdir)
SP 207     filename=os.path.join("./",Dir.fullpath(),host['runs'][0].getLastVTU())
c89a71 208     print(filename)
SP 209     if sys.version_info>=(3,0):
210         print("Preview works only with python 2.7")
211         exit(1)
212     if host['name'] == socket.gethostname():
213         target_runs=getTargetRunIdxList(args)
214         #if target_runs==None:
215         #    target_runs=list(range(1,len(host['runs'])+1))
216         #for i in target_runs:
217         #    host['runs'][i-1].start()
218         reader=vtkXMLUnstructuredGridReader()
219         reader.SetFileName(filename)
220         reader.Update() # Needed because of GetScalarRange
221         output = reader.GetOutput()
222         scalar_range = output.GetScalarRange()
223
224         # Create the mapper that corresponds the objects of the vtk file
225         # into graphics elements
226         mapper = vtkDataSetMapper()
227         mapper.SetInput(output)
228         mapper.SetScalarRange(scalar_range)
229
230         # Create the Actor
231         actor = vtkActor()
232         actor.SetMapper(mapper)
233
234         # Create the Renderer
235         renderer = vtkRenderer()
236         renderer.AddActor(actor)
237         renderer.SetBackground(0, 0, 0) # Set background to white
238
239         # Create the RendererWindow
240         renderer_window = vtkRenderWindow()
241         renderer_window.AddRenderer(renderer)
c7f52e 242         
SP 243 # Set up a check for aborting rendering.
c89a71 244         # Create the RendererWindowInteractor and display the vtk_file
SP 245         interactor = vtkRenderWindowInteractor()
246         interactor.SetRenderWindow(renderer_window)
247         interactor.Initialize()
c7f52e 248 #         interactor.AddObserver("TimerEvent", RenderUpdate)
SP 249 #        timerIDR = interactor.CreateRepeatingTimer(1000)
c89a71 250         interactor.Start()
SP 251
c7f52e 252 def RenderUpdate(obj, event):
SP 253     # obj will be the object generating the event.  In this case it
254     # is renWin
255     window=obj.GetRenderWindow() #.GetRenderer()
256     renderer=window.GetRenderers()
257     try:
258         actor=renderer.GetActors()
259     except:
260         pass
261     actor=renderer.GetActors()
262     print(actor)
263     
c89a71 264
7169f1 265
SP 266 def getListOfHostConfigurationByHostname(hosts,host):
267     rhost=[]
268     for chost in hosts:
269         if chost['name'] in host:
270             rhost.append(chost)
271     return rhost
9f5ff5 272
SP 273
274
7169f1 275 def start(hosts,argv=sys.argv[1:]):
SP 276     args=vars(ParseCLIArguments(argv))
277     #print(vars(args))
278
279     #Backward compatibility... If running just on localmode, the host specification is unnecessary. Check if only Runs are specified
280     try:
281         test_host=hosts[0]['name']
282     except:
283         print("Network mode disabled. Old syntax detected.")
284         host={'name':socket.gethostname(),'address':'127.0.0.1', 'runs':hosts}
285         perform_action(args,host)
286         exit(0)
287
288
289     #find the host at which the action is attended
290     if args['host']==None:
291         if(args['status']==False and args['version']==False):
292             hosts=getListOfHostConfigurationByHostname(hosts,socket.gethostname())
293     else:
294         hosts=getListOfHostConfigurationByHostname(hosts,args['host'])
295     if len(hosts)==0:
296         print ('Hostname "{}" does not exist in configuration file. Please check the spelling'.format(args['host'][0]))
297         exit(1)
298
299     if not args['local_only']:
300             hosts=copyConfigAndConnect(hosts)
301
302     #do local stuff:
303     for host in hosts:
304         if host['name'] == socket.gethostname():
305             if(args['html']):
306                 print("Host <font color='orange'>"+host['name']+"</font> reports the following:")
307             else:
308                 print("Host "+bcolors.WARNING+host['name']+bcolors.ENDC+" reports the following:")
309             perform_action(args,host)
310         elif not args['local_only']:
311             output=host['_conn'].execute('python3 ./remote_control.py -x '+" ".join(argv))
312             for line in output:
313                 print(line.replace('\n',''))
314
315
316     if not args['local_only']:
317         print("Closing connections to remote hosts")
318         for host in hosts:
319             if(host['name'] !=socket.gethostname()):
320                 host['_conn'].disconnect()
9f5ff5 321
SP 322
323