-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
1275 lines (1085 loc) · 38 KB
/
install.sh
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
# TODO: create mock tests https://poisel.info/posts/2022-05-10-shell-unit-tests/
PS4='Line ${LINENO}: '
get_name_platform() {
if uname -a | grep -i "GNU/Linux" >/dev/null; then
awk -F= '$1=="ID" { print $2 ;}' /etc/os-release
elif uname -a | grep -i "darwin" >/dev/null; then
echo "darwin"
# elif [[ "$OSTYPE" == "cygwin" ]]; then
# # POSIX compatibility layer and Linux environment emulation for Windows
# elif [[ "$OSTYPE" == "msys" ]]; then
# # Lightweight shell and GNU utilities compiled for Windows (part of MinGW)
# elif [[ "$OSTYPE" == "win32" ]]; then
# # I'm not sure this can happen.
# elif [[ "$OSTYPE" == "freebsd"* ]]; then
# # ...
# else
# # Unknown.
fi
}
add_flags() {
# e - script stops on error (return != 0)
# u - error if undefined variable
# o pipefail - script fails if one of piped command fails
# x - output each line (debug)
set -euox pipefail
}
add_flags
get_sony_xperia_10_password() {
PASSWORD=$(aws secretsmanager get-secret-value --secret-id sony_xperia_10 --query 'SecretString' --output text | grep -o '"PASSWORD":"[^"]*' | grep -o '[^"]*$')
}
prepare_device() {
echo "${PASSWORD}" | devel-su pkcon -y --allow-reinstall install gcc sudo make
echo "${PASSWORD}" | devel-su bash -c 'echo "# User rules for nemo
nemo ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/rules-for-user-nemo'
}
run_sshd_on_device() {
devel-su systemctl restart sshd
}
install_bash() {
BASH_VERSION=5.2
NAME_BASH=bash-${BASH_VERSION}
set +eo
exe=$(
exec 2>/dev/null
readlink "/proc/$$/exe"
)
add_flags
case "$exe" in
*/busybox)
echo "It's a busybox shell."
prepare_device
cd ~/
curl -O https://ftp.gnu.org/gnu/bash/${NAME_BASH}.tar.gz
tar -xzvf ${NAME_BASH}.tar.gz
cd ${NAME_BASH}
./configure --prefix=/usr \
--bindir=/bin \
--htmldir=/usr/share/doc/${NAME_BASH} \
--without-bash-malloc \
--with-installed-readline
make
sudo make install
;;
esac
bash --version
}
# builtin variables
RED='\033[0;31m'
BLUE='\033[1;36m'
#SDK_VERSION='3.9.6'
#SDK_FILE_NAME="SailfishSDK-${SDK_VERSION}-linux64-offline.run"
export DEBIAN_FRONTEND=noninteractive
export DEBCONF_NONINTERACTIVE_SEEN=true
DOCKER_REPO="ghcr.io/spiritecosse/bible"
SSH_ID_RSA="${HOME}/.ssh/id_rsa"
SSH_ID_RSA_PUB="${HOME}/.ssh/id_rsa.pub"
TEMP_SSH_ID_RSA="${HOME}/.id_rsa"
TEMP_SSH_ID_RSA_PUB="${HOME}/.id_rsa.pub"
PATH=$HOME/bin:/usr/local/bin:$PATH
if [[ -z ${LLVM_TAG+x} ]]; then
export LLVM_TAG="17.0.6"
fi
# Default values
funcs=main
# Arguments handling
while ((${#} > 0)); do
case "${1}" in
'--func='*) funcs="${1#*=}" ;; # Handles --opt1
esac
shift
done
if [[ -z ${ARCH+x} ]]; then
ARCH=$(uname -m)
fi
PLATFORM_HOST=$(get_name_platform)
if [[ -z ${PLATFORM+x} ]]; then
PLATFORM=$(get_name_platform)
fi
if [[ -z ${EC2_INSTANCE_NAME+x} ]]; then
EC2_INSTANCE_NAME=backup_server
fi
if [[ -z ${RELEASE+x} ]]; then
RELEASE="latest"
fi
echo "RELEASE: ${RELEASE}"
echo "PLATFORM: ${PLATFORM}"
echo "ARCH: ${ARCH}"
echo "EC2_INSTANCE_NAME: ${EC2_INSTANCE_NAME}"
BUILD_FOLDER_NAME="${PLATFORM}_${ARCH}"
if [[ -z ${SRC_FOLDER_NAME+x} ]]; then
SRC_FOLDER_NAME="${PLATFORM}_${ARCH}_src"
fi
if [[ -z ${BUILD_FOLDER+x} ]]; then
BUILD_FOLDER="${HOME}/${BUILD_FOLDER_NAME}"
fi
mkdir -p "${BUILD_FOLDER}"
FILE_TAR=${BUILD_FOLDER_NAME}.tar
FILE=${FILE_TAR}.gz
FILE_SRC_TAR=${SRC_FOLDER_NAME}.tar
FILE_SRC=${FILE_SRC_TAR}.gz
BACKUP_FILE_PATH="${HOME}/${FILE}"
BACKUP_FILE_SRC_PATH="${HOME}/${FILE_SRC}"
DESTINATION_PATH="/usr/share/nginx/html/backups/"
HTTP_FILE="https://bible-backups.s3.amazonaws.com/${FILE}"
HTTP_FILE_SRC="https://bible-backups.s3.amazonaws.com/${FILE_SRC}"
if [[ -z ${SRC+x} ]]; then
SRC="${HOME}/${SRC_FOLDER_NAME}"
fi
mkdir -p "${SRC}"
download_cert() {
if [[ ! -f "${1}${2}" ]]; then
sudo mkdir -p "${1}"
aws s3 cp s3://"foxy-certs/${EC2_INSTANCE_NAME}/${2}" .
sudo cp "${2}" "${1}"
fi
}
put_certs() {
download_cert /etc/ssl/certs/ ca_bundle.crt
download_cert /etc/ssl/certs/ certificate.crt
download_cert /etc/ssl/private/ private.key
}
chown_current_user() {
sudo chown -R "$(whoami):$(id -g -n)" .
}
install_jq() {
# TODO: add prepare: install sudo make git
if [[ ! $(jq --help) ]]; then
git clone https://github.com/stedolan/jq.git
cd jq
autoreconf -i
./configure --disable-maintainer-mode
make
sudo make install
fi
}
get_pending_time_shutdown() {
date --date "@$(head -1 /run/systemd/shutdown/scheduled | cut -c6-15)"
}
set_rsync_params() {
RSYNC_PARAMS_UPLOAD_SOURCE_CODE=(-rv --size-only --progress --stats --human-readable --exclude '.idea')
}
install_pigz() {
PIGZ_VERSION=2.7
NAME_PIGZ=pigz-${PIGZ_VERSION}
if [[ $(pigz --version | awk -F ' ' '{print $2}') != "${PIGZ_VERSION}" ]]; then
cd ~/
curl -O https://zlib.net/pigz/${NAME_PIGZ}.tar.gz
tar -xzvf ${NAME_PIGZ}.tar.gz
cd ${NAME_PIGZ}
make
sudo cp -f pigz /usr/bin/
cd ~/
fi
pigz --version
}
aws_get_host() {
EC2_INSTANCE_HOST=$(aws ec2 describe-instances --instance-ids "${EC2_INSTANCE}" --query "Reservations[*].Instances[*].[PublicIpAddress]" --output text)
while [[ "$EC2_INSTANCE_HOST" == "None" ]]; do
sleep 5
aws_get_host
done
}
aws_wait_status_running() {
EC2_INSTANCE_STATUS=$(aws_get_instance_status)
while [[ "$EC2_INSTANCE_STATUS" != "running" ]]; do
sleep 5
aws_wait_status_running
done
}
set_ec2_instance() {
EC2_INSTANCE=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=${EC2_INSTANCE_NAME}" --query 'Reservations[*].Instances[*].[InstanceId]' --output text)
}
aws_stop() {
set_ec2_instance
aws ec2 stop-instances --instance-ids "${EC2_INSTANCE}"
}
aws_get_instance_status() {
aws ec2 describe-instance-status --instance-ids "${EC2_INSTANCE}" --query "InstanceStatuses[*].InstanceState.Name" --output text
}
install_aws() {
if ! which aws; then
if [[ "${PLATFORM_HOST}" == "ubuntu" ]]; then
install_for_ubuntu python3-pip
elif [[ "${PLATFORM_HOST}" == "sailfishos" ]]; then
sudo zypper -n install python3-pip
fi
sudo pip install awscli
aws --version
fi
mkdir -p ~/.aws/
echo "[default]
region = ${AWS_REGION}" >~/.aws/config
}
set_ssh() {
if [[ "${PLATFORM_HOST}" == "ubuntu" ]]; then
install_for_ubuntu openssh-client
elif [[ "${PLATFORM_HOST}" == "sailfishos" ]]; then
sudo zypper -n install openssh
fi
mkdir -p "${HOME}"/.ssh
touch ~/.ssh/known_hosts
chmod 0700 "${HOME}"/.ssh
if [[ ! -f "${SSH_ID_RSA}" ]]; then
ssh-keygen -t rsa -q -f "${SSH_ID_RSA}" -N ""
chmod 600 "${SSH_ID_RSA}"
chmod 600 "${SSH_ID_RSA_PUB}"
fi
}
get_ec2_instance_user() {
EC2_INSTANCE_USER=$(aws secretsmanager get-secret-value --secret-id "${EC2_INSTANCE_NAME}" --query 'SecretString' --output text | grep -o '"EC2_INSTANCE_USER":"[^"]*' | grep -o '[^"]*$')
}
get_ec2_instance_host() {
EC2_INSTANCE_HOST=$(aws secretsmanager get-secret-value --secret-id "${EC2_INSTANCE_NAME}" --query 'SecretString' --output text | grep -o '"EC2_INSTANCE_HOST":"[^"]*' | grep -o '[^"]*$')
}
get_ec2_get_config_app() {
CONFIG_APP=$(aws secretsmanager get-secret-value --secret-id "${EC2_INSTANCE_NAME}" --query 'SecretString' --output text | jq -r '.CONFIG_APP')
echo "${CONFIG_APP}" > "${SRC}"/config.json
}
get_ec2_github_token() {
GIT_HUB_TOKEN_REGISTRY=$(aws secretsmanager get-secret-value --secret-id github --query 'SecretString' --output text | grep -o '"GIT_HUB_TOKEN_REGISTRY":"[^"]*' | grep -o '[^"]*$')
}
get_ec2_instance_identify_file() {
IDENTITY_FILE=$(aws secretsmanager get-secret-value --secret-id "${EC2_INSTANCE_NAME}" --query 'SecretString' --output text | grep -o '"IDENTITY_FILE":"[^"]*' | grep -o '[^"]*$')
}
get_ec2_instance_foxy_client() {
FOXY_CLIENT=$(aws secretsmanager get-secret-value --secret-id "${EC2_INSTANCE_NAME}" --query 'SecretString' --output text | grep -o '"FOXY_CLIENT":"[^"]*' | grep -o '[^"]*$')
}
get_ec2_instance_foxy_admin() {
FOXY_ADMIN=$(aws secretsmanager get-secret-value --secret-id "${EC2_INSTANCE_NAME}" --query 'SecretString' --output text | grep -o '"FOXY_ADMIN":"[^"]*' | grep -o '[^"]*$')
}
get_ec2_instance_app_cloud_name() {
APP_CLOUD_NAME=$(aws secretsmanager get-secret-value --secret-id "${EC2_INSTANCE_NAME}" --query 'SecretString' --output text | grep -o '"APP_CLOUD_NAME":"[^"]*' | grep -o '[^"]*$')
}
get_ec2_instance_sentry() {
SENTRY_DSN=$(aws secretsmanager get-secret-value --secret-id "${EC2_INSTANCE_NAME}" --query 'SecretString' --output text | grep -o '"SENTRY_DSN":"[^"]*' | grep -o '[^"]*$')
}
ssh_copy_id() {
set_ssh
sshpass -v -p ${SERVER_PASSWORD} ssh-copy-id -o StrictHostKeyChecking=no "${SERVER_USER}@${SERVER_HOST}"
}
aws_start() {
if [[ $(aws_get_instance_status) != "running" ]]; then
if [[ ! $(aws ec2 start-instances --instance-ids "${EC2_INSTANCE}") ]]; then
sleep 3
aws_start
else
aws_wait_status_running
fi
fi
}
rsync_from_host_to_sever() {
ssh_copy_id
set_rsync_params
rsync "${RSYNC_PARAMS_UPLOAD_SOURCE_CODE[@]}" --checksum --ignore-times --delete --include "3rdparty/*.cmake" --exclude "config.json" --exclude "3rdparty/*" --exclude "cmake-build-debug" "${BUILD_FOLDER}" "${SERVER_USER}@${SERVER_HOST}:~/" # TODO: check why i nee --checksum --ignore-times to transfer files to ec2 but it doesn't work for github action ubuntu ???
# ssh "${SERVER_USER}@${SERVER_SERVER}" "cd \$BUILD_FOLDER && curl https://spiritecosse.github.io/aws-sailfish-sdk/install.sh | bash -s -- --func=\"chown_current_user\""
}
prepare_aws_instance() {
install_aws
get_ec2_instance_host
get_ec2_instance_user
# set_ec2_instance
# aws_start
# aws_get_host
# set_up_instance_aws_host_to_known_hosts "${EC2_INSTANCE_HOST}"
}
download_backup() {
# if [[ "${PLATFORM_HOST}" == "ubuntu" ]]; then
# install_for_ubuntu bc
# elif [[ "${PLATFORM_HOST}" == "sailfishos" ]]; then
# sudo zypper -n install bc
# fi
rm -f "${1}"_*
SEC=$SECONDS
count=0
start=0
for i in $(seq 1 "${4}" "${3}"); do
end=$(python3 -c "start = int(${start})
end = int(start + ${4} - 1)
size = int(${3})
print(size if end > size else end)")
curl -r "${start}"-"${end}" "${2}" -o "${1}_${count}" &
count=$(echo "${count}" + 1 | bc)
start=$(python3 -c "print(int(${start}) + int(${4}))")
done
wait
echo "after downloads : $((SECONDS - SEC))"
cat $(ls "${1}"_* | sort -V) >"${1}"
rm -f "${1}"_*
}
system_prepare_ubuntu() {
if [[ "$(whoami)" == "root" ]]; then
apt update
apt install -y sudo
fi
sudo apt update -y
sudo apt upgrade -y
# sudo apt dist-upgrade -y
# sudo dpkg --configure -a
}
file_get_size() {
curl -sI "${1}" | grep -i Content-Length | awk '{print ($2+0)}'
}
download_backup_from_aws() {
cd ~/
if [[ -d "${5}" ]]; then
ls -la "${5}"
ls -la .
return
fi
if [[ $(aws s3 ls s3://bible-backups/"${1}") ]]; then
download_backup "${4}" "${3}" "$(file_get_size "${3}")" "$(python3 -c "print(100 * 1024 * 1024)")"
wait
unpigz -v "${1}" # TODO: this line is broken on the ubuntu, i will fix it in the future
tar -xf "${2}"
rm -f "${4}"* "${2}"
chown_current_user
else
echo "Cannot find file ${1} on aws s3"
mkdir -p "${5}"
fi
ls -la "${5}"
ls -la .
}
upload_backup() {
cd "${HOME}"
tar --use-compress-program="pigz -k " -cf "${1}" "${2}"
SEC=$SECONDS
aws s3 cp "${1}" s3://bible-backups
echo "after aws s3 cp : $((SECONDS - SEC))"
rm "${1}"
}
deploy_qml_files_to_device() {
# if [[ $(find . -mmin -3 -type f | grep "qml") && ! $(find . -mmin -3 -type f | grep ".cpp" && find . -mmin -3 -type f | grep ".h") ]]; then
# set_rsync_params
# rsync --rsync-path="sudo rsync" "${RSYNC_PARAMS_UPLOAD_SOURCE_CODE[@]}" qml/ "[email protected]:/usr/share/bible/qml";
# fi
set_rsync_params
rsync --rsync-path="sudo rsync" "${RSYNC_PARAMS_UPLOAD_SOURCE_CODE[@]}" --checksum --ignore-times --delete qml/ "[email protected]:/usr/share/harbour-freebible/qml"
ssh "[email protected]" "
export ARCH=${ARCH}
export RELEASE=${RELEASE}
export PLATFORM=${PLATFORM}
curl https://spiritecosse.github.io/aws-sailfish-sdk/install.sh | bash -s -- --func=\"run_app_on_device\"
"
}
get_device_ip() {
DEVICE_IP=$(echo "${SSH_CLIENT}" | awk '{ print $1}')
}
set_up_instance_host_to_known_hosts() {
set_ssh
if ! grep "$1" ~/.ssh/known_hosts; then
if [[ ! $(ssh-keyscan -H "$1") ]]; then
set_up_instance_host_to_known_hosts "$1"
return
else
SSH_KEYSCAN=$(ssh-keyscan -H "$1")
fi
printf "#start %s\n%s\n#end %s\n" "$1" "$SSH_KEYSCAN" "$1" >>~/.ssh/known_hosts
sshpass -p "${PASSWORD}" ssh-copy-id -i "${SSH_ID_RSA_PUB}" "${EC2_INSTANCE_USER}@${DEVICE_IP}"
fi
}
install_sshpass() {
# Add install sshpass to Dockerfile
if [[ ! $(which sshpass) ]]; then
cd ~/
curl -O -L https://deac-riga.dl.sourceforge.net/project/sshpass/sshpass/1.10/sshpass-1.10.tar.gz
tar -xf sshpass-1.10.tar.gz
cd sshpass-1.10
./configure
make
sudo make install
fi
}
set_access_ssh_to_device() {
install_sshpass
get_device_ip
get_ec2_instance_user
get_sony_xperia_10_password
set_up_instance_host_to_known_hosts "${DEVICE_IP}"
}
install_asan() {
if [[ "${PLATFORM_HOST}" == "ubuntu" ]]; then
system_prepare_ubuntu
install_for_ubuntu libasan6
elif [[ "${PLATFORM_HOST}" == "sailfishos" ]]; then
sudo zypper -n install libasan
fi
}
mb2_set_target() {
alias mb2="mb2 --target SailfishOS-$RELEASE-$ARCH"
}
mb2_cmake_build() {
cd "${BUILD_FOLDER}"
mb2_set_target
chown_current_user
mb2 build-init "${SRC}"
rm -fr rpm
cp -fr "${SRC}"/rpm .
mb2 build-requires
mb2 cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/usr -DBUILD_TESTING=ON -DCODE_COVERAGE=ON -S "${SRC}" -B "${BUILD_FOLDER}"
mb2 cmake --build . -j "$((2 * $(getconf _NPROCESSORS_ONLN)))"
}
set_clang_variables() {
# include if its match with debian or ubuntu
if [[ "${PLATFORM_HOST}" == "ubuntu" ]] || [[ "${PLATFORM_HOST}" == "debian" ]]; then
if [[ "${ARCH}" == "x86_64" ]]; then
export CLANG_FILE_PATH="clang+llvm-${LLVM_TAG}-${ARCH}-linux-gnu-ubuntu-22.04"
elif [[ "${ARCH}" == "aarch64" ]]; then
export CLANG_FILE_PATH="clang+llvm-${LLVM_TAG}-${ARCH}-linux-gnu"
fi
elif [[ "${PLATFORM_HOST}" == "darwin" ]]; then
export CLANG_FILE_PATH="clang+llvm-${LLVM_TAG}-${ARCH}-apple-${PLATFORM_HOST}22.0"
fi
export CLANG_FILE_FILE="${CLANG_FILE_PATH}.tar.xz"
export llvm_path_root="$HOME/${CLANG_FILE_PATH}/"
export llvm_path="$HOME/${CLANG_FILE_PATH}/bin"
}
install_clang() {
set_clang_variables
if [[ ! -d "${llvm_path}" ]]; then
cd ~/
HTTP_FILE="https://github.com/llvm/llvm-project/releases/download/llvmorg-${LLVM_TAG}/${CLANG_FILE_FILE}"
curl -O -L "${HTTP_FILE}"
ls -lah
tar xf "${CLANG_FILE_FILE}"
# cd llvm-project
# mkdir -p build
# cd build
# -DLLVM_DISTRIBUTION_COMPONENTS="clang-apply-replacements;clang-format;clang-query;clang-resource-headers;clang-tidy;clang;clangd;clang-extdef-mapping;cmake-exports;dsymutil;lld;llvm-addr2line;llvm-ar;llvm-as;llvm-cov;llvm-cvtres;llvm-cxxmap;llvm-dlltool;llvm-dwp;llvm-dwarfdump;llvm-install-name-tool;llvm-lib;llvm-lipo;llvm-nm;llvm-objcopy;llvm-objdump;llvm-pdbutil;llvm-profdata;llvm-ranlib;llvm-rc;llvm-readelf;llvm-strings;llvm-strip;llvm-symbolizer;llvm-windres;LTO;builtins;compiler-rt;cxx-headers;compiler-rt"
# cmake -DCMAKE_INSTALL_PREFIX=/usr -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;compiler-rt;lld;llvm;llvm-cov;libcxx;libcxxabi" -DLLVM_TARGETS_TO_BUILD='ARM;X86;AArch64' -DLLVM_USE_SANITIZER="Address;Memory;Undefined;MemoryWithOrigins;Thread;Leak" -DCMAKE_BUILD_TYPE=Release -G "Ninja" ..
# ### possible to set up -DLLVM_ENABLE_PROJECTS=all
# ninja
# sudo ninja install
# clang --version
# llvm-cov --version
fi
if [[ ":$PATH:" != *":$llvm_path:"* ]]; then
export PATH="$llvm_path:$PATH"
echo "Added $llvm_path to PATH."
else
echo "$llvm_path is already in PATH."
fi
if [[ "${PLATFORM_HOST}" == "darwin" ]]; then
echo -e "\e[33mWARNING: Dont forget to run: install_name_tool -add_rpath ${CLANG_FILE_PATH}/lib/ /path/bin/server.\e[0m"
fi
}
create_server_python() {
system_prepare_ubuntu
install_for_ubuntu python3-pip
install_aws
sudo pip install flask
sh -c "cat <<EOF > foxy_server.py
from flask import Flask, send_file
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=80)
EOF"
sudo python3 foxy_server.py
}
create_config_file() {
echo "{
\"listeners\": [
{
\"address\": \"0.0.0.0\",
\"port\": 443,
\"https\": true,
\"cert\": \"$CERT_PATH\",
\"key\": \"$KEY_PATH\"
}
],
\"db_clients\": [
{
\"name\": \"default\",
\"rdbms\": \"postgresql\",
\"host\": \"localhost\",
\"port\": 5432,
\"dbname\": \"$SERVER_PSQL_DBNAME\",
\"user\": \"$SERVER_PSQL_USER\",
\"passwd\": \"$SERVER_PSQL_PASSWORD\",
\"is_fast\": true,
\"connection_number\": 1,
\"filename\": \"\"
},
{
\"name\": \"default_not_fast\",
\"rdbms\": \"postgresql\",
\"host\": \"localhost\",
\"port\": 5432,
\"dbname\": \"$SERVER_PSQL_DBNAME\",
\"user\": \"$SERVER_PSQL_USER\",
\"passwd\": \"$SERVER_PSQL_PASSWORD\",
\"is_fast\": false,
\"connection_number\": 1,
\"filename\": \"\"
}
]
}" > "${BUILD_FOLDER}"/config.json
config_file="/etc/supervisor/conf.d/foxy_server.conf"
# Create the file with the specified content using sudo
sudo sh -c "cat <<EOF > $config_file
[program:foxy_server]
command=${BUILD_FOLDER}/foxy_server
autostart=true
autorestart=true
stderr_logfile=/var/log/foxy_server.err.log
stdout_logfile=/var/log/foxy_server.out.log
EOF"
}
supervisorctl() {
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl restart foxy_server
sudo supervisorctl status
}
install_for_ubuntu() {
programs=()
for lib in "$@"; do
if [[ ! $(dpkg -s ${lib}) ]]; then
echo "============================================= install ${lib} ===================================================="
sudo apt-get install -y ${lib}
fi
programs+=("${lib}: $(dpkg -s ${lib} | grep Status && dpkg -s ${lib} | grep Version || echo 'Not installed')\n")
done
if [[ "${programs}" ]]; then
log_app_msg "Installed programs: "
echo -e "${programs[@]}"
fi
}
set_tz() {
sudo timedatectl set-timezone Europe/Madrid
timedatectl
}
foxy_sever_libs() {
system_prepare_ubuntu
install_for_ubuntu sudo gnupg lsb-release curl ca-certificates wget software-properties-common gnupg
curl -fsSL https://apt.llvm.org/llvm.sh | sudo bash -s 17
clang-17 --version
# System preparation and library installation
system_prepare_ubuntu
install_for_ubuntu uuid-dev libjsoncpp-dev cmake make zlib1g-dev supervisor jq libpq-dev micro unzip nlohmann-json3-dev libcurl4-openssl-dev libboost-all-dev git curl xz-utils rsync sshpass libunwind-dev binutils-dev libunwind8 ninja-build postgresql postgresql-client
}
rsync_share_to_src() {
install_for_ubuntu rsync
mkdir -p "${SRC}"
cd "${SRC}"
set_rsync_params
sudo rsync "${RSYNC_PARAMS_UPLOAD_SOURCE_CODE[@]}" --delete --include "3rdparty/*.cmake" --exclude "3rdparty/*" /share/ .
chown_current_user
ls -la .
}
cmake_build() {
# Print the variables
echo "APP_CLOUD_NAME: $APP_CLOUD_NAME"
echo "APP_BUCKET_HOST: $APP_BUCKET_HOST"
echo "CMAKE_BUILD_TYPE: $CMAKE_BUILD_TYPE"
echo "FOXY_ADMIN: $FOXY_ADMIN"
echo "FOXY_CLIENT: $FOXY_CLIENT"
# echo "SENTRY_DSN: $SENTRY_DSN"
echo "SERVER_HOST: $SERVER_HOST"
echo "SERVER_USER: $SERVER_USER"
echo "SERVER_PASSWORD: $SERVER_PASSWORD"
echo "SERVER_PSQL_DBNAME: $SERVER_PSQL_DBNAME"
echo "SERVER_PSQL_USER: $SERVER_PSQL_USER"
echo "SERVER_PSQL_PASSWORD: $SERVER_PSQL_PASSWORD"
echo "TWITTER_ACCESS_TOKEN: $TWITTER_ACCESS_TOKEN"
echo "TWITTER_ACCESS_TOKEN_SECRET: $TWITTER_ACCESS_TOKEN_SECRET"
echo "TWITTER_API_KEY: $TWITTER_API_KEY"
echo "TWITTER_API_SECRET: $TWITTER_API_SECRET"
echo "TWITTER_BEARER_TOKEN: $TWITTER_BEARER_TOKEN"
echo "LLVM_TAG: $LLVM_TAG"
echo "KEY_PATH: $KEY_PATH"
echo "CERT_PATH: $CERT_PATH"
echo "BUILD_FOLDER: $BUILD_FOLDER"
echo "SRC: $SRC"
echo "CONFIG_APP_PATH: $CONFIG_APP_PATH"
echo "FOXY_HTTP_PORT: $FOXY_HTTP_PORT"
echo "ENVIRONMENT: $ENVIRONMENT"
echo "PINTEREST_API_HOST: $PINTEREST_API_HOST"
echo "PINTEREST_ACCESS_TOKEN: $PINTEREST_ACCESS_TOKEN"
echo "PINTEREST_BOARD_ID: $PINTEREST_BOARD_ID"
# Mount directories
echo "Mounting current directory to SRC: $SRC"
echo "Mounting ~/build to BUILD_FOLDER: $BUILD_FOLDER"
if [[ -z ${CMAKE_BUILD_TYPE+x} ]]; then
CMAKE_BUILD_TYPE=Debug
fi
# Set the environment variables for the C and C++ compilers
git config --global --add safe.directory "${SRC}"
# check existing folder $llvm_path
cd "${SRC}"
chown_current_user
cd "${BUILD_FOLDER}"
cmake -G Ninja \
-DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}" \
-DBUILD_TESTING=ON \
-DCODE_COVERAGE=ON \
-DCMAKE_C_COMPILER=clang-17 \
-DCMAKE_CXX_COMPILER=clang++-17 \
-S "${SRC}" \
-B "${BUILD_FOLDER}"
cmake --build .
}
remove_folders_from_compile_commands() {
# Directories to exclude
declare -a dirs=("build" "cmake-build-debug" "3rdparty")
# Path to the compile_commands.json file
file="${BUILD_FOLDER}/compile_commands.json"
# Create a temporary file
temp=$(mktemp)
# Copy the original file to the temporary file
cp "$file" "$temp"
# For each directory
for dir in "${dirs[@]}"; do
# Use jq to remove entries where the "file" field contains the directory
jq 'map(select(.file | test("'"$dir"'") | not))' "$temp" > "$file"
# Copy the modified file back to the temporary file for the next iteration
cp "$file" "$temp"
done
# Remove the temporary file
rm "$temp"
}
get_last_modified_file() {
LAST_RPM=$(ls -lt *.rpm | head -1 | awk '{ print $9 }')
}
rpm_install_app() {
cd "${BUILD_FOLDER}"
get_last_modified_file
sudo pkcon -y --allow-reinstall install zypper
sudo zypper -n install --allow-unsigned-rpm --force --details ${LAST_RPM}
}
remove_build_file() {
mkdir -p ~/"${BUILD_FOLDER_NAME}"
cd ~/"${BUILD_FOLDER_NAME}"
rm -fr *
}
download_backup_build_from_aws() {
download_backup_from_aws "${FILE}" "${FILE_TAR}" "${HTTP_FILE}" "${BACKUP_FILE_PATH}" "${BUILD_FOLDER}"
}
download_backup_src_from_aws() {
download_backup_from_aws "${FILE_SRC}" "${FILE_SRC_TAR}" "${HTTP_FILE_SRC}" "${BACKUP_FILE_SRC_PATH}" "${SRC}"
}
mb2_build() {
cd "${BUILD_FOLDER}"
chown_current_user
mb2_set_target
pwd
ls -la .
mb2 build "${SRC}"
}
mb2_deploy_to_device() {
cd "${SRC}"
chown_current_user # because : fatal: Unable to create '/home/mersdk/sailfishos_armv7hl_src/.git/index.lock': Permission denied
install_aws
mb2_build
cd "${BUILD_FOLDER}/RPMS"
ls -lah
set_access_ssh_to_device # Todo make async
get_last_modified_file
run_commands_on_device remove_build_file
scp "${LAST_RPM}" "${EC2_INSTANCE_USER}@${DEVICE_IP}:~/${BUILD_FOLDER_NAME}"
run_commands_on_device rpm_install_app
}
mb2_make_clean() {
cd "${BUILD_FOLDER}"
chown_current_user
mb2_set_target
mb2 make clean
}
mb2_run_tests() {
cd "${BUILD_FOLDER}"
chown_current_user
mb2_set_target
mb2 build-shell ctest --output-on-failure
}
run_app_on_device() {
sudo usermod -aG systemd-journal $(whoami)
systemd-run --user harbour-freebible
journalctl -f /usr/bin/harbour-freebible
}
mb2_run_ccov_all_capture() {
cd "${BUILD_FOLDER}"
chown_current_user
mkdir ccov
mb2 build-shell make ccov-all-capture
}
run_commands_on_device() {
install_aws
set_access_ssh_to_device
ssh "${EC2_INSTANCE_USER}@${DEVICE_IP}" "
export PASSWORD=\"${PASSWORD}\"
export ARCH=${ARCH}
export RELEASE=${RELEASE}
export PLATFORM=${PLATFORM}
curl https://spiritecosse.github.io/aws-sailfish-sdk/install.sh | bash -s -- --func=\"$1\"
"
}
codecov_push_results() {
cd "${BUILD_FOLDER}"
curl -Os https://uploader.codecov.io/latest/linux/codecov
chmod +x codecov
./codecov -t "${CODECOV_TOKEN}" -f ccov/all-merged.info
}
rsync_share_to_build() {
cd "${BUILD_FOLDER}"
set_rsync_params
sudo rsync "${RSYNC_PARAMS_UPLOAD_SOURCE_CODE[@]}" "${SRC}/" .
chown_current_user
}
code_coverage() {
if [[ "${PLATFORM_HOST}" == "ubuntu" ]]; then
system_prepare_ubuntu
install_for_ubuntu curl pigz
elif [[ "${PLATFORM_HOST}" == "sailfishos" ]]; then
sudo zypper -n install curl pigz
fi
df -h
download_backup_build_from_aws
download_backup_src_from_aws
rsync_share_to_src
# rsync_share_to_build
mb2_cmake_build
upload_backup "${FILE}" "${BUILD_FOLDER_NAME}"
upload_backup "${FILE_SRC}" "${SRC_FOLDER_NAME}"
mb2_run_tests
mb2_run_ccov_all_capture
codecov_push_results
wait
}
release() {
if [[ "${PLATFORM_HOST}" == "ubuntu" ]]; then
system_prepare_ubuntu
install_for_ubuntu curl pigz
elif [[ "${PLATFORM_HOST}" == "sailfishos" ]]; then
sudo zypper -n install curl pigz
fi
df -h
download_backup_build_from_aws
download_backup_src_from_aws
rsync_share_to_src
mb2_make_clean
mb2_build
cd "${BUILD_FOLDER}"
sudo cp -f RPMS/*.rpm /share/output
}
log_app_msg() {
echo -ne "[${BLUE}INFO] $@\n"
}
log_failure_msg() {
echo -ne "[${RED}ERROR] $@\n"
}
install_deps() {
system_prepare_ubuntu
# install_for_ubuntu sudo systemd libxcb1 libx11-xcb1 libxcb1 libxcb-glx0 libfontconfig1 libx11-data libx11-xcb1 libxcb-icccm4 libxcb-image0 libxcb-keysyms1 libxcb-randr0 libxcb-render-util0 libxcb-shape0 libxcb-sync1 libxcb-xfixes0 libxcb-xinerama0 libxcb-xkb1 libsm6 libxkbcommon-x11-0 libwayland-egl1 libegl-dev libxcomposite1 libwayland-cursor0 libharfbuzz-dev libxi-dev libtinfo5 ca-certificates curl gnupg lsb-release mesa-utils libgl1-mesa-glx micro lsb-release sudo zsh git
install_for_ubuntu sudo systemd ca-certificates curl micro lsb-release zsh
}
install_virtualbox() {
sudo apt-get install -y virtualbox
log_app_msg "virtualbox has installed successfully."
}
install_docker() {
# install docker
# https://docs.docker.com/engine/install/ubuntu/
if ! which docker; then
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list >/dev/null
sudo apt-get update -y
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
sudo usermod -aG docker "$USER"
log_app_msg "Manage Docker as a non-root user. Successfully."
exit 0
fi
log_app_msg "docker already installed."
}
rm_sdk_settings() {
rm -fr ~/.config/SailfishSDK
}
docker_removecontainers() {
if [[ $(docker ps -aq) ]]; then
docker stop $(docker ps -aq)
fi
if [[ $(docker ps -aq) ]]; then
docker rm $(docker ps -aq)
fi
}
docker_armaggedon() {
docker_removecontainers
docker network prune -f
if [[ $(docker images --filter dangling=true -qa) ]]; then
docker rmi -f $(docker images --filter dangling=true -qa)
fi
if [[ $(docker volume ls --filter dangling=true -q) ]]; then
docker volume rm $(docker volume ls --filter dangling=true -q)
fi
if [[ $(docker images -qa) ]]; then
docker rmi -f $(docker images -qa)
fi
}
set_envs() {
LIBGL_ALWAYS_INDIRECT="LIBGL_ALWAYS_INDIRECT=1"
if ! grep "$LIBGL_ALWAYS_INDIRECT" ~/.bashrc; then
echo "$LIBGL_ALWAYS_INDIRECT" >>~/.bashrc
fi
if ! grep "$LIBGL_ALWAYS_INDIRECT" ~/.zshrc; then
echo "$LIBGL_ALWAYS_INDIRECT" >>~/.zshrc
fi
PATH_="export PATH=$HOME/bin:/usr/local/bin:\$PATH"
if ! grep "$PATH_" ~/.bashrc; then
echo "$PATH_" >>~/.bashrc
fi
if ! grep "$PATH_" ~/.zshrc; then
echo "$PATH_" >>~/.zshrc
fi
}
set_zsh_by_default() {
sudo chsh -s $(which zsh) $(whoami)
}
set_zsh_by_default_user() {
sudo chsh -s $(which zsh) "$1"
}
install_ohmyzsh() {
if [[ ! -d ".oh-my-zsh" && -z ${DOCKER_RUNNING+x} ]]; then
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
fi
set_envs
}
git_submodule_remove() {