-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.cfg
1158 lines (939 loc) · 34.7 KB
/
func.cfg
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
source update.cfg #In Raspberry Pi it may require the whole path
trap dynamically_updated SIGUSR2
#----------------------------------------------------------------------------------------------
#
# This function handles failure reports.It sends an issue to Github and rollbacks to previous version
# Issue includes commit number, commit author as asignee and label of failure
#
# Parameters: Failed commit number Label of failure ("compile error", "Merging conflict")
#
#-----------------------------------------------------------------------------------------------
function update_failed ()
{
#Get number of failed commitment and save it in configuration file
#cat /dev/null >../temp_files/update.conf
#echo $1 >>../temp_files/update.conf
sed -i '/failed_commit*./d' /home/fatima/Raspi_sw/Update_sw/update.cfg #/home/pi/git/update.cfg
echo "export failed_commit="$1 >> /home/fatima/Raspi_sw/Update_sw/update.cfg #/home/pi/git/update.cfg
#Send email alert
#echo "Update software compilation failed"| mail -s "P$
#Find out commit's author. Now this is already done while checking signature
#echo "commit author" $author
#Send issue and assign it to the author of the failed commit
curl -H 'Authorization: token 4ba329d592f0243d946a3d8ca51c0b569c6c0ebc' -d '{
"title": "commit '"$1"'",
"body": "'"$2"''"$3"'",
"assignee": "'"$author"'",
"labels": ["'"$2"'","bug"]
}' https://api.github.com/repos/Facj/Repo1/issues > ../temp_files/error.log 2>&1
#Rollback to previous version and undo changes that have already been performed
git reset --hard ORIG_HEAD >/dev/null 2>&1
#compile_update #Changed c files are already in the c_files array
}
#----------------------------------------------------------------------------------------------
#
# This function handles security problems reports.It sends an issue to Github.
# Issue includes commit number and label of security problem. No asignee is provided.
#
# Parameters: Unsafe commit number Label of failure
#
#-----------------------------------------------------------------------------------------------
function security_problem ()
{
#Get number of unsafe commitment and save it in configuration file
#cat /dev/null >../temp_files/update.conf
#echo $1 >>../temp_files/update.conf
sed -i '/failed_commit*./d' /home/fatima/Raspi_sw/Update_sw/update.cfg #/home/pi/git/update.cfg
echo "export failed_commit="$1 >>/home/fatima/Raspi_sw/Update_sw/update.cfg #/home/pi/git/update.cfg
#Send issue and assign it to the author of the failed commit
curl -H 'Authorization: token 4ba329d592f0243d946a3d8ca51c0b569c6c0ebc' -d '{
"title": "commit '"$1"'",
"body": "'"$2"'",
"labels": ["'"$2"'","security"]
}' https://api.github.com/repos/Facj/Repo1/issues > ../temp_files/error.log 2>&1
}
#----------------------------------------------------------------------------------------------
#
# This function compiles C update files.
# If not successful it calls the function update_failed and returns 1.
#
# Parameters: commit_number
#
#-----------------------------------------------------------------------------------------------
function compile_update()
{
y=0
while [ $y -lt ${#makefiles[@]} ]; do
used_makefiles[$y]=false
#echo $y
((y++))
done
t=0
for z in "${c_files[@]}"; do
y=0
compiled=false
for x in "${makefiles[@]}"; do
#echo $x
exec=$(grep $z $x)
if [ $? -eq 0 ];then
#echo "Hay $z"
if ! "${used_makefiles[$y]}" ; then
#echo "No exec_before"
path=$(echo $x | sed -e 's/\/Makefile//')
#echo $path
make clean -C $path >dev/null 2>&1
make -C $path >/dev/null 2>&1
if [ $? -ne 0 ];then
update_failed $1 "Compilation error" " in $x"
return 1
fi
used_makefiles[$y]=true
fi
#If the compiled file is not a test, it is saved in compiled_c_filese
compiled=true
name_start=$(echo ${exec:0:4})
if [ "$name_start" != "test" ]
then
exec=$(echo $exec | grep -Po '(.*)(:)')
compiled_c_files[$t]=$(echo $exec | sed 's/://')
((t++))
fi
#else
#echo "No hay $z"
fi
((y++))
done
if ! "$compiled"; then
exec=$(echo $z | awk -F'.' '{print ""$1}')
#echo "compile $exec"
gcc $z -o $exec >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo $x "compile failed"
update_failed $1 "Compilation error" " in $x"
return 1
else
#If the compiled file is not a test, it is saved in compiled_c_files
name_start=$(echo ${exec:0:4})
if [ "$name_start" != "test" ]
then
compiled_c_files[$t]=$exec
((t++))
fi
fi
fi
done
return 0
}
#----------------------------------------------------------------------------------------------
#
# This function sorts the update files in arrays according to the programming language (C and Python).
# It leaves out the Python test files, so as to be treated in different way than source files.
# C test files are left out once compilated.
#
# Parameters: commit_number
#
#-----------------------------------------------------------------------------------------------
function sort_update_files()
{
y=0
z=0
#Check if updated files are C or Python source files
for x in "${changed_files[@]}"; do
arr=$(echo $x | awk -F'.' '{print ""$2}')
if [ "$arr" == "c" ]
then
c_files[$y]=$x
((y++))
else if [ "$arr" == "py" ]
then
name_start=$(echo ${x:0:4})
if [ "$name_start" != "test" ]
then
python_files[$z]=$x
((z++))
fi
fi
fi
done
return 0
}
#----------------------------------------------------------------------------------------------
#
# This function gets the current version number of the dynamically updatable c files changed in the update.
# So as to a file can be recognized as dynamically updatable -v must be provided and on the output
# version number and "dynamically updatable" must appear
#
# Parameters: no parameters
#
#-----------------------------------------------------------------------------------------------
function get_current_versions()
{
y=0
for x in "${c_files[@]}"; do
exec=$(echo $x | awk -F'.' '{print ""$1}')
#echo "Get version from $exec"
prid1=( $(pidof $exec) )
ver=$(./$exec -v) &
sleep 0.1
prid2=( $(pidof $exec) )
if [ ${#prid1[@]} -lt ${#prid2[@]} ]; then
#echo "$x running"
kill ${prid2[0]}
current_c_versions[$y]="No"
#echo "version not provided"
((y++))
else
#echo "version provided"
output=$(./$exec -v)
version=$(echo $output | grep -Po '(version )\d+(?:\.\d+){1}')
version=$(echo $version | grep -Po '\d+(?:\.\d+){1}')
dyn=$(echo $output | grep "Dynamically updatable")
compatible=$(echo $output | grep -Po '(Compatible from )\d+(?:\.\d+){1}')
compatible=$(echo $compatible | grep -Po '\d+(?:\.\d+){1}')
#echo "Compatible with $compatible"
#echo $version
if [ -n "$dyn" ] && [ -n "$version" ]
then
current_c_versions[$y]=$version
else
current_c_versions[$y]="No"
fi
((y++))
fi
done
return 0
}
#----------------------------------------------------------------------------------------------
#
# This function checks what files have been updated and saves them in the array changed_files
#
# Parameters: commit_number
#
#-----------------------------------------------------------------------------------------------
function check_updated_files()
{
x=0
changed=$(git whatchanged $1 -1 --pretty=oneline)
#echo $changed_files
read -r line <<< "$1" #First line is ignored
while read -r line;do
if [ $x -ne 0 ]
then
arr=$(echo $line | awk -F'M ' '{print ""$2}')
changed_files[$x]=$arr
#echo ${changed_files[$x]}
fi
((x++))
done <<< "$changed"
}
function check_rollback_files(){
x=1
#Check what has changed in the rollback
changed=$(git diff --raw $1 $2)
#echo "changed = " $changed
while read -r line;do
if [ $x -ne 0 ]
then
arr=$(echo $line | awk -F'M ' '{print ""$2}')
changed_files[$x]=$arr
#echo ${changed_files[$x]}
fi
((x++))
done <<< "$changed"
}
#-----------------------------------------------------------------------------------------------------------
#
# This function is called once the update has been successfully downloaded and compiled (if required).
# It updates global variables: failed commit, previous update (saved so as rollback is possible in the future.
#
# Parameters: no parameters
#
#------------------------------------------------------------------------------------------------------------
function successful_update(){
#echo $previous_update $current_commit $failed_commit
sed -i '/failed_commit*./d' /home/fatima/Raspi_sw/Update_sw/update.cfg #/home/pi/git/update.cfg #not working without complete path
sed -i '/previous_update*./d' /home/fatima/Raspi_sw/Update_sw/update.cfg #/home/pi/git/update.cfg
sed -i '/current_commit*./d' /home/fatima/Raspi_sw/Update_sw/update.cfg #/home/pi/git/update.cfg
echo "export failed_commit=\"\"" >>/home/fatima/Raspi_sw/Update_sw/update.cfg #/home/pi/git/update.cfg
echo "export previous_update="$current_commit >>/home/fatima/Raspi_sw/Update_sw/update.cfg #/home/pi/git/update.cfg
echo "export current_commit="$(git rev-parse HEAD) >>/home/fatima/Raspi_sw/Update_sw/update.cfg #/home/pi/git/update.cfg
#echo $previous_update $current_commit
}
#-----------------------------------------------------------------------------------------------------------
#
# This function checks it there are new commitments in the repository that have not been proved wrong before.
# If variable received is true it calls the function that checks if commits have been tagged and signed
# If new commits availale returns 0.
#
# Parameters: key_check
#
#------------------------------------------------------------------------------------------------------------
function check_available_update(){
#Check if there are new commits
git fetch origin >/dev/null 2>&1
if $1; then
check_available_signed_update
return $?
else
cherry_out=$(git cherry master origin)
#echo "result from cherry:" $cherry_out
if [ "$cherry_out" != "" ]
then
#Check last commit id
cherry_out=$(git cherry master origin)
while read -r line;do
last_line=$line
done <<< "$cherry_out"
new_v=${last_line:2:40}
#Check if this commitment has been proved wrong before
#old_v=$(head -n 1 ../temp_files/update.conf)
old_v=$failed_commit
if [ "$old_v" == "" ] || [ "$new_v" != "$old_v" ]
then
return 0
else
return 1
fi
else
return 1
fi
fi
}
#-----------------------------------------------------------------------------------------------------------
#
# This function checks it there are new commitments in the repository that haven been tagged, signed by trusted
# source and not proved wrong before.
# If new trusted commits available returns 0.
#
# Parameters: no parameters
#
#------------------------------------------------------------------------------------------------------------
function check_available_signed_update(){
typeset lines;
#old_v=$(head -n 1 ../temp_files/update.conf)
old_v=$failed_commit
git describe --abbrev=0 >/dev/null 2>&1
if [ $? -eq 0 ]
then
current_tag=$(git describe --abbrev=0)
fi
x=0
#echo $current_tag
#Check last commit id
cherry_out=$(git cherry master origin)
while read -r line;do
lines[x]=$line
x=$((x+1))
done <<< "$cherry_out"
if [ "$cherry_out" != "" ]
then
element=${#lines[@]} ;
for x in "${lines[@]}"; do
element=$((element-1));
new_v=${lines[element]:2:40}
#echo $element $new_v
#Check if this commitment has been proved wrong before
#If yes, not correct updates available
#If not, check the tag
if [ "$old_v" == "" ] || [ "$new_v" != "$old_v" ]
then
new_tag=$(git describe $new_v --abbrev=0) </dev/null 2>&1
if [ "$current_tag" == "$new_tag" ]
then
#echo "Not a new tag.Not updating" $new_tag
return 1;
else
#echo "New tag" $new_tag
commit_tag=$(git describe $new_v)
if [ "$new_tag" == "$commit_tag" ]
then
#echo "This commit is a new tag.Verify"
git verify-tag $new_tag >../temp_files/verified_signature.txt 2>&1
if [ $? -ne 0 ]
then
echo "Not signed or untrusted signature" #Signature doesn't exist or is not in our keyring
security_problem $new_v "Not signed or untrusted signature"
#return 1
else
#echo "Trusted signature" #Signature in our keyring
check_update_author $new_v
check_signature_holder
if [ $? -eq 0 ]
then
return 0
else
echo "Commiter is not the signature holder"
security_problem $new_v "Signature theft"
#return 1
fi
fi
#else
#echo "This commit is not tagged. Search for the tag commit"
fi
fi
else
return 1
fi
done
fi
return 1
}
#-----------------------------------------------------------------------------------------------------------
#
# This function checks who is the owner of the signature provided in the tag that is being check.
# If it matches the author of the tag commit it returns 0.
# If not it returns 1
#
# Parameters: no parameters
#
#------------------------------------------------------------------------------------------------------------
function check_signature_holder(){
#check_author's email
author_email=$(findGitHubEmail $author)
#echo $author $author_email
#Check signature holder
holder=$(grep -Eio '[a-z0-9._-]+@[a-z0-9.-]+[a-z]{2,4}' ../temp_files/verified_signature.txt)
#Compare checking the email address
if [ "$holder" == "$author_email" ]
then
#echo "Signature correct"
return 0
else
echo -e "Signature theft.\nUpdate signed by = $author_email. Signature holder = $holder"
return 1
fi
}
#-----------------------------------------------------------------------------------------------------------
#
# This function sends HTTP request through GitHub API to find out who is the author of the update
# Parameters: no parameters
#
#------------------------------------------------------------------------------------------------------------
function check_update_author(){
#Find out commit's author
curl -s -X GET https://api.github.com/repos/Facj/Repo1/git/commits/$new_v > ../temp_files/json_author.txt 2>&1
#author=$(cat json_author.txt | jq '.author.name')
author=$(awk '/name/' ../temp_files/json_author.txt |sed -n 1p|awk -F": " '{split($2,a,",");print a[1]}')
author=$(echo $author | tr -d '"') #Remove character "" from author string
}
#-----------------------------------------------------------------------------------------------------------
#
# This function sends HTTP request through GitHub API to find out who is the author of the self_update
# Parameters: no parameters
#
#------------------------------------------------------------------------------------------------------------
function check_self_update_author(){
#Find out commit's author
curl -s -X GET https://api.github.com/repos/Facj/Update_sw/git/commits/$new_v > ../temp_files/json_author.txt 2>&1
#author=$(cat json_author.txt | jq '.author.name')
author=$(awk '/name/' ../temp_files/json_author.txt |sed -n 1p|awk -F": " '{split($2,a,",");print a[1]}')
author=$(echo $author | tr -d '"') #Remove character "" from author string
}
#-----------------------------------------------------------------------------------------------------------
#
# This function checks if the update compiled files are current running processes and changes them for updated
#processes.
#
# Parameters: commit_number
#
#------------------------------------------------------------------------------------------------------------
function change_running_version()
{
signaled_processes=0
updated_processes=0
index=0
temp=0
supported=true
for x in "${compiled_c_files[@]}"; do
exec=$x
prid=$(pidof $x)
case "$prid" in
[0-9]*) #echo "$x running index $index"
dynamic_update_supported $index #check if dynamic updates are supported by both old and new version
if [ $? -eq 0 ]
then
kill -s SIGUSR2 $prid
((signaled_processes++))
temp=$(date --date="+60 second" +%s)
else
echo "Dynamic update not supported"
supported=false
if $forced; then
kill $prid
echo "Forced update"
./$exec &
fi
fi
;;
## *) echo "$x not running";;
esac
((index++))
done
if [ $signaled_processes -gt 0 ]; then
now=$(date +%s)
echo "Waiting for updated processes..."
while [ "$now" -lt "$temp" ] && [ $updated_processes -lt $signaled_processes ]; do
now=$(date +%s)
done
if [ $updated_processes -lt $signaled_processes ]; then
echo "update_failed" $1 "Dynamic update failure"
return 1
else
echo "Dynamic update successful"
return 0
fi
else if $supported; then
echo "The updated processes are not running"
fi
fi
}
#-----------------------------------------------------------------------------------------------------------
#
# This function changes the frequency of update checking by modifying the crontab entry.
# Frequency can be given in minutes(m), hours(h) or days(d)
#
# Parameters: m/d/h frequency_value
#
#------------------------------------------------------------------------------------------------------------
function change_frequency(){
#Remove previous crontab entry
crontab -l | grep -v update.sh |crontab
if [ $2 -eq 0 ] #If the value received is 0 automatic updates are disabled
then
echo "Automatic update check disabled"
return
fi
#Create new crontab entry
case $1 in
m) var="*/$2 * * * *";;
h) var="0 */$2 * * *";;
d) var="0 0 */$2 * *";;
esac
#echo "$var bash /home/pi/git/update.sh >>/home/pi/git/update_record.txt"
#Write entry to crontab
{ crontab -l
echo "$var bash /home/pi/git/update.sh >>/home/pi/git/update_record.txt"
} | (sleep 1; crontab -)
echo "Crontab updated"
}
#-----------------------------------------------------------------------------------------------------------
#
# This function scans the repository for unittest for the compiled c files and the python files.
#
# Parameters: commit_number
#
#------------------------------------------------------------------------------------------------------------
function check_and_run_tests()
{
for x in "${compiled_c_files[@]}"; do
find "test_$x.c" >/dev/null 2>&1
if [ $? -eq 0 ]; then #If test available, it is run whether it is required or not
./test_$x >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "Test for $x failed"
update_failed $1 "Test failed" ". Test $x has been unsuccessful"
return 1
#else
# echo "Test for $x has been SUCCESS"
fi
else
if $require_test #Test required but not available
then
echo "Required test for $x is not provided"
update_failed $1 "No test provided" ". Required test for $x is not provided"
return 1
fi
fi
done
#The same with Python files
for x in "${python_files[@]}"; do
find "test_$x" >/dev/null 2>&1
if [ $? -eq 0 ]; then #If test available, it is run whether it is required or not
python test_$x >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "Test for $x failed"
update_failed $1 "Test failed" ". Test $x has been unsuccessful"
return 1
#else
# echo "Test for $x has been SUCCESS"
fi
else
if $require_test #Test required but not available
then
echo "Required test for $x is not provided"
update_failed $1 "No test provided" ". Required test for $x is not provided"
return 1
fi
fi
done
}
#-----------------------------------------------------------------------------------------------------------
#
# This function checks email of the given GitHub user
#
# Parameters: GitHub_user_name
#
#------------------------------------------------------------------------------------------------------------
# FindGitHubEmail (https://github.com/hodgesmr/FindGitHubEmail)
# Copyright (c) 2013 Matt Hodges (http://matthodges.com)
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
function findGitHubEmail(){
USER=$1
PROFILERESPONSE=`curl -s https://api.github.com/users/$USER`
EMAIL=`echo "$PROFILERESPONSE" | grep "\"email\":" | sed -e's/[,|"]//g' | awk '{print $(NF)}' | grep -v null`
if [ -z "$EMAIL" ] ; then
EVENTRESPONSE=`curl -s https://api.github.com/users/$USER/events/public`
EMAIL=`echo "$EVENTRESPONSE" | grep "\"email\":" | sed -e's/[,|"]//g' | sort | uniq -c | sort -n | awk '{print $(NF)}' | grep -v null | tail -n1`
fi
if [ -n "$EMAIL" ] ; then
echo "$EMAIL"
fi
}
#-----------------------------------------------------------------------------------------------------------
#
# This function is called once the update has been successfully downloaded and compiled (if required).
# It updates glob
# Parameters: no parameters
#
#------------------------------------------------------------------------------------------------------------
function rollback_update(){
#Check if it's possible to rollback
if [ -z "$previous_update" ]; then
echo -e "Rollback to previous update not available.\nIt is not possible to perform several rollbacks in a row"
return 1
fi
if $dynamic; then
echo -e "Dynamic rollback not supported"
return 1
fi
#echo $previous_update $current_commit
check_rollback_files $previous_update $current_commit
#echo $changed_files
git reset --hard $previous_update
#Compile and execute
if $compile; then
sort_update_files $previous_update
compile_update
if [ $? -ne 0 ]; then
echo "Compilation error" #Could that happen???
#else
#echo ${compiled_c_files[*]}
#successful_rollback
fi
#else
#successful_rollback
fi
sed -i '/previous_update*./d' /home/fatima/Raspi_sw/Update_sw/update.cfg #/home/pi/git/update.cfg
sed -i '/current_commit*./d' /home/fatima/Raspi_sw/Update_sw/update.cfg #/home/pi/git/update.cfg
sed -i '/failed_commit*./d' /home/fatima/Raspi_sw/Update_sw/update.cfg #/home/pi/git/update.cfg
echo "export failed_commit="$current_commit >>/home/fatima/Raspi_sw/Update_sw/update.cfg #/home/pi/git/update.cfg #Update that has just been undone is considered not wanted
echo "export current_commit="$previous_update >>/home/fatima/Raspi_sw/Update_sw/update.cfg #/home/pi/git/update.cfg
echo "export previous_update=\"\"" >>/home/fatima/Raspi_sw/Update_sw/update.cfg #/home/pi/git/update.cfg
}
#-----------------------------------------------------------------------------------------------------------
#
# This function is called when a SIGUSR2 signal is received, which means a process has been dinamically update
# successfully. It increments the updated_processes counter.
#
# Parameters: no parameters
#
#------------------------------------------------------------------------------------------------------------
function dynamically_updated()
{
((updated_processes ++))
}
#-----------------------------------------------------------------------------------------------------------
#
# This function cheks if a running process support dynamic updates. Both the old and the new must support them
# and must be compatible for data conversion
#
# Parameters: exec_file_index
#
#------------------------------------------------------------------------------------------------------------
function dynamic_update_supported()
{
exec=${compiled_c_files[$1]}
#echo "Get version of $exec"
#Check if current version supports dynamic updates
#echo "Running version ${current_c_versions[$1]}"
if [ "${current_c_versions[$1]}" == "No" ]
then
return 1
else
prid1=( $(pidof $exec) )
ver=$(./$exec -v) &
sleep 0.1
prid2=( $(pidof $exec) )
if [ ${#prid1[@]} -lt ${#prid2[@]} ]; then
#echo "$x running"
kill ${prid2[0]}
current_c_versions[$y]="No"
#echo "version not provided"
((y++))
else
output=$(./$x -v)
version=$(echo $output | grep -Po '(version )\d+(?:\.\d+){1}')
version=$(echo $version | grep -Po '\d+(?:\.\d+){1}')
dyn=$(echo $output | grep "Dynamically updatable")
compatible=$(echo $output | grep -Po '(Compatible from )\d+(?:\.\d+){1}')
compatible=$(echo $compatible | grep -Po '\d+(?:\.\d+){1}')
#echo "version $version compatible from $compatible"
if [ -n "$compatible" ] && [ -n "$dyn" ] && [ -n "$version" ]
then
compare_versions $compatible ${current_c_versions[$1]}
return $?
else
return 1
fi
fi
fi
}
#-----------------------------------------------------------------------------------------------------------
#
# This function compares to version numbers to find out if they are compatible for data conversion.
# If compatible, it returns 0. If not, it returns 1.
#
# Parameters: $version_from_which_new_compatibility_starts $running_version
#
#------------------------------------------------------------------------------------------------------------
function compare_versions()
{
y=0
for x in $(echo $1 | tr "." " ") ; do
version_1[$y]=$x
((y++))
done
y=0
for x in $(echo $2 | tr "." " ") ; do
version_2[$y]=$x
((y++))
done
#Check which version number is greater
if [ "${#version_1[@]}" -le "${#version_2[@]}" ]
then
loop=${#version_1[@]}
else
loop=${#version_2[@]}
fi
index=0
let loop=loop-1
while [ $index -le $loop ]
do
if [ "${version_2[$index]}" -lt "${version_1[$index]}" ]; then
return 1
fi
if [ "${version_2[$index]}" -gt "${version_1[$index]}" ]; then
return 0 #$2 is greater
fi
let index=index+1
done
if [ "${#version_1[@]}" -le "${#version_2[@]}" ]
then
return 0
else
return 1
fi
}
#-----------------------------------------------------------------------------------------------------------
#
# This function handles the self-update process. If no update is available or the self-update process fails,
# it returns 0 so the update process can continue. Otherwise it returns 1 and the updater restarts itself.
#
# Parameters: no parameters
#
#------------------------------------------------------------------------------------------------------------
function self_update()
{
cd $updater_path
check_self_update
if [ $? -ne 0 ]
then
#echo "No self-update available"
return 0
fi
# echo "Self-update available" #$new_v
#New commitment
git merge FETCH_HEAD --no-edit >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "Merging failed"
self_update_failed $new_v "Merging conflict"
return 0
fi
#Test the new version. Rollback if failed
bash test_update.sh
if [ $? -ne 0 ]
then
update_failed $new_v "Test failed"
return 0
else
#Self-update successfully accomplished
successful_self_update
return 1
fi
}
#-----------------------------------------------------------------------------------------------------------
#
# This function checks if there are valid available updates for the update system. Signature checks cannot
# be avoided.
# If new trusted commits available returns 0.
#
# Parameters: no parameters
#
#------------------------------------------------------------------------------------------------------------
function check_self_update(){
typeset lines;
old_v=$self_failed_commit
#Check if there are new commits
git fetch origin >/dev/null 2>&1
#Get current tag
git describe --abbrev=0 >/dev/null 2>&1
if [ $? -eq 0 ]
then
current_tag=$(git describe --abbrev=0)
fi
x=0
#echo $current_tag
#Check last commit id
cherry_out=$(git cherry master origin)
while read -r line;do
lines[x]=$line
x=$((x+1))
done <<< "$cherry_out"