-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatcher.py
More file actions
1454 lines (1266 loc) · 47.4 KB
/
Copy pathpatcher.py
File metadata and controls
1454 lines (1266 loc) · 47.4 KB
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
import logging
import os
import shutil
from base import *
STRICT_PARSE = True
def format_device_name( deviceList ):
if deviceList:
nlist = list()
for de in deviceList:
if de == 'se':
de = 'service-engine'
elif de == 'sr':
de = 'service-router'
elif de == 'cdsm':
de = 'content-delivery-system-manager'
nlist.append( de )
return nlist
return None
def gen_md5sum_value( spath ):
cmd = 'md5sum ' + spath
logging.debug( 'popen cmd:'+cmd )
fin = os.popen( cmd, 'r' )
md5sum = ''
for line in fin:
logging.debug( line )
segs = line.split()
md5sum = segs[0]
break
fin.close()
return md5sum
def gen_check_version_func( fout ):
lines = 'function check_version() {\n'
lines += ' local target_version="$1"\n'
lines += ' cds_version=$(/ruby/bin/exec -c "show version" | grep "Content Delivery System Software Release" | awk \'{ print $6$8 }\')\n'
lines += ' if [[ "$cds_version" = "$target_version" ]]; then\n'
lines += ' echo "Current cds-is version $cds_version"\n'
lines += ' return 0\n'
lines += ' else\n'
lines += ' echo "Patch is only applied to cds-is $target_version (but current is $cds_version), aborting."\n'
lines += ' return 23\n'
lines += ' fi\n'
lines += '}\n'
lines += ' \n'
fout.write( lines )
def gen_mount_sw( fout, lhead, perm, sleepTime=10 ):
lines = lhead + 'mount -n -o remount,' + perm + ' /sw\n'
lines += lhead + 'if [ $? -ne 0 ]; then\n'
lines += lhead + ' echo "ERROR: unable to remount partition as ' + perm + ' mode"\n'
lines += lhead + ' echo "Please try the patch later or reload the device and retry."\n'
lines += lhead + ' exit 28\n'
lines += lhead + 'fi\n'
lines += lhead + 'sleep ' + str(sleepTime) + '\n'
fout.write( lines )
def __gen_stop_service_func( self, fout ):
lines = 'function stop_service(){\n'
lines += ' ##### check and stop\n'
lines += ' local tarsvc="$1"\n'
lines += ' local tarproc="$2"\n'
lines += ' local stime=$3\n'
lines += ' echo "stopping service $tarsvc..."\n'
lines += ' /ruby/bin/nodemgr_clt stop $tarsvc\n'
lines += ' sleep $stime\n\n'
lines += ' pidof $tarproc > /dev/null\n'
lines += ' pidof_rc=$?\n'
lines += ' if [ $pidof_rc -eq 0 ]; then\n'
lines += ' echo "ERROR: process $tarproc not stopped"\n'
lines += ' echo "please check the $tarproc process and retry the patch later."\n'
lines += ' echo "exit 25"\n'
lines += ' else\n'
lines += ' echo "process stopped, go on..."\n'
lines += ' fi\n'
lines += '}\n'
fout.write( lines )
def __gen_start_service_func( self, fout ):
lines = 'function start_service(){\n'
lines += ' ##### check and start\n'
lines += ' local tarsvc="$1"\n'
lines += ' local tarproc="$2"\n'
lines += ' local stime=$3\n'
lines += ' echo "starting service $tarsvc..."\n'
lines += ' /ruby/bin/nodemgr_clt start $tarsvc\n'
lines += ' sleep $stime\n\n'
lines += ' pidof $tarproc > /dev/null\n'
lines += ' pidof_rc=$?\n'
lines += ' if [ $pidof_rc -eq 0 ]; then\n'
lines += ' echo "process restarted, patching succeeded on this."\n'
lines += ' else\n'
lines += ' echo "ERROR: process $tarproc restarting failed"\n'
lines += ' echo "please revert the patch and check the $tarproc process"\n'
lines += ' echo "exit 26"\n'
lines += ' fi\n'
lines += '}\n'
fout.write( lines )
def gen_check_device_mode_func( fout, lhead ):
lines = lhead + 'function check_device_mode() {\n'
lines += lhead + ' local setMode="$1"\n'
lines += lhead + ' local device_mode=$(/ruby/bin/exec -c "show device-mode current" | grep mode | awk \'{ print $4 }\')\n'
lines += lhead + ' if [[ "$device_mode" = "$setMode" ]]; then\n'
lines += lhead + ' echo "Current device mode $device_mode"\n'
lines += lhead + ' return 1 \n'
lines += lhead + ' else\n'
lines += lhead + ' #echo "Patch is not allowed to install in $device_mode"\n'
lines += lhead + ' return 0\n'
lines += lhead + ' fi\n'
lines += lhead + '}\n'
fout.write( lines )
def gen_check_device_mode( fout, lhead, deviceList ):
if not deviceList:
return
lines = ''
lines += lhead + 'echo ""\n'
lines += lhead + 'local isRightMode=0\n'
lines += lhead + 'local allowMode=0\n'
lines += lhead + 'echo "checking device mode, allowed:' + str(deviceList) + '"\n'
for de in deviceList:
lines += lhead + 'if [ $isRightMode -eq 0 ]; then\n'
lines += lhead + ' check_device_mode "' + de + '"\n'
lines += lhead + ' isRightMode=$?\n'
lines += lhead + ' if [ $isRightMode -eq 1 ]; then\n'
lines += lhead + ' echo "patch will install for ' + de + '"\n'
lines += lhead + ' allowMode=1\n'
lines += lhead + ' fi\n'
lines += lhead + 'fi\n'
lines += lhead + 'if [ $allowMode -eq 0 ]; then\n'
lines += lhead + ' echo "patch is not allowed for this device mode"\n'
lines += lhead + ' /ruby/bin/exec -c "show device-mode current"\n'
lines += lhead + ' exit 44\n'
lines += lhead + 'fi\n'
fout.write( lines )
def gen_check_service_func( fout, lhead ):
lines = lhead + 'function check_service(){\n'
lines += lhead + ' local cmd="$1"\n'
lines += lhead + ' local gstr="$2"\n'
lines += lhead + ' ret=`/sw/merlot/bin/runExecCLI $cmd | grep "$gstr"`\n'
lines += lhead + ' echo "$ret"\n'
lines += lhead + ' if [ -z "$ret" ]; then\n'
lines += lhead + ' return 0\n'
lines += lhead + ' else\n'
lines += lhead + ' return 1\n'
lines += lhead + ' fi\n'
lines += lhead + '}\n\n'
fout.write( lines )
def gen_check_proc_started( fout, lhead, proc, isKeyword, svc=None ):
lines = ''
if isKeyword:
lines += lhead + 'ret=`ps aux | grep "' + proc + '" | grep -v "grep"`\n'
else:
lines += lhead + 'ret=`pidof ' + proc + '`\n'
lines += lhead + 'if [ -z "$ret" ]; then\n'
if svc:
lines += lhead + ' echo "ERROR: ' + proc + ' retarting failed in service:' + svc + ' with ret:$ret"\n'
else:
lines += lhead + ' echo "ERROR: ' + proc + ' retarting failed with ret:$ret"\n'
lines += lhead + ' echo "Please run patch revert and then check the ' + proc + ' proccess."\n'
lines += lhead + ' exit 26\n'
lines += lhead + 'else\n'
lines += lhead + ' echo "Process ' + proc + ' started, go on..."\n'
lines += lhead + 'fi\n\n'
fout.write( lines )
def gen_check_proc_stopped( fout, lhead, proc, isKeyword, svc=None ):
lines = ''
if isKeyword:
lines += lhead + 'ret=`ps aux | grep "' + proc + '" | grep -v "grep"`\n'
else:
lines += lhead + 'ret=`pidof ' + proc + '`\n'
lines += lhead + 'if [ -z "$ret" ]; then\n'
lines += lhead + ' echo "Process ' + proc + ' stopped, go on..."\n'
lines += lhead + 'else\n'
if svc:
lines += lhead + ' echo "ERROR: ' + proc + ' stopping failed in service:' + svc + ' with ret:$ret"\n'
else:
lines += lhead + ' echo "ERROR: ' + proc + ' stopping failed with ret:$ret"\n'
lines += lhead + ' echo "Please check the ' + proc + ' process and retry the patch later."\n'
lines += lhead + ' exit 25\n'
lines += lhead + 'fi\n\n'
fout.write( lines )
class ServiceCheck( BaseObject ):
def __init__( self, parent ):
self.cli = None
self.enable = None
self.disable = None
def init_config( self, parent ):
if not self.cli:
return False
if not self.enale and not self.disable:
return False
return True
class Service( BaseObject ):
def __init__( self, parent ):
self.name = None
self.waitTime = parent.waitTime
self.processList = None
self.check = None
def add_process( self, process, isKeyword, checkType ):
if self.processList is None:
self.processList = list()
self.processList.append( (process, isKeyword, checkType) )
def init_config( self, parent ):
if self.name is None:
logging.error( 'servie name not set' )
return False
return True
def gen_start( self, fout, lhead ):
lines = ''
if self.check:
lines += lhead + 'if [ $' + self.name + '_enable -eq 1 ]; then\n'
lines += lhead + 'echo "starting service:' + self.name + '..."\n'
lines += lhead + ' /ruby/bin/nodemgr_clt start ' + self.name + '\n'
lines += lhead + 'else\n'
lines += lhead + ' echo "' + self.name + ' is disabled, no need to start"\n'
lines += lhead + 'fi\n'
else:
lines += lhead + 'echo "starting service:' + self.name + '..."\n'
lines += lhead + '/ruby/bin/nodemgr_clt start ' + self.name + '\n'
fout.write( lines )
def gen_check_service( self, fout, lhead ):
lines = ''
if self.check:
lines += lhead + 'echo "check service:' + self.name + '"...\n'
cstr = self.check.enable
if not cstr:
cstr = self.check.disable
lines += lhead + 'check_service "' + self.check.cli + '" "' + cstr + '"\n'
if self.check.enable:
lines += lhead + self.name + '_enable=$?\n'
else:
lines += lhead + self.name + '_enable=$((1-$?))\n'
lines += lhead + 'if [ $' + self.name + '_enable -eq 1 ]; then\n'
lines += lhead + ' echo "' + self.name + ' is enabled"\n'
lines += lhead + 'else\n'
lines += lhead + ' echo "' + self.name + ' is disabled"\n'
lines += lhead + 'fi\n'
fout.write( lines )
def gen_stop( self, fout, lhead ):
lines = ''
lines += lhead + 'echo "stopping service:' + self.name + '..."\n'
lines += lhead + '/ruby/bin/nodemgr_clt stop ' + self.name + '\n'
fout.write( lines )
def gen_check_started( self, fout, lhead, cmdType ):
if not self.processList:
return
ihead = lhead
if self.check:
lines = lhead + 'if [ $' + self.name + '_enable -eq 1 ]; then\n'
fout.write( lines )
ihead += '\t'
checkCmd = 'apply'
if cmdType == 'apply':
checkCmd = 'revert'
for (proc, isKeyword, checkType) in self.processList:
if checkType.find('stop') < 0 and checkType.find(checkCmd) < 0:
gen_check_proc_started( fout, ihead, proc, isKeyword, self.name )
if self.check:
lines = lhead + 'else\n'
lines += lhead + ' echo "' + self.name + ' is disabled, no need to check"\n'
lines += lhead + 'fi\n'
fout.write( lines )
def gen_check_stopped( self, fout, lhead, cmdType ):
if not self.processList:
return
checkCmd = 'apply'
if cmdType == 'apply':
checkCmd = 'revert'
for (proc, isKeyword, checkType) in self.processList:
if checkType.find('start') < 0 and checkType.find(checkCmd) < 0:
gen_check_proc_stopped( fout, lhead, proc, isKeyword, self.name )
class Process( Service ):
def __init__( self, parent ):
super(Process, self).__init__( parent )
#self.name = None
self.startCmd = None
self.stopCmd = None
def init_config( self, parent ):
if self.name is None:
logging.error( 'process name not set' )
return False
if self.startCmd is None:
logging.error( 'startCmd not set' )
return False
if self.stopCmd is None:
self.stopCmd = 'kill -9 ' + self.name
return True
def gen_start( self, fout, lhead ):
lines = ''
if self.check:
lines += lhead + 'if [ $' + self.name + '_enable -eq 1 ]; then\n'
lines += lhead + ' echo "starting process:' + self.name + '..."\n'
lines += lhead + '\t' + self.startCmd + '\n'
lines += lhead + 'else\n'
lines += lhead + ' echo "' + self.name + ' is disabled, no need to start"\n'
lines += lhead + 'fi\n'
else:
lines += lhead + 'echo "starting process:' + self.name + '..."\n'
lines += lhead + self.startCmd + '\n'
fout.write( lines )
def gen_stop( self, fout, lhead ):
lines = lhead + self.stopCmd + '\n'
fout.write( lines )
def gen_check_started( self, fout, lhead, cmdType ):
ihead = lhead
if self.check:
lines = lhead + 'if [ $' + self.name + '_enable -eq 1 ]; then\n'
fout.write( lines )
ihead += '\t'
gen_check_proc_started( fout, ihead, self.name, False )
if self.check:
lines = lhead + 'else\n'
lines += lhead + ' echo "' + self.name + ' is disabled, no need to check"\n'
lines += lhead + 'fi\n'
fout.write( lines )
def gen_check_stopped( self, fout, lhead, cmdType ):
gen_check_proc_stopped( fout, lhead, self.name, False )
class ServiceBlock( BaseObject ):
def __init__( self ):
self.services = list()
self.waitTime = 10 #seconds
def is_empty( self ):
return len(self.services) == 0
def add_service( self, service ):
self.services.append( (service, True) )
def add_process( self, process ):
self.services.append( (process, False) )
def init_config( self, parent ):
slist = list()
for (service, isSer) in self.services:
if service.init_config(self):
slist.append( (service, isSer) )
logging.debug( 'inited one service:'+str(service) )
else:
logging.error( 'invalid service'+str(service) )
if STRICT_PARSE:
return False
self.services = slist
return len(self.services) > 0
def gen_start( self, fout, lhead, cmdType ):
lines = lhead + 'echo "try starting services..."\n'
fout.write( lines )
idx = len(self.services) - 1
while idx >= 0:
(service, isSer) = self.services[idx]
idx -= 1
service.gen_start( fout, lhead )
lines = lhead + 'sleep ' + str(self.waitTime) + '\n'
lines += lhead + 'echo ""\n'
lines += lhead + 'echo ""\n'
lines += lhead + 'echo "checking if services started..."\n'
fout.write( lines )
idx = len(self.services) - 1
while idx >= 0:
(service, isSer) = self.services[idx]
idx -= 1
service.gen_check_started( fout, lhead, cmdType )
def gen_stop( self, fout, lhead, cmdType ):
for (service, isSer) in self.services:
service.gen_check_service( fout, lhead )
lines = lhead + 'echo "try stopping services..."\n'
fout.write( lines )
for (service, isSer) in self.services:
service.gen_stop( fout, lhead )
lines = lhead + 'sleep ' + str(self.waitTime) + '\n'
lines += lhead + 'echo ""\n'
lines += lhead + 'echo ""\n'
lines += lhead + 'echo "checking if services stopped..."\n'
fout.write( lines )
for (service, isSer) in self.services:
service.gen_check_stopped( fout, lhead, cmdType )
class Binary( BaseObject ):
def __init__( self, parent ):
self.src = None
self.dst = None
self.dstfile = None
self.itype = None
class BinBlock( BaseObject ):
def __init__( self, parent, defSrcDir=None, defDstDir=None ):
self.defSrcDir = defSrcDir
self.defDstDir = defDstDir
self.binList = list()
self.isInSW = False
self.specialList = None
def add_binary( self, binary ):
self.binList.append( binary )
def init_config( self, parent ):
if self.defSrcDir is None:
self.defSrcDir = '/sw/unicorn/bin'
if self.defDstDir is None:
self.defDstDir = '/sw/unicorn/bin'
bmap = dict()
for binary in self.binList:
if not self.__init_bin( bmap, binary ):
if STRICT_PARSE:
return False
self.binList = bmap.values()
if not self.binList or len(self.binList) == 0:
logging.error( 'no available binary set in this block:'+str(self) )
return False
#check if is in /sw
for binary in self.binList:
dst = binary.dst
if dst.startswith( '/sw' ):
self.isInSW = True
break
return True
def __init_bin( self, bmap, binary ):
src = binary.src
if src is None or len(src) == 0:
return False
dst = binary.dst
#if indicate the source files
if os.path.exists( src ):
if os.path.isfile(src):
if dst is None:
dst = self.defDstDir
bname = os.path.basename( src )
if binary.dstfile:
dst = binary.dstfile
else:
dst = os.path.join( dst, bname )
binary.src = src
binary.dst = dst
bmap[dst] = binary
logging.debug( 'inited one binary:'+str(binary) )
else:
if dst is None:
logging.error( 'you must set the dst element for directory binary:'+str(binary) )
return False
#include many files here
for fname in os.listdir(src):
fpath = os.path.join( src, fname )
if os.path.isdir(fpath):
continue
#the dst must be a directory
#we won't consider dstfile here, as the src is a directory
cdst = os.path.join( dst, fname )
subBin = Binary( self )
subBin.src = fpath
subBin.dst = cdst
subBin.itype = binary.itype
bmap[cdst] = subBin
logging.debug( 'inited one sub binary:'+str(subBin) )
#if not set the source files, it means only set the source name
#try to get the source file from the default directory
else:
if dst is None:
dst = self.defDstDir
src = os.path.join( self.defSrcDir, src )
if not os.path.exists( src ):
logging.error( 'source file not exist:'+src )
return False
if binary.dstfile:
dst = binary.dstfile
else:
dst = os.path.join( dst, bname )
binary.src = src
binary.dst = dst
bmap[dst] = binary
logging.debug( 'inited one binary:'+str(binary) )
return True
def gen_file_vars( self, fout, lhead, offset ):
lines = ''
idx = offset
self.specialList = list()
for bnry in self.binList:
if bnry.itype:
#this is special binary, need special way to install it
self.specialList.append( bnry )
if bnry.itype == 'new':
continue
lines += lhead + 'source_file' + str(idx) + '="' + bnry.src + '"\n'
lines += lhead + 'target_file' + str(idx) + '="' + bnry.dst + '"\n'
lines += lhead + 'backup_file' + str(idx) + '="' + bnry.dst + '-${time_tag}-bak"\n\n'
idx += 1
fout.write( lines )
self.totalTargets = idx - offset
return self.totalTargets
def gen_special_apply( self, fout, lhead ):
if not self.specialList:
return
for bnry in self.specialList:
if bnry.itype == 'kofile':
lines = lhead + 'echo ""\n'
lines += lhead + 'echo "applying kofile:' + bnry.src + '..."\n'
fout.write( lines )
self.__gen_kofile( fout, lhead, bnry.src, bnry.dst )
elif bnry.itype == 'new':
lines = lhead + 'mv ' + bnry.src + ' ' + bnry.dst + '\n'
fout.write( lines )
def gen_special_revert( self, fout, lhead ):
if not self.specialList:
return
for bnry in self.specialList:
if bnry.itype == 'kofile':
lines = lhead + 'echo ""\n'
lines += lhead + 'echo "\nreverting kofile:' + bnry.src + '..."\n'
fout.write( lines )
self.__gen_kofile( fout, lhead, bnry.dst, bnry.dst )
elif bnry.itype == 'new':
lines = lhead + 'rm -rf ' + bnry.dst + '\n'
fout.write( lines )
def __gen_kodir( self, fout, lhead, dst ):
idx = dst.rfind( '/' )
kodir = dst[0:idx]
lines = lhead + 'kodir=' + kodir + '\n'
lines += lhead + 'if [ ! -d $kodir ];then\n'
lines += lhead + ' mkdir -p $kodir\n'
lines += lhead + 'fi\n\n'
fout.write( lines )
def __gen_kofile( self, fout, lhead, src, dst ):
bname = os.path.basename( src )
koname = bname.split( '.' )[0]
logging.info( 'gen ko:'+koname )
self.__gen_kodir( fout, lhead, dst )
lines = lhead + '#check the used part of ' + koname + ', must be 0 before patching\n'
lines += lhead + 'echo "Check if the ' + koname + ' used is 0 ..."\n'
lines += lhead + 'lsmod | awk \'{print $1, $2, $3}\' | grep ' + koname + '\n'
lines += lhead + 'used=`lsmod | awk \'{print $1, $2, $3}\' | grep ' + koname + ' | awk \'{print $3}\'`\n'
lines += lhead + 'if [ ! -z "$used" ]; then\n'
lines += lhead + ' if [ "$used" != "0" ]; then\n'
lines += lhead + ' echo "Fatal error, the ' + koname + ' is still being used, cannot patch"\n'
lines += lhead + ' exit 1\n'
lines += lhead + ' fi\n'
lines += lhead + ' #only remove when it exists\n'
lines += lhead + ' `rmmod ' + koname + '`;\n'
lines += lhead + ' status=$?\n'
lines += lhead + ' if [ $status -ne 0 ]; then\n'
lines += lhead + ' echo "Fatal error, rmmod ' + koname + ' failed"\n'
lines += lhead + ' exit $status\n'
lines += lhead + ' fi\n'
lines += lhead + 'fi\n'
lines += lhead + '\n'
lines += lhead + '#check ' + koname + ', no any more\n'
lines += lhead + 'echo "Check if the ' + koname + ' is removed ..."\n'
lines += lhead + 'lsmod | awk \'{print $1, $2, $3}\' | grep ' + koname + '\n'
lines += lhead + 'used=`lsmod | awk \'{print $1, $2, $3}\' | grep ' + koname + ' | awk \'{print $3}\'`\n'
lines += lhead + 'if [ ! -z "$used" ]; then\n'
lines += lhead + ' echo "Fatal error, failed to remove the ' + koname + ', cannot patch"\n'
lines += lhead + ' exit 1\n'
lines += lhead + 'fi\n'
lines += lhead + '\n'
lines += lhead + '`insmod ' + src + '`;\n'
lines += lhead + 'status=$?\n'
lines += lhead + 'if [ $status -ne 0 ]; then\n'
lines += lhead + ' echo "Fatal error, insmod ' + koname + ' failed"\n'
lines += lhead + ' exit $status\n'
lines += lhead + 'fi\n'
lines += lhead + '\n'
lines += lhead + '#check ' + koname + ' again, appear\n'
lines += lhead + 'echo "Check if the ' + koname + ' is inserted ..."\n'
lines += lhead + 'lsmod | awk \'{print $1, $2, $3}\' | grep ' + koname + '\n'
lines += lhead + 'used=`lsmod | awk \'{print $1, $2, $3}\' | grep ' + koname + ' | awk \'{print $3}\'`\n'
lines += lhead + 'if [ -z "$used" ]; then\n'
lines += lhead + ' echo "Fatal error, failed to insert the ' + koname + ', patch failed"\n'
lines += lhead + ' exit 1\n'
lines += lhead + 'fi\n\n'
fout.write( lines )
def gen_sanity_check( self, fout, lhead ):
total = self.totalTargets
lines = lhead + 'local bkupCount=0\n'
lines += lhead + 'for (( i=1; i <=' + len(total) + '; i++ ))\n'
lines += lhead + 'do\n'
lines += lhead + ' backup_file=backup_file$i\n'
lines += lhead + ' backup_file=${!backup_file}\n'
lines += lhead + ' if [[ -f $backup_file ]]; then\n'
lines += lhead + ' bkupCount=$(($bkupCount+1))\n'
lines += lhead + ' fi\n'
lines += lhead + 'done\n'
lines += lhead + '# check if all files were installed\n'
lines += lhead + 'if [ $bkupCount -eq 0 ]; then\n'
lines += lhead + ' echo "patch not installed\n'
lines += lhead + 'elif [ $bkupCount -lt ' + str(total) + ' ]; then\n'
lines += lhead + ' echo "ERROR: patch not install completely($bkupCount/' + str(total) + '), please revert the patch and try again!\n'
lines += lhead + ' return 22\n'
lines += lhead + 'else\n'
lines += lhead + ' echo "patch already exists, aborting."\n'
lines += lhead + ' return 21\n'
lines += lhead + 'fi\n'
fout.write( lines )
class Library( Binary ):
def __init__( self, parent ):
super(Library, self).__init__( parent )
class LibBlock( BinBlock ):
def __init__( self, parent ):
super(LibBlock, self).__init__( parent, '/sw/unicorn/lib', '/sw/unicorn/lib' )
class Component( BaseObject ):
def __init__( self, parent ):
self.name = None
self.deviceList = None
self.serviceBlock = None
self.binBlock = None
self.libBlock = None
self.isInSW = False
self.skipMount = False
self.deviceMode = None
self.compDir = None
self.orgCompDir = None
self.md5File = None
self.scriptName = None
self.needReload = False
def init_config( self, parent ):
if self.name is None:
logging.error( 'component name not set:'+str(self) )
return False
if self.deviceList:
nlist = list()
for de in self.deviceList:
if de == 'se':
de = 'service-engine'
elif de == 'sr':
de = 'service-router'
elif de == 'cdsm':
de = 'content-delivery-system-manager'
nlist.append( de )
self.deviceList = nlist
if self.serviceBlock:
if not self.serviceBlock.init_config( self ):
logging.error( 'init service block failed:'+str(self.serviceBlock) )
self.serviceBlock = None
else:
logging.debug( 'inited one service block:'+str(self.serviceBlock) )
if self.binBlock:
if not self.binBlock.init_config( self ):
logging.error( 'init binary block failed:'+str(self.binBlock) )
self.binBlock = None
if STRICT_PARSE:
return False
else:
logging.debug( 'inited one bin block:'+str(self.binBlock) )
if self.binBlock.isInSW:
self.isInSW = True
if self.libBlock:
if not self.libBlock.init_config( self ):
logging.error( 'init library block failed:'+str(self.libBlock) )
self.libBlock = None
if STRICT_PARSE:
return False
else:
logging.debug( 'inited one lib block:'+str(self.libBlock) )
if self.libBlock.isInSW:
self.isInSW = True
if not self.binBlock and not self.libBlock:
logging.error( 'no available binary file in this component:'+str(self) )
return False
return True
def generate( self, pdir, orgPdir ):
logging.info( 'gen in component:'+str(self) )
self.compDir = os.path.join( pdir, self.name )
self.orgCompDir = os.path.join( orgPdir, self.name )
if not mkdir(self.compDir):
logging.error( 'create component directory failed:'+str(self) )
return False
srcDir = os.path.join( self.compDir, 'src' )
orgSrcDir = 'src'
if not mkdir(srcDir):
logging.error( 'create component src directory failed:'+str(self) )
return False
self.__copy_source_files( srcDir, orgSrcDir )
self.__gen_md5sum_file( srcDir )
self.scriptName = self.name + '.sh'
spath = os.path.join( self.compDir, self.scriptName )
fout = open( spath, 'w' )
self.__gen_head( fout )
fout.write( '\n' )
gen_check_device_mode_func( fout, '' )
fout.write( '\n' )
gen_check_service_func( fout, '' )
fout.write( '\n' )
self.__gen_apply_func( fout )
fout.write( '\n' )
self.__gen_revert_func( fout )
fout.write( '\n' )
self.__gen_verify_func( fout )
fout.write( '\n' )
lines = 'case "$1" in\n'
lines += ' apply)\n'
lines += ' patch_apply\n'
lines += ' ;;\n'
lines += ' revert)\n'
lines += ' patch_revert\n'
lines += ' ;;\n'
lines += ' verify)\n'
lines += ' patch_verify\n'
lines += ' ;;\n'
lines += ' *)\n'
lines += ' echo $"Usage: $0 {apply|revert|verify}"\n'
lines += 'esac\n'
lines += 'cd $org_pwd\n'
fout.write( lines )
fout.close()
return True
def __gen_head( self, fout ):
lines = '#!/bin/sh\n'
lines += '# patch for ' + self.name + '\n\n'
lines += 'component="' + self.name + '"\n'
lines += 'md5sum_file="' + self.md5File + '"\n\n'
lines += 'org_pwd=$PWD\n'
lines += 'cd ' + self.orgCompDir + '\n\n'
fout.write( lines )
offset = 1
if self.binBlock:
offset += self.binBlock.gen_file_vars( fout, '', offset )
if self.libBlock:
offset += self.libBlock.gen_file_vars( fout, '', offset )
lines = 'total_files=' + str(offset-1) + '\n'
fout.write( lines )
def __gen_apply_func( self, fout ):
lines = 'function patch_apply(){\n'
lines += ' echo ""\n'
lines += ' echo "#########################################################"\n'
lines += ' echo "# Patching $component..."\n\n'
fout.write( lines )
lhead = '\t'
self.__gen_check_patched( fout, lhead )
self.__gen_check_device_mode( fout, lhead )
self.__gen_check_md5sum( fout, lhead )
if self.serviceBlock:
fout.write( '\n' )
self.serviceBlock.gen_stop( fout, lhead, 'apply' )
else:
logging.info( 'no service block to stop' )
self.__gen_mount( fout, lhead, 'rw' )
self.__gen_install_bin( fout, lhead )
if not self.skipMount:
self.__gen_mount( fout, lhead, 'ro' )
if self.serviceBlock:
fout.write( '\n' )
self.serviceBlock.gen_start( fout, lhead, 'apply' )
else:
logging.info( 'no service block to start' )
lines = ''
lines += '}\n'
fout.write( lines )
def __gen_revert_func( self, fout ):
lines = 'function patch_revert(){\n'
lines += ' echo ""\n'
lines += ' echo "#########################################################"\n'
lines += ' echo "# Reverting $component..."\n\n'
fout.write( lines )
lhead = '\t'
self.__gen_check_reverted( fout, lhead )
if self.serviceBlock:
self.serviceBlock.gen_stop( fout, lhead, 'revert' )
self.__gen_mount( fout, lhead, 'rw' )
self.__gen_revert_bin( fout, lhead )
if not self.skipMount:
self.__gen_mount( fout, lhead, 'ro' )
if self.serviceBlock:
self.serviceBlock.gen_start( fout, lhead, 'revert' )
lines = ''
lines += '}\n'
fout.write( lines )
def __gen_verify_func( self, fout ):
lines = 'function patch_verify(){\n'
lines += ' echo ""\n'
lines += ' echo "#########################################################"\n'
lines += ' echo "# Verifying $component..."\n\n'
lhead = '\t'
lines += lhead + 'local installed=1\n'
lines += lhead + 'for (( i=1; i<=total_files; i++ ))\n'
lines += lhead + 'do\n'
lines += lhead + ' source_file_to_check=source_file$i\n'
lines += lhead + ' source_file_to_check=${!source_file_to_check}\n'
lines += lhead + ' file_to_check=target_file$i\n'
lines += lhead + ' file_to_check=${!file_to_check}\n'
lines += lhead + ' source_filename_nopath=$(echo "$source_file_to_check" | sed "s/.*\/\(.\+\)$/\\1/")\n'
lines += lhead + ' expected_md5=$(grep "/$source_filename_nopath$" $md5sum_file | awk \'{ print $1 }\')\n'
lines += lhead + ' patched_md5=$(md5sum "$file_to_check" | awk \'{ print $1 }\')\n'
lines += lhead + ' \n'
lines += lhead + ' echo "Actual checksum is $patched_md5 for target file $file_to_check."\n'
lines += lhead + ' echo "Expected checksum is $expected_md5."\n'
lines += lhead + ' \n'
lines += lhead + ' if [[ "$patched_md5" = "$expected_md5" ]]; then \n'
lines += lhead + ' installed=$(($installed&1))\n'
lines += lhead + ' else\n'
lines += lhead + ' installed=$(($installed&0))\n'
lines += lhead + ' fi\n'
lines += lhead + 'done\n'
lines += lhead + 'if [[ $installed -ne 0 ]]; then\n'
lines += lhead + ' echo "Verification: patch has been installed."\n'
lines += lhead + 'else\n'
lines += lhead + ' echo "Verification: patch is not installed."\n'
lines += lhead + 'fi\n'
lines += '}\n'
fout.write( lines )
def __gen_check_patched( self, fout, lhead ):
lines = lhead + '##### sanity check\n'
lines += lhead + 'local bkupCount=0\n'
lines += lhead + 'for (( i=1; i<=total_files; i++ ))\n'
lines += lhead + 'do\n'
lines += lhead + ' backup_file=backup_file$i\n'
lines += lhead + ' backup_file=${!backup_file}\n'
lines += lhead + ' if [[ -f $backup_file ]]; then\n'
lines += lhead + ' bkupCount=$(($bkupCount+1))\n'
lines += lhead + ' fi\n'
lines += lhead + 'done\n\n'
lines += lhead + '# check if all files were installed\n'
lines += lhead + 'if [ $bkupCount -eq 0 ]; then\n'
lines += lhead + ' echo "sanity check: patch not installed, go on..."\n'
lines += lhead + 'elif [ $bkupCount -lt $total_files ]; then\n'
lines += lhead + ' echo "ERROR: patch not install completely($bkupCount/$total_files), please revert the patch and try again!"\n'
lines += lhead + ' return 22\n'
lines += lhead + 'else\n'
lines += lhead + ' echo "patch already exists, aborting."\n'
lines += lhead + ' return 21\n'
lines += lhead + 'fi\n\n'
fout.write( lines )
def __gen_check_reverted( self, fout, lhead ):
lines = lhead + '##### sanity check\n'
lines += lhead + 'local bkupCount=0\n'
lines += lhead + 'for (( i=1; i<=total_files; i++ ))\n'
lines += lhead + 'do\n'
lines += lhead + ' backup_file=backup_file$i\n'
lines += lhead + ' backup_file=${!backup_file}\n'
lines += lhead + ' if [[ -f $backup_file ]]; then\n'
lines += lhead + ' bkupCount=$(($bkupCount+1))\n'
lines += lhead + ' fi\n'
lines += lhead + 'done\n\n'
lines += lhead + '# check if all files were installed\n'
lines += lhead + 'if [ $bkupCount -eq 0 ]; then\n'
lines += lhead + ' echo "sanity check: patch not installed, aborting."\n'
lines += lhead + ' return 31\n'
lines += lhead + 'elif [ $bkupCount -lt $total_files ]; then\n'
lines += lhead + ' echo "ERROR: patch not install completely($bkupCount/$total_files)"\n'
lines += lhead + 'else\n'
lines += lhead + ' echo "sanity check: patch installed, go on..."\n'
lines += lhead + 'fi\n\n'
fout.write( lines )
def __gen_check_device_mode( self, fout, lhead ):
if not self.deviceList:
return
lines = ''
lines += lhead + 'echo ""\n'
lines += lhead + 'local isRightMode=0\n'
lines += lhead + 'local allowMode=0\n'
lines += lhead + 'echo "checking device mode, allowed:' + str(self.deviceList) + '"\n'
for de in self.deviceList:
lines += lhead + 'if [ $isRightMode -eq 0 ]; then\n'
lines += lhead + ' check_device_mode "' + de + '"\n'
lines += lhead + ' isRightMode=$?\n'
lines += lhead + ' if [ $isRightMode -eq 1 ]; then\n'
lines += lhead + ' echo "patch will install for ' + de + '"\n'
lines += lhead + ' allowMode=1\n'
lines += lhead + ' fi\n'
lines += lhead + 'fi\n'
lines += lhead + 'if [ $allowMode -eq 0 ]; then\n'
lines += lhead + ' echo "patch is not allowed for this device mode"\n'
lines += lhead + ' /ruby/bin/exec -c "show device-mode current"\n'
lines += lhead + ' exit 44\n'
lines += lhead + 'fi\n'
fout.write( lines )
def __gen_check_md5sum( self, fout, lhead ):
lines = lhead + 'md5sum --status -c $md5sum_file\n'
lines += lhead + 'md5_rc=$?\n'
lines += lhead + 'if [ $md5_rc -ne 0 ]; then\n'
lines += lhead + ' echo "ERROR: patch file corrupted, aborting, please check"\n'
lines += lhead + ' exit 24\n'
lines += lhead + 'fi\n'
fout.write( lines )