-
Notifications
You must be signed in to change notification settings - Fork 10
/
command_callbacks.py
2915 lines (1918 loc) · 93.1 KB
/
command_callbacks.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: Library of command callback methods. A large portion of the code were
directly transferred here from the original seash. Each command function
is assigned to the appropriate levels of the respective command's
dictionary, and are called upon by command_parser.py's dispatch method
when executing a user's command input.
All callback methods should take in two arguments:
-a command dictionary, input_dict, that holds and reflects the user's command input
-the environment_dict holding the following variables:
host
port
expnum
filename
cmdargs
defaulttarget
defaultkeyname
currenttarget
currentkeyname
handleinfo
autosave
In order to pull user-inputed arguments from the command dictionary, the
following code is used throughout for consistency and ease:
command_key = input_dict.keys()[0]
# Iterates through the dictionary to retrieve the user's filename input
while input_dict[command_key]['name'] is not 'filename':
input_dict = input_dict[command_key]['children']
command_key = input_dict.keys()[0]
...and command_key will either contain the user's file name, found by searching
for the appropriate command dictionary with the name 'filename' and taking the
key associated with it, or it will hold the key of the last command dictionary
in the input dictionary chain. It is important that the name of the argument
command dictionary is unique and spelled correctly.
Method names should reflect the expected command string used to executed that
command dictionary's command callback. For example, the command "show resources
to file"'s method should be called show_resources_to_filename.
Commands with slight variations in function depending on how the user inputted
it should use a separate method for each variation in its command string. For
example, 'browse' would have the method named 'browse', while 'browse [type]'
would have a separate method called 'browse_args'
"""
# For access to additional methods in command handling
import seash_helper
# For access to various global variables kept track of throughout a seash session
import seash_global_variables
# To be able to modify the command dictionary
import seash_dictionary
# To be able to look up what a module commanddict contains (for modulehelp)
import seash_modules
# To be able to throw certain exceptions designed specifically for seash
import seash_exceptions
import seash_modules
import os.path
import sys
# For reverse DNS lookups (of IP address to names)
import socket
# XXX Importing repyportability and dy_import_module's more than
# XXX once in an import chain overwrites the Repy API calls and
# XXX disables any overrides (e.g. Affix's). We thus use
# XXX seash_helper's copy of dylink to pull in the stuff we need.
time = seash_helper.dy_import_module("time.r2py")
rsa = seash_helper.dy_import_module("rsa.r2py")
listops = seash_helper.dy_import_module("listops.r2py")
nmclient = seash_helper.nmclient
# For lookups from Seattle's advertise services
advertise = seash_helper.dy_import_module("advertise.r2py")
# For `loadstate` and `savestate`
serialize = seash_helper.dy_import_module("serialize.r2py")
# The versions of Seattle that we officially support.
SUPPORTED_PROG_PLATFORMS = ["repyV1", "repyV2"]
# set the target, then handle other operations
def on_target(input_dict, environment_dict):
command_key = input_dict.keys()[0]
# Iterates through the dictionary to retrieve the user's target name
while input_dict[command_key]['name'] is not 'ontarget':
input_dict = input_dict[command_key]['children']
command_key = input_dict.keys()[0]
if command_key not in seash_global_variables.targets:
raise seash_exceptions.UserError("Error: Unknown target '"+command_key+"'")
# set the target and strip the rest...
environment_dict['currenttarget'] = command_key
# Set default if there's no follow-up commands
if not input_dict[command_key]['children']:
environment_dict['defaulttarget'] = environment_dict['currenttarget']
# set the keys, then handle other operations
def as_keyname(input_dict, environment_dict):
command_key = input_dict.keys()[0]
# Iterates through the dictionary to retrieve the user's keyname input
while input_dict[command_key]['name'] is not 'askeyname':
input_dict = input_dict[command_key]['children']
command_key = input_dict.keys()[0]
if command_key not in seash_global_variables.keys:
raise seash_exceptions.UserError("Error: Unknown identity '"+command_key+"'")
# set the target and strip the rest...
environment_dict['currentkeyname'] = command_key
# Set default if there's no follow-up commands
if not input_dict[command_key]['children']:
environment_dict['defaultkeyname'] = environment_dict['currentkeyname']
###Show Commands###
# show -- Does nothing
def show(input_dict, environment_dict):
pass
# show info -- Display general information about the vessels
def show_info(input_dict, environment_dict):
if not environment_dict['currenttarget']:
raise seash_exceptions.UserError("Error, command requires a target")
for longname in seash_global_variables.targets[environment_dict['currenttarget']]:
if seash_global_variables.vesselinfo[longname]['information']:
print longname, seash_global_variables.vesselinfo[longname]['information']
else:
print longname, "has no information (try 'update' or 'list')"
# show targets -- Display a list of targets
def show_targets(input_dict, environment_dict):
for target in seash_global_variables.targets:
if len(seash_global_variables.targets[target]) == 0:
print target, "(empty)"
continue
# this is a vesselentry
if target == seash_global_variables.targets[target][0]:
continue
print target, seash_global_variables.targets[target]
# show groups -- Display a list of groups
def show_groups(input_dict, environment_dict):
for target in seash_global_variables.targets:
if target == "%all":
print target, seash_global_variables.targets[target]
continue
# this is individual target number
if target.startswith('%'):
continue
if len(seash_global_variables.targets[target]) == 0:
print target, seash_global_variables.targets[target]
continue
# this is a vesselentry
if target == seash_global_variables.targets[target][0]:
continue
print target, seash_global_variables.targets[target]
# show keys -- Display the known keys
def show_keys(input_dict, environment_dict):
for identity in seash_global_variables.keys:
print identity,seash_global_variables.keys[identity]['publickey'],seash_global_variables.keys[identity]['privatekey']
# show identities -- Display the known identities
def show_identities(input_dict, environment_dict):
for keyname in seash_global_variables.keys:
print keyname,
if seash_global_variables.keys[keyname]['publickey']:
print "PUB",
if seash_global_variables.keys[keyname]['privatekey']:
print "PRIV",
print
# show users -- Display the user keys for the vessels
def show_users(input_dict, environment_dict):
if not environment_dict['currenttarget']:
raise seash_exceptions.UserError("Error, command requires a target")
for longname in seash_global_variables.targets[environment_dict['currenttarget']]:
if 'userkeys' in seash_global_variables.vesselinfo[longname]:
if seash_global_variables.vesselinfo[longname]['userkeys'] == []:
print longname,"(no keys)"
continue
print longname,
# we'd like to say 'joe's public key' instead of '3453 2323...'
for key in seash_global_variables.vesselinfo[longname]['userkeys']:
for identity in seash_global_variables.keys:
if seash_global_variables.keys[identity]['publickey'] == key:
print identity,
break
else:
print seash_helper.fit_string(rsa.rsa_publickey_to_string(key), 15),
print
else:
print longname, "has no information (try 'update' or 'list')"
# show ownerinfo -- Display owner information for the vessels
def show_ownerinfo(input_dict, environment_dict):
if not environment_dict['currenttarget']:
raise seash_exceptions.UserError("Error, command requires a target")
for longname in seash_global_variables.targets[environment_dict['currenttarget']]:
if 'ownerinfo' in seash_global_variables.vesselinfo[longname]:
print longname, "'"+seash_global_variables.vesselinfo[longname]['ownerinfo']+"'"
# list all of the info...
else:
print longname, "has no information (try 'update' or 'list')"
# show advertise -- Display advertisement information about the vessels
def show_advertise(input_dict, environment_dict):
if not environment_dict['currenttarget']:
raise seash_exceptions.UserError("Error, command requires a target")
for longname in seash_global_variables.targets[environment_dict['currenttarget']]:
if 'advertise' in seash_global_variables.vesselinfo[longname]:
if seash_global_variables.vesselinfo[longname]['advertise']:
print longname, "on"
else:
print longname, "off"
# list all of the info...
else:
print longname, "has no information (try 'update' or 'list')"
# show owner -- Display a vessel's owner
def show_owner(input_dict, environment_dict):
if not environment_dict['currenttarget']:
raise seash_exceptions.UserError("Error, command requires a target")
for longname in seash_global_variables.targets[environment_dict['currenttarget']]:
if 'ownerkey' in seash_global_variables.vesselinfo[longname]:
# we'd like to say 'joe public key' instead of '3453 2323...'
ownerkey = seash_global_variables.vesselinfo[longname]['ownerkey']
for identity in seash_global_variables.keys:
if seash_global_variables.keys[identity]['publickey'] == ownerkey:
print longname, identity+" pubkey"
break
else:
print longname, seash_helper.fit_string(rsa.rsa_publickey_to_string(ownerkey), 15)
else:
print longname, "has no information (try 'update' or 'list')"
# show files -- Display a list of files in the vessel (*)
def show_files(input_dict, environment_dict):
if not environment_dict['currenttarget']:
raise seash_exceptions.UserError("Error, command requires a target")
# print the list of files in the vessels (seash method)
retdict = seash_helper.contact_targets(seash_global_variables.targets[environment_dict['currenttarget']], seash_helper.showfiles_target)
goodlist = []
faillist = []
for longname in retdict:
# True means it worked
if retdict[longname][0]:
# let's sort the files...
fileliststring = retdict[longname][1]
filelist = fileliststring.split()
filelist.sort()
fileliststring = " ".join(filelist)
print "Files on '"+longname+"': '"+fileliststring+"'"
goodlist.append(longname)
else:
faillist.append(longname)
seash_helper.print_vessel_errors(retdict)
if goodlist and faillist:
seash_global_variables.targets['filesgood'] = goodlist
seash_global_variables.targets['filesfail'] = faillist
print "Added group 'filesgood' with "+str(len(seash_global_variables.targets['filesgood']))+" targets and 'filesfail' with "+str(len(seash_global_variables.targets['filesfail']))+" targets"
# show log [to file] -- Display the log from the vessel (*)
def show_log(input_dict, environment_dict):
writeoutputtofile = False
if not environment_dict['currenttarget']:
raise seash_exceptions.UserError("Error, command requires a target")
# print the vessel logs...
retdict = seash_helper.contact_targets(seash_global_variables.targets[environment_dict['currenttarget']], seash_helper.showlog_target)
goodlist = []
faillist = []
for longname in retdict:
# True means it worked
if retdict[longname][0]:
if writeoutputtofile:
# write to a file if requested.
outputfilename = outputfileprefix +'.'+ longname
outputfo = file(outputfilename,'w')
outputfo.write(retdict[longname][1])
outputfo.close()
print "Wrote log as '"+outputfilename+"'."
else:
# else print it to the terminal
print "Log from '"+longname+"':"
print retdict[longname][1]
# regardless, this is a good node
goodlist.append(longname)
else:
faillist.append(longname)
seash_helper.print_vessel_errors(retdict)
if goodlist and faillist:
seash_global_variables.targets['loggood'] = goodlist
seash_global_variables.targets['logfail'] = faillist
print "Added group 'loggood' with "+str(len(seash_global_variables.targets['loggood']))+" targets and 'logfail' with "+str(len(seash_global_variables.targets['logfail']))+" targets"
# show log to file
def show_log_to_file(input_dict, environment_dict):
command_key = input_dict.keys()[0]
# Iterates through the dictionary to retrieve the user's filename
while input_dict[command_key]['name'] is not 'filename':
input_dict = input_dict[command_key]['children']
command_key = input_dict.keys()[0]
writeoutputtofile = True
# handle '~'
fileandpath = os.path.expanduser(command_key)
outputfileprefix = fileandpath
if not environment_dict['currenttarget']:
raise seash_exceptions.UserError("Error, command requires a target")
# print the vessel logs...
retdict = seash_helper.contact_targets(seash_global_variables.targets[environment_dict['currenttarget']], seash_helper.showlog_target)
goodlist = []
faillist = []
for longname in retdict:
# True means it worked
if retdict[longname][0]:
if writeoutputtofile:
# write to a file if requested.
outputfilename = outputfileprefix +'.'+ longname
outputfo = file(outputfilename,'w')
outputfo.write(retdict[longname][1])
outputfo.close()
print "Wrote log as '"+outputfilename+"'."
else:
# else print it to the terminal
print "Log from '"+longname+"':"
print retdict[longname][1]
# regardless, this is a good node
goodlist.append(longname)
else:
faillist.append(longname)
seash_helper.print_vessel_errors(retdict)
if goodlist and faillist:
seash_global_variables.targets['loggood'] = goodlist
seash_global_variables.targets['logfail'] = faillist
print "Added group 'loggood' with "+str(len(seash_global_variables.targets['loggood']))+" targets and 'logfail' with "+str(len(seash_global_variables.targets['logfail']))+" targets"
# show resources -- Display the resources / restrictions for the vessel (*)
def show_resources(input_dict, environment_dict):
if not environment_dict['currenttarget']:
raise seash_exceptions.UserError("Error, command requires a target")
retdict = seash_helper.contact_targets(seash_global_variables.targets[environment_dict['currenttarget']], seash_helper.showresources_target)
faillist = []
goodlist = []
for longname in retdict:
# True means it worked
if retdict[longname][0]:
print "Resource data for '"+longname+"':"
print retdict[longname][1]
goodlist.append(longname)
else:
faillist.append(longname)
seash_helper.print_vessel_errors(retdict)
if goodlist and faillist:
seash_global_variables.targets['resourcegood'] = goodlist
seash_global_variables.targets['resourcefail'] = faillist
print "Added group 'resourcegood' with "+str(len(seash_global_variables.targets['resourcegood']))+" targets and 'resourcefail' with "+str(len(seash_global_variables.targets['resourcefail']))+" targets"
# show offcut -- Display the offcut resource for the node (*)
def show_offcut(input_dict, environment_dict):
if not environment_dict['currenttarget']:
raise seash_exceptions.UserError("Error, command requires a target")
# we should only visit a node once...
nodelist = listops.listops_uniq(seash_helper.longnamelist_to_nodelist(seash_global_variables.targets[environment_dict['currenttarget']]))
retdict = seash_helper.contact_targets(nodelist, seash_helper.showoffcut_target)
for nodename in retdict:
if retdict[nodename][0]:
print "Offcut resource data for '"+nodename+"':"
print retdict[nodename][1]
seash_helper.print_vessel_errors(retdict)
# show ip [to file] -- Display the ip addresses of the nodes
def show_ip(input_dict, environment_dict):
if not environment_dict['currenttarget']:
raise seash_exceptions.UserError("Error, command requires a target")
# print to the terminal (stdout)
# write data here...
outfo = sys.stdout
# we should only visit a node once...
printedIPlist = []
for longname in seash_global_variables.targets[environment_dict['currenttarget']]:
thisnodeIP = seash_global_variables.vesselinfo[longname]['IP']
if thisnodeIP not in printedIPlist:
printedIPlist.append(thisnodeIP)
print >> outfo, thisnodeIP
# show ip to file
def show_ip_to_file(input_dict, environment_dict):
if not environment_dict['currenttarget']:
raise seash_exceptions.UserError("Error, command requires a target")
command_key = input_dict.keys()[0]
# Iterates through the dictionary to retrieve the user's filename
while input_dict[command_key]['name'] is not 'filename':
input_dict = input_dict[command_key]['children']
command_key = input_dict.keys()[0]
outfo = open(command_key,"w+")
# we should only visit a node once...
printedIPlist = []
for longname in seash_global_variables.targets[environment_dict['currenttarget']]:
thisnodeIP = seash_global_variables.vesselinfo[longname]['IP']
if thisnodeIP not in printedIPlist:
printedIPlist.append(thisnodeIP)
print >> outfo, thisnodeIP
# if it's a file, close it...
outfo.close()
# show hostname -- Display the hostnames of the nodes
def show_hostname(input_dict, environment_dict):
if not environment_dict['currenttarget']:
raise seash_exceptions.UserError("Error, command requires a target")
# we should only visit a node once...
printedIPlist = []
for longname in seash_global_variables.targets[environment_dict['currenttarget']]:
thisnodeIP = seash_global_variables.vesselinfo[longname]['IP']
if thisnodeIP not in printedIPlist:
printedIPlist.append(thisnodeIP)
print thisnodeIP,
try:
nodeinfo = socket.gethostbyaddr(thisnodeIP)
except (socket.herror,socket.gaierror, socket.timeout, socket.error):
print 'has unknown host information'
else:
print 'is known as',nodeinfo[0]
# show timeout -- Display the timeout for nodes
def show_timeout(input_dict, environment_dict):
print seash_global_variables.globalseashtimeout
# show uploadrate -- Display the file upload rate
def show_uploadrate(input_dict, environment_dict):
print seash_global_variables.globaluploadrate
# add target (to group)
def add_target(input_dict, environment_dict):
command_key = input_dict.keys()[0]
# Iterates through the dictionary to retrieve the user's target name
while input_dict[command_key]['name'] is not 'target':
input_dict = input_dict[command_key]['children']
command_key = input_dict.keys()[0]
source = command_key
dest = environment_dict['currenttarget']
# okay, now source and dest are set. Time to add the nodes in source
# to the group dest...
if source not in seash_global_variables.targets:
raise seash_exceptions.UserError("Error, unknown target '"+source+"'")
if dest not in seash_global_variables.targets:
if not seash_helper.valid_targetname(dest):
raise seash_exceptions.UserError("Error, invalid target name '"+dest+"'")
seash_global_variables.targets[dest] = []
if seash_helper.is_immutable_targetname(dest):
raise seash_exceptions.UserError("Can't modify the contents of '"+dest+"'")
# source - dest has what we should add (items in source but not dest)
addlist = listops.listops_difference(seash_global_variables.targets[source],seash_global_variables.targets[dest])
if len(addlist) == 0:
raise seash_exceptions.UserError("No targets to add (the target is already in '"+dest+"')")
for item in addlist:
seash_global_variables.targets[dest].append(item)
# add target to group
def add_target_to_group(input_dict, environment_dict):
command_key = input_dict.keys()[0]
# Iterates through the dictionary to retrieve the user's target name
while input_dict[command_key]['name'] is not 'target':
input_dict = input_dict[command_key]['children']
command_key = input_dict.keys()[0]
source = command_key
# Iterates through the dictionary to retrieve the user's group argument
while input_dict[command_key]['name'] is not 'args':
input_dict = input_dict[command_key]['children']
command_key = input_dict.keys()[0]
dest = command_key
# okay, now source and dest are set. Time to add the nodes in source
# to the group dest...
if source not in seash_global_variables.targets:
raise seash_exceptions.UserError("Error, unknown target '"+source+"'")
if dest not in seash_global_variables.targets:
if not seash_helper.valid_targetname(dest):
raise seash_exceptions.UserError("Error, invalid target name '"+dest+"'")
seash_global_variables.targets[dest] = []
if seash_helper.is_immutable_targetname(dest):
raise seash_exceptions.UserError("Can't modify the contents of '"+dest+"'")
# source - dest has what we should add (items in source but not dest)
addlist = listops.listops_difference(seash_global_variables.targets[source],seash_global_variables.targets[dest])
if len(addlist) == 0:
raise seash_exceptions.UserError("No targets to add (the target is already in '"+dest+"')")
for item in addlist:
seash_global_variables.targets[dest].append(item)
# add to group
def add_to_group(input_dict, environment_dict):
command_key = input_dict.keys()[0]
# Iterates through the dictionary to retrieve the user's group argument
while input_dict[command_key]['name'] is not 'args':
input_dict = input_dict[command_key]['children']
command_key = input_dict.keys()[0]
source = environment_dict['currenttarget']
dest = command_key
# okay, now source and dest are set. Time to add the nodes in source
# to the group dest...
if source not in seash_global_variables.targets:
raise seash_exceptions.UserError("Error, unknown target '"+source+"'")
if dest not in seash_global_variables.targets:
if not seash_helper.valid_targetname(dest):
raise seash_exceptions.UserError("Error, invalid target name '"+dest+"'")
seash_global_variables.targets[dest] = []
if seash_helper.is_immutable_targetname(dest):
raise seash_exceptions.UserError("Can't modify the contents of '"+dest+"'")
# source - dest has what we should add (items in source but not dest)
addlist = listops.listops_difference(seash_global_variables.targets[source],seash_global_variables.targets[dest])
if len(addlist) == 0:
raise seash_exceptions.UserError("No targets to add (the target is already in '"+dest+"')")
for item in addlist:
seash_global_variables.targets[dest].append(item)
# remove target (from group)
def remove_target(input_dict, environment_dict):
command_key = input_dict.keys()[0]
# Iterates through the dictionary to retrieve the user's target name
while input_dict[command_key]['name'] is not 'target':
input_dict = input_dict[command_key]['children']
command_key = input_dict.keys()[0]
source = command_key
dest = environment_dict['currenttarget']
# time to check args and do the ops
if source not in seash_global_variables.targets:
raise seash_exceptions.UserError("Error, unknown target '"+source+"'")
if dest not in seash_global_variables.targets:
raise seash_exceptions.UserError("Error, unknown group '"+dest+"'")
if seash_helper.is_immutable_targetname(dest):
raise seash_exceptions.UserError("Can't modify the contents of '"+dest+"'")
# find the items to remove (the items in both dest and source)
removelist = listops.listops_intersect(seash_global_variables.targets[dest],seash_global_variables.targets[source])
if len(removelist) == 0:
raise seash_exceptions.UserError("No targets to remove (no items from '"+source+"' are in '"+dest+"')")
# it's okay to end up with an empty group. We'll leave it...
for item in removelist:
seash_global_variables.targets[dest].remove(item)
# remove target from group
def remove_target_from_group(input_dict, environment_dict):
command_key = input_dict.keys()[0]
# Iterates through the dictionary to retrieve the user's target name
while input_dict[command_key]['name'] is not 'target':
input_dict = input_dict[command_key]['children']
command_key = input_dict.keys()[0]
source = command_key
# Iterates through the dictionary to retrieve the user's group name
while input_dict[command_key]['name'] is not 'group':
input_dict = input_dict[command_key]['children']
command_key = input_dict.keys()[0]
dest = command_key
# time to check args and do the ops
if source not in seash_global_variables.targets:
raise seash_exceptions.UserError("Error, unknown target '"+source+"'")
if dest not in seash_global_variables.targets:
raise seash_exceptions.UserError("Error, unknown group '"+dest+"'")
if seash_helper.is_immutable_targetname(dest):
raise seash_exceptions.UserError("Can't modify the contents of '"+dest+"'")
# find the items to remove (the items in both dest and source)
removelist = listops.listops_intersect(seash_global_variables.targets[dest],seash_global_variables.targets[source])
if len(removelist) == 0:
raise seash_exceptions.UserError("No targets to remove (no items from '"+source+"' are in '"+dest+"')")
# it's okay to end up with an empty group. We'll leave it...
for item in removelist:
seash_global_variables.targets[dest].remove(item)
# remove from group
def remove_from_group(input_dict, environment_dict):
source = environment_dict['currenttarget']
command_key = input_dict.keys()[0]
# Iterates through the dictionary to retrieve the user's group name
while input_dict[command_key]['name'] is not 'group':
input_dict = input_dict[command_key]['children']
command_key = input_dict.keys()[0]
dest = command_key
# time to check args and do the ops
if source not in seash_global_variables.targets:
raise seash_exceptions.UserError("Error, unknown target '"+source+"'")
if dest not in seash_global_variables.targets:
raise seash_exceptions.UserError("Error, unknown group '"+dest+"'")
if seash_helper.is_immutable_targetname(dest):
raise seash_exceptions.UserError("Can't modify the contents of '"+dest+"'")
# find the items to remove (the items in both dest and source)
removelist = listops.listops_intersect(seash_global_variables.targets[dest],seash_global_variables.targets[source])
if len(removelist) == 0:
raise seash_exceptions.UserError("No targets to remove (no items from '"+source+"' are in '"+dest+"')")
# it's okay to end up with an empty group. We'll leave it...
for item in removelist:
seash_global_variables.targets[dest].remove(item)
# move target to group
def move_target_to_group(input_dict, environment_dict):
command_key = input_dict.keys()[0]
# Iterates through the dictionary to retrieve the user's target name
while input_dict[command_key]['name'] is not 'target':
input_dict = input_dict[command_key]['children']
command_key = input_dict.keys()[0]
moving = command_key
source = environment_dict['currenttarget']
# Iterates through the dictionary to retrieve the user's group name
while input_dict[command_key]['name'] is not 'group':
input_dict = input_dict[command_key]['children']
command_key = input_dict.keys()[0]
dest = command_key
# check args...
if source not in seash_global_variables.targets:
raise seash_exceptions.UserError("Error, unknown group '"+source+"'")
if moving not in seash_global_variables.targets:
raise seash_exceptions.UserError("Error, unknown group '"+moving+"'")
if dest not in seash_global_variables.targets:
raise seash_exceptions.UserError("Error, unknown group '"+dest+"'")
if seash_helper.is_immutable_targetname(dest):
raise seash_exceptions.UserError("Can't modify the contents of '"+source+"'")
if seash_helper.is_immutable_targetname(dest):
raise seash_exceptions.UserError( "Can't modify the contents of '"+dest+"'")
removelist = listops.listops_intersect(seash_global_variables.targets[source], seash_global_variables.targets[moving])
if len(removelist) == 0:
raise seash_exceptions.UserError("Error, '"+moving+"' is not in '"+source+"'")
addlist = listops.listops_difference(removelist, seash_global_variables.targets[dest])
if len(addlist) == 0:
raise seash_exceptions.UserError("Error, the common items between '"+source+"' and '"+moving+"' are already in '"+dest+"'")
for item in removelist:
seash_global_variables.targets[source].remove(item)
for item in addlist:
seash_global_variables.targets[dest].append(item)
# contact host:port[:vessel] -- Communicate with a node
def contact(input_dict, environment_dict):
if environment_dict['currentkeyname'] == None or not seash_global_variables.keys[environment_dict['currentkeyname']]['publickey']:
raise seash_exceptions.UserError("Error, must contact as an identity")
command_key = input_dict.keys()[0]
# Iterates through the dictionary to retrieve the user's argument
while input_dict[command_key]['name'] is not 'args':
input_dict = input_dict[command_key]['children']
command_key = input_dict.keys()[0]
if len(command_key.split(':')) == 2:
environment_dict['host'], portstring = command_key.split(':')
environment_dict['port'] = int(portstring)
vesselname = None
elif len(command_key.split(':')) == 3:
environment_dict['host'], portstring,vesselname = command_key.split(':')
environment_dict['port'] = int(portstring)
else:
raise seash_exceptions.UserError("Error, usage is contact host:port[:vessel]")
# get information about the node's vessels
thishandle = nmclient.nmclient_createhandle(environment_dict['host'], environment_dict['port'], privatekey = seash_global_variables.keys[environment_dict['currentkeyname']]['privatekey'], publickey = seash_global_variables.keys[environment_dict['currentkeyname']]['publickey'], vesselid = vesselname, timeout = seash_global_variables.globalseashtimeout)
ownervessels, uservessels = nmclient.nmclient_listaccessiblevessels(thishandle,seash_global_variables.keys[environment_dict['currentkeyname']]['publickey'])
newidlist = []
# determine if we control the specified vessel...
if vesselname:
if vesselname in ownervessels or vesselname in uservessels:
longname = environment_dict['host']+":"+str(environment_dict['port'])+":"+vesselname
# no need to set the vesselname, we did so above...
id = seash_helper.add_vessel(longname,environment_dict['currentkeyname'],thishandle)
newidlist.append('%'+str(id)+"("+longname+")")
else:
raise seash_exceptions.UserError("Error, cannot access vessel '"+vesselname+"'")
# we should add anything we can access
else:
for vesselname in ownervessels:
longname = environment_dict['host']+":"+str(environment_dict['port'])+":"+vesselname
if longname not in seash_global_variables.targets:
# set the vesselname
# NOTE: we leak handles (no cleanup of thishandle).
# I think we don't care...
newhandle = nmclient.nmclient_duplicatehandle(thishandle)
environment_dict['handleinfo'] = nmclient.nmclient_get_handle_info(newhandle)
environment_dict['handleinfo']['vesselname'] = vesselname
nmclient.nmclient_set_handle_info(newhandle, environment_dict['handleinfo'])
id = seash_helper.add_vessel(longname,environment_dict['currentkeyname'],newhandle)
newidlist.append('%'+str(id)+"("+longname+")")
for vesselname in uservessels: