forked from aws-solutions/media-insights-on-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-s3-dist.sh
executable file
·786 lines (708 loc) · 31.4 KB
/
build-s3-dist.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
#!/bin/bash
###############################################################################
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
#
# PURPOSE:
# Build cloud formation templates for the Media Insights Engine
#
# USAGE:
# ./build-s3-dist.sh [-h] [-v] [--no-layer] --template-bucket {TEMPLATE_BUCKET} --code-bucket {CODE_BUCKET} --version {VERSION} --region {REGION}
# TEMPLATE_BUCKET should be the name for the S3 bucket location where MIE
# cloud formation templates should be saved.
# CODE_BUCKET should be the name for the S3 bucket location where cloud
# formation templates should find Lambda source code packages.
# VERSION should be in a format like v1.0.0
# REGION needs to be in a format like us-east-1
#
# The following options are available:
#
# -h | --help Print usage
# -v | --verbose Print script debug info
# --no-layer Do not build AWS Lamda layer
#
###############################################################################
trap cleanup_and_die SIGINT SIGTERM ERR
usage() {
msg "$msg"
cat <<EOF
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] [--no-layer] --template-bucket TEMPLATE_BUCKET --code-bucket CODE_BUCKET --version VERSION --region REGION
Available options:
-h, --help Print this help and exit (optional)
-v, --verbose Print script debug info (optional)
--no-layer Do not build AWS Lamda layer (optional)
--template-bucket S3 bucket to put cloud formation templates
--code-bucket S3 bucket to put Lambda code packages
--version Arbitrary string indicating build version
--region AWS Region, formatted like us-west-2
EOF
exit 1
}
cleanup_and_die() {
trap - SIGINT SIGTERM ERR
echo "Trapped signal."
cleanup
die 1
}
cleanup() {
# Deactivate and remove the temporary python virtualenv used to run this script
if [[ "$VIRTUAL_ENV" != "" ]];
then
deactivate
#rm -rf "$VENV"
echo "------------------------------------------------------------------------------"
echo "Cleaning up complete"
echo "------------------------------------------------------------------------------"
fi
}
msg() {
echo >&2 -e "${1-}"
}
die() {
local msg=$1
local code=${2-1} # default exit status 1
msg "$msg"
exit "$code"
}
parse_params() {
# default values of variables set from params
flag=0
param=''
while :; do
case "${1-}" in
-h | --help) usage ;;
-v | --verbose) set -x ;;
--no-layer) NO_LAYER=1 ;;
--template-bucket)
global_bucket="${2}"
shift
;;
--code-bucket)
regional_bucket="${2}"
shift
;;
--version)
version="${2}"
shift
;;
--region)
region="${2}"
shift
;;
-?*) die "Unknown option: $1" ;;
*) break ;;
esac
shift
done
args=("$@")
# check required params and arguments
[[ -z "${global_bucket}" ]] && usage "Missing required parameter: template-bucket"
[[ -z "${regional_bucket}" ]] && usage "Missing required parameter: code-bucket"
[[ -z "${version}" ]] && usage "Missing required parameter: version"
[[ -z "${region}" ]] && usage "Missing required parameter: region"
return 0
}
parse_params "$@"
msg "Build parameters:"
msg "- Template bucket: ${global_bucket}"
msg "- Code bucket: ${regional_bucket}-${region}"
msg "- Version: ${version}"
msg "- Region: ${region}"
msg "- Build layer? $(if [[ -z $NO_LAYER ]]; then echo 'Yes, please.'; else echo 'No, thanks.'; fi)"
echo ""
sleep 3
s3domain="s3.$region.amazonaws.com"
# Check if region is supported:
if [ "$region" != "us-east-1" ] &&
[ "$region" != "us-east-2" ] &&
[ "$region" != "us-west-1" ] &&
[ "$region" != "us-west-2" ] &&
[ "$region" != "eu-west-1" ] &&
[ "$region" != "eu-west-2" ] &&
[ "$region" != "eu-central-1" ] &&
[ "$region" != "ap-south-1" ] &&
[ "$region" != "ap-northeast-1" ] &&
[ "$region" != "ap-southeast-1" ] &&
[ "$region" != "ap-southeast-2" ] &&
[ "$region" != "ap-northeast-1" ] &&
[ "$region" != "ap-northeast-2" ]; then
echo "ERROR. Rekognition operations are not supported in region $region"
exit 1
fi
# Make sure wget is installed
if ! [ -x "$(command -v wget)" ]; then
echo "ERROR: Command not found: wget"
echo "ERROR: wget is required for downloading lambda layers."
echo "ERROR: Please install wget and rerun this script."
exit 1
fi
# Build source S3 Bucket
if [[ ! -x "$(command -v aws)" ]]; then
echo "ERROR: This script requires the AWS CLI to be installed. Please install it then run again."
exit 1
fi
# Get reference for all important folders
build_dir="$PWD"
global_dist_dir="$build_dir/global-s3-assets"
regional_dist_dir="$build_dir/regional-s3-assets"
dist_dir="$build_dir/dist"
source_dir="$build_dir/../source"
# Create and activate a temporary Python environment for this script.
echo "------------------------------------------------------------------------------"
echo "Creating a temporary Python virtualenv for this script"
echo "------------------------------------------------------------------------------"
python -c "import os; print (os.getenv('VIRTUAL_ENV'))" | grep -q None
if [ $? -ne 0 ]; then
echo "ERROR: Do not run this script inside Virtualenv. Type \`deactivate\` and run again.";
exit 1;
fi
echo "Using virtual python environment:"
VENV=$(mktemp -d) && echo "$VENV"
command -v python3 > /dev/null
if [ $? -ne 0 ]; then
echo "ERROR: install Python3 before running this script"
exit 1
fi
python3 -m venv "$VENV"
source "$VENV"/bin/activate
pip3 install wheel
pip3 install --quiet boto3 chalice docopt pyyaml jsonschema aws_xray_sdk
export PYTHONPATH="$PYTHONPATH:$source_dir/lib/MediaInsightsEngineLambdaHelper/"
if [ $? -ne 0 ]; then
echo "ERROR: Failed to install required Python libraries."
exit 1
fi
echo "------------------------------------------------------------------------------"
echo "Create distribution directory"
echo "------------------------------------------------------------------------------"
# Setting up directories
echo "rm -rf $global_dist_dir"
rm -rf "$global_dist_dir"
echo "mkdir -p $global_dist_dir"
mkdir -p "$global_dist_dir"
echo "mkdir -p $global_dist_dir/website"
mkdir -p "$global_dist_dir"/website
echo "rm -rf $regional_dist_dir"
rm -rf "$regional_dist_dir"
echo "mkdir -p $regional_dist_dir"
mkdir -p "$regional_dist_dir"
echo "------------------------------------------------------------------------------"
echo "Building MIEHelper package"
echo "------------------------------------------------------------------------------"
cd "$source_dir"/lib/MediaInsightsEngineLambdaHelper || exit 1
rm -rf build
rm -rf dist
rm -rf Media_Insights_Engine_Lambda_Helper.egg-info
python3 setup.py bdist_wheel > /dev/null
echo -n "Created: "
find "$source_dir"/lib/MediaInsightsEngineLambdaHelper/dist/
cd "$build_dir"/ || exit 1
if [[ ! -z "${NO_LAYER}" ]]; then
echo "------------------------------------------------------------------------------"
echo "Downloading Lambda Layers"
echo "------------------------------------------------------------------------------"
echo "Downloading https://rodeolabz-$region.$s3domain/media_insights_engine/media_insights_engine_lambda_layer_python3.6.zip"
wget -q https://rodeolabz-"$region"."$s3domain"/media_insights_engine/media_insights_engine_lambda_layer_python3.6.zip
echo "Downloading https://rodeolabz-$region.$s3domain/media_insights_engine/media_insights_engine_lambda_layer_python3.7.zip"
wget -q https://rodeolabz-"$region"."$s3domain"/media_insights_engine/media_insights_engine_lambda_layer_python3.7.zip
echo "Downloading https://rodeolabz-$region.$s3domain/media_insights_engine/media_insights_engine_lambda_layer_python3.8.zip"
wget -q https://rodeolabz-"$region"."$s3domain"/media_insights_engine/media_insights_engine_lambda_layer_python3.8.zip
echo "Copying Lambda layer zips to $dist_dir:"
mv media_insights_engine_lambda_layer_python3.6.zip "$regional_dist_dir"
mv media_insights_engine_lambda_layer_python3.7.zip "$regional_dist_dir"
mv media_insights_engine_lambda_layer_python3.8.zip "$regional_dist_dir"
cd "$build_dir" || exit 1
else
echo "------------------------------------------------------------------------------"
echo "Building Lambda Layers"
echo "------------------------------------------------------------------------------"
# Build MediaInsightsEngineLambdaHelper Python package
cd "$build_dir"/lambda_layer_factory/ || exit 1
rm -f media_insights_engine_lambda_layer_python*.zip*
rm -f Media_Insights_Engine*.whl
cp -R "$source_dir"/lib/MediaInsightsEngineLambdaHelper .
cd MediaInsightsEngineLambdaHelper/ || exit 1
echo "Building MIE Lambda Helper python library"
python3 setup.py bdist_wheel > /dev/null
cp dist/*.whl ../
cp dist/*.whl "$source_dir"/lib/MediaInsightsEngineLambdaHelper/dist/
echo "MIE Lambda Helper python library is at $source_dir/lib/MediaInsightsEngineLambdaHelper/dist/"
cd "$source_dir"/lib/MediaInsightsEngineLambdaHelper/dist/ || exit 1
ls -1 "$(pwd)"/*.whl
if [ $? -ne 0 ]; then
echo "ERROR: Failed to build MIE Lambda Helper python library"
exit 1
fi
cd "$build_dir"/lambda_layer_factory/ || exit 1
rm -rf MediaInsightsEngineLambdaHelper/
file=$(ls Media_Insights_Engine*.whl)
# Note, $(pwd) will be mapped to /packages/ in the Docker container used for building the Lambda zip files. We reference /packages/ in requirements.txt for that reason.
# Add the whl file to requirements.txt if it is not already there
mv requirements.txt requirements.txt.old
cat requirements.txt.old | grep -v "Media_Insights_Engine_Lambda_Helper" > requirements.txt
echo "/packages/$file" >> requirements.txt;
# Build Lambda layer zip files and rename them to the filenames expected by media-insights-stack.yaml. The Lambda layer build script runs in Docker.
# If Docker is not installed, then we'll use prebuilt Lambda layer zip files.
echo "Running build-lambda-layer.sh:"
rm -rf lambda_layer-python-* lambda_layer-python*.zip
if `./build-lambda-layer.sh requirements.txt > /dev/null`; then
mv lambda_layer-python3.6.zip media_insights_engine_lambda_layer_python3.6.zip
mv lambda_layer-python3.7.zip media_insights_engine_lambda_layer_python3.7.zip
mv lambda_layer-python3.8.zip media_insights_engine_lambda_layer_python3.8.zip
rm -rf lambda_layer-python-3.6/ lambda_layer-python-3.7/ lambda_layer-python-3.8/
echo "Lambda layer build script completed.";
else
echo "WARNING: Lambda layer build script failed. We'll use a pre-built Lambda layers instead.";
echo "Downloading https://rodeolabz-$region.$s3domain/media_insights_engine/media_insights_engine_lambda_layer_python3.6.zip"
wget -q https://rodeolabz-"$region"."$s3domain"/media_insights_engine/media_insights_engine_lambda_layer_python3.6.zip
echo "Downloading https://rodeolabz-$region.$s3domain/media_insights_engine/media_insights_engine_lambda_layer_python3.7.zip"
wget -q https://rodeolabz-"$region"."$s3domain"/media_insights_engine/media_insights_engine_lambda_layer_python3.7.zip
echo "Downloading https://rodeolabz-$region.$s3domain/media_insights_engine/media_insights_engine_lambda_layer_python3.8.zip"
wget -q https://rodeolabz-"$region"."$s3domain"/media_insights_engine/media_insights_engine_lambda_layer_python3.8.zip
fi
echo "Copying Lambda layer zips to $regional_dist_dir:"
mv media_insights_engine_lambda_layer_python3.6.zip "$regional_dist_dir"
mv media_insights_engine_lambda_layer_python3.7.zip "$regional_dist_dir"
mv media_insights_engine_lambda_layer_python3.8.zip "$regional_dist_dir"
mv requirements.txt.old requirements.txt
cd "$build_dir" || exit 1
fi
echo "------------------------------------------------------------------------------"
echo "CloudFormation Templates"
echo "------------------------------------------------------------------------------"
echo "Preparing template files:"
cp "$source_dir/operators/operator-library.yaml" "$global_dist_dir/media-insights-operator-library.template"
cp "$build_dir/media-insights-stack.yaml" "$global_dist_dir/media-insights-stack.template"
cp "$build_dir/media-insights-test-operations-stack.yaml" "$global_dist_dir/media-insights-test-operations-stack.template"
cp "$build_dir/media-insights-dataplane-streaming-stack.template" "$global_dist_dir/media-insights-dataplane-streaming-stack.template"
find "$global_dist_dir"
echo "Updating template source bucket in template files with '$global_bucket'"
echo "Updating code source bucket in template files with '$regional_bucket'"
echo "Updating solution version in template files with '$version'"
new_global_bucket="s/%%GLOBAL_BUCKET_NAME%%/$global_bucket/g"
new_regional_bucket="s/%%REGIONAL_BUCKET_NAME%%/$regional_bucket/g"
new_version="s/%%VERSION%%/$version/g"
# Update templates in place. Copy originals to [filename].orig
sed -i.orig -e "$new_global_bucket" "$global_dist_dir/media-insights-stack.template"
sed -i.orig -e "$new_regional_bucket" "$global_dist_dir/media-insights-stack.template"
sed -i.orig -e "$new_version" "$global_dist_dir/media-insights-stack.template"
sed -i.orig -e "$new_global_bucket" "$global_dist_dir/media-insights-operator-library.template"
sed -i.orig -e "$new_regional_bucket" "$global_dist_dir/media-insights-operator-library.template"
sed -i.orig -e "$new_version" "$global_dist_dir/media-insights-operator-library.template"
sed -i.orig -e "$new_global_bucket" "$global_dist_dir/media-insights-test-operations-stack.template"
sed -i.orig -e "$new_regional_bucket" "$global_dist_dir/media-insights-test-operations-stack.template"
sed -i.orig -e "$new_version" "$global_dist_dir/media-insights-test-operations-stack.template"
sed -i.orig -e "$new_global_bucket" "$global_dist_dir/media-insights-dataplane-streaming-stack.template"
sed -i.orig -e "$new_regional_bucket" "$global_dist_dir/media-insights-dataplane-streaming-stack.template"
sed -i.orig -e "$new_version" "$global_dist_dir/media-insights-dataplane-streaming-stack.template"
echo "------------------------------------------------------------------------------"
echo "Operators"
echo "------------------------------------------------------------------------------"
# ------------------------------------------------------------------------------"
# Operator Failed Lambda
# ------------------------------------------------------------------------------"
echo "Building 'operator failed' function"
cd "$source_dir/operators/operator_failed" || exit 1
[ -e dist ] && rm -rf dist
mkdir -p dist
zip -q dist/operator_failed.zip operator_failed.py
cp "./dist/operator_failed.zip" "$regional_dist_dir/operator_failed.zip"
rm -rf ./dist
# ------------------------------------------------------------------------------"
# Mediainfo Operation
# ------------------------------------------------------------------------------"
echo "Building Mediainfo function"
cd "$source_dir/operators/mediainfo" || exit 1
# Make lambda package
[ -e dist ] && rm -rf dist
mkdir -p dist
# Add the app code to the dist zip.
zip -q dist/mediainfo.zip mediainfo.py
# Zip is ready. Copy it to the distribution directory.
cp "./dist/mediainfo.zip" "$regional_dist_dir/mediainfo.zip"
rm -rf ./dist
# ------------------------------------------------------------------------------"
# Mediaconvert Operations
# ------------------------------------------------------------------------------"
echo "Building Media Convert function"
cd "$source_dir/operators/mediaconvert" || exit 1
[ -e dist ] && rm -rf dist
mkdir -p dist
zip -q dist/start_media_convert.zip start_media_convert.py
zip -q dist/get_media_convert.zip get_media_convert.py
cp "./dist/start_media_convert.zip" "$regional_dist_dir/start_media_convert.zip"
cp "./dist/get_media_convert.zip" "$regional_dist_dir/get_media_convert.zip"
rm -rf ./dist
# ------------------------------------------------------------------------------"
# Thumbnail Operations
# ------------------------------------------------------------------------------"
echo "Building Thumbnail function"
cd "$source_dir/operators/thumbnail" || exit 1
# Make lambda package
[ -e dist ] && rm -rf dist
mkdir -p dist
if ! [ -d ./dist/start_thumbnail.zip ]; then
zip -q -r9 ./dist/start_thumbnail.zip .
elif [ -d ./dist/start_thumbnail.zip ]; then
echo "Package already present"
fi
zip -q -g dist/start_thumbnail.zip start_thumbnail.py
cp "./dist/start_thumbnail.zip" "$regional_dist_dir/start_thumbnail.zip"
if ! [ -d ./dist/check_thumbnail.zip ]; then
zip -q -r9 ./dist/check_thumbnail.zip .
elif [ -d ./dist/check_thumbnail.zip ]; then
echo "Package already present"
fi
zip -q -g dist/check_thumbnail.zip check_thumbnail.py
cp "./dist/check_thumbnail.zip" "$regional_dist_dir/check_thumbnail.zip"
rm -rf ./dist
# ------------------------------------------------------------------------------"
# Transcribe Operations
# ------------------------------------------------------------------------------"
echo "Building Transcribe functions"
cd "$source_dir/operators/transcribe" || exit 1
[ -e dist ] && rm -rf dist
mkdir -p dist
zip -q -g ./dist/start_transcribe.zip ./start_transcribe.py
zip -q -g ./dist/get_transcribe.zip ./get_transcribe.py
cp "./dist/start_transcribe.zip" "$regional_dist_dir/start_transcribe.zip"
cp "./dist/get_transcribe.zip" "$regional_dist_dir/get_transcribe.zip"
rm -rf ./dist
# ------------------------------------------------------------------------------"
# Create Captions Operations
# ------------------------------------------------------------------------------"
echo "Building Webcaptions function"
cd "$source_dir/operators/captions" || exit
[ -e dist ] && rm -rf dist
mkdir -p dist
[ -e package ] && rm -r package
mkdir -p package
echo "preparing packages from requirements.txt"
# Package dependencies listed in requirements.txt
pushd package || exit 1
# Handle distutils install errors with setup.cfg
touch ./setup.cfg
echo "[install]" > ./setup.cfg
echo "prefix= " >> ./setup.cfg
pip3 install --quiet -r ../requirements.txt --target .
zip -q -r9 ../dist/webcaptions.zip .
popd || exit 1
zip -g ./dist/webcaptions.zip ./webcaptions.py
cp "./dist/webcaptions.zip" "$regional_dist_dir/webcaptions.zip"
# ------------------------------------------------------------------------------"
# Translate Operations
# ------------------------------------------------------------------------------"
echo "Building Translate function"
cd "$source_dir/operators/translate" || exit 1
[ -e dist ] && rm -rf dist
mkdir -p dist
[ -e package ] && rm -rf package
mkdir -p package
echo "create requirements for lambda"
# Make lambda package
pushd package || exit 1
echo "create lambda package"
# Handle distutils install errors
touch ./setup.cfg
echo "[install]" > ./setup.cfg
echo "prefix= " >> ./setup.cfg
pip3 install --quiet -r ../requirements.txt --target .
if ! [ -d ../dist/start_translate.zip ]; then
zip -q -r9 ../dist/start_translate.zip .
elif [ -d ../dist/start_translate.zip ]; then
echo "Package already present"
fi
popd || exit 1
zip -q -g ./dist/start_translate.zip ./start_translate.py
cp "./dist/start_translate.zip" "$regional_dist_dir/start_translate.zip"
rm -rf ./dist ./package
# ------------------------------------------------------------------------------"
# Polly operators
# ------------------------------------------------------------------------------"
echo "Building Polly function"
cd "$source_dir/operators/polly" || exit 1
[ -e dist ] && rm -rf dist
mkdir -p dist
zip -q -g ./dist/start_polly.zip ./start_polly.py
zip -q -g ./dist/get_polly.zip ./get_polly.py
cp "./dist/start_polly.zip" "$regional_dist_dir/start_polly.zip"
cp "./dist/get_polly.zip" "$regional_dist_dir/get_polly.zip"
rm -rf ./dist
# ------------------------------------------------------------------------------"
# Comprehend operators
# ------------------------------------------------------------------------------"
echo "Building Comprehend function"
cd "$source_dir/operators/comprehend" || exit 1
[ -e dist ] && rm -rf dist
[ -e package ] && rm -rf package
for dir in ./*;
do
echo "$dir"
cd "$dir" || exit 1
mkdir -p dist
mkdir -p package
echo "creating requirements for lambda"
# Package dependencies listed in requirements.txt
pushd package || exit 1
# Handle distutils install errors with setup.cfg
touch ./setup.cfg
echo "[install]" > ./setup.cfg
echo "prefix= " >> ./setup.cfg
if [[ $dir == "./key_phrases" ]]; then
if ! [ -d ../dist/start_key_phrases.zip ]; then
zip -q -r9 ../dist/start_key_phrases.zip .
elif [ -d ../dist/start_key_phrases.zip ]; then
echo "Package already present"
fi
if ! [ -d ../dist/get_key_phrases.zip ]; then
zip -q -r9 ../dist/get_key_phrases.zip .
elif [ -d ../dist/get_key_phrases.zip ]; then
echo "Package already present"
fi
popd || exit 1
zip -q -g dist/start_key_phrases.zip start_key_phrases.py
zip -q -g dist/get_key_phrases.zip get_key_phrases.py
echo "$PWD"
cp ./dist/start_key_phrases.zip "$regional_dist_dir/start_key_phrases.zip"
cp ./dist/get_key_phrases.zip "$regional_dist_dir/get_key_phrases.zip"
mv -f ./dist/*.zip "$regional_dist_dir"
elif [[ "$dir" == "./entities" ]]; then
if ! [ -d ../dist/start_entity_detection.zip ]; then
zip -q -r9 ../dist/start_entity_detection.zip .
elif [ -d ../dist/start_entity_detection.zip ]; then
echo "Package already present"
fi
if ! [ -d ../dist/get_entity_detection.zip ]; then
zip -q -r9 ../dist/get_entity_detection.zip .
elif [ -d ../dist/get_entity_detection.zip ]; then
echo "Package already present"
fi
popd || exit 1
echo "$PWD"
zip -q -g dist/start_entity_detection.zip start_entity_detection.py
zip -q -g dist/get_entity_detection.zip get_entity_detection.py
mv -f ./dist/*.zip "$regional_dist_dir"
fi
rm -rf ./dist ./package
cd ..
done;
# ------------------------------------------------------------------------------"
# Rekognition operators
# ------------------------------------------------------------------------------"
echo "Building Rekognition functions"
cd "$source_dir/operators/rekognition" || exit 1
# Make lambda package
echo "creating lambda packages"
# All the Python dependencies for Rekognition functions are in the Lambda layer, so
# we can deploy the zipped source file without dependencies.
zip -q -r9 generic_data_lookup.zip generic_data_lookup.py
zip -q -r9 start_celebrity_recognition.zip start_celebrity_recognition.py
zip -q -r9 check_celebrity_recognition_status.zip check_celebrity_recognition_status.py
zip -q -r9 start_content_moderation.zip start_content_moderation.py
zip -q -r9 check_content_moderation_status.zip check_content_moderation_status.py
zip -q -r9 start_face_detection.zip start_face_detection.py
zip -q -r9 check_face_detection_status.zip check_face_detection_status.py
zip -q -r9 start_face_search.zip start_face_search.py
zip -q -r9 check_face_search_status.zip check_face_search_status.py
zip -q -r9 start_label_detection.zip start_label_detection.py
zip -q -r9 check_label_detection_status.zip check_label_detection_status.py
zip -q -r9 start_person_tracking.zip start_person_tracking.py
zip -q -r9 check_person_tracking_status.zip check_person_tracking_status.py
zip -q -r9 start_text_detection.zip start_text_detection.py
zip -q -r9 check_text_detection_status.zip check_text_detection_status.py
zip -q -r9 check_text_detection_status.zip check_text_detection_status.py
zip -q -r9 start_technical_cue_detection.zip start_technical_cue_detection.py
zip -q -r9 check_technical_cue_status.zip check_technical_cue_status.py
zip -q -r9 start_shot_detection.zip start_shot_detection.py
zip -q -r9 check_shot_detection_status.zip check_shot_detection_status.py
mv -f ./*.zip "$regional_dist_dir"
# ------------------------------------------------------------------------------"
# Test operators
# ------------------------------------------------------------------------------"
echo "Building test operators"
cd "$source_dir/operators/test" || exit
[ -e dist ] && rm -rf dist
mkdir -p dist
zip -q -g ./dist/test_operations.zip ./test.py
cp "./dist/test_operations.zip" "$regional_dist_dir/test_operations.zip"
rm -rf ./dist
echo "------------------------------------------------------------------------------"
echo "DynamoDB Stream Function"
echo "------------------------------------------------------------------------------"
echo "Building DDB Stream function"
cd "$source_dir/dataplanestream" || exit 1
[ -e dist ] && rm -rf dist
mkdir -p dist
[ -e package ] && rm -rf package
mkdir -p package
echo "preparing packages from requirements.txt"
# Package dependencies listed in requirements.txt
pushd package || exit 1
# Handle distutils install errors with setup.cfg
touch ./setup.cfg
echo "[install]" > ./setup.cfg
echo "prefix= " >> ./setup.cfg
which pip3
python3 -c "import boto3"
pip3 install --quiet -r ../requirements.txt --target .
zip -q -r9 ../dist/ddbstream.zip .
popd || exit 1
zip -q -g dist/ddbstream.zip ./*.py
cp "./dist/ddbstream.zip" "$regional_dist_dir/ddbstream.zip"
rm -rf ./dist ./package
echo "------------------------------------------------------------------------------"
echo "Workflow Scheduler"
echo "------------------------------------------------------------------------------"
echo "Building Workflow scheduler"
cd "$source_dir/workflow" || exit 1
[ -e dist ] && rm -rf dist
mkdir -p dist
[ -e package ] && rm -rf package
mkdir -p package
echo "preparing packages from requirements.txt"
# Package dependencies listed in requirements.txt
pushd package || exit 1
# Handle distutils install errors with setup.cfg
touch ./setup.cfg
echo "[install]" > ./setup.cfg
echo "prefix= " >> ./setup.cfg
pip3 install --quiet -r ../requirements.txt --target .
zip -q -r9 ../dist/workflow.zip .
popd || exit 1
zip -q -g dist/workflow.zip ./*.py
cp "./dist/workflow.zip" "$regional_dist_dir/workflow.zip"
rm -rf ./dist ./package/
echo "------------------------------------------------------------------------------"
echo "Workflow API Stack"
echo "------------------------------------------------------------------------------"
echo "Building Workflow Lambda function"
cd "$source_dir/workflowapi" || exit 1
[ -e dist ] && rm -rf dist
mkdir -p dist
if ! [ -x "$(command -v chalice)" ]; then
echo 'Chalice is not installed. It is required for this solution. Exiting.'
exit 1
fi
# Remove chalice deployments to force redeploy when there are changes to configuration only
# Otherwise, chalice will use the existing deployment package
[ -e .chalice/deployments ] && rm -rf .chalice/deployments
echo "running chalice..."
chalice package --merge-template external_resources.json dist
echo "...chalice done"
echo "cp ./dist/sam.json $global_dist_dir/media-insights-workflowapi-stack.template"
cp dist/sam.json "$global_dist_dir"/media-insights-workflowapi-stack.template
if [ $? -ne 0 ]; then
echo "ERROR: Failed to build workflow api template"
exit 1
fi
echo "cp ./dist/deployment.zip $regional_dist_dir/workflowapi.zip"
cp ./dist/deployment.zip "$regional_dist_dir"/workflowapi.zip
if [ $? -ne 0 ]; then
echo "ERROR: Failed to build workflow api template"
exit 1
fi
rm -rf ./dist
echo "------------------------------------------------------------------------------"
echo "Workflow Execution DynamoDB Stream Function"
echo "------------------------------------------------------------------------------"
echo "Building Workflow Execution DDB Stream function"
cd "$source_dir/workflowstream" || exit 1
[ -e dist ] && rm -r dist
mkdir -p dist
[ -e package ] && rm -r package
mkdir -p package
echo "preparing packages from requirements.txt"
# Package dependencies listed in requirements.txt
pushd package || exit 1
# Handle distutils install errors with setup.cfg
touch ./setup.cfg
echo "[install]" > ./setup.cfg
echo "prefix= " >> ./setup.cfg
pip3 install --quiet -r ../requirements.txt --target .
zip -q -r9 ../dist/workflowstream.zip .
popd || exit 1
zip -q -g dist/workflowstream.zip ./*.py
cp "./dist/workflowstream.zip" "$regional_dist_dir/workflowstream.zip"
echo "------------------------------------------------------------------------------"
echo "Dataplane API Stack"
echo "------------------------------------------------------------------------------"
echo "Building Dataplane Stack"
cd "$source_dir/dataplaneapi" || exit 1
[ -e dist ] && rm -rf dist
mkdir -p dist
if ! [ -x "$(command -v chalice)" ]; then
echo 'Chalice is not installed. It is required for this solution. Exiting.'
exit 1
fi
# Remove chalice deployments to force redeploy when there are changes to configuration only
# Otherwise, chalice will use the existing deployment package
[ -e .chalice/deployments ] && rm -rf .chalice/deployments
chalice package --merge-template external_resources.json dist
echo "cp ./dist/sam.json $global_dist_dir/media-insights-dataplane-api-stack.template"
cp dist/sam.json "$global_dist_dir"/media-insights-dataplane-api-stack.template
if [ $? -ne 0 ]; then
echo "ERROR: Failed to build dataplane api template"
exit 1
fi
echo "cp ./dist/deployment.zip $regional_dist_dir/dataplaneapi.zip"
cp ./dist/deployment.zip "$regional_dist_dir"/dataplaneapi.zip
if [ $? -ne 0 ]; then
echo "ERROR: Failed to build dataplane api template"
exit 1
fi
rm -rf ./dist
echo "------------------------------------------------------------------------------"
echo "Creating deployment package for anonymous data logger"
echo "------------------------------------------------------------------------------"
echo "Building anonymous data logger"
cd "$source_dir/anonymous-data-logger" || exit 1
[ -e dist ] && rm -rf dist
mkdir -p dist
[ -e package ] && rm -rf package
mkdir -p package
echo "create requirements for lambda"
# Make lambda package
pushd package || exit 1
echo "create lambda package"
# Handle distutils install errors
touch ./setup.cfg
echo "[install]" > ./setup.cfg
echo "prefix= " >> ./setup.cfg
pip3 install --quiet -r ../requirements.txt --target .
cp -R ../lib .
if ! [ -d ../dist/anonymous-data-logger.zip ]; then
zip -q -r9 ../dist/anonymous-data-logger.zip .
elif [ -d ../dist/anonymous-data-logger.zip ]; then
echo "Package already present"
fi
popd || exit 1
zip -q -g ./dist/anonymous-data-logger.zip ./anonymous-data-logger.py
cp "./dist/anonymous-data-logger.zip" "$regional_dist_dir/anonymous-data-logger.zip"
#rm -rf ./dist ./package
# Skip copy dist to S3 if building for solution builder because
# that pipeline takes care of copying the dist in another script.
if [ "$TEMPLATE_OUTPUT_BUCKET" != "solutions-reference" ] && [ "$TEMPLATE_OUTPUT_BUCKET" != "solutions-test-reference" ]; then
echo "------------------------------------------------------------------------------"
echo "Copy dist to S3"
echo "------------------------------------------------------------------------------"
cd "$build_dir"/ || exit 1
echo "Copying the prepared distribution to:"
echo "s3://$global_bucket/aws-media-insights-engine/$version/"
echo "s3://${regional_bucket}-${region}/aws-media-insights-engine/$version/"
set -x
aws s3 sync $global_dist_dir s3://$global_bucket/aws-media-insights-engine/$version/
aws s3 sync $regional_dist_dir s3://${regional_bucket}-${region}/aws-media-insights-engine/$version/
set +x
echo "------------------------------------------------------------------------------"
echo "S3 packaging complete"
echo "------------------------------------------------------------------------------"
echo ""
echo "Template to deploy:"
echo "TEMPLATE='"https://"$global_bucket"."$s3domain"/aws-media-insights-engine/"$version"/media-insights-stack.template"'"
# Save the template URI for test automation scripts:
touch templateUrl.txt
echo "https://"$global_bucket"."$s3domain"/aws-media-insights-engine/"$version"/media-insights-stack.template" > templateUrl.txt
fi
cleanup
echo "Done"
exit 0