From bb9c345b8aee5d6f353150ae00e2f7243969653a Mon Sep 17 00:00:00 2001 From: tom doron Date: Thu, 18 Jun 2020 21:42:51 -0700 Subject: [PATCH 1/3] examples refresh 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 --- Examples/LambdaFunctions/Dockerfile | 2 +- .../scripts/build-and-package.sh | 13 +++++++-- Examples/LambdaFunctions/scripts/config.sh | 7 +++-- Examples/LambdaFunctions/scripts/deploy.sh | 27 ++++--------------- Examples/LambdaFunctions/scripts/package.sh | 8 ++++-- .../LambdaFunctions/scripts/sam-deploy.sh | 9 ++----- .../scripts/serverless-deploy.sh | 11 +++----- .../scripts/serverless-remove.sh | 4 ++- Examples/LocalDebugging/MyLambda/Dockerfile | 2 +- .../LocalDebugging/MyLambda/scripts/deploy.sh | 26 +++++------------- .../MyLambda/scripts/package.sh | 8 ++++-- 11 files changed, 49 insertions(+), 68 deletions(-) diff --git a/Examples/LambdaFunctions/Dockerfile b/Examples/LambdaFunctions/Dockerfile index 7c1df8e5..d5315703 100644 --- a/Examples/LambdaFunctions/Dockerfile +++ b/Examples/LambdaFunctions/Dockerfile @@ -1,3 +1,3 @@ -FROM swiftlang/swift:nightly-master-amazonlinux2 +FROM swift:5.2-amazonlinux2 RUN yum -y install zip diff --git a/Examples/LambdaFunctions/scripts/build-and-package.sh b/Examples/LambdaFunctions/scripts/build-and-package.sh index b59f2314..4e45c486 100755 --- a/Examples/LambdaFunctions/scripts/build-and-package.sh +++ b/Examples/LambdaFunctions/scripts/build-and-package.sh @@ -18,13 +18,22 @@ set -eu executable=$1 workspace="$(pwd)/../.." +echo "-------------------------------------------------------------------------" +echo "preparing docker build image" +echo "-------------------------------------------------------------------------" +docker build . -t builder +echo "done" + echo "-------------------------------------------------------------------------" echo "building \"$executable\" lambda" echo "-------------------------------------------------------------------------" -docker run --rm -v "$workspace":/workspace -w /workspace/Examples/LambdaFunctions builder bash -cl "swift build --product $executable -c release -Xswiftc -g" +docker run --rm -v "$workspace":/workspace -w /workspace/Examples/LambdaFunctions builder \ + bash -cl "swift build --product $executable -c release" echo "done" echo "-------------------------------------------------------------------------" echo "packaging \"$executable\" lambda" echo "-------------------------------------------------------------------------" -docker run --rm -v "$workspace":/workspace -w /workspace/Examples/LambdaFunctions builder bash -cl "./scripts/package.sh $executable" +docker run --rm -v "$workspace":/workspace -w /workspace/Examples/LambdaFunctions builder \ + bash -cl "./scripts/package.sh $executable" +echo "done" diff --git a/Examples/LambdaFunctions/scripts/config.sh b/Examples/LambdaFunctions/scripts/config.sh index d6ec4b7f..d4ab9f6f 100755 --- a/Examples/LambdaFunctions/scripts/config.sh +++ b/Examples/LambdaFunctions/scripts/config.sh @@ -14,7 +14,6 @@ ##===----------------------------------------------------------------------===## DIR="$(cd "$(dirname "$0")" && pwd)" - executables=( $(swift package dump-package | sed -e 's|: null|: ""|g' | jq '.products[] | (select(.type.executable)) | .name' | sed -e 's|"||g') ) if [[ ${#executables[@]} = 0 ]]; then @@ -32,8 +31,8 @@ elif [[ ${#executables[@]} > 1 ]]; then fi echo "-------------------------------------------------------------------------" -echo "CONFIG" +echo "configuration" echo "-------------------------------------------------------------------------" -echo "DIR: $DIR" +echo "current dir: $DIR" echo "executable: $executable" -echo "-------------------------------------------------------------------------" \ No newline at end of file +echo "-------------------------------------------------------------------------" diff --git a/Examples/LambdaFunctions/scripts/deploy.sh b/Examples/LambdaFunctions/scripts/deploy.sh index 619826eb..8f556e95 100755 --- a/Examples/LambdaFunctions/scripts/deploy.sh +++ b/Examples/LambdaFunctions/scripts/deploy.sh @@ -17,42 +17,25 @@ set -eu # Lambda Function name (must exist in AWS Lambda) lambda_name=SwiftSample - # S3 bucket name to upload zip file (must exist in AWS S3) s3_bucket=swift-lambda-test -workspace="$(pwd)/../.." - DIR="$(cd "$(dirname "$0")" && pwd)" source $DIR/config.sh -echo -e "\ndeploying $executable" - -echo "-------------------------------------------------------------------------" -echo "preparing docker build image" -echo "-------------------------------------------------------------------------" -docker build . -t builder +workspace="$DIR/../.." -echo "-------------------------------------------------------------------------" -echo "building \"$executable\" lambda" -echo "-------------------------------------------------------------------------" -docker run --rm -v $workspace:/workspace -w /workspace builder \ - bash -cl "cd Examples/LambdaFunctions && \ - swift build --product $executable -c release -Xswiftc -g" -echo "done" +echo -e "\ndeploying $executable" -echo "-------------------------------------------------------------------------" -echo "packaging \"$executable\" lambda" -echo "-------------------------------------------------------------------------" -docker run --rm -v `pwd`:/workspace -w /workspace builder bash -cl "./scripts/package.sh $executable" +$DIR/build-and-package.sh "$executable" echo "-------------------------------------------------------------------------" echo "uploading \"$executable\" lambda to s3" echo "-------------------------------------------------------------------------" -aws s3 cp .build/lambda/$executable/lambda.zip s3://$s3_bucket/ +aws s3 cp ".build/lambda/$executable/lambda.zip" "s3://$s3_bucket/" echo "-------------------------------------------------------------------------" echo "updating \"$lambda_name\" to latest \"$executable\"" echo "-------------------------------------------------------------------------" -aws lambda update-function-code --function $lambda_name --s3-bucket $s3_bucket --s3-key lambda.zip +aws lambda update-function-code --function "$lambda_name" --s3-bucket "$s3_bucket" --s3-key lambda.zip diff --git a/Examples/LambdaFunctions/scripts/package.sh b/Examples/LambdaFunctions/scripts/package.sh index 190be3f8..7115418f 100755 --- a/Examples/LambdaFunctions/scripts/package.sh +++ b/Examples/LambdaFunctions/scripts/package.sh @@ -17,11 +17,15 @@ set -eu executable=$1 -target=.build/lambda/$executable +target=".build/lambda/$executable" rm -rf "$target" mkdir -p "$target" cp ".build/release/$executable" "$target/" -cp -Pv /usr/lib/swift/linux/lib*so* "$target" +#cp -Pv /usr/lib/swift/linux/lib*so* "$target" +# add the target deps based on ldd +ldd ".build/release/$executable" | grep swift | awk '{print $3}' | xargs cp -Pv -t "$target" +# add icu (ldd just gets the symlink) +cp -Pv /usr/lib/swift/linux/libicu*so* "$target" cd "$target" ln -s "$executable" "bootstrap" zip --symlinks lambda.zip * diff --git a/Examples/LambdaFunctions/scripts/sam-deploy.sh b/Examples/LambdaFunctions/scripts/sam-deploy.sh index 779ed0e9..d87d966d 100755 --- a/Examples/LambdaFunctions/scripts/sam-deploy.sh +++ b/Examples/LambdaFunctions/scripts/sam-deploy.sh @@ -18,15 +18,10 @@ source $DIR/config.sh echo -e "\ndeploying $executable" -echo "-------------------------------------------------------------------------" -echo "preparing docker build image" -echo "-------------------------------------------------------------------------" -docker build . -q -t builder - -$DIR/build-and-package.sh ${executable} +$DIR/build-and-package.sh "$executable" echo "-------------------------------------------------------------------------" echo "deploying using SAM" echo "-------------------------------------------------------------------------" -sam deploy --template "./scripts/SAM/${executable}-template.yml" $@ \ No newline at end of file +sam deploy --template "./scripts/SAM/$executable-template.yml" $@ diff --git a/Examples/LambdaFunctions/scripts/serverless-deploy.sh b/Examples/LambdaFunctions/scripts/serverless-deploy.sh index 7e2d7cb0..241ee7bf 100755 --- a/Examples/LambdaFunctions/scripts/serverless-deploy.sh +++ b/Examples/LambdaFunctions/scripts/serverless-deploy.sh @@ -13,20 +13,17 @@ ## ##===----------------------------------------------------------------------===## +set -eu + DIR="$(cd "$(dirname "$0")" && pwd)" source $DIR/config.sh echo -e "\ndeploying $executable" -echo "-------------------------------------------------------------------------" -echo "preparing docker build image" -echo "-------------------------------------------------------------------------" -docker build . -q -t builder - -$DIR/build-and-package.sh ${executable} +$DIR/build-and-package.sh "$executable" echo "-------------------------------------------------------------------------" echo "deploying using Serverless" echo "-------------------------------------------------------------------------" -serverless deploy --config "./scripts/serverless/${executable}-template.yml" --stage dev -v \ No newline at end of file +serverless deploy --config "./scripts/serverless/$executable-template.yml" --stage dev -v diff --git a/Examples/LambdaFunctions/scripts/serverless-remove.sh b/Examples/LambdaFunctions/scripts/serverless-remove.sh index f6b228c5..262c07cb 100755 --- a/Examples/LambdaFunctions/scripts/serverless-remove.sh +++ b/Examples/LambdaFunctions/scripts/serverless-remove.sh @@ -13,6 +13,8 @@ ## ##===----------------------------------------------------------------------===## +set -eu + DIR="$(cd "$(dirname "$0")" && pwd)" source $DIR/config.sh @@ -22,4 +24,4 @@ echo "-------------------------------------------------------------------------" echo "removing using Serverless" echo "-------------------------------------------------------------------------" -serverless remove --config "./scripts/serverless/${executable}-template.yml" --stage dev -v \ No newline at end of file +serverless remove --config "./scripts/serverless/$executable-template.yml" --stage dev -v diff --git a/Examples/LocalDebugging/MyLambda/Dockerfile b/Examples/LocalDebugging/MyLambda/Dockerfile index 7c1df8e5..d5315703 100644 --- a/Examples/LocalDebugging/MyLambda/Dockerfile +++ b/Examples/LocalDebugging/MyLambda/Dockerfile @@ -1,3 +1,3 @@ -FROM swiftlang/swift:nightly-master-amazonlinux2 +FROM swift:5.2-amazonlinux2 RUN yum -y install zip diff --git a/Examples/LocalDebugging/MyLambda/scripts/deploy.sh b/Examples/LocalDebugging/MyLambda/scripts/deploy.sh index 8c137d60..800840b9 100755 --- a/Examples/LocalDebugging/MyLambda/scripts/deploy.sh +++ b/Examples/LocalDebugging/MyLambda/scripts/deploy.sh @@ -17,21 +17,7 @@ set -eu lambda_name=SwiftSample s3_bucket=swift-lambda-test -executables=( $(swift package dump-package | sed -e 's|: null|: ""|g' | jq '.products[] | (select(.type.executable)) | .name' | sed -e 's|"||g') ) - -if [[ ${#executables[@]} = 0 ]]; then - echo "no executables found" - exit 1 -elif [[ ${#executables[@]} = 1 ]]; then - executable=${executables[0]} -elif [[ ${#executables[@]} > 1 ]]; then - echo "multiple executables found:" - for executable in ${executables[@]}; do - echo " * $executable" - done - echo "" - read -p "select which executables to deploy: " executable -fi +executable=MyLambda echo -e "\ndeploying $executable" @@ -39,19 +25,21 @@ echo "-------------------------------------------------------------------------" echo "preparing docker build image" echo "-------------------------------------------------------------------------" docker build . -t builder +echo "done" echo "-------------------------------------------------------------------------" echo "building \"$executable\" lambda" echo "-------------------------------------------------------------------------" -docker run --rm -v `pwd`/../../..:/workspace -w /workspace builder \ - bash -cl "cd Examples/EndToEndDebugging/MyLambda && - swift build --product $executable -c release -Xswiftc -g" +docker run --rm -v `pwd`/../../..:/workspace -w /workspace/Examples/LocalDebugging/MyLambda builder \ + bash -cl "swift build --product $executable -c release" echo "done" echo "-------------------------------------------------------------------------" echo "packaging \"$executable\" lambda" echo "-------------------------------------------------------------------------" -docker run --rm -v `pwd`:/workspace -w /workspace builder bash -cl "./scripts/package.sh $executable" +docker run --rm -v `pwd`:/workspace -w /workspace builder \ + bash -cl "./scripts/package.sh $executable" +echo "done" echo "-------------------------------------------------------------------------" echo "uploading \"$executable\" lambda to s3" diff --git a/Examples/LocalDebugging/MyLambda/scripts/package.sh b/Examples/LocalDebugging/MyLambda/scripts/package.sh index 190be3f8..7115418f 100755 --- a/Examples/LocalDebugging/MyLambda/scripts/package.sh +++ b/Examples/LocalDebugging/MyLambda/scripts/package.sh @@ -17,11 +17,15 @@ set -eu executable=$1 -target=.build/lambda/$executable +target=".build/lambda/$executable" rm -rf "$target" mkdir -p "$target" cp ".build/release/$executable" "$target/" -cp -Pv /usr/lib/swift/linux/lib*so* "$target" +#cp -Pv /usr/lib/swift/linux/lib*so* "$target" +# add the target deps based on ldd +ldd ".build/release/$executable" | grep swift | awk '{print $3}' | xargs cp -Pv -t "$target" +# add icu (ldd just gets the symlink) +cp -Pv /usr/lib/swift/linux/libicu*so* "$target" cd "$target" ln -s "$executable" "bootstrap" zip --symlinks lambda.zip * From 3b502eb52bb0376b8678cfa13d08290f9b42e6f5 Mon Sep 17 00:00:00 2001 From: tom doron Date: Tue, 23 Jun 2020 17:31:58 -0700 Subject: [PATCH 2/3] fixup --- Examples/LambdaFunctions/scripts/package.sh | 5 +---- Examples/LocalDebugging/MyLambda/scripts/package.sh | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/Examples/LambdaFunctions/scripts/package.sh b/Examples/LambdaFunctions/scripts/package.sh index 7115418f..17d5853b 100755 --- a/Examples/LambdaFunctions/scripts/package.sh +++ b/Examples/LambdaFunctions/scripts/package.sh @@ -21,11 +21,8 @@ target=".build/lambda/$executable" rm -rf "$target" mkdir -p "$target" cp ".build/release/$executable" "$target/" -#cp -Pv /usr/lib/swift/linux/lib*so* "$target" # add the target deps based on ldd -ldd ".build/release/$executable" | grep swift | awk '{print $3}' | xargs cp -Pv -t "$target" -# add icu (ldd just gets the symlink) -cp -Pv /usr/lib/swift/linux/libicu*so* "$target" +ldd ".build/release/$executable" | grep swift | awk '{print $3}' | xargs cp -Lv -t "$target" cd "$target" ln -s "$executable" "bootstrap" zip --symlinks lambda.zip * diff --git a/Examples/LocalDebugging/MyLambda/scripts/package.sh b/Examples/LocalDebugging/MyLambda/scripts/package.sh index 7115418f..17d5853b 100755 --- a/Examples/LocalDebugging/MyLambda/scripts/package.sh +++ b/Examples/LocalDebugging/MyLambda/scripts/package.sh @@ -21,11 +21,8 @@ target=".build/lambda/$executable" rm -rf "$target" mkdir -p "$target" cp ".build/release/$executable" "$target/" -#cp -Pv /usr/lib/swift/linux/lib*so* "$target" # add the target deps based on ldd -ldd ".build/release/$executable" | grep swift | awk '{print $3}' | xargs cp -Pv -t "$target" -# add icu (ldd just gets the symlink) -cp -Pv /usr/lib/swift/linux/libicu*so* "$target" +ldd ".build/release/$executable" | grep swift | awk '{print $3}' | xargs cp -Lv -t "$target" cd "$target" ln -s "$executable" "bootstrap" zip --symlinks lambda.zip * From 8c1cd2821e57d1879e4b3556d7b45c540a723284 Mon Sep 17 00:00:00 2001 From: tom doron Date: Tue, 23 Jun 2020 17:46:26 -0700 Subject: [PATCH 3/3] fixup --- Examples/LambdaFunctions/scripts/deploy.sh | 16 +++++++++------- .../LocalDebugging/MyLambda/scripts/deploy.sh | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Examples/LambdaFunctions/scripts/deploy.sh b/Examples/LambdaFunctions/scripts/deploy.sh index 8f556e95..3720b4d0 100755 --- a/Examples/LambdaFunctions/scripts/deploy.sh +++ b/Examples/LambdaFunctions/scripts/deploy.sh @@ -15,11 +15,6 @@ set -eu -# Lambda Function name (must exist in AWS Lambda) -lambda_name=SwiftSample -# S3 bucket name to upload zip file (must exist in AWS S3) -s3_bucket=swift-lambda-test - DIR="$(cd "$(dirname "$0")" && pwd)" source $DIR/config.sh @@ -30,12 +25,19 @@ echo -e "\ndeploying $executable" $DIR/build-and-package.sh "$executable" echo "-------------------------------------------------------------------------" -echo "uploading \"$executable\" lambda to s3" +echo "uploading \"$executable\" lambda to AWS S3" echo "-------------------------------------------------------------------------" +read -p "S3 bucket name to upload zip file (must exist in AWS S3): " s3_bucket +s3_bucket=${s3_bucket:-swift-lambda-test} # default for easy testing + aws s3 cp ".build/lambda/$executable/lambda.zip" "s3://$s3_bucket/" echo "-------------------------------------------------------------------------" -echo "updating \"$lambda_name\" to latest \"$executable\"" +echo "updating AWS Lambda to use \"$executable\"" echo "-------------------------------------------------------------------------" + +read -p "Lambda Function name (must exist in AWS Lambda): " lambda_name +lambda_name=${lambda_name:-SwiftSample} # default for easy testing + aws lambda update-function-code --function "$lambda_name" --s3-bucket "$s3_bucket" --s3-key lambda.zip diff --git a/Examples/LocalDebugging/MyLambda/scripts/deploy.sh b/Examples/LocalDebugging/MyLambda/scripts/deploy.sh index 800840b9..75be0ceb 100755 --- a/Examples/LocalDebugging/MyLambda/scripts/deploy.sh +++ b/Examples/LocalDebugging/MyLambda/scripts/deploy.sh @@ -15,9 +15,9 @@ set -eu +executable=MyLambda lambda_name=SwiftSample s3_bucket=swift-lambda-test -executable=MyLambda echo -e "\ndeploying $executable"