-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkhctl
executable file
·1452 lines (1253 loc) · 28.6 KB
/
khctl
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
#!/bin/bash
#set -x
#####################################
#
# IBM Coporation
# Project kittyhawk
#
# khctl simple inteface for managing
# nodes and networks
# main interface function is acquireNodes
#
#####################################
if [[ $BASH_VERSION != 3.1.* ]]; then shopt -s compat31; fi
PATH=$PATH:/sbin
declare -r KHBASEDIR=${KHCTL_BASEDIR:-/home/kh}
declare -r KHCONSDIR=${KHCTL_CONSDIR:-$KHBASEDIR/Consoles}
declare -r KHCTLDIR=${KHCTL_CTLDIR:-$KHBASEDIR/Ctl}
declare -r KHTUPLEDIR=${KH_TUPLEDIR:-$KHBASEDIR/KHTuples}
declare -r KHTUPLECTRLDIR=${KH_TUPLECTRLDIR:-$KHCTLDIR}
declare -r KHCONIDFILE=${KHCTL_CONIDFILE:-$KHCTLDIR/ConID}
declare -r KHCONIDSTART=${KHCTL_CONIDSTART:-2}
declare -r KHCONIDEND=${KHCTL_CONIDEND:-$(printf "%d" 0xffffffff)}
declare -r KHPNETIDFILE=${KHCTL_PNETIDFILE:-$KHCTLDIR/PNetID}
declare -r KHINETID=${KHCTL_INETID:-0}
declare -r KHXNETID=${KHCTL_XNETID:-1}
declare -r KHFREENETID=${KHCTL_FREENETID:-2}
declare -r KHPNETSRESERVED=${KHCTL_PNETSRESERVED:-3}
declare -r KHPNETIDSTART=${KHCTL_PNETIDSTART:-$KHPNETSRESERVED}
declare -r KHPNETIDEND=${KHCTL_PNETIDEND:-$(printf "%d" 0xffffffff)}
declare -r KHXNETMTUFILE=${KHCTL_XNETMTU:-/proc/device-tree/u-boot-env/sitemtu}
declare -r KHTTYPROCDIR=${KHCTL_TTYPROCDIR:-/proc/sys/dev/bgtty}
declare -r KHUSERPREFIX=${KHCTL_USERPREFIX:-con}
declare -r KHTOTALSIZEFILE=${KHCTL_TOTALSIZEFILE:-/proc/device-tree/u-boot-env/bgp_totalnodes}
declare -r KHRANKFILE=${KHCTL_RANKFILE:-/proc/device-tree/u-boot-env/bgp_rank}
declare -r KHCTRLTTY=${KHCTL_CTRLTTY:-/dev/bgtty0}
declare -r KHCTRLID=${KHCTL_CTRLID:-0}
declare -r KHTTYMAJOR=${KHCTL_TTYMAJOR:-230}
declare -r KHXIPFILE=${KHCTL_XIPFILE:-/proc/device-tree/u-boot-env/xip}
declare -r KHNATIPFILE=${KHCTL_NATFILE:-/proc/device-tree/u-boot-env/natip}
declare -r FREEOWNER='_FREE_'
declare -r FREEPNET=$KHFREENETID
declare -r FREEINETFLG=0
declare -r FREEXNETFLG=0
declare -r FREECONID=$KHCTRLID
declare BUFFERVAR
declare -i BUFFERTTY=1
typeset dofunc
function getNatIP
{
if [[ -a $KHNATIPFILE ]]; then
cat $KHNATIPFILE
fi
}
function myIPAddrs
{
local net
local ip
local rest
local rc
# If no argument all configured ip address are returned
# otherwise only for the speccified interface
ifconfig $1 2> /dev/null | grep 'inet addr' | while read net ip rest
do
ip=${ip##addr:}
if [[ $ip != 127.0.0.1 ]]
then
echo $ip
fi
done
}
function myInterfaces
{
local iface
local rest
ifconfig | grep Link | while read iface rest
do
echo $iface
done
}
function interfaceAddr
{
local iface
local ip
if [[ -z $1 ]]
then
echo "USAGE: internfaceAddr <ip>" >&2
return -1
fi
for iface in $(myInterfaces)
do
ip=$(ifconfig $iface | grep 'inet addr')
ip=${ip#*:}
ip=${ip%% *}
if [[ $ip = "$1"* ]]
then
echo $iface
return 0
fi
done
return -1
}
ttyRawWrite()
{
local data="$1"
local tty="$2"
echo "$data" > $tty
sleep 0.05
}
writeTTY()
{
local data="$1"
local tty="$2"
if (( $BUFFERTTY == 0 ))
then
ttyRawWrite "$data" $tty
else
TTYBUFFERVAR="${TTYBUFFERVAR}$data
"
fi
}
getRespfile()
{
echo /tmp/$$.respfile.$RANDOM
}
readUbootResponse()
{
# set -x
local tty=$1
local num=$2
local respfile=$3
local -i c=0
local response
if [[ ! -c $tty || -z $num || -z $respfile ]]
then
echo "USAGE: $FUNCNAME <tty> <num> <respfile>"
exit -1
fi
if [[ -e $respfile ]]
then
if ! rm $respfile
then
echo "ERROR: $FUNCNAME: could not remove $respfile"
return -1
fi
fi
touch ${respfile}.running
while read line
do
if [[ "$line" =~ "^nodeinfo: .*" ]]
then
response="$response$line\n"
(( c++ ))
if (( $c == $num ))
then
break
fi
fi
done < $tty
printf "$response" > $respfile
rm ${respfile}.running
return 0
}
gatherUbootResponse()
{
# set -x
local tty=$1
local num=$2
local respfile=$3
local readpid
if [[ ! -c $tty || -z $num || -z $respfile ]]
then
echo "USAGE: $FUNCNAME <tty> <num> <respfile>" >&2
return -1
fi
if [[ -a ${respfile}.running ]]
then
rm ${respfile}.running
fi
readUbootResponse $tty $num $respfile &
# give the child some time to start and get the tty open
# before we do any writes so that we capture all responses
while [[ ! -a ${respfile}.running ]]
do
sleep 0.5
done
readpid=$(jobs -p %%)
disown %%
echo $readpid > $respfile.pid
return 0
}
waitForUbootResponse()
{
local respfile=$1
local pollcnt=$2
local workerpid
local -i i
if [[ -z $respfile || -z $pollcnt ]]
then
echo "USAGE: $FUNCNAME <respfile> <pollcnt>"
return -1
fi
if [[ ! -e $respfile.pid ]]
then
echo "ERROR: could not fie $respfile.pid"
return -1
fi
workerpid=$(cat $respfile.pid)
rm $respfile.pid
if [[ -e $respfile ]]
then
return 0
fi
(( i=0 ))
while (( $i < $pollcnt ))
do
if [[ -e $respfile ]]
then
return 0
fi
(( i++ ))
sleep 0.05
done
if [[ -n $workerpid ]]
then
kill $workerpid >& /dev/null
fi
return -1
}
function getUbootNodeInfo
{
local tty=$1
local num=$2
local extra=$3
local respfile
local i
if [[ ! -c $tty || -z $num ]]
then
echo "USAGE: $FUNCNAME <tty> <num> [extra info to get]"
exit -1
fi
respfile=$(getRespfile)
(( i=0 ))
while (( $i<10 ))
do
gatherUbootResponse $tty $num $respfile
# bypass buffering and write directly to the tty
ttyRawWrite "echo nodeinfo: \$bgp_rank \$uni $extra" $tty
if waitForUbootResponse $respfile 1000
then
cat $respfile | grep '^nodeinfo'
rm $respfile
return 0
fi
(( i++ ))
done
if [[ -e $respfile ]]
then
rm $respfile
fi
return -1
}
clearTTYBuffer()
{
TTYBUFFERVAR=""
}
flushTTY()
{
local tty=$1
if (( $BUFFERTTY == 1 ))
then
ttyRawWrite "$TTYBUFFERVAR" $tty
# echo "$TTYBUFFERVAR" >> /tmp/TTYFlushBuffer
sleep 0.05
fi
}
isquish()
{
#set -x
local -i val
local -i min
local -i max
local -i started=0
local valuefmtstring="%d\n"
local rangefmtstring="%d %d\n"
if [[ -n "$1" ]]
then
valuefmtstring="$1"
fi
if [[ -n "$2" ]]
then
rangefmtstring="$2"
fi
while read val
do
if (( $started == 0 ))
then
((min=$val))
((max=$val))
((started=1))
else
if (( $val == ($max + 1) ))
then
((max=$val))
else
if (( $min == $max ))
then
printf "$valuefmtstring" "$min"
else
printf "$rangefmtstring" "$min" "$max"
fi
(( min=$val ))
(( max=$val ))
fi
fi
done
if (( $started == 1 ))
then
if (( $min == $max ))
then
printf "$valuefmtstring" "$min"
else
printf "$rangefmtstring" "$min" "$max"
fi
fi
return 0
}
configNodes()
{
local khuser=$1
local pnetids=$2
local inet=$3
local xnet=$4
local conid=$5
local num=$6
local nodes="$7"
local dir=$KHCONSDIR/$conid
local tty=$dir/tty
local n
local -i p
local -i j
local -i i
local -i ethoffset
local inetdesc
local tmp
local ubootnodeinfo
local tmpnodes
local itestlines
local itestline
local myiip
local xmtu
if [[ ! -c $KHCTRLTTY ]]
then
if [[ -a $KHCTRLTTY ]]; then rm -rf $KHCTRLTTY; fi
mknod $KHCTRLTTY c $KHTTYMAJOR $KHCTRLID
stty raw -clocal cread -echo -echok -echoe < $KHCTRLTTY
fi
clearTTYBuffer
tmpnodes="$(for n in $nodes; do echo $n; done)"
itestlines="$(echo "$tmpnodes" | isquish "itest \$bgp_rank == %d && setenv bgtty_sendid $conid && setenv bgtty_rcvid $conid" "itest \$bgp_rank >= %d && itest \$bgp_rank <= %d && setenv bgtty_sendid $conid && setenv bgtty_rcvid $conid")"
for itestline in "$itestlines"
do
writeTTY "$itestline" $KHCTRLTTY
done
flushTTY $KHCTRLTTY
clearTTYBuffer
# make sure all nodes have swung over to $tty
ubootnodeinfo=$(getUbootNodeInfo $tty $num)
if (( $? != 0 ))
then
echo "ERROR: $FUNCNAME: getUbootNodeInfo failed to confirm nodes allocated\n" >&2
fi
ethoffset=0
if (( $xnet == 1 ))
then
(( ethoffset++ ))
fi
if (( $inet == 1 ))
then
(( ethoffset++ ))
fi
j=2
tmp=( ${pnetids//,/ } )
i=${#tmp[@]}
(( i-- ))
while (( $i >= 0 ))
do
p=${tmp[$i]}
writeTTY "mketh $p $j" $tty
(( j++ ))
(( i-- ))
done
writeTTY "fdt rm /plb/ethernet@1" $tty
if (( $inet == 1 ))
then
writeTTY "mketh 2 1" $tty
if (( $ethoffset == 1 ))
then
inetdesc="network Internal : eth0"
writeTTY "setenv bg_eth0_netid 2" $tty
else
inetdesc="network Internal : eth1"
writeTTY "setenv bg_eth1_netid 2" $tty
fi
fi
writeTTY "fdt rm /plb/ethernet@0" $tty
if (( $xnet == 1 ))
then
if [[ -a $KHXNETMTUFILE ]]
then
xmtu=$(cat $KHXNETMTUFILE)
fi
writeTTY "mketh 1 0 $xmtu" $tty
writeTTY "setenv bg_eth0_netid 1" $tty
echo "network External : eth0"
fi
if (( $inet == 1 ))
then
echo "$inetdesc"
fi
j=$ethoffset
for p in ${pnetids//,/ }
do
writeTTY "setenv bg_eth${j}_netid $p" $tty
echo "network $p : eth${j}"
(( j++ ))
done
writeTTY "setenv bg_num_netids $j" $tty
myiip=$(myIPAddrs eth1)
if [[ -n "$myiip" ]]
then
writeTTY "setenv khctlserver $myiip" $tty
fi
writeTTY "setenv khctluser $khuser" $tty
#kludge for demo
writeTTY "itest \$demoenv == 1 && run dosettmpboot" $tty
flushTTY $tty
#added eth0ip as a kludge to make demo easier
if [[ -n $(getNatIP) ]]; then
ubootnodeinfo=$(getUbootNodeInfo $tty $num "\$eth0ip \$ionode_uni \$ionode_siteip \$bgp_rankinpset")
else
ubootnodeinfo=$(getUbootNodeInfo $tty $num "\$eth0ip")
fi
if (( $? != 0 ))
then
echo "ERROR: $FUNCNAME: getUbootNodeInfo failed to confirm nodes configured\n" >&2
fi
echo "$ubootnodeinfo"
return 0
}
function createCon
{
local khuser=$1
local conid=$2
local nodes="$3"
local dir=$KHCONSDIR/$conid
local tty=$dir/tty
local procfile=$KHTTYPROCDIR/$conid
if [[ -d $dir ]]
then
echo "ERROR: $dir already exists?!?" >&2
return -1
fi
mkdir -p $dir
echo "$nodes" > $dir/nodes
echo "$khuser" > $dir/owner
mknod $tty c $KHTTYMAJOR $conid
stty raw -clocal cread -echo -echok -echoe < $tty
if (( $num == 1 ))
then
echo "FIXME: using broadcast just for one node :-(" >&2
echo "$conid $conid b" > $procfile
else
echo "$conid $conid b" > $procfile
fi
return 0
}
function createConUser
{
local khuser=$1
local pnetid=$2
local conid=$3
local num=$4
local nodes="$5"
local dir=$KHCONSDIR/$conid
local conuser=$KHUSERPREFIX${conid}
local home=$dir/$conuser
local shell=$dir/conshell
local tty=$dir/tty
if [[ ! -d $dir ]]
then
echo "ERROR: createConUser: $dir does not exist?!?" >&2
return -1
fi
if ! mkdir $home
then
echo "ERROR: createConUser: could not make $home?!?" >&2
return -1
fi
if grep "^$conuser:" /etc/passwd
then
echo "ERROR: $conuser already in /etc/passwd" >&2
return -1
fi
# FIXME: Enable this when ready to try credentials
# cred=$(locked_ls_users "$khuser" "*")
# cred=${cred##*:}
# mkdir -p $home/.ssh
# echo "$cred" > $home/.ssh/authorizedkeys
cat >$shell <<EOF
#!/bin/bash
stty raw -clocal cread -echo -echok -echoe < $tty
if [[ \$TERM = KHREAD ]]
then
exec cat $tty
fi
if tty > /dev/null
then
# getting closer but still needs more
# stty raw -clocal cread -echo -echok -echoe
echo "$conuser: Console"
cat $tty > /dev/tty &
fi
cat > $tty
EOF
echo "$conuser::65534:65534:nobody:$home:$shell" >> /etc/passwd
chown $conuser $tty
chmod u+x $shell
chown $conuser $shell
}
assignNodes()
{
local khuser=$1
local pnetid=$2
local inet=$3
local xnet=$4
local conid=$5
local numNodes=$6
local nodes=( $7 )
local conuser=$KHUSERPREFIX${conid}
local conip
local i
local ip
local configdesc
local natip
if createCon $khuser $conid "${nodes[*]}"
then
if createConUser $khuser $pnetid $conid $num "${nodes[*]}"
then
configdesc=$(configNodes $khuser $pnetid $inet $xnet $conid $num "${nodes[*]}")
if (( $? == 0 ))
then
if [[ -n $SSH_CONNECTION ]]
then
conip=${SSH_CONNECTION% *}
conip=${conip##* }
natip=$(getNatIP)
if [[ -n $natip ]]; then
if [[ $conip = $(cat $KHXIPFILE) ]]; then
conip=$natip
fi
fi
echo $conuser@$conip
else
for i in $(myInterfaces)
do
ip=$(myIPAddrs $i)
if [[ -n $ip ]]
then
echo $conuser@$ip
fi
done
fi
echo "protocol: uboot"
echo "$configdesc"
echo "nodes: ${nodes[*]}"
return 0
fi
fi
fi
return -1
}
function getTotalNodes
{
local rc
if [[ -a $KHTOTALSIZEFILE ]]
then
rc=$(cat $KHTOTALSIZEFILE)
fi
echo $rc
}
function getMyRank
{
local rc
if [[ -a $KHRANKFILE ]]
then
rc=$(cat $KHRANKFILE)
fi
echo $rc
}
#
# FIXME: This does not handle roleover and not sure it behaves well
# with really big allocs
#
locked_getNewPNetIDs()
{
local num=$1
local rc
if [[ -z $num ]]
then
num=1
fi
if [[ ! -e $KHPNETIDFILE ]]
then
echo $KHPNETIDSTART > $KHPNETIDFILE
fi
rc=$(cat $KHPNETIDFILE)
if (( ($rc + ($num-1)) > $KHPNETIDEND ))
then
echo "ERROR: locked_getNewPNetIDs: not enough id : NYI" >&2
return -1
fi
echo $(( $rc + $num )) > $KHPNETIDFILE
if (( $num > 1 ))
then
eval echo {${rc}..$(( $rc + ($num-1) ))}
else
echo $rc
fi
return 0
}
locked_undoPnetIDs()
{
local num=$1
local rc
local cur
if [[ -z $num ]]
then
echo "USAGE: $FUNCNAME: <num>" >&2
return -1
fi
if [[ -e $KHPNETIDFILE ]]
then
cur=$(cat $KHPNETIDFILE)
echo $(( $cur - $num )) > $KHPNETIDFILE
return 0
fi
return -1
}
locked_getNewConID()
{
local rc
if [[ ! -e $KHCONIDFILE ]]
then
echo $KHCONIDSTART > $KHCONIDFILE
fi
rc=$(cat $KHCONIDFILE)
if (( $rc == $KHCONIDEND ))
then
echo "ERROR: locked_getNewConID: last id reached: NYI" >&2
return -1
fi
echo $(( $rc + 1 )) > $KHCONIDFILE
echo $rc
return 0
}
locked_undoConID()
{
local conid=$1
local rc
local cur
if [[ -e $KHCONIDFILE ]]
then
cur=$(cat $KHCONIDFILE)
# sanity check
if (( ($conid + 1) == $cur ))
then
echo $conid > $KHCONIDFILE
return 0
fi
fi
return -1
}
function lock
{
local lockname=$1
local lockfile=$KHTUPLECTRLDIR/${lockname}.lock
if [[ -z $lockname ]]
then
echo "USAGE: $FUNCNAME <lock>" >&2
return -1
fi
if [[ ! -d $KHTUPLECTRLDIR ]]
then
if ! mkdir -p $KHTUPLECTRLDIR
then
echo "ERROR: $FUNCNAME: $KHTUPLECTRLDIR does not exist "\
"and cannot be created" >&2
exit -1
fi
fi
# Acquire lock
lockfile -s1 $lockfile
}
function unlock
{
local lockname=$1
local lockfile=$KHTUPLECTRLDIR/${lockname}.lock
if [[ -z $lockname ]]
then
echo "USAGE: $FUNCNAME <lock>" >&2
return -1
fi
# Release lock
rm -f $lockfile
}
function locked_unchecked_tupleCreateType
{
local type=$1
local typedir=$KHTUPLEDIR/$type
if [[ -d $typedir ]]
then
echo "ERROR: $FUNCNAME: $typedir already exists" >&2
return -1
fi
if ! mkdir -p $typedir
then
echo "ERROR: $FUNCNAME: could not create typedir" >&2
return -1
fi
}
function tupleCreateType
{
local type=$1
if [[ -z $type ]]
then
echo "USAGE: $FUNCNAME <type>" >&2
return -1
fi
lock tuples
locked_unchecked_tupleCreateType $type
unlock tuples
return 0
}
locked_unchecked_tupleTypeExists()
{
local type=$1
local typedir=$KHTUPLEDIR/$type
if [[ -d $typedir ]]
then
return 0
else
return -1
fi
}
function tupleTypeExists
{
local type=$1
local rc=0
if [[ -z $type ]]
then
echo "USAGE: $FUNCNAME <type>" >&2
return -1
fi
lock tuples
locked_unchecked_tupleTypeExists $type
rc=$?
unlock tuples
return $rc
}
locked_unchecked_tupleCmd()
{
local cmd=$1
local type=$2
local tuplepattern=$3
local rc=0
cd $KHTUPLEDIR/$type
if eval $cmd $tuplepattern 2>/dev/null
then
rc=0
else
# eval echo "ERROR: $FUNCNAME: $cmd $typedir/$tuplepattern: FAILED" >&2
rc=-1
fi
return $rc
}
function tupleCmd
{
local cmd=$1
local type=$2
local tuplepattern=$3
local rc=0
if [[ -z $cmd || -z $type || -z $tuplepattern ]]
then
echo "USAGE: $FUNCNAME <cmd> <type> <tuplepattern>" >&2
return -1
fi
type -a $cmd >/dev/null 2>&1
if (( $? != 0 ))
then
echo "ERROR: $FUNCNAME: $cmd is not a executable command" >&2
return -1
fi
lock tuples
if locked_unchecked_tupleTypeExists $type
then
locked_unchecked_tupleCmd $cmd $type "$tuplepattern"
rc=$?
else
echo "ERROR: $FUNCNAME: Tuple type $type does not exist"
rc=-1
fi
unlock tuples
return $rc
}
#
# FIXME: This is stupid and expensive on big lists
# fix this to improve performance
#
function commalisttopat
{
local cl="$1"
local a
if [[ -n $cl ]]
then
a=( ${cl//,/ } )
if (( ${#a[@]} == 1 ))
then
echo "$cl"
else
echo "{$cl}"
fi
fi
}
function validkhuser
{
local khuser=$1
if [[ "$khuser" =~ "^[a-zA-Z]+[a-zA-Z0-9]*$" ]]
then
return 0
else
return -1
fi
}
### Node nodeid:owner:pnetids:inet:xnet:consid ###
function locked_touch_nodes
{
local nodeidpat="$1"
local ownerpat="$2"
local pnetidpat="$3"
local inetflgpat="$4"
local xnetflgpat="$5"
local considpat="$6"
if [[ -z $nodeidpat || -z $ownerpat || -z $pnetidpat || -z $inetflgpat ||
-z $xnetflgpat || -z $considpat ]]
then
echo "USAGE: $FUNCNAME <nodeidpat> <ownerpat> <pnetidpat> <inetflgpat>"\
" <xnetflgpat> <considpat>"
return -1
fi
locked_unchecked_tupleCmd touch Node \
"$nodeidpat:$ownerpat:$pnetidpat:$inetflgpat:$xnetflgpat:$considpat"
return $?
}
### Node nodeid:owner:pnetids:inet:xnet:consid ###