Skip to content

Commit b9224e2

Browse files
authored
examples deployment scripts refresh (#133)
motivation: make sure examples are up tp date changes: * use swift:5.2 images instead of nighlies * compute dependencies to make zipfile as small as possible * make scripts more consistent
1 parent 2bac896 commit b9224e2

File tree

11 files changed

+50
-73
lines changed

11 files changed

+50
-73
lines changed

Examples/LambdaFunctions/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
FROM swiftlang/swift:nightly-master-amazonlinux2
1+
FROM swift:5.2-amazonlinux2
22

33
RUN yum -y install zip

Examples/LambdaFunctions/scripts/build-and-package.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,22 @@ set -eu
1818
executable=$1
1919
workspace="$(pwd)/../.."
2020

21+
echo "-------------------------------------------------------------------------"
22+
echo "preparing docker build image"
23+
echo "-------------------------------------------------------------------------"
24+
docker build . -t builder
25+
echo "done"
26+
2127
echo "-------------------------------------------------------------------------"
2228
echo "building \"$executable\" lambda"
2329
echo "-------------------------------------------------------------------------"
24-
docker run --rm -v "$workspace":/workspace -w /workspace/Examples/LambdaFunctions builder bash -cl "swift build --product $executable -c release -Xswiftc -g"
30+
docker run --rm -v "$workspace":/workspace -w /workspace/Examples/LambdaFunctions builder \
31+
bash -cl "swift build --product $executable -c release"
2532
echo "done"
2633

2734
echo "-------------------------------------------------------------------------"
2835
echo "packaging \"$executable\" lambda"
2936
echo "-------------------------------------------------------------------------"
30-
docker run --rm -v "$workspace":/workspace -w /workspace/Examples/LambdaFunctions builder bash -cl "./scripts/package.sh $executable"
37+
docker run --rm -v "$workspace":/workspace -w /workspace/Examples/LambdaFunctions builder \
38+
bash -cl "./scripts/package.sh $executable"
39+
echo "done"

Examples/LambdaFunctions/scripts/config.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
##===----------------------------------------------------------------------===##
1515

1616
DIR="$(cd "$(dirname "$0")" && pwd)"
17-
1817
executables=( $(swift package dump-package | sed -e 's|: null|: ""|g' | jq '.products[] | (select(.type.executable)) | .name' | sed -e 's|"||g') )
1918

2019
if [[ ${#executables[@]} = 0 ]]; then
@@ -32,8 +31,8 @@ elif [[ ${#executables[@]} > 1 ]]; then
3231
fi
3332

3433
echo "-------------------------------------------------------------------------"
35-
echo "CONFIG"
34+
echo "configuration"
3635
echo "-------------------------------------------------------------------------"
37-
echo "DIR: $DIR"
36+
echo "current dir: $DIR"
3837
echo "executable: $executable"
39-
echo "-------------------------------------------------------------------------"
38+
echo "-------------------------------------------------------------------------"

Examples/LambdaFunctions/scripts/deploy.sh

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,29 @@
1515

1616
set -eu
1717

18-
# Lambda Function name (must exist in AWS Lambda)
19-
lambda_name=SwiftSample
20-
21-
# S3 bucket name to upload zip file (must exist in AWS S3)
22-
s3_bucket=swift-lambda-test
23-
24-
workspace="$(pwd)/../.."
25-
2618
DIR="$(cd "$(dirname "$0")" && pwd)"
2719
source $DIR/config.sh
2820

21+
workspace="$DIR/../.."
22+
2923
echo -e "\ndeploying $executable"
3024

31-
echo "-------------------------------------------------------------------------"
32-
echo "preparing docker build image"
33-
echo "-------------------------------------------------------------------------"
34-
docker build . -t builder
25+
$DIR/build-and-package.sh "$executable"
3526

3627
echo "-------------------------------------------------------------------------"
37-
echo "building \"$executable\" lambda"
28+
echo "uploading \"$executable\" lambda to AWS S3"
3829
echo "-------------------------------------------------------------------------"
39-
docker run --rm -v $workspace:/workspace -w /workspace builder \
40-
bash -cl "cd Examples/LambdaFunctions && \
41-
swift build --product $executable -c release -Xswiftc -g"
42-
echo "done"
4330

44-
echo "-------------------------------------------------------------------------"
45-
echo "packaging \"$executable\" lambda"
46-
echo "-------------------------------------------------------------------------"
47-
docker run --rm -v `pwd`:/workspace -w /workspace builder bash -cl "./scripts/package.sh $executable"
31+
read -p "S3 bucket name to upload zip file (must exist in AWS S3): " s3_bucket
32+
s3_bucket=${s3_bucket:-swift-lambda-test} # default for easy testing
33+
34+
aws s3 cp ".build/lambda/$executable/lambda.zip" "s3://$s3_bucket/"
4835

4936
echo "-------------------------------------------------------------------------"
50-
echo "uploading \"$executable\" lambda to s3"
37+
echo "updating AWS Lambda to use \"$executable\""
5138
echo "-------------------------------------------------------------------------"
5239

53-
aws s3 cp .build/lambda/$executable/lambda.zip s3://$s3_bucket/
40+
read -p "Lambda Function name (must exist in AWS Lambda): " lambda_name
41+
lambda_name=${lambda_name:-SwiftSample} # default for easy testing
5442

55-
echo "-------------------------------------------------------------------------"
56-
echo "updating \"$lambda_name\" to latest \"$executable\""
57-
echo "-------------------------------------------------------------------------"
58-
aws lambda update-function-code --function $lambda_name --s3-bucket $s3_bucket --s3-key lambda.zip
43+
aws lambda update-function-code --function "$lambda_name" --s3-bucket "$s3_bucket" --s3-key lambda.zip

Examples/LambdaFunctions/scripts/package.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ set -eu
1717

1818
executable=$1
1919

20-
target=.build/lambda/$executable
20+
target=".build/lambda/$executable"
2121
rm -rf "$target"
2222
mkdir -p "$target"
2323
cp ".build/release/$executable" "$target/"
24-
cp -Pv /usr/lib/swift/linux/lib*so* "$target"
24+
# add the target deps based on ldd
25+
ldd ".build/release/$executable" | grep swift | awk '{print $3}' | xargs cp -Lv -t "$target"
2526
cd "$target"
2627
ln -s "$executable" "bootstrap"
2728
zip --symlinks lambda.zip *

Examples/LambdaFunctions/scripts/sam-deploy.sh

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,10 @@ source $DIR/config.sh
1818

1919
echo -e "\ndeploying $executable"
2020

21-
echo "-------------------------------------------------------------------------"
22-
echo "preparing docker build image"
23-
echo "-------------------------------------------------------------------------"
24-
docker build . -q -t builder
25-
26-
$DIR/build-and-package.sh ${executable}
21+
$DIR/build-and-package.sh "$executable"
2722

2823
echo "-------------------------------------------------------------------------"
2924
echo "deploying using SAM"
3025
echo "-------------------------------------------------------------------------"
3126

32-
sam deploy --template "./scripts/SAM/${executable}-template.yml" $@
27+
sam deploy --template "./scripts/SAM/$executable-template.yml" $@

Examples/LambdaFunctions/scripts/serverless-deploy.sh

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,17 @@
1313
##
1414
##===----------------------------------------------------------------------===##
1515

16+
set -eu
17+
1618
DIR="$(cd "$(dirname "$0")" && pwd)"
1719
source $DIR/config.sh
1820

1921
echo -e "\ndeploying $executable"
2022

21-
echo "-------------------------------------------------------------------------"
22-
echo "preparing docker build image"
23-
echo "-------------------------------------------------------------------------"
24-
docker build . -q -t builder
25-
26-
$DIR/build-and-package.sh ${executable}
23+
$DIR/build-and-package.sh "$executable"
2724

2825
echo "-------------------------------------------------------------------------"
2926
echo "deploying using Serverless"
3027
echo "-------------------------------------------------------------------------"
3128

32-
serverless deploy --config "./scripts/serverless/${executable}-template.yml" --stage dev -v
29+
serverless deploy --config "./scripts/serverless/$executable-template.yml" --stage dev -v

Examples/LambdaFunctions/scripts/serverless-remove.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
##
1414
##===----------------------------------------------------------------------===##
1515

16+
set -eu
17+
1618
DIR="$(cd "$(dirname "$0")" && pwd)"
1719
source $DIR/config.sh
1820

@@ -22,4 +24,4 @@ echo "-------------------------------------------------------------------------"
2224
echo "removing using Serverless"
2325
echo "-------------------------------------------------------------------------"
2426

25-
serverless remove --config "./scripts/serverless/${executable}-template.yml" --stage dev -v
27+
serverless remove --config "./scripts/serverless/$executable-template.yml" --stage dev -v
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
FROM swiftlang/swift:nightly-master-amazonlinux2
1+
FROM swift:5.2-amazonlinux2
22

33
RUN yum -y install zip

Examples/LocalDebugging/MyLambda/scripts/deploy.sh

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,31 @@
1515

1616
set -eu
1717

18+
executable=MyLambda
1819
lambda_name=SwiftSample
1920
s3_bucket=swift-lambda-test
20-
executables=( $(swift package dump-package | sed -e 's|: null|: ""|g' | jq '.products[] | (select(.type.executable)) | .name' | sed -e 's|"||g') )
21-
22-
if [[ ${#executables[@]} = 0 ]]; then
23-
echo "no executables found"
24-
exit 1
25-
elif [[ ${#executables[@]} = 1 ]]; then
26-
executable=${executables[0]}
27-
elif [[ ${#executables[@]} > 1 ]]; then
28-
echo "multiple executables found:"
29-
for executable in ${executables[@]}; do
30-
echo " * $executable"
31-
done
32-
echo ""
33-
read -p "select which executables to deploy: " executable
34-
fi
3521

3622
echo -e "\ndeploying $executable"
3723

3824
echo "-------------------------------------------------------------------------"
3925
echo "preparing docker build image"
4026
echo "-------------------------------------------------------------------------"
4127
docker build . -t builder
28+
echo "done"
4229

4330
echo "-------------------------------------------------------------------------"
4431
echo "building \"$executable\" lambda"
4532
echo "-------------------------------------------------------------------------"
46-
docker run --rm -v `pwd`/../../..:/workspace -w /workspace builder \
47-
bash -cl "cd Examples/EndToEndDebugging/MyLambda &&
48-
swift build --product $executable -c release -Xswiftc -g"
33+
docker run --rm -v `pwd`/../../..:/workspace -w /workspace/Examples/LocalDebugging/MyLambda builder \
34+
bash -cl "swift build --product $executable -c release"
4935
echo "done"
5036

5137
echo "-------------------------------------------------------------------------"
5238
echo "packaging \"$executable\" lambda"
5339
echo "-------------------------------------------------------------------------"
54-
docker run --rm -v `pwd`:/workspace -w /workspace builder bash -cl "./scripts/package.sh $executable"
40+
docker run --rm -v `pwd`:/workspace -w /workspace builder \
41+
bash -cl "./scripts/package.sh $executable"
42+
echo "done"
5543

5644
echo "-------------------------------------------------------------------------"
5745
echo "uploading \"$executable\" lambda to s3"

Examples/LocalDebugging/MyLambda/scripts/package.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ set -eu
1717

1818
executable=$1
1919

20-
target=.build/lambda/$executable
20+
target=".build/lambda/$executable"
2121
rm -rf "$target"
2222
mkdir -p "$target"
2323
cp ".build/release/$executable" "$target/"
24-
cp -Pv /usr/lib/swift/linux/lib*so* "$target"
24+
# add the target deps based on ldd
25+
ldd ".build/release/$executable" | grep swift | awk '{print $3}' | xargs cp -Lv -t "$target"
2526
cd "$target"
2627
ln -s "$executable" "bootstrap"
2728
zip --symlinks lambda.zip *

0 commit comments

Comments
 (0)