-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder
executable file
·1450 lines (1218 loc) · 41.8 KB
/
builder
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/sh
if [[ ! -f ./infer ]];
then
echo "Infer tool missing!"
exit 1
fi
source ./infer # utility functions like parse_yaml
if [[ -z $THINX_ROOT ]];
then
export THINX_ROOT=/opt/thinx/thinx-device-api
fi
echo ""
echo "-=[ ☢ THiNX IoT WORKER BUILDER " $(jq .version < package.json) " ☢ ]=-"
echo "."
# do not exit when subsequent tools fail...
set +e
# Swarm Support
randomstring()
{
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w ${1:-32} | head -n 1
}
NODE_ID=$(docker info -f {{.Swarm.NodeID}})
# RETURNS ERROR IF /var/run/docker.sock is not readable by worker!
SWARM=false
if [[ -z $NODE_ID ]];
then
echo "Non swarm mode. Make sure /var/run/docker.sock is readable by the worker on this node in order to run in Swarm mode."
SWARM=false
else
echo "This is a swarm node ${NODE_ID}"
SWARM=true
fi
BUILD_IMAGE=suculent/arduino-docker-build
swarmbuild()
{
#echo "Examining networks..."
# docker network ls returns --network cz-kgr-thinx_internal \
LOG_PATH=$3
BUILD_IMAGE=$2
WORKDIR=$1
# nodemcu container has different startup command (build)
if [[ ! -z "$(echo $BUILD_IMAGE | grep nodemcu)" ]];
then
BUILD_IMAGE="$BUILD_IMAGE build"
fi
FIND="\/mnt\/data\/"
DEPLOY_PATH=/mnt/data/deploy
REPOS_PATH=/mnt/data/repos
# replaces /mnt/data with /mnt/gluster/thinx (or DATA_PATH if set) for Service init
if [[ -z "${DATA_PATH}"]];
then
DATA_PATH="\/mnt\/gluster\/thinx\/"
fi
WORKDIR=$(echo "$WORKDIR" | sed "s/$FIND/$DATA_PATH/")
DEPLOY_PATH=$(echo "$DEPLOY_PATH" | sed "s/$FIND/$DATA_PATH/")
REPOS_PATH=$(echo "$REPOS_PATH" | sed "s/$FIND/$DATA_PATH/")
echo "WORKDIR: ${WORKDIR}"
echo "DEPLOY_PATH: ${DEPLOY_PATH}"
echo "REPOS_PATH: ${REPOS_PATH}"
UNIQUE_NAME="thinx_build-$(randomstring 16)"
SERVICE_COMMAND="docker service create \
--restart-condition=none \
--mount type=bind,source=/var/run/docker.sock,destination=/var/run/docker.sock \
--container-label owner=thinx \
--limit-cpu=1 \
--replicas=1 \
--reserve-memory=750MB \
--name $UNIQUE_NAME \
--mount type=bind,source=$WORKDIR,destination=/opt/workspace \
--mount type=bind,source=$DEPLOY_PATH,destination=/mnt/data/deploy \
--mount type=bind,source=$REPOS_PATH,destination=/mnt/data/repos \
$BUILD_IMAGE"
echo "$SERVICE_COMMAND"
echo "Starting Build Service..."
$SERVICE_COMMAND
echo "» Extracting Docker Service Log:"
INFO=$(docker service ls | grep $UNIQUE_NAME)
echo $INFO
ITERATIONS=0
# 15 seconds x 4 = 1 minute; 4 x 15 minutes (max build duration) = 60
MAX_ITERATIONS=60
RUNNING=true
# write logs in background; should be killed later
docker service logs $UNIQUE_NAME | tee -a "$LOG_PATH" &
while $RUNNING
do
ITERATIONS=$((ITERATIONS+1))
sleep 30
DSTATUS=$(docker service ls | grep $UNIQUE_NAME)
echo "Current service status: $DSTATUS"
if [[ ! -z "$(echo ${DSTATUS} | grep -q \"1/1\")" ]];
then
echo "Still running..."
fi
if [[ ! -z "$(echo ${DSTATUS} | grep -q \"task: non-zero exit\")" ]];
then
echo "[worker] Service task failure - non-zero exit found:"
echo ${DSTATUS}
docker service rm $UNIQUE_NAME
RUNNING=false
fi
if [[ ! -z "$(echo $DSTATUS | grep -q \"0/0\")" ]];
then
echo "Build completed."
# append service logs to catch THINX BUILD SUCCESSFUL phrase; serves to decide to extract OUTFILE laters
docker service logs $UNIQUE_NAME | tee -a "$LOG_PATH"
# might get cleaned a bit later, but how do we do that? tagging?
docker service rm $UNIQUE_NAME
RUNNING=false
fi
if [[ "$DSTATUS" == *"No such image"* ]];
then
echo "Service image failure."
docker service rm $UNIQUE_NAME
RUNNING=false
fi
if [[ "$DSTATUS" == *"invalid"* ]];
then
echo "Service configuration failure."
docker service rm $UNIQUE_NAME
RUNNING=false
fi
if [[ -z "$DSTATUS" ]];
then
echo "Service failure."
RUNNING=false
fi
if [[ "$ITERATIONS" -ge "$MAX_ITERATIONS" ]];
then
echo "Build Timed Out, terminating service."
docker service rm $UNIQUE_NAME
RUNNING=false
fi
done
}
# Default/mock values only
OWNER_ID='cedc16bb6bb06daaa3ff6d30666d91aacd6e3efbf9abbc151b4dcade59af7c12' # name of folder where workspaces reside
RUN=true # dry-run switch
DEVICE='UNKNOWN' # builds for no device by default, not even ANY
OPEN=false # show build result in Finder
BUILD_ID='test-build-id'
ORIGIN=$(pwd)
UDID='f8e88e40-43c8-11e7-9ad3-b7281c2b9610'
GIT_BRANCH='origin/master'
ENV_HASH='cafebabe' # default hardcoded in firmware
# ./builder --id=test-build-id --owner=cedc16bb6bb06daaa3ff6d30666d91aacd6e3efbf9abbc151b4dcade59af7c12 --udid=a80cc610-4faf-11e7-9a9c-41d4f7ab4083 --git=git@github.com:suculent/thinx-firmware-esp8266.git --branch=origin/master
for i in "$@"
do
case $i in
-i=*|--id=*)
BUILD_ID="${i#*=}"
;;
-o=*|--owner=*)
OWNER_ID="${i#*=}"
;;
-a=*|--alias=*)
DEVICE_ALIAS="${i#*=}"
;;
-e=*|--env=*)
ENV_VARS="${i#*=}"
;;
-f=*|--fcid=*)
FCID="${i#*=}"
;;
-m=*|--mac=*)
MAC="${i#*=}"
;;
-g=*|--git=*)
GIT_REPO="${i#*=}"
;;
-b=*|--branch=*)
GIT_BRANCH="${i#*=}"
;;
-d|--dry-run)
RUN=false
;;
--open)
OPEN=true
;;
-u=*|--udid=*)
UDID="${i#*=}"
;;
-w=*|--workdir=*)
WORKDIR="${i#*=}"
;;
*)
# unknown option
;;
esac
done
# later used in DevSec and NodeMCU
CONFIG_ROOT=$(pwd)
if [[ ! -f $CONFIG_ROOT/conf/config.json ]];
then
CONFIG_ROOT=/mnt/data
fi
if [ -f $CONFIG_ROOT/conf/config.json ];
then
# from app_config.data_root
DATA_ROOT_X=$(cat $CONFIG_ROOT/conf/config.json | jq .data_root)
DATA_ROOT="$(echo $DATA_ROOT_X | sed 's/\"//g')"
else
DATA_ROOT=/mnt/data
fi
# from app_config.build_root
BUILD_ROOT=$DATA_ROOT/repos
# from app_config.deploy_root
OWNER_ID_HOME=$DATA_ROOT/deploy/$OWNER_ID
TARGET_PATH=$DATA_ROOT/deploy/$OWNER_ID/$UDID
echo "Target path : ${TARGET_PATH}"
echo "-"
DEPLOYMENT_PATH=$OWNER_ID_HOME/$UDID/$BUILD_ID
echo "Deployment path: ${DEPLOYMENT_PATH}"
echo "-"
mkdir -p $DEPLOYMENT_PATH
chmod -R 777 $DEPLOYMENT_PATH
touch $DEPLOYMENT_PATH/.write
if [[ ! -f $DEPLOYMENT_PATH/.write ]];
then
echo "ERROR: Deployment path $DEPLOYMENT_PATH not writable by Worker shell! Exiting build."
exit 6
fi
# Create user-referenced folder in public www space
LOG_PATH="${DEPLOYMENT_PATH}/build.log"
if [[ ! -f $LOG_PATH ]];
then
echo "Creating new logfile at: $LOG_PATH"
else
echo "Found existing logfile at: $LOG_PATH"
cat $LOG_PATH
fi
touch $LOG_PATH
if [[ -f "lint.txt" ]];
then
echo "Found LINT results in current folder:" | tee -a "${LOG_PATH}"
echo "lint.txt" | tee -a "${LOG_PATH}"
fi
if [[ -f "../lint.txt" ]];
then
echo "Found LINT results in parent folder:" | tee -a "${LOG_PATH}"
echo "../lint.txt" | tee -a "${LOG_PATH}"
fi
echo "Logging to ${LOG_PATH}" | tee -a "${LOG_PATH}"
echo "Owner workspace: ${OWNER_ID_HOME}" | tee -a "${LOG_PATH}"
echo "Making deployment path: ${DEPLOYMENT_PATH}" | tee -a "${LOG_PATH}"
# TODO: Fix this, depends on protocol (changes with git+ssh)
# extract the protocol
proto="$(echo $GIT_REPO | grep :// | sed -e's,^\(.*://\).*,\1,g')"
if [[ -z $proto ]];
then
proto="git-ssl"
fi
## Following works for the HTTPS protocol, not GIT+SSL
# remove the protocol
url="$(echo ${GIT_REPO/$proto/})"
user="$(echo $url | grep @ | cut -d@ -f1)"
host="$(echo ${url/$user@/} | cut -d/ -f1)"
# by request - try to extract the port; support custom git ports in future
port="$(echo $host | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')"
if [[ -z $port ]];
then
port=22
fi
REPO_PATH="$(echo $url | grep / | cut -d/ -f2-)"
REPO_NAME="$(echo $url | grep / | cut -d/ -f3-)"
echo
if [[ "$user" == "git" ]];
then
echo "Overriding for user git and git-ssl..." | tee -a "${LOG_PATH}"
proto="git-ssl"
len=${#REPO_NAME}
OLDHOST=$host
echo "host-x: $host" | tee -a "${LOG_PATH}"
GIT_PATH=$REPO_PATH
REPO_PATH="$(echo $GIT_PATH | sed 's/.git//g')"
REPO_NAME="$(echo $url | grep / | cut -d/ -f2-)"
echo "REPO_NAME C: $REPO_NAME" | tee -a "${LOG_PATH}"
user="$(echo $OLDHOST | grep : | cut -d: -f2-)"
host="$(echo $OLDHOST | grep : | cut -d: -f1)"
else
echo "In git-https mode, user is also from url..." | tee -a "${LOG_PATH}"
user=$(echo $url | grep / | cut -d/ -f2-)
user=$(echo $user | grep / | cut -d/ -f1)
fi
echo
# make sure to remove trailing git for HTTP URLs as well...
# REPO_PATH=$BUILD_PATH/${REPO_PATH%.git}
REPO_PATH=${REPO_PATH%.git}
REPO_NAME=${REPO_NAME%.git}
# TODO: only if $REPO_NAME contains slash(es)
BUILD_PATH=$BUILD_ROOT/$OWNER_ID/$UDID/$BUILD_ID
if [[ ! -d $BUILD_PATH ]];
then
mkdir -p $BUILD_PATH
fi
echo $BUILD_PATH | tee -a "${LOG_PATH}"
cd $BUILD_PATH
# Fetch submodules if any
SINK=""
if [[ -d "${BUILD_PATH}/${REPO_NAME}" ]];
then
echo "Directory $REPO_NAME exists, entering..." | tee -a "${LOG_PATH}"
cd $BUILD_PATH/$REPO_NAME
echo "Current path: $(pwd)" | tee -a "${LOG_PATH}"
ls -la | tee -a "${LOG_PATH}"
SINK=$BUILD_PATH/$REPO_NAME
cd $SINK
echo "Current path: $(pwd)" | tee -a "${LOG_PATH}"
ls -la | tee -a "${LOG_PATH}"
ls -la * | tee -a "${LOG_PATH}"
else
echo "Current path: $(pwd)" | tee -a "${LOG_PATH}"
ls | tee -a "${LOG_PATH}"
echo "REPO_NAME ${REPO_NAME} does not exist, entering * instead..." | tee -a "${LOG_PATH}"
SINK=$BUILD_PATH/*
echo "Entering SINK ${SINK}" | tee -a "${LOG_PATH}"
if [[ -d "$SINK" ]];
then
cd $SINK
echo "Current path: $(pwd)" | tee -a "${LOG_PATH}"
ls -la | tee -a "${LOG_PATH}"
ls -la * | tee -a "${LOG_PATH}"
fi
fi
echo "Current path: $(pwd)" | tee -a "${LOG_PATH}"
ls -la | tee -a "${LOG_PATH}"
echo "Updating submodules..." | tee -a "${LOG_PATH}"
git submodule update --init --recursive | tee -a "${LOG_PATH}"
if [[ ! -d "$SINK/.git" ]];
then
echo "WARNING! No .git folder on path: $BUILD_PATH/$REPO_PATH/.git" | tee -a "${LOG_PATH}"
else
cd $SINK | tee -a "${LOG_PATH}"
fi
COMMIT=$(git rev-parse HEAD)
echo "Fetched commit ID: ${COMMIT}" | tee -a "${LOG_PATH}"
VERSION=$(git rev-list --all --count)
echo "Repository version/revision: ${VERSION}" | tee -a "${LOG_PATH}"
# Search for thinx.yml
nodemcu_build_type="firmware"
nodemcu_build_float=true
micropython_build_type="firmware"
micropython_platform="esp8266"
YML=$(find $BUILD_PATH/$REPO_NAME -name "thinx.yml")
if [[ ! -z "$YML" ]];
then
#echo "Found ${YML}, reading..." | tee -a "${LOG_PATH}"
echo "Reading thinx.yml build configuration: $YML"
echo "Parsing YAML..."
PARSED=$(parse_yaml "$YML" "")
eval "$PARSED"
else
exit 3
fi
# Overwrite Thinx.h file (should be required)
echo "Searching THiNX-File in $BUILD_PATH/$REPO_NAME..." | tee -a "${LOG_PATH}"
if [[ -z $THINX_HOSTNAME ]];
then
echo "THINX_HOSTNAME must be set!"
# exit 1
fi
THINX_CLOUD_URL="${THINX_HOSTNAME}"
THINX_MQTT_URL="${THINX_HOSTNAME}"
if [[ ! -z $DEVICE_ALIAS ]];
then
THINX_ALIAS=$DEVICE_ALIAS
else
THINX_ALIAS="vanilla"
fi
echo "Changing workdir to ${BUILD_PATH}/${REPO_NAME}"
cd $BUILD_PATH/$REPO_NAME
THX_VERSION="$(git describe --abbrev=0 --tags)"
if [[ $? > 0 ]];
then
THX_VERSION="1.0"
fi
THX_REVISION="$(git rev-list --all --count)"
if [[ $? > 0 ]];
then
THX_REVISION="1"
fi
REPO_NAME="$( basename $BUILD_PATH/$REPO_PATH )"
BUILD_DATE=$(date +%Y-%m-%d)
# Build
PLATFORM=$(infer_platform $SINK)
echo "[builder] Inferred platform: ${PLATFORM}"
LANGUAGE=$(language_for_platform $PLATFORM)
LANGUAGE_NAME=$(language_name $LANGUAGE)
if [[ -z $PLATFORM ]];
then
echo "No language. Cannot continue builder."
exit 1
fi
if [[ -z $LANGUAGE ]];
then
echo "No language. Cannot continue builder."
exit 1
fi
echo "Building for platform '${PLATFORM}' in language '${LANGUAGE_NAME}'..." | tee -a "${LOG_PATH}"
SHA="0x00000000"
OUTFILE="<none>"
BUILD_SUCCESS=false
# If running inside Docker, we'll start builders as siblings
if [ -f /.dockerenv ];
then
DOCKER_PREFIX="-v /var/run/docker.sock:/var/run/docker.sock"
else
DOCKER_PREFIX=""
fi
cd $WORKDIR
echo "Running DevSec..." | tee -a "${LOG_PATH}"
### DevSec Implementation Begin --> ###
# NOTE: This applies to (C-based) builds only with DevSec support;
# Builder searches for the signature placeholder inside a subfolder.
# (This header should not be placed in project root to prevent being auto-imported;
# which causes duplicate definitions and linker error.)
# Fetche SSID and PASS for DevSec
ENVS=$(find "$BUILD_PATH/$REPO_NAME" -name "environment.json" | head -n 1)
if [[ ! -f "$ENVS" ]];
then
echo "No environment.json found" | tee -a "${LOG_PATH}"
else
if [ ! -f "$THINX_FILE" ];
then
echo "WTF THINX_FILE does not exist?" | tee -a "${LOG_PATH}"
else
# DEPRECATED: Use ENV_SSID and ENV_PASS to set devsec_ssid and devsec_pass
# Actually in latest code, credentials are transmitted and stored at rest in CouchDB encrypted using transfer_key.
# On build those are pushed to thinx.yml overwriting devsec.ssid and devsec.pass keys using ENVIRONMENT_CSSID and ENVIRONMENT_CPASS
# From point of view, storing those values in environment.json is incorrect, because this file should not contain CSSID and CPASS (but only global SSID and PASS if any)
if [[ ! -f "$ENVS" ]];
then
echo "No environment.json found" | tee -a "${LOG_PATH}"
else
echo "Overriding DevSec values from: ${ENVS}" | tee -a "${LOG_PATH}"
echo
while IFS='' read -r keyname; do
VAL=$(jq '.'$keyname $ENVS)
if [[ "${keyname}" == "pass" ]];
then
devsec_pass=$(echo ${VAL} | sed 's/^"//;s/\"*$//') # trim leading/trailing '\"'
echo "Overriding devsec_pass: ${devsec_pass}" | tee -a "${LOG_PATH}"
fi
if [[ "${keyname}" == "ssid" ]];
then
devsec_ssid=$(echo ${VAL} | sed 's/^"//;s/\"*$//') # trim leading/trailing '\"'
echo "Overriding devsec_ssid: ${devsec_ssid}" | tee -a "${LOG_PATH}"
fi
if [[ "${keyname}" == "ckey" ]];
then
devsec_ckey=$(echo ${VAL} | sed 's/^"//;s/\"*$//') # trim leading/trailing '\"'
echo "Overriding devsec_ckey: ${devsec_ckey}" | tee -a "${LOG_PATH}"
fi
done < <(jq -r 'keys[]' $ENVS)
fi
fi
fi
# Fetches path and rebuilds the signature file if found and all required arguments are available...
SIGNATURE_FILE=$(find . -maxdepth 3 -name "embedded_signature.h")
if [[ ! -z "$SIGNATURE_FILE" ]];
then
if [[ -f "$SIGNATURE_FILE" ]];
then
# TODO: Validate inputs before doing this... MAC length and FCID length must be exactly 6, etc. Should be implemented in signer.
if [[ ! -z "$FCID" && ! -z "$MAC" && ! -z "${devsec_ckey}" ]];
then
echo "DevSec building signature into ${SIGNATURE_FILE}" | tee -a "${LOG_PATH}"
# This makes sure the -c "CKEY" argument does not fall apart due to spaces...
# in case the CKEY would contain + this needs to be changed to another character, but not \n like in many examples in the wild.
# WARNING! INSECURE DEBUG! Remove when not needed.
echo "[REM DevSec] MAC: ${MAC}" | tee -a "${LOG_PATH}"
echo "[REM DevSec] FCID: ${FCID}" | tee -a "${LOG_PATH}"
echo "[REM DevSec] devsec_ssid: ${devsec_ssid}" | tee -a "${LOG_PATH}"
echo "[REM DevSec] devsec_pass: ${devsec_pass}" | tee -a "${LOG_PATH}"
echo "[REM DevSec] devsec_ckey: ${devsec_ckey}" | tee -a "${LOG_PATH}"
SAVED_IFS=$IFS
IFS='+'
DEVSEC_CONTENTS=$("$THINX_ROOT/devsec" -m ${MAC} -f ${FCID} -s ${devsec_ssid} -p ${devsec_pass} -c "${devsec_ckey}")
DEVSEC_SUCCESS=$?
IFS=$SAVED_IFS
if [[ $DEVSEC_SUCCESS == 0 ]];
then
echo "$DEVSEC_CONTENTS" > "$(pwd)/$SIGNATURE_FILE"
echo "DevSec Device Signature file generated..." | tee -a "${LOG_PATH}"
echo "$DEVSEC_CONTENTS" > "${LOG_PATH}"
else
echo "DevSec Failed, keeping signature file intact." | tee -a "${LOG_PATH}"
echo $RESULT | tee -a "${LOG_PATH}"
exit 0
fi
else
echo "[DevSec] Skipping, configuration incomplete. FCID: $FCID, MAC: $MAC, CKEY: ${devsec_ckey} " | tee -a "${LOG_PATH}"
fi
else
echo "[DevSec] Signature file not found at $SIGNATURE_FILE in $(ls)" | tee -a "${LOG_PATH}"
fi
fi
### <-- DevSec Implementation End ###
echo "Building for ${PLATFORM} in WORKDIR: $(pwd)" | tee -a "${LOG_PATH}"
### Builder Implementation Begin --> ###
case $PLATFORM in
nodejs)
# WARNING! This is a specific builder.
# Should only copy. Basic NodeJS client supports git and should fetch repo on its own.
OUTFILE=${DEPLOYMENT_PATH}/build
touch $OUTFILE
# zip -rq "${DEPLOYMENT_PATH}/${BUILD_ID}.zip" | tee -a "${LOG_PATH}" ./* # zip artefacts
BUILD_SUCCESS=true
echo "There's nothing to build on NodeJS projects." | tee -a "${LOG_PATH}"
OUTPATH=${DEPLOYMENT_PATH}
# ls | tee -a "${LOG_PATH}"
if [[ ! ${RUN} ]];
then
echo "☢ Dry-run ${BUILD_ID} completed. Skipping actual deployment." | tee -a "${LOG_PATH}"
STATUS='DRY_RUN_OK'
else
# Check Artifacts
if [[ $BUILD_SUCCESS == true ]] ;
then
echo "NodeJS Build: Listing output directory: " | tee -a "${LOG_PATH}"
pwd | tee -a "${LOG_PATH}"
# ls | tee -a "${LOG_PATH}"
STATUS='OK'
else
STATUS='FAILED'
fi
fi
;;
micropython)
# WARNING! This is a specific builder (like NodeMCU).
# Injects thinx to esp8266/modules in firmware mode. Should also prebuild SPIFFS.
BUILD_TYPE=$micropython_build_type
if [[ "$BUILD_TYPE" == "file" ]];
then
echo "Build type: file" | tee -a "${LOG_PATH}"
OUTFILE=${DEPLOYMENT_PATH}/boot.py
cp -vf $WORKDIR/*.py ${DEPLOYMENT_PATH} # copy all .py files without building
zip -rq "${DEPLOYMENT_PATH}/${BUILD_ID}.zip" | tee -a "${LOG_PATH}" ./* # zip artefacts
else
echo "Build type: firmware (or undefined)" | tee -a "${LOG_PATH}"
OUTFILE=${DEPLOYMENT_PATH}/firmware.bin
if [[ -z "$(find $OUTFILE -type f -size +10000c 2>/dev/null)" ]];
then
rm -rf $OUTFILE
BUILD_SUCCESS=false
echo "Docker build failed, build artifact size is below 10k." | tee -a "${LOG_PATH}"
fi
fi
OUTPATH=${DEPLOYMENT_PATH}
echo "Micropython Build: Customizing firmware..." | tee -a "${LOG_PATH}"
UPY_FILES=$(find $WORKDIR -name *.py)
for pyfile in ${UPY_FILES[@]}; do
if [[ "$BUILD_TYPE" == "firmware" ]];
then
FSPATH=$WORKDIR/$(basename ${pyfile}) # we should already stand in this folder
if [[ -f "$FSPATH" ]];
then
rm -rf $FSPATH
cp -vf "${pyfile}" $FSPATH
zip -rq "${DEPLOYMENT_PATH}/${BUILD_ID}.zip" ${pyfile} ./* # zip artefacts
fi
else
cp -vf "${pyfile}" "$DEPLOYMENT_PATH"
zip -rq "${DEPLOYMENT_PATH}/${BUILD_ID}.zip" ${pyfile} ./* # zip artefacts
fi
done
if [[ $SWARM == false ]];
then
if [[ "$BUILD_TYPE" == "firmware" ]];
then
echo "Micropython Build: Running Dockerized builder..." | tee -a "${LOG_PATH}"
set -o pipefail
docker pull suculent/micropython-docker-build
docker run ${DOCKER_PREFIX} --cpus=1.0 --rm -t -v $(pwd)/modules:/micropython/esp8266/modules --workdir /micropython/esp8266 suculent/micropython-docker-build | tee -a "${LOG_PATH}"
echo "${PIPESTATUS[@]}"
set +o pipefail
if [[ ! -z "$(grep 'THiNX BUILD SUCCESSFUL' ${LOG_PATH})" ]];
then
BUILD_SUCCESS=true
echo "Zipping artifacts to ${BUILD_ID}.zip..." | tee -a "${LOG_PATH}"
zip -rq "${DEPLOYMENT_PATH}/${BUILD_ID}.zip" ${LOG_PATH} ./build/** # zip artefacts
fi
if [[ -z "$(find $OUTFILE -type f -size +10000c 2>/dev/null)" ]];
then
rm -rf $OUTFILE
BUILD_SUCCESS=false
echo "Docker build failed, build artifact size is below 10k." | tee -a "${LOG_PATH}"
fi
echo "[micropython] Docker completed <<<"
fi
else
swarmbuild $WORKDIR suculent/micropython-docker-build $LOG_PATH
fi
# ls | tee -a "${LOG_PATH}"
if [[ ! ${RUN} ]];
then
echo "☢ Dry-run ${BUILD_ID} completed. Skipping actual deployment." | tee -a "${LOG_PATH}"
STATUS='DRY_RUN_OK'
else
# Check Artifacts
if [[ $BUILD_SUCCESS == true ]] ;
then
echo "NodeMCU Build: Listing output directory: " | tee -a "${LOG_PATH}"
pwd | tee -a "${LOG_PATH}"
# ls | tee -a "${LOG_PATH}"
echo "NodeMCU Build: Listing binary artifacts: " | tee -a "${LOG_PATH}"
# ls ./bin | tee -a "${LOG_PATH}"
if [[ "$BUILD_TYPE" == "firmware" ]];
then
cp -v ./build/*.bin "$OUTPATH" | tee -a "${LOG_PATH}"
zip -rq "${DEPLOYMENT_PATH}/${BUILD_ID}.zip" ${LOG_PATH} ./build/* # zip artefacts
rm -rf ./build/*
fi
echo "Micropython Build: DEPLOYMENT_PATH: " $DEPLOYMENT_PATH
ls "$DEPLOYMENT_PATH" | tee -a "${LOG_PATH}"
STATUS='OK'
else
STATUS='FAILED'
fi
fi
;;
nodemcu)
# WARNING! This is a specific builder (like Micropython).
# Source files must be copied from source folder to the WORKDIR
# which is actually a source of nodemcu-firmware (esp-open-sdk).
DROP_INTEGER_USE_FLOAT=$nodemcu_build_float
if [[ $DROP_INTEGER_USE_FLOAT == true ]];
then
OUTFILE_PREFIX='nodemcu_integer'
INTEGER_ONLY=true
DOCKER_PARAMS="-e INTEGER_ONLY=true"
else
OUTFILE_PREFIX='nodemcu_float'
FLOAT_ONLY=true
DOCKER_PARAMS="-e FLOAT_ONLY=true"
fi
BUILD_TYPE=$nodemcu_build_type
if [[ $BUILD_TYPE == "file" ]];
then
echo "Build type: file" | tee -a "${LOG_PATH}"
OUTFILE=${DEPLOYMENT_PATH}/thinx.lua
zip -rq "${DEPLOYMENT_PATH}/${BUILD_ID}.zip" ${LOG_PATH} ${OUTFILE} # zip artefacts
else
echo "Build type: firmware (or undefined)" | tee -a "${LOG_PATH}"
OUTFILE=${DEPLOYMENT_PATH}/firmware.bin
zip -rq "${DEPLOYMENT_PATH}/${BUILD_ID}.zip" ${LOG_PATH} ${OUTFILE} # zip artefacts
if [[ -z "$(find $OUTFILE -type f -size +10000c 2>/dev/null)" ]];
then
rm -rf $OUTFILE
BUILD_SUCCESS=false
echo "Docker build failed, build artifact size is below 10k." | tee -a "${LOG_PATH}"
fi
fi
OUTPATH=${DEPLOYMENT_PATH}
echo "NodeMCU Build: Cleaning SPIFFS folder..." | tee -a "${LOG_PATH}"
if [ -f ${DEPLOYMENT_PATH}/local/fs/* ];
then
echo "Cleaning local/fs" | tee -a "${LOG_PATH}"
# rm -rf ${DEPLOYMENT_PATH}/local/fs/** # cleanup first
fi
# Copy firmware sources to current working directory
cp -vfR $THINX_ROOT/builders/nodemcu-firmware/* .
CONFIG_PATH="./local/fs/thinx.json"
if [ -f $CONFIG_PATH ];
then
echo "NodeMCU Build: Deconfiguring..." | tee -a "${LOG_PATH}"
rm -rf $CONFIG_PATH
fi
echo "NodeMCU Build: Configuring..." | tee -a "${LOG_PATH}"
cp "./thinx_build.json" $CONFIG_PATH
FILES=$(find . -maxdepth 1 -name "*.lua")
echo "NodeMCU Build: FILES:" | tee -a "${LOG_PATH}"
echo ${FILES} | tee -a "${LOG_PATH}"
echo "NodeMCU Build: Customizing firmware..." | tee -a "${LOG_PATH}"
if [[ "$BUILD_TYPE" == "firmware" ]];
then
# build into filesystem root
for luafile in ${FILES[@]}; do
FSPATH=./local/fs/$(basename ${luafile})
if [[ -f $FSPATH ]];
then
rm -rf $FSPATH
cp -vf "${luafile}" $FSPATH
fi
if [ -f ./bin/* ];
then
echo "NodeMCU Build: Cleaning bin & map files..." | tee -a "${LOG_PATH}"
rm -rf ./bin/*
fi
done
if [[ $SWARM == false ]];
then
echo "NodeMCU Build: Running Dockerized builder..." | tee -a "${LOG_PATH}"
echo "running Docker >>>"
set -o pipefail
docker pull suculent/nodemcu-docker-build
docker run ${DOCKER_PREFIX} --cpus=1.0 --rm -t ${DOCKER_PARAMS} -v `pwd`:/opt/nodemcu-firmware suculent/nodemcu-docker-build build | tee -a "${LOG_PATH}"
echo "${PIPESTATUS[@]}"
if [[ ! -z "$(grep 'THiNX BUILD SUCCESSFUL' ${LOG_PATH})" ]];
then
BUILD_SUCCESS=true
zip -rq "${DEPLOYMENT_PATH}/${BUILD_ID}.zip" ${LOG_PATH} ./bin/* # zip artefacts
fi
echo "[nodemcu] Docker completed <<<"
else
swarmbuild $WORKDIR suculent/nodemcu-docker-build $LOG_PATH
fi
else
# deploy Lua files without building
cp -vf *.lua "$DEPLOYMENT_PATH"
zip -rq "${DEPLOYMENT_PATH}/${BUILD_ID}.zip" ${LOG_PATH} ${FILES} # zip artefacts
fi
if [[ ! ${RUN} ]];
then
echo "☢ Dry-run ${BUILD_ID} completed. Skipping actual deployment." | tee -a "${LOG_PATH}"
STATUS='DRY_RUN_OK'
else
if [[ $BUILD_SUCCESS == true ]] ;
then
echo "NodeMCU Build: Listing output directory: " | tee -a "${LOG_PATH}"
pwd | tee -a "${LOG_PATH}"
# ls | tee -a "${LOG_PATH}"
echo "NodeMCU Build: Listing binary artifacts: " | tee -a "${LOG_PATH}"
# ls ./bin | tee -a "${LOG_PATH}"
if [[ "$BUILD_TYPE" == "firmware" ]];
then
echo "NodeMCU Build: Copying binary artifacts..." | tee -a "${LOG_PATH}"
cp -v "./bin/${OUTFILE_PREFIX}*.bin" "${DEPLOYMENT_PATH}/firmware.bin" | tee -a "${LOG_PATH}"
fi
echo "NodeMCU Build: DEPLOYMENT_PATH: " $DEPLOYMENT_PATH
# ls "$DEPLOYMENT_PATH" | tee -a "${LOG_PATH}"
zip -rq "${DEPLOYMENT_PATH}/${BUILD_ID}.zip" ${LOG_PATH} ./bin/* # zip artefacts
STATUS='OK'
else
STATUS='FAILED'
fi
fi
;;
mongoose)
OUTFILE=${DEPLOYMENT_PATH}/fw.zip
OUTPATH=${DEPLOYMENT_PATH}
# should copy thinx.json into ./fs/thinx.json
TNAME=$(find . -name "thinx.json")
if [[ -z $TNAME ]];
then
if [[ ! -d "./fs" ]];
then
mkdir ./fs
fi
TNAME=$(pwd)/fs/thinx.json
fi
echo "Moving thinx_build.json to $TNAME" | tee -a "${LOG_PATH}"
cp "./thinx_build.json" "$TNAME"
if [[ $SWARM == false ]];
then
docker pull suculent/mongoose-docker-build
DCMD="docker run ${DOCKER_PREFIX} --cpus=1.0 --rm -t -v $(pwd):/opt/mongoose-builder suculent/mongoose-docker-build"
echo "running Docker ${DCMD} >>>" | tee -a "${LOG_PATH}"
set -o pipefail
"$DCMD"
echo "${PIPESTATUS[@]}"
if [[ ! -z "$(grep 'THiNX BUILD SUCCESSFUL' ${LOG_PATH})" ]];
then
if [[ -f $(pwd)/build/fw.zip ]];
then
BUILD_SUCCESS=true
zip -rq "${DEPLOYMENT_PATH}/${BUILD_ID}.zip" ${LOG_PATH} ./build/* # zip artefacts
else
echo "OUTFILE not created." | tee -a "${LOG_PATH}"
fi
fi
echo "[mongoose] Docker completed <<<"
else
swarmbuild $WORKDIR suculent/mongoose-docker-build $LOG_PATH
fi
# Exit on dry run...
if [[ ! ${RUN} ]];
then
echo "☢ Dry-run ${BUILD_ID} completed. Skipping actual deployment." | tee -a "${LOG_PATH}"
STATUS='DRY_RUN_OK'
else
# Check Artifacts
if [[ $BUILD_SUCCESS == true ]] ;
then
STATUS='OK'
cp $(pwd)/build/fw.zip $OUTFILE
# ls "$BUILD_PATH/build" | tee -a "${LOG_PATH}"
unzip "${BUILD_PATH}/build/fw.zip" "$DEPLOYMENT_PATH" | tee -a "${LOG_PATH}"
# ls "$DEPLOYMENT_PATH" | tee -a "${LOG_PATH}"
echo "[builder.sh]" $MSG; echo $MSG | tee -a "${LOG_PATH}"
zip -rq "${DEPLOYMENT_PATH}/${BUILD_ID}.zip" ${LOG_PATH} ./build/* # zip artefacts
else
STATUS='FAILED'
fi
fi
;;
arduino)
cd $BUILD_PATH/$REPO_NAME
#pwd
#ls
THINX_FILE=$( find . -name "thinx.h" | head -n 1)
if [[ -z "$THINX_FILE" ]];
then
echo "[arduino] WARNING! No THiNX-File found! in $BUILD_PATH/$REPO_NAME: $THINX_FILE" | tee -a "${LOG_PATH}"
# exit 1 # will deprecate on modularization for more platforms
else
#echo "[arduino] Using THiNX-File: ${THINX_FILE/$(pwd)//}" | tee -a "${LOG_PATH}"
echo "[arduino] Using THiNX-File: ${THINX_FILE}" | tee -a "${LOG_PATH}"
ENVOUT=$(find $BUILD_PATH/$REPO_NAME -name "environment.json" | head -n 1)
if [[ ! -f $ENVOUT ]];
then
echo "No environment.json found"
else
if [ ! -f "$THINX_FILE" ];
then
echo "WTF THINX_FILE does not exist?"
else
echo "[arduino] Will write ENV_HASH to ${THINX_FILE}"
# generate checksum and get rid of trailing "-"
ENV_HASH=$(echo ${ENVOUT} | sha256sum | awk '{ print $1 }')
# prepare new header line in C
LINE="#define ENV_HASH \"${ENV_HASH}\""
# delete old line/placeholder
sed -i '/ENV_HASH/d' ${THINX_FILE}
# add new line with checksum
echo -e ${LINE} >> ${THINX_FILE}
echo "THINX_FILE saved."
fi
fi
fi
OUTFILE=${DEPLOYMENT_PATH}/firmware.bin
#WORKDIR=$(pwd)
if [[ $SWARM == false ]];
then
set -o pipefail
echo "Docker: Starting THiNX Arduino Builder Container in folder" $(pwd)
docker pull suculent/arduino-docker-build
DCMD="docker run ${DOCKER_PREFIX} --cpus=1.0 -t -v $(pwd):/opt/workspace suculent/arduino-docker-build"
echo "command: ${DCMD}"
$DCMD | tee -a "${LOG_PATH}"