Skip to content

examples deployment scripts refresh #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Examples/LambdaFunctions/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
FROM swiftlang/swift:nightly-master-amazonlinux2
FROM swift:5.2-amazonlinux2

RUN yum -y install zip
13 changes: 11 additions & 2 deletions Examples/LambdaFunctions/scripts/build-and-package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to fill a knowledge gap on my site: What's the benefit of using bash -cl "cmd" vs. just cmd

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"
7 changes: 3 additions & 4 deletions Examples/LambdaFunctions/scripts/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 "-------------------------------------------------------------------------"
echo "-------------------------------------------------------------------------"
39 changes: 12 additions & 27 deletions Examples/LambdaFunctions/scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,29 @@

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

workspace="$DIR/../.."

echo -e "\ndeploying $executable"

echo "-------------------------------------------------------------------------"
echo "preparing docker build image"
echo "-------------------------------------------------------------------------"
docker build . -t builder
$DIR/build-and-package.sh "$executable"

echo "-------------------------------------------------------------------------"
echo "building \"$executable\" lambda"
echo "uploading \"$executable\" lambda to AWS S3"
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 "-------------------------------------------------------------------------"
echo "packaging \"$executable\" lambda"
echo "-------------------------------------------------------------------------"
docker run --rm -v `pwd`:/workspace -w /workspace builder bash -cl "./scripts/package.sh $executable"
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 "uploading \"$executable\" lambda to s3"
echo "updating AWS Lambda to use \"$executable\""
echo "-------------------------------------------------------------------------"

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

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
5 changes: 3 additions & 2 deletions Examples/LambdaFunctions/scripts/package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ 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"
# add the target deps based on ldd
ldd ".build/release/$executable" | grep swift | awk '{print $3}' | xargs cp -Lv -t "$target"
cd "$target"
ln -s "$executable" "bootstrap"
zip --symlinks lambda.zip *
9 changes: 2 additions & 7 deletions Examples/LambdaFunctions/scripts/sam-deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" $@
sam deploy --template "./scripts/SAM/$executable-template.yml" $@
11 changes: 4 additions & 7 deletions Examples/LambdaFunctions/scripts/serverless-deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
serverless deploy --config "./scripts/serverless/$executable-template.yml" --stage dev -v
4 changes: 3 additions & 1 deletion Examples/LambdaFunctions/scripts/serverless-remove.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
##
##===----------------------------------------------------------------------===##

set -eu

DIR="$(cd "$(dirname "$0")" && pwd)"
source $DIR/config.sh

Expand All @@ -22,4 +24,4 @@ echo "-------------------------------------------------------------------------"
echo "removing using Serverless"
echo "-------------------------------------------------------------------------"

serverless remove --config "./scripts/serverless/${executable}-template.yml" --stage dev -v
serverless remove --config "./scripts/serverless/$executable-template.yml" --stage dev -v
2 changes: 1 addition & 1 deletion Examples/LocalDebugging/MyLambda/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
FROM swiftlang/swift:nightly-master-amazonlinux2
FROM swift:5.2-amazonlinux2

RUN yum -y install zip
26 changes: 7 additions & 19 deletions Examples/LocalDebugging/MyLambda/scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,31 @@

set -eu

executable=MyLambda
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

echo -e "\ndeploying $executable"

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"
Expand Down
5 changes: 3 additions & 2 deletions Examples/LocalDebugging/MyLambda/scripts/package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ 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"
# add the target deps based on ldd
ldd ".build/release/$executable" | grep swift | awk '{print $3}' | xargs cp -Lv -t "$target"
cd "$target"
ln -s "$executable" "bootstrap"
zip --symlinks lambda.zip *