-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkash.sh
1729 lines (1485 loc) · 51.8 KB
/
kash.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
#!/usr/bin/env bash
### Variables provided by this script
### - TMP_DIR: a path where to write temp files
### - OS_ID: debian or ubuntu or alpine ...
### - OS_VERSION:
### - CI: true or false
### - CI_ID: github or gitlab or travis or empty (CI = false)
### Docs for CI systems:
### - github actions: https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
### - gitlab ci: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
### - travis ci: https://docs.travis-ci.com/user/environment-variables/#default-environment-variables
### Host detection
###
. /etc/os-release
OS_ID=$ID
OS_VERSION=${VERSION_ID:-}
# VERSION_ID is an optional field, do what we can to deduce when missing
if [[ "$OS_VERSION" = "" ]]; then
case "$OS_ID" in
debian)
OS_VERSION=$(cat /etc/debian_version)
OS_VERSION=${OS_VERSION%%.*}
;;
*)
OS_VERSION=unknown
;;
esac
fi
echo "Running on ${OS_ID}-${OS_VERSION}"
CI=false
CI_ID=
if [ "${GITHUB_ACTIONS:-}" = true ]; then
CI_ID="github"
# Add ~/.local/bin to PATH
mkdir -p "$HOME/.local/bin"
export PATH=$PATH:$HOME/.local/bin
elif [ "${GITLAB_CI:-}" = true ]; then
CI_ID="gitlab"
# Add ~/.local/bin to PATH
mkdir -p "$HOME/.local/bin"
export PATH=$PATH:$HOME/.local/bin
elif [ "${TRAVIS:-}" = true ]; then
CI_ID="travis"
fi
if [ -n "$CI_ID" ]; then
CI=true
echo "Running in CI mode ($CI_ID) ..."
# Make sure we have the requirements to run kash functions
set +e
command -v curl >/dev/null 2>&1 && command -v git >/dev/null 2>&1 && command -v sha256sum >/dev/null 2>&1 && command -v unzip >/dev/null 2>&1
RC=$?
set -e
if [ "$RC" -ne 0 ]; then
case "$OS_ID" in
debian | ubuntu)
if [ "$(id -u)" -eq 0 ]; then
apt-get update && apt-get --no-install-recommends --yes install sudo curl ca-certificates coreutils git unzip
else
sudo apt-get update && sudo apt-get --no-install-recommends --yes install curl ca-certificates coreutils git unzip
fi
;;
alpine)
apk update && apk add curl ca-certificates coreutils git unzip
;;
*)
;;
esac
fi
# Emulate development k-mongo when running on CI
cat <<EOF > ~/.local/bin/k-mongo
#!/usr/bin/env bash
mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork --port 27017
EOF
chmod a+x ~/.local/bin/k-mongo
# Most CI services understand ANSI colors
export TERM=xterm-color
# Allow nvm to work on alpine distro (downloads an unofficial build targeting musl libc)
# See:
# - https://github.com/nvm-sh/nvm/issues/1102#issuecomment-1112898778
# - https://github.com/nvm-sh/nvm/pull/3212
if [ "$OS_ID" = "alpine" ]; then
export NVM_NODEJS_ORG_MIRROR="https://unofficial-builds.nodejs.org/download/release" # Set up unofficial builds
fi
fi
# If nvm is present, make it available to script
if [ -d "$HOME/.nvm" ]; then
. "$HOME/.nvm/nvm.sh"
fi
# If sonar-scanner-cli is present, add it to PATH
# See install_sonar_scanner_cli
if [ -d "$HOME/.local/sonar-scanner" ]; then
export PATH=$PATH:$HOME/.local/sonar-scanner/bin
fi
# Define a TMP_DIR to operate with temp files
if [ -n "${RUNNER_TEMP:-}" ]; then # RUNNER_TEMP is Github Action specific
TMP_DIR="$RUNNER_TEMP"
else
TMP_DIR="$(mktemp -d -p "${XDG_RUNTIME_DIR:-}" kalisio.XXXXXX)"
fi
### Requirements
###
# https://github.com/mikefarah/yq/releases
YQ_VERSION=4.40.5
# https://github.com/FiloSottile/age/releases
AGE_VERSION=1.1.1
# https://github.com/getsops/sops/releases
SOPS_VERSION=3.8.1
# https://github.com/kubernetes/kubernetes/tree/master/CHANGELOG
KUBECTL_VERSION=1.28.13
# https://github.com/helm/helm/releases
HELM_VERSION=3.14.4
# https://github.com/helmfile/helmfile/releases
HELMFILE_VERSION=0.167.1
# https://github.com/derailed/k9s/releases
K9S_VERSION=0.32.4
# https://github.com/nvm-sh/nvm/releases
NVM_VERSION=0.39.7
# https://nodejs.org/en/about/previous-releases#looking-for-latest-release-of-a-version-branch
NODE16_VERSION=16.20.2
NODE18_VERSION=18.19.1
NODE20_VERSION=20.11.1
NODE22_VERSION=22.3.0
# https://www.mongodb.com/try/download/community
MONGODB7_VERSION=7.0.15
MONGODB8_VERSION=8.0.3
# https://docs.sonarsource.com/sonarqube-server/latest/analyzing-source-code/scanners/sonarscanner/
SONAR_SCANNER_CLI_VERSION=6.2.1.4610
# Install yq in ~/.local/bin
# Arg1: a writable folder where to write downloaded files
install_yq() {
local DL_ROOT=$1
local DL_PATH="$DL_ROOT/yq"
if [ ! -d "$DL_PATH" ]; then
mkdir -p "$DL_PATH" && cd "$DL_PATH"
curl -OLsS https://github.com/mikefarah/yq/releases/download/v${YQ_VERSION}/yq_linux_amd64.tar.gz
# checksum has to be extracted from custom file ...
curl -OLsS https://github.com/mikefarah/yq/releases/download/v${YQ_VERSION}/checksums
curl -OLsS https://github.com/mikefarah/yq/releases/download/v${YQ_VERSION}/checksums_hashes_order
curl -OLsS https://github.com/mikefarah/yq/releases/download/v${YQ_VERSION}/extract-checksum.sh
chmod u+x extract-checksum.sh
./extract-checksum.sh "SHA-256" "yq_linux_amd64.tar.gz" | awk '{ print $2 " " $1}' | sha256sum --check --quiet
cd ~-
fi
cd "$DL_PATH"
tar xf yq_linux_amd64.tar.gz
mv yq_linux_amd64 ~/.local/bin/yq
chmod u+x ~/.local/bin/yq
cd ~-
}
# Call this to ensure yq is available
ensure_yq() {
set +e
command -v yq >/dev/null 2>&1
local RC=$?
set -e
if [ "$RC" -ne 0 ]; then
mkdir -p "$TMP_DIR/dl"
install_yq "$TMP_DIR/dl"
fi
}
# Install age in ~/.local/bin
# Arg1: a writable folder where to write downloaded files
install_age() {
local DL_ROOT=$1
local DL_PATH="$DL_ROOT/age"
if [ ! -d "$DL_PATH" ]; then
mkdir -p "$DL_PATH" && cd "$DL_PATH"
curl -OLsS https://github.com/FiloSottile/age/releases/download/v${AGE_VERSION}/age-v${AGE_VERSION}-linux-amd64.tar.gz
# no checksum ...
cd ~-
fi
cd "$DL_PATH"
tar xf age-v${AGE_VERSION}-linux-amd64.tar.gz
cp age/age ~/.local/bin
cp age/age-keygen ~/.local/bin
cd ~-
}
# Call this to ensure age is available
ensure_age() {
set +e
command -v age >/dev/null 2>&1
local RC=$?
set -e
if [ "$RC" -ne 0 ]; then
mkdir -p "$TMP_DIR/dl"
install_age "$TMP_DIR/dl"
fi
}
# Install sops in ~/.local/bin
# Arg1: a writable folder where to write downloaded files
install_sops() {
local DL_ROOT=$1
local DL_PATH="$DL_ROOT/sops"
if [ ! -d "$DL_PATH" ]; then
mkdir -p "$DL_PATH" && cd "$DL_PATH"
curl -OLsS https://github.com/getsops/sops/releases/download/v${SOPS_VERSION}/sops-v${SOPS_VERSION}.linux.amd64
curl -OLsS https://github.com/getsops/sops/releases/download/v${SOPS_VERSION}/sops-v${SOPS_VERSION}.checksums.txt
sha256sum --ignore-missing --quiet -c sops-v${SOPS_VERSION}.checksums.txt
cd ~-
fi
cd "$DL_PATH"
cp sops-v${SOPS_VERSION}.linux.amd64 ~/.local/bin/sops
chmod u+x ~/.local/bin/sops
cd ~-
}
# Call this to ensure sops is available
ensure_sops() {
set +e
command -v sops >/dev/null 2>&1
local RC=$?
set -e
if [ "$RC" -ne 0 ]; then
mkdir -p "$TMP_DIR/dl"
install_sops "$TMP_DIR/dl"
fi
}
# Install code climate test reporter in ~/.local/bin
# Arg1: a writable folder where to write downloaded files
install_cc_test_reporter() {
local DL_ROOT=$1
local DL_PATH="$DL_ROOT/cc"
if [ ! -d "$DL_PATH" ]; then
mkdir -p "$DL_PATH" && cd "$DL_PATH"
curl -OLsS https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64
curl -OLsS https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64.sha256
sha256sum --ignore-missing --quiet -c test-reporter-latest-linux-amd64.sha256
cd ~-
fi
cd "$DL_PATH"
cp test-reporter-latest-linux-amd64 ~/.local/bin/cc-test-reporter
chmod +x ~/.local/bin/cc-test-reporter
cd ~-
}
# Sends test coverage to code climate
# Arg1: code climate identifier for authentication
# Arg2: prefix to use when using format-coverage (can be empty)
send_coverage_to_cc() {
local CC_TEST_REPORTER_ID=$1
local CC_PREFIX=${2:-}
~/.local/bin/cc-test-reporter format-coverage -t lcov --add-prefix "$CC_PREFIX" coverage/lcov.info
~/.local/bin/cc-test-reporter upload-coverage -r "$CC_TEST_REPORTER_ID"
}
# Make sure nvm is installed
# Arg1: a writable folder where to write downloaded files
# NOTE: also define 'yarn' as a default package, ie. it'll be automatically
# installed with each node version
install_nvm() {
local DL_ROOT=$1
local DL_PATH="$DL_ROOT/nvm"
# Node builds for alpine x64/musl required libstdc++
# See. https://github.com/nvm-sh/nvm/issues/1102#issuecomment-550572252
if [ "$OS_ID" = "alpine" ] && [ "$CI" = true ]; then
apk add libstdc++
fi
mkdir -p "$DL_PATH" && cd "$DL_PATH"
curl -OLsS https://raw.githubusercontent.com/nvm-sh/nvm/v${NVM_VERSION}/install.sh
# Make sure current user has a .bashrc where nvm installer will setup things since we mandate bash as execution shell
if [ ! -f "$HOME/.bashrc" ]; then touch "$HOME/.bashrc"; fi
bash ./install.sh
# We always use yarn as package manager, so tell nvm to install it with every node installation
# cf. https://github.com/nvm-sh/nvm?tab=readme-ov-file#default-global-packages-from-file-while-installing
bash -i -c 'echo yarn >> $NVM_DIR/default-packages'
cd ~-
}
# Install node16, requires nvm to be installed
install_node16() {
bash -i -c "nvm install $NODE16_VERSION"
}
# Install node18, requires nvm to be installed
install_node18() {
bash -i -c "nvm install $NODE18_VERSION"
}
# Install node20, requires nvm to be installed
install_node20() {
bash -i -c "nvm install $NODE20_VERSION"
}
# Install node22, requires nvm to be installed
install_node22() {
bash -i -c "nvm install $NODE22_VERSION"
}
# Install mongo7 in ~/.local/bin/mongo7
# Arg1: a writable folder where to write downloaded files
# NOTE: each mongo version is installed in a separate folder to support multiple versions
install_mongo7() {
local DL_ROOT=$1
local DL_PATH="$DL_ROOT/mongo7"
mkdir -p "$DL_PATH" && cd "$DL_PATH"
case "$OS_ID" in
debian)
local MONGODB_SUFFIX=debian12-${MONGODB7_VERSION}
;;
ubuntu)
local MONGODB_SUFFIX=ubuntu2204-${MONGODB7_VERSION}
;;
*)
esac
curl -OLsS "https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-${MONGODB_SUFFIX}.tgz"
tar xf "mongodb-linux-x86_64-${MONGODB_SUFFIX}.tgz"
mkdir -p ~/.local/bin/mongo7
cp -fR "mongodb-linux-x86_64-${MONGODB_SUFFIX}/bin/mongod" ~/.local/bin/mongo7
sudo mkdir -p /var/lib/mongo7 && sudo mkdir -p /var/log/mongodb7
sudo chmod a+rwx /var/lib/mongo7 && sudo chmod a+rwx /var/log/mongodb7
cd ~-
}
# Install mongo8 in ~/.local/bin/mongo8
# Arg1: a writable folder where to write downloaded files
# NOTE: each mongo version is installed in a separate folder to support multiple versions
install_mongo8() {
local DL_ROOT=$1
local DL_PATH="$DL_ROOT/mongo8"
mkdir -p "$DL_PATH" && cd "$DL_PATH"
case "$OS_ID" in
debian)
local MONGODB_SUFFIX=debian12-${MONGODB8_VERSION}
;;
ubuntu)
local MONGODB_SUFFIX=ubuntu2204-${MONGODB8_VERSION}
;;
*)
esac
curl -OLsS "https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-${MONGODB_SUFFIX}.tgz"
tar xf "mongodb-linux-x86_64-${MONGODB_SUFFIX}.tgz"
mkdir -p ~/.local/bin/mongo8
cp -fR "mongodb-linux-x86_64-${MONGODB_SUFFIX}/bin/mongod" ~/.local/bin/mongo8
sudo mkdir -p /var/lib/mongo8 && sudo mkdir -p /var/log/mongodb8
sudo chmod a+rwx /var/lib/mongo8 && sudo chmod a+rwx /var/log/mongodb8
cd ~-
}
# Install sonar-scanner in ~/.local/sonar-scanner-cli
# Arg1: a writable folder where to write downloaded files
install_sonar_scanner_cli() {
local DL_ROOT=$1
local DL_PATH="$DL_ROOT/sonarscannercli"
if [ ! -d "$DL_PATH" ]; then
mkdir -p "$DL_PATH" && cd "$DL_PATH"
curl -OLsS https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-${SONAR_SCANNER_CLI_VERSION}-linux-x64.zip
unzip sonar-scanner-cli-${SONAR_SCANNER_CLI_VERSION}-linux-x64.zip
cd ~-
fi
cd "$DL_PATH"
mv sonar-scanner-${SONAR_SCANNER_CLI_VERSION}-linux-x64 ~/.local/sonar-scanner
cd ~-
}
# Install kubectl in ~/.local/bin
# Expected args:
# 1. a writable folder where to write downloaded files
install_kubectl() {
local DL_ROOT=$1
local DL_PATH="$DL_ROOT/kubectl"
if [ ! -d "$DL_PATH" ]; then
mkdir -p "$DL_PATH" && cd "$DL_PATH"
curl -OLsS https://dl.k8s.io/release/v${KUBECTL_VERSION}/bin/linux/amd64/kubectl && \
curl -OLsS https://dl.k8s.io/release/v${KUBECTL_VERSION}/bin/linux/amd64/kubectl.sha256 && \
echo "$(cat kubectl.sha256) kubectl" | sha256sum --check
cd ~-
fi
cd "$DL_PATH"
mv kubectl ~/.local/bin/kubectl
chmod u+x ~/.local/bin/yq
cd ~-
}
# Install helm in ~/.local/bin
# Expected args:
# 1. a writable folder where to write downloaded files
install_helm() {
local DL_ROOT=$1
local DL_PATH="$DL_ROOT/helm"
if [ ! -d "$DL_PATH" ]; then
mkdir -p "$DL_PATH" && cd "$DL_PATH"
curl -OLsS https://get.helm.sh/helm-v${HELM_VERSION}-linux-amd64.tar.gz
curl -OLsS https://get.helm.sh/helm-v${HELM_VERSION}-linux-amd64.tar.gz.sha256sum
sha256sum --ignore-missing --quiet -c helm-v${HELM_VERSION}-linux-amd64.tar.gz.sha256sum
cd ~-
fi
cd "$DL_PATH"
tar xf helm-v${HELM_VERSION}-linux-amd64.tar.gz
cp linux-amd64/helm ~/.local/bin
cd ~-
}
# Install helmfile in ~/.local/bin
# Expected args:
# 1. a writable folder where to write downloaded files
install_helmfile() {
local DL_ROOT=$1
local DL_PATH="$DL_ROOT/helmfile"
if [ ! -d "$DL_PATH" ]; then
mkdir -p "$DL_PATH" && cd "$DL_PATH"
curl -OLsS https://github.com/helmfile/helmfile/releases/download/v${HELMFILE_VERSION}/helmfile_${HELMFILE_VERSION}_linux_amd64.tar.gz
curl -OLsS https://github.com/helmfile/helmfile/releases/download/v${HELMFILE_VERSION}/helmfile_${HELMFILE_VERSION}_checksums.txt
sha256sum --ignore-missing --quiet -c helmfile_${HELMFILE_VERSION}_checksums.txt
cd ~-
fi
cd "$DL_PATH"
tar xf helmfile_${HELMFILE_VERSION}_linux_amd64.tar.gz
cp helmfile ~/.local/bin
cd ~-
}
# Install k9s in ~/.local/bin
# Expected args:
# 1. a writable folder where to write downloaded files
install_k9s() {
local DL_ROOT=$1
local DL_PATH="$DL_ROOT/k9s"
if [ ! -d "$DL_PATH" ]; then
mkdir -p "$DL_PATH" && cd "$DL_PATH"
curl -OLsS https://github.com/derailed/k9s/releases/download/v${K9S_VERSION}/k9s_Linux_amd64.tar.gz
curl -OLsS https://github.com/derailed/k9s/releases/download/v${K9S_VERSION}/checksums.sha256
sha256sum --ignore-missing --quiet -c checksums.sha256
cd ~-
fi
cd "$DL_PATH"
tar xf k9s_Linux_amd64.tar.gz
cp k9s ~/.local/bin
cd ~-
}
# Install listed requirements
# Usage: install_reqs mongo7 nvm node16 yq
install_reqs() {
mkdir -p "$TMP_DIR/dl"
for REQ in "$@"; do
echo "Installing $REQ ..."
install_"$REQ" "$TMP_DIR/dl"
done
}
# Select which node version is active (ie. which one is started when calling node)
use_node() {
local VERSION=$1
nvm use "$VERSION"
}
# Select which mongo version is active (ie. which one is started when calling mongod)
use_mongo() {
local VERSION=$1
if [ "$CI" = true ]; then
# CI k-mongo will use whatever binary is in $HOME/.local/bin
# Binaries
ln -sf "$HOME/.local/bin/mongo$VERSION/mongod" ~/.local/bin
# And working dirs
sudo ln -sf "/var/lib/mongo$VERSION" /var/lib/mongo
sudo ln -sf "/var/log/mongodb$VERSION" /var/log/mongodb
echo "Now using mongo $VERSION:"
"$HOME/.local/bin/mongod" --version
else
# Developer's k-mongo will use MONGO_VERSION
export MONGO_VERSION="$VERSION"
fi
}
### Utils
###
get_json_value() {
local JSON_SRC="$1"
local JSON_FIELD="$2"
ensure_yq
yq --output-format=yaml ".$JSON_FIELD" "$JSON_SRC"
}
# Extract version major from a version string.
# Expected args:
# 1. the version string
get_semver_major() {
local VERSION="$1"
local VERSION_REGEX="^([0-9]+)(\.[0-9]+)?(\.[0-9]+)?$"
if [[ "$VERSION" =~ $VERSION_REGEX ]]; then
printf "%s" "${BASH_REMATCH[1]}"
fi
}
# Extract version minor from a version string.
# Expected args:
# 1. the version string
get_semver_minor() {
local VERSION="$1"
local VERSION_REGEX="^[0-9]+\.([0-9]+)(\.[0-9]+)?$"
if [[ "$VERSION" =~ $VERSION_REGEX ]]; then
printf "%s" "${BASH_REMATCH[1]}"
fi
}
# Extract version minor from a version string.
# Expected args:
# 1. the version string
get_semver_patch() {
local VERSION="$1"
local VERSION_REGEX="^[0-9]+\.[0-9]+\.([0-9]+)$"
if [[ "$VERSION" =~ $VERSION_REGEX ]]; then
printf "%s" "${BASH_REMATCH[1]}"
fi
}
### Git
###
# Returns the current git tag (or empty string if not on a tag)
# Arg1: the repository root
get_git_tag() {
case "$CI_ID" in
gitlab)
echo "${CI_COMMIT_TAG:-}"
;;
# github)
# if [ "$GITHUB_REF_TYPE" = "tag" ]; then
# echo "$GITHUB_REF_NAME"
# fi
# ;;
# travis)
# echo "${TRAVIS_TAG:-}"
# ;;
*)
local REPO_ROOT="$1"
cd "$REPO_ROOT"
git tag --points-at
cd ~-
;;
esac
}
# Returns the current git branch (might be empty string if on a tag and repo was checked out with --depth 1)
# Arg1: the repository root
get_git_branch() {
case "$CI_ID" in
gitlab)
if [ -z "${CI_COMMIT_TAG:-}" ]; then
echo "$CI_COMMIT_REF_NAME"
fi
;;
# github)
# if [ "$GITHUB_REF_TYPE" = "branch" ]; then
# echo "$GITHUB_REF_NAME"
# fi
# ;;
# travis)
# if [ -z "$TRAVIS_TAG" ]; then
# echo "$TRAVIS_BRANCH"
# fi
# ;;
*)
local REPO_ROOT="$1"
cd "$REPO_ROOT"
git branch --show-current
cd ~-
;;
esac
}
# Returns the current git commit sha, always defined
# Arg1: the repository root
get_git_commit_sha() {
local REPO_ROOT="$1"
cd "$REPO_ROOT"
git rev-parse HEAD
# case "$CI_ID" in
# github)
# echo "$GITHUB_SHA"
# ;;
# gitlab)
# echo "$CI_COMMIT_SHA"
# ;;
# travis)
# echo "$TRAVIS_COMMIT"
# ;;
# *)
# git rev-parse HEAD
# ;;
# esac
cd ~-
}
# Returns the current git commit _short_ sha, always defined
# Arg1: the repository root
get_git_commit_short_sha() {
local REPO_ROOT="$1"
cd "$REPO_ROOT"
git rev-parse --short HEAD
cd ~-
}
# Returns the list of changed files between two commits
# Arg1: commit to (default to latest known)
# Arg2: commit from (defaults to the one before arg1)
# NOTE: requires git history to work (ie probably not with shallow clone)
# NOTE: needs to be called from inside a git repo
# get_git_changed_files() {
# local COMMIT0=${1:-HEAD}
# local COMMIT1=${2:-"$COMMIT0"^}
# cd "$REPO_ROOT"
# if [ -z "$CI_ID" ]; then
# git diff --name-only "$COMMIT0" "$COMMIT1"
# fi
# cd ~-
# }
# Returns current commit message
# Arg1: the repository root
get_git_commit_message() {
local REPO_ROOT="$1"
cd "$REPO_ROOT"
git show -s --pretty=%B
cd ~-
}
# Returns current commit author name
# Arg1: the repository root
get_git_commit_author_name() {
local REPO_ROOT="$1"
cd "$REPO_ROOT"
git show -s --pretty=%an
cd ~-
}
# Returns current commit author email
# Arg1: the repository root
get_git_commit_author_email() {
local REPO_ROOT="$1"
cd "$REPO_ROOT"
git show -s --pretty=%ae
cd ~-
}
# Shallow clone a repo (with no history), will also (shallow) clone submodules.
# Expected args:
# 1. the url of the repo to clone
# 2. the directory where to clone the repo
# 3. the ref to clone (branch or tag)
git_shallow_clone() {
local REPO_URL=$1
local REPO_DIR=$2
local REPO_REF=${3:-}
local GIT_OPS="--depth 1 --recurse-submodules --shallow-submodules"
if [ -n "$REPO_REF" ]; then
GIT_OPS="$GIT_OPS --branch $REPO_REF"
fi
git clone $GIT_OPS "$REPO_URL" "$REPO_DIR"
}
### Github
###
# Deploys generated documentation using github pages system.
# Arg1: the repository url
# Arg2: the folder where documentation has been generated
# Arg3: the author name to use when commiting the updated documentation.
# Arg4: the author email to use when commiting the updated documentation.
# Arg5: the commit message.
# Arg6: the branch where to commit the documentation (defaults to gh-pages)
deploy_gh_pages() {
local REPO_URL="$1"
local DOCS_DIR="$2"
local COMMIT_AUTHOR_NAME="$3"
local COMMIT_AUTHOR_EMAIL="$4"
local COMMIT_MESSAGE="$5"
local DOCS_BRANCH="${6:-gh-pages}"
local WORK_DIR
WORK_DIR="$(mktemp -d -p "$TMP_DIR" gh_pages.XXXXXX)"
# Clone repo to a temp location
git clone --depth 1 --branch "$DOCS_BRANCH" "$REPO_URL" "$WORK_DIR"
# Copy built doc
cp -fR "$DOCS_DIR"/* "$WORK_DIR"
# Add new doc and commit (add a .nojekyll file to skip Github jekyll processing)
cd "$WORK_DIR" && touch .nojekyll && git add --all && git -c user.name="$COMMIT_AUTHOR_NAME" -c user.email="$COMMIT_AUTHOR_EMAIL" commit --message "$COMMIT_MESSAGE"
# Push
git push origin "$DOCS_BRANCH"
}
### Log
###
KASH_TXT_B="\e["
KASH_TXT_E="m"
KASH_TXT_BOLD="${KASH_TXT_B}1${KASH_TXT_E}"
KASH_TXT_RESET="${KASH_TXT_B}0${KASH_TXT_E}"
# Creates a foldable log section in CI systems
# Arg1: the section title
# NOTE: foldable section must be terminated using end_group and the same $TITLE
begin_group() {
local TITLE="$1"
if [ "$CI" = true ]; then
if [ "$CI_ID" = "github" ]; then
# see https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines
echo "::group::$TITLE"
elif [ "$CI_ID" = "gitlab" ]; then
# see https://docs.gitlab.com/ee/ci/jobs/#custom-collapsible-sections
local SECTION
SECTION=$(echo "$TITLE" | tr ' .:-' '_')
echo -e "section_start:$(date +%s):$SECTION\r\e[0K\e[95m$TITLE\e[0m"
elif [ "$CI_ID" = "travis" ]; then
# see
echo "travis_fold:start:$TITLE"
fi
else
echo -e "${KASH_TXT_BOLD}%< --- $TITLE ------${KASH_TXT_RESET}"
fi
}
# Terminates a foldable log section in CI systems
# Arg1: the section title
end_group() {
local TITLE="$1"
if [ "$CI" = true ]; then
if [ "$CI_ID" = "github" ]; then
echo "::endgroup::"
elif [ "$CI_ID" = "gitlab" ]; then
local SECTION
SECTION=$(echo "$TITLE" | tr ' .:-' '_')
echo -e "section_end:$(date +%s):$SECTION\r\e[0K"
elif [ "$CI_ID" = "travis" ]; then
echo "travis_fold:end:$TITLE"
fi
else
echo -e "${KASH_TXT_BOLD}------ $TITLE --- >%${KASH_TXT_RESET}"
fi
}
### Slack
###
slack_send() {
local PAYLOAD="$1"
local URL="$2"
curl -X POST -H "Content-type: application/json" --data "$PAYLOAD" "$URL"
}
# Push a simple message to a slack channel
# Arg1: the Slack webhook where to push the message
# Arg2: the message (can be markdown formatted)
slack_log() {
local URL="$1"
local MSG="$2"
local PAYLOAD="{ blocks: [ { \"type\": \"section\", \"text\": { \"type\": \"mrkdwn\", \"text\": \"$MSG\" } } ] }"
slack_send "$PAYLOAD" "$URL"
}
# Push a colored message to a slack channel
# Arg1: the Slack webhook where to push the message
# Arg2: the message (can be markdown formatted)
# Arg3: the color to use (as and hex value)
slack_color_log() {
local URL="$1"
local MSG="$2"
local COLOR="$3"
local PAYLOAD="{ attachments: [ { \"color\": \"$COLOR\", blocks: [ { \"type\": \"section\", \"text\": { \"type\": \"mrkdwn\", \"text\": \"$MSG\" } } ] } ] }"
slack_send "$PAYLOAD" "$URL"
}
# Report ci job result to slack channel
# Expected usage is to do the following:
# trap 'slack_ci_report "$ROOT_DIR" "CI step name" "$?" "$SLACK_WEBHOOK_APPS"' EXIT
# Exit code 0 = success, anything else is failure
# Arg1: the repository root
# Arg2: the exit code of the ci job
# Arg3: the slack webhook where to push report
slack_ci_report() {
local REPO_DIR="$1"
local CI_STEP_NAME="$2"
local RET_CODE="$3"
local SLACK_WEBHOOK="$4"
local STATUS="success"
local COLOR="#2eb886"
if [ "$RET_CODE" != "0" ]; then STATUS="failed"; COLOR="#a30200"; fi
local MESSAGE
case "$CI_ID" in
github)
MESSAGE=$(printf "*%s*: %s %s *@%s* (%s, <%s|repo>, <%s|commit>, <%s|run>)" \
"$GITHUB_REPOSITORY" \
"$CI_STEP_NAME" \
"$STATUS" \
"$GITHUB_REF_NAME" \
"$GITHUB_ACTOR" \
"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY" \
"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/commit/$GITHUB_SHA" \
"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID")
;;
*)
;;
esac
slack_color_log "$SLACK_WEBHOOK" "$MESSAGE" "$COLOR"
}
# Report e2e test result to slack channel
# Arg1: the app name
# Arg2: the exit code of the tests
# Arg3: the slack webhook where to push report
# Arg4: link to chrome logs
# Arg5: link to screenshots
slack_e2e_report() {
local APP="$1"
local RET_CODE="$2"
local SLACK_WEBHOOK="$3"
local CHROME_LOGS_LINK="$4"
local SCREEN_LINK="$5"
local STATUS="success"
local COLOR="#2eb886"
if [ "$RET_CODE" != "0" ]; then STATUS="failed"; COLOR="#a30200"; fi
local MESSAGE
MESSAGE=$(printf "*%s*: run_e2e_tests %s" \
"$APP" \
"$STATUS")
if [ -n "$CHROME_LOGS_LINK" ] && [ -n "$SCREEN_LINK" ]; then
MESSAGE+=" (<${CHROME_LOGS_LINK}|chrome logs> | <${SCREEN_LINK}|screenshots>)"
elif [ -n "$CHROME_LOGS_LINK" ]; then
MESSAGE+=" (<${CHROME_LOGS_LINK}|chrome logs>"
elif [ -n "$SCREEN_LINK" ]; then
MESSAGE+=" (<${SCREEN_LINK}|screenshots>)"
fi
slack_color_log "$SLACK_WEBHOOK" "$MESSAGE" "$COLOR"
}
### SOPS
###
# Generates the decrypted filename for the given encrypted file
# Arg1: the encrypted filename
# NOTE: decrypted file will be XXXXX.dec.ext
enc2dec() {
local ENC="$1"
local BASENAME
BASENAME="$(basename "$ENC")"
local FILENAME="${BASENAME%%.*}"
local EXTENSION="${BASENAME##*.}"
local DEC
DEC="$(dirname "$ENC")/${FILENAME}.dec.${EXTENSION}"
echo "$DEC"
}
# Generates the encrypted filename for the given decrypted file
# Arg1: the decrypted filename
# NOTE: decrypted file will be XXXXX.dec.ext
dec2enc() {
local DEC="$1"
local BASENAME
BASENAME="$(basename "$DEC")"
local FILENAME="${BASENAME%%.*}"
local EXTENSION="${BASENAME##*.}"
local ENC
ENC="$(dirname "$DEC")/${FILENAME}.enc.${EXTENSION}"
echo "$ENC"
}
# Loads environment variables from encrypted env files
# Usage: load_env_files /path/to/file1.enc.env /path/to/file2.enc.env /path/to/file3.enc.env
# NOTE: requires SOPS_AGE_KEY or SOPS_AGE_KEY_FILE to be defined. If not, will defaults to $DEVELOPMENT_DIR/age/keys.txt
load_env_files() {
# Use developer key unless one of SOPS_AGE_KEY or SOPS_AGE_KEY_FILE is already defined (eg. CI)
if [ -z "${SOPS_AGE_KEY-}" ] && [ -z "${SOPS_AGE_KEY_FILE-}" ]; then
export SOPS_AGE_KEY_FILE="$DEVELOPMENT_DIR/age/keys.txt"
fi
for ENC in "$@"; do
if [ -f "$ENC" ]; then
local DEC
DEC="$(enc2dec "$ENC")"
sops --decrypt --output "$DEC" "$ENC"
set -a && . "$DEC" && set +a
fi
done
}
# Decrypt files containing secrets and define an environment variable pointing on the decrypted filename
# Usage: load_value_files /path/to/FOO_PASSWORD.enc.value /path/to/BLAH_SECRET.enc.value
# Will decrypt the files and define FOO_PASSWORD to the decrypted filename. It can be used to feed the decrypted value from stdin.
load_value_files() {
# Use developer key unless one of SOPS_AGE_KEY or SOPS_AGE_KEY_FILE is already defined (eg. CI)
if [ -z "${SOPS_AGE_KEY-}" ] && [ -z "${SOPS_AGE_KEY_FILE-}" ]; then
export SOPS_AGE_KEY_FILE="$DEVELOPMENT_DIR/age/keys.txt"
fi
for ENC in "$@"; do
if [ -f "$ENC" ]; then
local DEC
DEC="$(enc2dec "$ENC")"
sops --decrypt --output "$DEC" "$ENC"
local BASENAME
BASENAME=$(basename "$DEC")
local VAR_NAME="${BASENAME%%.*}"
# Define (and export) $filename as env var with path to decrypted file as value
declare -gx "$VAR_NAME"="$DEC"
fi
done
}
### Kalisio
###
# Returns the kalisio flavor based on the git ref (tag or branch name).
# Known flavors are 'dev', 'test' and 'prod'.
# Expected args:
# 1. the git ref name
get_flavor_from_git_ref() {
local GIT_REF=$1
case "$GIT_REF" in
# Will match anything beginning with 'prod-'
prod-*)
printf "prod"
;;
# Will match single 'test' or anything beginning with 'test-'
test | test-*)
printf "test"
;;
# Anything else is 'dev' flavor
*)
printf "dev"
;;
esac
}
# Returns the kalisio version based on the git ref (tag or branch name).
# Version may be MAJOR.MINOR (eg. with 'test' flavors) or MAJOR.MINOR.PATCH (eg. with 'prod' flavors)
# Expected args:
# 1. the git ref name
get_version_from_git_ref() {
local GIT_REF=$1
local VERSION_REGEX="-v([0-9]+\.[0-9]+(\.[0-9]+)?)"
if [[ "$GIT_REF" =~ $VERSION_REGEX ]]; then
printf "%s" "${BASH_REMATCH[1]}"
fi
}