forked from SeattleTestbed/seash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seash_helper.py
1090 lines (729 loc) · 31 KB
/
seash_helper.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Author: Alan Loh
Module: Holds all non-callback seash-related methods here.
This would include:
-Helper functions
-Functions that operate on a target
"""
###Helper Methods###
import seash_global_variables
import seash_exceptions
from repyportability import *
add_dy_support(locals())
# Use local clock for time if there is no network connectivity
import time as pythontime
# Used for error checking during file handling
import errno
# Enable the use of Affixes
affix_stack = dy_import_module('affix_stack.r2py')
affix_obj = affix_stack.AffixStack('(CoordinationAffix)(NamingAndResolverAffix)')
# Override openconnection so that nmclient will use Affixes
openconnection = affix_obj.openconnection
time = dy_import_module("time.r2py")
rsa = dy_import_module("rsa.r2py")
listops = dy_import_module("listops.r2py")
parallelize = dy_import_module("parallelize.r2py")
domainnameinfo = dy_import_module("domainnameinfo.r2py")
# Finds IP-location mappings for `show location`
geoip_client = dy_import_module("geoip_client.r2py")
# For loadstate and savestate
serialize = dy_import_module("serialize.r2py")
nmclient = dy_import_module("nmclient.r2py")
def update_time():
"""
<Purpose>
Updates the time that is used internally for nodemanager communications.
<Arguments>
None
<Side Effects>
Updates the time within time.r2py.
If there is no network connection, the local clock is used.
<Exceptions>
None
<Returns>
None
"""
# Since we import time.r2py, we will only use the local clock if none of
# the default time servers respond.
# LAW: Disabling for now as it seems to be being triggered even though
# TCP/NTP time updates properly. Its bringing up false negatives on
# the unit tests.
# time_register_method('local', local_updatetime)
time.time_updatetime(34612)
def local_updatetime(port):
"""
<Purpose>
Callback for time_interface.r2py to update the time that is used
internally for nodemanager communications.
<Arguments>
port:
The port to update on. This is not used however. It is only
specified to adhere to the function signature expected by
time_interface.r2py.
<Side Effects>
If we reach this function, then it means that other time server updates
failed. We will notify the user of the failure, and set time.r2py to
use the local clock.
<Exceptions>
None
<Returns>
None
"""
print 'Time update failed, could not connect to any time servers...'
print 'Your network connection may be down.'
print "Falling back to using your computer's local clock."
print
# time.time() gives us the # of seconds since 1970, whereas the NTP
# services gives us the # of seconds since 1900.
time.time_settime(pythontime.time() + time.time_seconds_from_1900_to_1970)
# Saves the current state to file. Helper method for the savestate
# command. (Added by Danny Y. Huang)
def savestate(statefn, handleinfo, host, port, expnum, filename, cmdargs,
defaulttarget, defaultkeyname, autosave, currentkeyname):
# obtain the handle info dictionary
for longname in seash_global_variables.vesselinfo.keys():
vessel_handle = seash_global_variables.vesselinfo[longname]['handle']
handleinfo[longname] = nmclient.nmclient_get_handle_info(vessel_handle)
state = {}
state['targets'] = seash_global_variables.targets
state['keys'] = seash_global_variables.keys
state['vesselinfo'] = seash_global_variables.vesselinfo
state['nextid'] = seash_global_variables.nextid
state['handleinfo'] = handleinfo
state['host'] = host
state['port'] = port
state['expnum'] = expnum
state['filename'] = filename
state['cmdargs'] = cmdargs
state['defaulttarget'] = defaulttarget
state['defaultkeyname'] = defaultkeyname
state['autosave'] = autosave
state['globalseashtimeout'] = seash_global_variables.globalseashtimeout
state['globaluploadrate'] = seash_global_variables.globaluploadrate
# serialize states and encrypt
if seash_global_variables.keys.has_key(defaultkeyname):
cypher = rsa.rsa_encrypt(serialize.serialize_serializedata(state), seash_global_variables.keys[currentkeyname]['publickey'])
else:
raise seash_exceptions.UserError("The keyname '" + defaultkeyname + "' is not loaded.")
# writing encrypted serialized states to file
# Exceptions are caught outside of the method
try:
state_obj = open(statefn, 'w')
state_obj.write(cypher)
finally:
state_obj.close()
def is_immutable_targetname(targetname):
if targetname.startswith('%') or ':' in targetname:
return True
return False
def valid_targetname(targetname):
if targetname.startswith('%') or ':' in targetname or ' ' in targetname:
return False
return True
def fit_string(stringdata, length):
if len(stringdata) > length:
return stringdata[:length-3]+'...'
return stringdata
nextidlock = createlock()
def atomically_get_nextid():
# mutex around getting an id
nextidlock.acquire(True)
myid = seash_global_variables.nextid
seash_global_variables.nextid = seash_global_variables.nextid + 1
nextidlock.release()
return myid
# adds a vessel and returns the new ID...
def add_vessel(longname, keyname, vesselhandle):
seash_global_variables.vesselinfo[longname] = {}
seash_global_variables.vesselinfo[longname]['handle'] = vesselhandle
seash_global_variables.vesselinfo[longname]['keyname'] = keyname
seash_global_variables.vesselinfo[longname]['IP'] = longname.split(':')[0]
seash_global_variables.vesselinfo[longname]['port'] = int(longname.split(':')[1])
seash_global_variables.vesselinfo[longname]['vesselname'] = longname.split(':')[2]
# miscelaneous information about the vessel (version, nodeID, etc.)
seash_global_variables.vesselinfo[longname]['information'] = {}
# set up a reference to myself...
seash_global_variables.targets[longname] = [longname]
myid = atomically_get_nextid()
# add my id...
seash_global_variables.targets['%'+str(myid)] = [longname]
seash_global_variables.vesselinfo[longname]['ID'] = '%'+str(myid)
# add me to %all...
seash_global_variables.targets['%all'].append(longname)
return myid
def copy_vessel(longname, newvesselname):
newhandle = nmclient.nmclient_duplicatehandle(seash_global_variables.vesselinfo[longname]['handle'])
newlongname = seash_global_variables.vesselinfo[longname]['IP']+":"+str(seash_global_variables.vesselinfo[longname]['port'])+":"+newvesselname
add_vessel(newlongname,seash_global_variables.vesselinfo[longname]['keyname'],newhandle)
return newlongname
def delete_vessel(longname):
# remove the item...
del seash_global_variables.vesselinfo[longname]
# remove the targets that reference it...
for target in seash_global_variables.targets.copy():
# if in my list...
if longname in seash_global_variables.targets[target]:
# if this is the %num entry or longname entry...
if ('%' in target and target != '%all') or longname == target:
del seash_global_variables.targets[target]
continue
# otherwise remove the item from the list...
seash_global_variables.targets[target].remove(longname)
def longnamelist_to_nodelist(longnamelist):
retlist = []
for longname in longnamelist:
nodename = seash_global_variables.vesselinfo[longname]['IP']+":"+str(seash_global_variables.vesselinfo[longname]['port'])
retlist.append(nodename)
return retlist
def find_handle_for_node(nodename):
for longname in seash_global_variables.vesselinfo:
if longname.rsplit(':',1)[0] == nodename:
return seash_global_variables.vesselinfo[longname]['handle']
raise IndexError("Cannot find handle for '"+nodename+"'")
#################### functions that operate on a target
MAX_CONTACT_WORKER_THREAD_COUNT = 10
# This function abstracts out contacting different nodes. It spawns off
# multiple worker threads to handle the clients...
# by a threaded model in the future...
# NOTE: entries in targetlist are assumed by me to be unique
def contact_targets(targetlist, func, *args):
phandle = parallelize.parallelize_initfunction(targetlist, func, MAX_CONTACT_WORKER_THREAD_COUNT, *args)
while not parallelize.parallelize_isfunctionfinished(phandle):
sleep(.1)
# I'm going to change the format slightly...
resultdict = parallelize.parallelize_getresults(phandle)
# There really shouldn't be any exceptions in any of the routines...
if resultdict['exception']:
print "WARNING: ",resultdict['exception']
# I'm going to convert the format to be targetname (as the key) and
# a value with the return value...
retdict = {}
for nameandretval in resultdict['returned']:
retdict[nameandretval[0]] = nameandretval[1]
return retdict
# This function abstracts out contacting different nodes. It is obsoleted by
# the threaded model... This code is retained for testing reasons only
def simple_contact_targets(targetlist, func, *args):
retdict = {}
# do the function on each target, returning a dict of results.
for target in targetlist:
retdict[target] = func(target,*args)
return retdict
# used in show files
def showfiles_target(longname):
vesselname = seash_global_variables.vesselinfo[longname]['vesselname']
try:
filedata = nmclient.nmclient_signedsay(seash_global_variables.vesselinfo[longname]['handle'],"ListFilesInVessel",vesselname)
except nmclient.NMClientException, e:
return (False, str(e))
else:
return (True, filedata)
# used in show log
def showlog_target(longname):
vesselname = seash_global_variables.vesselinfo[longname]['vesselname']
try:
logdata = nmclient.nmclient_signedsay(seash_global_variables.vesselinfo[longname]['handle'],"ReadVesselLog",vesselname)
except nmclient.NMClientException, e:
return (False, str(e))
else:
return (True, logdata)
# used in show resources
def showresources_target(longname):
vesselname = seash_global_variables.vesselinfo[longname]['vesselname']
try:
resourcedata = nmclient.nmclient_rawsay(seash_global_variables.vesselinfo[longname]['handle'],"GetVesselResources",vesselname)
except nmclient.NMClientException, e:
return (False, str(e))
else:
return (True, resourcedata)
# used in show offcut
def showoffcut_target(nodename):
vesselhandle = find_handle_for_node(nodename)
try:
offcutdata = nmclient.nmclient_rawsay(vesselhandle,"GetOffcutResources")
except nmclient.NMClientException, e:
return (False, str(e))
else:
return (True, offcutdata)
def browse_target(node, currentkeyname, targetgroup='browsegood'):
# NOTE: I almost think I should skip those nodes that I know about from
# previous browse commands. Perhaps I should have an option on the browse
# command?
host, portstring = node.split(':')
port = int(portstring)
# get information about the node's vessels
try:
nodehandle = nmclient.nmclient_createhandle(host, port,
privatekey = seash_global_variables.keys[currentkeyname]['privatekey'],
publickey = seash_global_variables.keys[currentkeyname]['publickey'],
timeout=seash_global_variables.globalseashtimeout)
except nmclient.NMClientException,e:
return (False, str(e))
try:
# need to contact the node to get the list of vessels we can perform
# actions on...
ownervessels, uservessels = nmclient.nmclient_listaccessiblevessels(nodehandle,seash_global_variables.keys[currentkeyname]['publickey'])
retlist = []
# we should add anything we can access (whether a user or owner vessel)
for vesselname in ownervessels + uservessels:
longname = host+":"+str(port)+":"+vesselname
# if we haven't discovered the vessel previously...
if longname not in seash_global_variables.targets:
# set the vesselname in the handle
newhandle = nmclient.nmclient_duplicatehandle(nodehandle)
handleinfo = nmclient.nmclient_get_handle_info(newhandle)
handleinfo['vesselname'] = vesselname
nmclient.nmclient_set_handle_info(newhandle, handleinfo)
# then add the vessel to the target list, etc.
# add_vessel has no race conditions as long as longname is unique
# (and it should be unique)
id = add_vessel(longname,currentkeyname,newhandle)
seash_global_variables.targets[targetgroup].append(longname)
# and append some information to be printed...
retlist.append('%'+str(id)+"("+longname+")")
finally:
nmclient.nmclient_destroyhandle(nodehandle)
return (True, retlist)
def list_or_update_target(longname):
vesselname = seash_global_variables.vesselinfo[longname]['vesselname']
try:
vesseldict = nmclient.nmclient_getvesseldict(seash_global_variables.vesselinfo[longname]['handle'])
except nmclient.NMClientException, e:
return (False, str(e))
else:
# updates the dictionary of our node information (dictionary used in show,
# etc.)
for key in vesseldict['vessels'][vesselname]:
seash_global_variables.vesselinfo[longname][key] = vesseldict['vessels'][vesselname][key]
# Update the "information" (version number, etc.)
del vesseldict['vessels']
seash_global_variables.vesselinfo[longname]['information'] = vesseldict
return (True,)
def upload_target(longname, remotefn, filedata):
vesselname = seash_global_variables.vesselinfo[longname]['vesselname']
try:
# add the file data...
nmclient.nmclient_signedsay(seash_global_variables.vesselinfo[longname]['handle'], "AddFileToVessel", vesselname, remotefn, filedata)
except nmclient.NMClientException, e:
return (False, str(e))
else:
return (True,)
def download_target(longname, localfn, remotefn):
vesselname = seash_global_variables.vesselinfo[longname]['vesselname']
try:
# get the file data...
retrieveddata = nmclient.nmclient_signedsay(seash_global_variables.vesselinfo[longname]['handle'], "RetrieveFileFromVessel", vesselname, remotefn)
except nmclient.NMClientException, e:
return (False, str(e))
else:
writefn = localfn+"."+longname.replace(':','_')
# write to the local filename (replacing ':' with '_')...
fileobj = open(writefn,"w")
fileobj.write(retrieveddata)
fileobj.close()
# for output...
return (True, writefn)
def cat_target(longname, remotefn):
vesselname = seash_global_variables.vesselinfo[longname]['vesselname']
try:
# get the file data...
retrieveddata = nmclient.nmclient_signedsay(seash_global_variables.vesselinfo[longname]['handle'], "RetrieveFileFromVessel", vesselname, remotefn)
except nmclient.NMClientException, e:
return (False, str(e))
else:
# and return it..
return (True, retrieveddata)
def delete_target(longname, remotefn):
vesselname = seash_global_variables.vesselinfo[longname]['vesselname']
try:
# delete the file...
nmclient.nmclient_signedsay(seash_global_variables.vesselinfo[longname]['handle'], "DeleteFileInVessel", vesselname, remotefn)
except nmclient.NMClientException, e:
return (False, str(e))
else:
return (True,)
def start_target(longname, argstring, prog_platform):
vesselname = seash_global_variables.vesselinfo[longname]['vesselname']
# start the program
try:
# Backwards compatibility with old nodemanagers that don't support
# StartVesselEX
if prog_platform == "repyV1":
nmclient.nmclient_signedsay(
seash_global_variables.vesselinfo[longname]['handle'],
"StartVessel", vesselname, argstring)
else:
nmclient.nmclient_signedsay(
seash_global_variables.vesselinfo[longname]['handle'],
"StartVesselEx", vesselname, prog_platform, argstring)
except nmclient.NMClientException, e:
print str(e)
return (False, str(e))
else:
return (True,)
def stop_target(longname):
vesselname = seash_global_variables.vesselinfo[longname]['vesselname']
try:
# stop the programs
nmclient.nmclient_signedsay(seash_global_variables.vesselinfo[longname]['handle'], "StopVessel", vesselname)
except nmclient.NMClientException, e:
return (False, str(e))
else:
return (True,)
def reset_target(longname):
vesselname = seash_global_variables.vesselinfo[longname]['vesselname']
try:
# reset the target
nmclient.nmclient_signedsay(seash_global_variables.vesselinfo[longname]['handle'], "ResetVessel", vesselname)
except nmclient.NMClientException, e:
return (False, str(e))
else:
return (True,)
def run_target(longname, filename, filedata, argstring, prog_platform):
vesselname = seash_global_variables.vesselinfo[longname]['vesselname']
try:
nmclient.nmclient_signedsay(seash_global_variables.vesselinfo[longname]['handle'], "AddFileToVessel", vesselname, filename, filedata)
# Backwards compatibility with old nodemanagers that don't support
# StartVesselEX
if prog_platform == "repyV1":
nmclient.nmclient_signedsay(
seash_global_variables.vesselinfo[longname]['handle'],
"StartVessel", vesselname, argstring)
else:
nmclient.nmclient_signedsay(
seash_global_variables.vesselinfo[longname]['handle'],
"StartVesselEx", vesselname, prog_platform, argstring)
except nmclient.NMClientException, e:
return (False, str(e))
else:
return (True,)
# didn't test...
def split_target(longname, resourcedata):
vesselname = seash_global_variables.vesselinfo[longname]['vesselname']
try:
newvesselnames = nmclient.nmclient_signedsay(seash_global_variables.vesselinfo[longname]['handle'], "SplitVessel", vesselname, resourcedata)
except nmclient.NMClientException, e:
return (False, str(e))
else:
newname1 = copy_vessel(longname, newvesselnames.split()[0])
newname2 = copy_vessel(longname, newvesselnames.split()[1])
delete_vessel(longname)
return (True,(newname1,newname2))
# didn't test...
def join_target(nodename, nodedict):
if len(nodedict[nodename]) < 2:
# not enough vessels, nothing to do
return (False, None)
# I'll iterate through the vessels, joining one with the current
# (current starts as the first vessel and becomes the "new vessel")
currentvesselname = seash_global_variables.vesselinfo[nodedict[nodename][0]]['vesselname']
currentlongname = nodedict[nodename][0]
# keep a list of what I replace...
subsumedlist = [currentlongname]
for longname in nodedict[nodename][1:]:
vesselname = seash_global_variables.vesselinfo[longname]['vesselname']
try:
newvesselname = nmclient.nmclient_signedsay(seash_global_variables.vesselinfo[longname]['handle'], "JoinVessels", currentvesselname, vesselname)
except nmclient.NMClientException, e:
return (False, str(e))
else:
newname = copy_vessel(longname, newvesselname)
delete_vessel(longname)
delete_vessel(currentlongname)
subsumedlist.append(longname)
currentlongname = newname
currentvesselname = newvesselname
else:
return (True, (currentlongname,subsumedlist))
# didn't test...
def setowner_target(longname, newowner):
vesselname = seash_global_variables.vesselinfo[longname]['vesselname']
try:
nmclient.nmclient_signedsay(seash_global_variables.vesselinfo[longname]['handle'], "ChangeOwner", vesselname, rsa.rsa_publickey_to_string(seash_global_variables.keys[newowner]['publickey']))
except nmclient.NMClientException, e:
return (False, str(e))
else:
return (True,)
# didn't test...
def setadvertise_target(longname, newadvert):
vesselname = seash_global_variables.vesselinfo[longname]['vesselname']
try:
# do the actual advertisement changes
nmclient.nmclient_signedsay(seash_global_variables.vesselinfo[longname]['handle'], "ChangeAdvertise", vesselname, newadvert)
except nmclient.NMClientException, e:
return (False, str(e))
else:
return (True,)
def setownerinformation_target(longname, newownerinformation):
vesselname = seash_global_variables.vesselinfo[longname]['vesselname']
try:
# do the actual advertisement changes
nmclient.nmclient_signedsay(seash_global_variables.vesselinfo[longname]['handle'], "ChangeOwnerInformation", vesselname, newownerinformation)
except nmclient.NMClientException, e:
return (False, str(e))
else:
return (True,)
def setusers_target(longname, userkeystring):
vesselname = seash_global_variables.vesselinfo[longname]['vesselname']
try:
nmclient.nmclient_signedsay(seash_global_variables.vesselinfo[longname]['handle'], "ChangeUsers", vesselname, userkeystring)
except nmclient.NMClientException, e:
return (False, str(e))
else:
return (True,)
# Checks if both keys are setup
def check_key_set(name):
if (name in seash_global_variables.keys and 'publickey' in seash_global_variables.keys[name] and 'privatekey' in seash_global_variables.keys[name] and seash_global_variables.keys[name]['publickey'] and seash_global_variables.keys[name]['privatekey']):
if not check_key_pair_compatibility(name):
raise seash_exceptions.UserError("Error: Mis-matched Public/Private key pair!")
# Check the keys to make sure they are compatible, for the given name
def check_key_pair_compatibility(name):
# Check for both sets of keys
setPublic = seash_global_variables.keys[name]['publickey']
setPrivate = seash_global_variables.keys[name]['privatekey']
# Check for a mis-match
match = rsa.rsa_matching_keys(setPrivate, setPublic)
return match
# Reload the handles of a node. Used when "loadstate" is invoked. Returns a
# tuple (success, e), where success is a boolean and e is a string of error
# messages. Added by Danny Y. Huang.
def reload_target(longname, handleinfo):
host, portstring, vesselname = longname.split(':')
port = int(portstring)
try:
priKey = handleinfo[longname]['privatekey']
pubKey = handleinfo[longname]['publickey']
except KeyError:
error = ("Vessel is absent in the handleinfo dictionary.")
return (False, error)
# find the user who has these keys
thiskeyname = ""
for keyname in seash_global_variables.keys.keys():
if (seash_global_variables.keys[keyname]['publickey'] == pubKey and
seash_global_variables.keys[keyname]['privatekey'] == priKey):
thiskeyname = keyname
break
if not thiskeyname:
return (False, "User with keyname '" + keyname + "' is not found.")
# create new handle for the vessel
try:
vessel_handle = nmclient.nmclient_createhandle(host, port,
privatekey = priKey, publickey = pubKey,
timeout=seash_global_variables.globalseashtimeout)
except nmclient.NMClientException, error:
return (False, str(error))
try:
nmclient.nmclient_set_handle_info(vessel_handle, handleinfo[longname])
seash_global_variables.vesselinfo[longname]['handle'] = vessel_handle
# hello test to see if the vessel is available
(ownervessels, uservessels) = nmclient.nmclient_listaccessiblevessels(vessel_handle, pubKey)
if not (ownervessels + uservessels):
return (False, "Vessel is not available for keyname " + keyname + ".")
except Exception, error:
# Catching unexpected exceptions
return (False, "General exception: " + str(error) + ".")
return (True, "")
# Determines if there's a need to temporarily change the vessel timeout
# to avoid timing out on bad connection speeds when uploading file.
def set_upload_timeout(filedata):
filesize = len(filedata)
est_upload_time = filesize / seash_global_variables.globaluploadrate
# sets the new timeout if necessary
if est_upload_time > seash_global_variables.globalseashtimeout:
for longname in seash_global_variables.vesselinfo:
thisvesselhandle = seash_global_variables.vesselinfo[longname]['handle']
thisvesselhandledict = nmclient.nmclient_get_handle_info(thisvesselhandle)
thisvesselhandledict['timeout'] = est_upload_time
nmclient.nmclient_set_handle_info(thisvesselhandle,thisvesselhandledict)
# Resets each vessel's timeout to the value of globalseashtimeout
def reset_vessel_timeout():
# resets each vessel's timeout to the original values before file upload
for longname in seash_global_variables.vesselinfo:
thisvesselhandle = seash_global_variables.vesselinfo[longname]['handle']
thisvesselhandledict = nmclient.nmclient_get_handle_info(thisvesselhandle)
thisvesselhandledict['timeout'] = seash_global_variables.globalseashtimeout
nmclient.nmclient_set_handle_info(thisvesselhandle,thisvesselhandledict)
def get_execution_platform(command, filename):
"""
<Purpose>
Returns the execution platform based on a best-guess approach using
the specified command, as well as the a file's extension. The
command takes precedence over the file extension. If the extension
is not recognized, then it will be assumed that it is repyV2.
<Arguments>
command: The command that should be parsed.
filename: The file whose repy version should be returned.
<Side Effects>
None
<Exceptions>
None
<Returns>
A string indicating which version of repy a program is in, based on
its file extension. This will be either "v1" or "v2".
"""
if command.endswith('v2'):
return 'repyV2'
elif command.endswith('v1'):
return 'repyV1'
# Information on extensions for repy programs can be found on #1286.
if filename.endswith('.r2py'):
return 'repyV2'
else:
return 'repyV1'
def print_vessel_errors(retdict):
"""
<Purpose>
Prints out any errors that occurred while performing an action on vessels,
in a human readable way.
Errors will be printed out in the following format:
description [reason]
Affected vessels: nodelist
To define a new error, add the following entry to ERROR_RESPONSES in this
function:
'error_identifier': {
'error': 'description for the error',
'reason': 'reason for the error' (optional).
'error_identifier'
This is the substring of the error that can be used to identify it.
Longer identifiers will have a higher priority over shorter identifiers.
For example, authentication errors could be identified using the string
'Insufficient Permissions'.
'error'
This is where you put the description for the error to show to the user.
'reason' (optional)
This is where you put clarification for the error to ease the user.
Additionally, you may put things that they could do to fix the error here,
if applicable. If you don't want to show a reason, don't include this key
in the dictionary.
Examples when you would not put a reason is if you received a timeout,
since the user can't do anything to fix them.
<Arguments>
retdict:
A list of longnames mapped against a tuple (Success?, Message/Errortext).
<Side Effects>
Prints error messages onto the screen. See documentation for ERROR_RESPONSES
for more information.
<Exceptions>
Exception
<Return>
None
"""
ERROR_RESPONSES = {
"Node Manager error 'Insufficient Permissions'": {
'error': "You lack sufficient permissions to perform this action.",
'reason': "Did you release the resource(s) by accident?"},
'timed out': {
'error':'Connection timed out.'},
"file not found": {
'error': "The specified file(s) could not be found.",
'reason': "Please check the filename."},
"Node Manager error 'Programming language platform is not supported.'": {
'error': "Requested platform is not supported by the target vessel."},
}
# A dictionary mapping error identifiers to a list of vessels that share
# that error.
error_longnames = {}
for longname in retdict:
# if the first item is true, then there is no error.
if not retdict[longname][0]:
matches = []
# Loop until we find the response
for error_string in ERROR_RESPONSES:
if error_string.lower() in retdict[longname][1].lower():
# This is the first match
if not matches:
matches = [error_string]
else:
# This is a better match, forget about the previous matches
if len(error_string) > len(matches[0]):
matches = [error_string]
elif len(error_string) == len(matches[0]):
matches.append(error_string)
# If there isn't a match, use the error string as an error identifier
if not matches:
errorid = retdict[longname][1]
else:
# There should not be more than 1 match for any error.
# If there is, log the error to a file.
if len(matches) != 1:
errfile = open('seasherrors.txt', 'a')
errorstring = "Multiple matches with same priority:" + '\n'.join(matches)
errfile.write(errorstring)
errfile.close()