Trisurf Monte Carlo simulator
Miha
2016-07-11 e818ba5bcc461af545665c78157be4e52252aa1c
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 *
a9c679 18     from . import VTKRendering
7169f1 19 #import io
5f4aea 20
SP 21
7169f1 22
SP 23 #Color definitions for terminal
24 class bcolors:
25     HEADER = '\033[95m'
26     OKBLUE = '\033[94m'
27     OKGREEN = '\033[92m'
28     WARNING = '\033[93m'
29     FAIL = '\033[91m'
30     ENDC = '\033[0m'
31     BOLD = '\033[1m'
32     UNDERLINE = '\033[4m'
33
34 #parses Command Line Arguments and returns the list of parsed values
35 def ParseCLIArguments(arguments):
36     parser = argparse.ArgumentParser(description='Manages (start, stop, status) multiple simulation processes of trisurf according to the configuration file.')
37     parser.add_argument('proc_no', metavar='PROC_NO', nargs='*',
38                 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.')
39     action_group=parser.add_mutually_exclusive_group(required=True)
40     action_group.add_argument('-c','--comment',nargs=1, help='append comment to current comment')
41     action_group.add_argument('--delete-comment', help='delete comment',action='store_true')
42     action_group.add_argument('-k','--kill','--stop','--suspend', help='stop/kill the process', action='store_true')
43     action_group.add_argument('-r','--run','--start','--continue', help='start/continue process', action='store_true')
44     action_group.add_argument('-s','--status',help='print status of the processes',action='store_true')
45     action_group.add_argument('-v','--version', help='print version information and exit', action='store_true')
46     action_group.add_argument('--web-server', type=int,metavar="PORT", nargs=1, help='EXPERIMENTAL: starts web server and never exist.')
c89a71 47     action_group.add_argument('-p','--preview',help='preview last VTU shape',action='store_true')
7169f1 48     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 49     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.')
50     parser.add_argument('--html', help='Generate HTML output', action="store_true")
51     parser.add_argument('-n', nargs='+', metavar='PROC_NO', type=int, help='OBSOLETE. Specifies process numbers.')
52     parser.add_argument('-R','--raw',help='print status and the rest of the information in raw format', action="store_true")
53     parser.add_argument('-x','--local-only',help='do not attempt to contact remote hosts. Run all operations only on local machine',action='store_true')
54     args = parser.parse_args(arguments)
55     return args
56
57
58 #gets version of trisurf currently running
a3f6b7 59 def getTrisurfVersion():
SP 60     p = subprocess.Popen('trisurf --version', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
61     lines=p.stdout.readlines()
62     version=re.findall(r'[0-9a-f]{7}(?:-dirty)?', lines[0].decode('ascii'))
63     p.wait()
64     if(len(version)):
65         return version[0]
66     else:
67         return "unknown version"
68
9f5ff5 69
7169f1 70
SP 71 def copyConfigAndConnect(hosts):
72     print("Connecting to remote hosts and copying config files, tapes and snapshots")
73     for host in hosts:
74         if(host['name'] !=socket.gethostname()): #if I am not the computer named in host name
75             try:
76                 username=host['username']
77             except:
78                 username=os.getusername() #default username is current user user's name
79             try:
80                 port=host['port']
81             except:
82                 port=22 #default ssh port
83             rm=Remote.Connection(hostname=host['address'],username=username, port=port)
84             rm.connect()
85             rm.send_file(__file__,'remote_control.py')
86             for run in host['runs']:
87                 try:
88                     rm.send_file(run.tapeFile,run.tapeFile)
89                 except:
90                     pass
91                 try:
92                     rm.send_file(run.snapshotFile,run.snapshotFile)
93                 except:
94                     pass
95             host['_conn']= rm
96     # we are connected to all hosts...
97     return hosts
98
99
100
101 def getTargetRunIdxList(args):
102     target_runs=list(map(int,args['proc_no']))
103     if len(target_runs)==0:
104         #check if obsolete -n flags have numbers
105         target_runs=args['n']
106         if target_runs==None:
107             return None
108     target_runs=list(set(target_runs))
109     return target_runs
110
111
112
113 def status_processes(args,host):
114     target_runs=getTargetRunIdxList(args)
115     if target_runs==None:
116         target_runs=list(range(1,len(host['runs'])+1))
117     report=[]
118     for i in target_runs:
119         line=host['runs'][i-1].getStatistics()
120         line.insert(0,i)
121         report.append(line)
122     if(args['raw']):
123         print(report)
124     else:
125         if(args['html']):
126             tablefmt='html'
9f5ff5 127         else:
7169f1 128             tablefmt='fancy_grid'
SP 129         print(tabulate.tabulate(report,headers=["Run no.", "Run start time", "ETA", "Status", "PID", "Path", "Comment"], tablefmt=tablefmt))
130     return
131
132 def run_processes(args,host):
133     target_runs=getTargetRunIdxList(args)
134     if target_runs==None:
135         target_runs=list(range(1,len(host['runs'])+1))
136     for i in target_runs:
137         host['runs'][i-1].start()
138     return
139
140 def kill_processes(args,host):
141     target_runs=getTargetRunIdxList(args)
142     if target_runs==None:
143         if args['force']==True:
144             target_runs=list(range(1,len(host['runs'])+1))
145         else:
146             print("Not stopping all processes on the host. Run with --force flag if you are really sure to stop all simulations")
147             return
148     for i in target_runs:
149         host['runs'][i-1].stop()
150     return
151
152 def comment_processes(args,host):
153     target_runs=getTargetRunIdxList(args)
154     if target_runs==None:
155         target_runs=list(range(1,len(host['runs'])+1))
156     for i in target_runs:
157         host['runs'][i-1].writeComment(args['comment'][0],'a')
158     print("Comment added")
159     return
160
161 def delete_comments(args,host):
162     target_runs=getTargetRunIdxList(args)
163     if target_runs==None:
164         if args['force']==True:
165             target_runs=list(range(1,len(host['runs'])+1))
166         else:
167             print("Not deleting comments on all posts on the host. Run with --force flag if you are really sure to delete all comments")
168             return
169     for i in target_runs:
170         host['runs'][i-1].writeComment("")
171     print("Comment deleted")
172     return
173
174
175 def start_web_server(args,host):
176     print('Server listening on port {}'.format(args['web_server'][0]))
61d2e7 177     if sys.version_info>=(3,0):
SP 178         WebTrisurf.WebServer(port=args['web_server'][0])
179     else:
180         print("Cannot start WebServer in python 2.7")
7169f1 181     exit(0)
SP 182
183 def perform_action(args,host):
184     #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
185     if args['run']:
186         run_processes(args,host)
187     elif args['kill']:
188         kill_processes(args,host)
189     elif args['status']:
190         status_processes(args,host)
191     elif args['comment']!= None:
192         comment_processes(args,host)
193     elif args['delete_comment']:
194         delete_comments(args,host)
195     elif args['web_server']!=None:
196         start_web_server(args,host)
c89a71 197     elif args['preview']:
SP 198         preview_vtu(args,host)
7169f1 199     else: #version requested
SP 200         print(getTrisurfVersion())
201     return
202
203
c89a71 204
SP 205 def preview_vtu(args,host):
206     #only for localhost at the moment
207     if sys.version_info>=(3,0):
208         print("Preview works only with python 2.7")
209         exit(1)
210     if host['name'] == socket.gethostname():
a9c679 211         VTKRendering.Renderer(args,host)
7169f1 212
SP 213 def getListOfHostConfigurationByHostname(hosts,host):
214     rhost=[]
215     for chost in hosts:
216         if chost['name'] in host:
217             rhost.append(chost)
218     return rhost
9f5ff5 219
SP 220
221
7169f1 222 def start(hosts,argv=sys.argv[1:]):
SP 223     args=vars(ParseCLIArguments(argv))
224     #print(vars(args))
225
226     #Backward compatibility... If running just on localmode, the host specification is unnecessary. Check if only Runs are specified
227     try:
228         test_host=hosts[0]['name']
229     except:
230         print("Network mode disabled. Old syntax detected.")
231         host={'name':socket.gethostname(),'address':'127.0.0.1', 'runs':hosts}
232         perform_action(args,host)
233         exit(0)
234
235
236     #find the host at which the action is attended
237     if args['host']==None:
238         if(args['status']==False and args['version']==False):
239             hosts=getListOfHostConfigurationByHostname(hosts,socket.gethostname())
240     else:
241         hosts=getListOfHostConfigurationByHostname(hosts,args['host'])
242     if len(hosts)==0:
243         print ('Hostname "{}" does not exist in configuration file. Please check the spelling'.format(args['host'][0]))
244         exit(1)
245
246     if not args['local_only']:
247             hosts=copyConfigAndConnect(hosts)
248
249     #do local stuff:
250     for host in hosts:
251         if host['name'] == socket.gethostname():
252             if(args['html']):
253                 print("Host <font color='orange'>"+host['name']+"</font> reports the following:")
254             else:
255                 print("Host "+bcolors.WARNING+host['name']+bcolors.ENDC+" reports the following:")
256             perform_action(args,host)
257         elif not args['local_only']:
258             output=host['_conn'].execute('python3 ./remote_control.py -x '+" ".join(argv))
259             for line in output:
260                 print(line.replace('\n',''))
261
262
263     if not args['local_only']:
264         print("Closing connections to remote hosts")
265         for host in hosts:
266             if(host['name'] !=socket.gethostname()):
267                 host['_conn'].disconnect()
9f5ff5 268
SP 269
270