diff --git a/docker-compose.yml b/docker-compose.yml index 2e6ca25..35e1c10 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -59,8 +59,8 @@ services: SERVICES: sqs,lambda,cloudwatch,s3 DOCKER_HOST: unix:///var/run/docker.sock PORT_WEB_UI: 9070 - #LAMBDA_EXECUTOR: docker # está dando erro via docker - LAMBDA_EXECUTOR: local + LAMBDA_EXECUTOR: docker # está dando erro via docker + # LAMBDA_EXECUTOR: local DEBUG: 1 HOSTNAME_EXTERNAL: localstack networks: diff --git a/examples/lambda_api/.gitignore b/examples/lambda_api/.gitignore index 9fee715..fd1ae97 100644 --- a/examples/lambda_api/.gitignore +++ b/examples/lambda_api/.gitignore @@ -59,3 +59,4 @@ imagedefinitions.json config/integration.en /node_modules/ +/lambda-full.zip diff --git a/examples/lambda_api/.projectrc b/examples/lambda_api/.projectrc index 04c7fe5..52da2fb 100644 --- a/examples/lambda_api/.projectrc +++ b/examples/lambda_api/.projectrc @@ -1,2 +1,5 @@ PROJECT_NAME=template-serverless-lambda-python-lambda-api NETWORK_NAME=service-python +APP_QUEUE=test-queue +APP_LAMBDA_NAME=lambda_api +APP_LAMBDA_EVENT_SOURCE=false \ No newline at end of file diff --git a/examples/lambda_api/app.py b/examples/lambda_api/app.py index 01afb78..d2c05d7 100644 --- a/examples/lambda_api/app.py +++ b/examples/lambda_api/app.py @@ -292,7 +292,7 @@ def event_list(event_type): data = {} response.set_data(data) - response.set_total() + response.set_total(len(data)) # event_tracker.track(event_hash, data) return response.get_response(status_code) diff --git a/examples/lambda_api/docker/python/Dockerfile b/examples/lambda_api/docker/python/Dockerfile index f738b1c..8cee492 100644 --- a/examples/lambda_api/docker/python/Dockerfile +++ b/examples/lambda_api/docker/python/Dockerfile @@ -32,14 +32,19 @@ RUN apk add --no-cache build-base \ # upgrade pip RUN pip install --upgrade pip +RUN rm -Rf ./vendor/* + # Install requirements COPY requirements.txt requirements-vendor.txt ./ RUN pip install --no-cache-dir -r requirements.txt \ && pip install --no-cache-dir -r requirements-vendor.txt +# Flask compatibility with Python 3.8 +RUN pip uninstall dataclasses -y +RUN rm -Rf ./vendor/dataclasses-0.8.dist-info/ ./vendor/dataclasses.py + # Copy project files COPY . . -#CMD [ "flask", "run", "--host", "0.0.0.0" ] -CMD [ "python3", "server.py" ] +CMD [ "flask", "run", "--host", "0.0.0.0" ] diff --git a/examples/lambda_api/requirements-vendor.txt b/examples/lambda_api/requirements-vendor.txt index 4bd5050..1db657b 100644 --- a/examples/lambda_api/requirements-vendor.txt +++ b/examples/lambda_api/requirements-vendor.txt @@ -1,2 +1 @@ -boto3 -pymysql \ No newline at end of file +boto3 \ No newline at end of file diff --git a/examples/lambda_api/requirements.txt b/examples/lambda_api/requirements.txt index 03c5729..550124c 100644 --- a/examples/lambda_api/requirements.txt +++ b/examples/lambda_api/requirements.txt @@ -8,3 +8,4 @@ Flask pyyaml apispec marshmallow +pymysql diff --git a/examples/lambda_api/scripts/boot-lambda.sh b/examples/lambda_api/scripts/boot-lambda.sh new file mode 100755 index 0000000..d9149e9 --- /dev/null +++ b/examples/lambda_api/scripts/boot-lambda.sh @@ -0,0 +1,97 @@ +#!/bin/bash +# ----------------------------------------------------------------------------- +# Current file variables +# ----------------------------------------------------------------------------- +debug=false +parent_folder="../" +current_path=$(pwd)/ +current_path_basename=$(basename $(pwd)) +current_file_full_path=$0 +# echo $current_filepath +current_file_name=$(basename -- "$0") +# echo $current_filename +if [ $current_file_full_path = $current_file_name ] || [ $current_file_full_path = "./$current_file_name" ]; then + current_file_full_path="./${current_file_full_path}" + current_file_path="./" +else + current_file_path="${current_file_full_path/$current_file_name/''}" +fi + +current_file_path_basename=$(basename -- "$current_file_path") +#echo "xxxxx current_file_path_basename $current_file_path_basename" + +if [ -z "$current_file_path_basename" ] || [ $current_file_path = "./" ]; then +# echo 'aq' + current_parent_folder="../" +else +# echo 'naq' + current_file_path_basename=$current_file_path_basename/ + current_parent_folder="${current_file_path/$current_file_path_basename/''}" +fi + + +if [ debug ]; then + echo '----------------------------------------' + echo "$0 - Script variables" + echo '----------------------------------------' + echo "current_path: $current_path" + echo "current_path_basename: $current_path_basename" + echo "current_file_full_path: $current_file_full_path" + echo "current_file_name: $current_file_name" + echo "current_file_path: $current_file_path" + echo "current_parent_folder: $current_parent_folder" + echo '----------------------------------------' +fi + +echo '----------------------------------------' +echo "$0 - Booting lambda" +echo '----------------------------------------' +echo 'Installing dependencies...' +echo "Requirements file: ${current_parent_folder}requirements.txt" +if test -f ${current_parent_folder}requirements.txt; then + python3 -m pip install -r ${current_parent_folder}requirements.txt -t ${current_parent_folder}vendor +# cat ${current_parent_folder}requirements.txt + echo "requirements..." +fi + +echo "Requirements file: ${current_parent_folder}requirements-vendor.txt" +if test -f ${current_parent_folder}requirement.s-vendor.txt; then + python3 -m pip install -r ${current_parent_folder}requirements-vendor.txt -t ${current_parent_folder}vendor +# cat ${current_parent_folder}requirements-vendor.txt + echo "requirements vendor..." +fi + +read -p "Press enter to continue..." + +#echo 'Creating resource dependencies...' +#echo "${current_parent_folder}scripts/localstack/lambda/create-function-from-s3.sh" + +if test -f "${current_parent_folder}scripts/localstack/lambda/create-function-from-s3.sh"; then + + if test -f ${current_parent_folder}.projectrc; then + source ${current_parent_folder}.projectrc + fi + + if [ -z "$APP_LAMBDA_NAME" ]; then + echo 'APP_LAMBDA_NAME not defined' + exit 1 + else + echo '----------------------------------------' + echo "$0 - Creating the lambda: $APP_LAMBDA_NAME" + echo '----------------------------------------' + ${current_parent_folder}scripts/localstack/lambda/create-function-from-s3.sh $current_filename_path $APP_LAMBDA_NAME + + read -p "Press enter to continue..." + + if test $APP_LAMBDA_EVENT_SOURCE = true;then + echo '----------------------------------------' + echo "$0 - Creating the event source: $APP_LAMBDA_NAME" + echo '----------------------------------------' + ${current_parent_folder}scripts/localstack/lambda/create-event-source-mapping.sh $APP_LAMBDA_NAME $APP_QUEUE + else + echo 'There is no event source for this lambda' + fi + fi +else + echo "File not found: ${current_parent_folder}scripts/localstack/lambda/create-function-from-s3.sh" +fi diff --git a/examples/lambda_api/scripts/boot-lambdas.sh b/examples/lambda_api/scripts/boot-lambdas.sh deleted file mode 100755 index 2df53bf..0000000 --- a/examples/lambda_api/scripts/boot-lambdas.sh +++ /dev/null @@ -1,10 +0,0 @@ -echo 'Booting lambda...' -echo 'Installing dependencies...' -# ./scripts/venv.sh -python3 -m pip install -r ./requirements.txt -t ./vendor -python3 -m pip install -r ./requirements-vendor.txt -t ./vendor - -echo 'Creating resource dependencies...' -# create resource dependencies -./scripts/localstack/lambda/create-function-from-s3.sh lambda_sqs -#./scripts/localstack/lambda/create-event-source-mapping.sh lambda_sqs test-queue \ No newline at end of file diff --git a/examples/lambda_api/scripts/boot-queues.sh b/examples/lambda_api/scripts/boot-queues.sh index bfa2e80..5e1eda4 100755 --- a/examples/lambda_api/scripts/boot-queues.sh +++ b/examples/lambda_api/scripts/boot-queues.sh @@ -1 +1,31 @@ -./scripts/localstack/sqs/create-queue.sh test-queue +#!/bin/bash +current_path=$(basename $(pwd)) +current_filename=$(basename -- "$0") +current_filename_path=$0 +# echo $current_filename_path +current_filename_path="${current_filename_path/$current_filename/''}" +# echo $current_filename_path +current_filename_path="${current_filename_path/scripts\//''}" +# echo $current_filename_path +current_filename_path_basename=$(basename -- "$current_filename_path") + +echo "current_path: $current_path" +echo "current_filename: $current_filename" +echo "current_filename_path: $current_filename_path" +echo "current_filename_path_basename: $current_filename_path_basename" + +if test -f ${current_filename_path}/scripts/localstack/sqs/create-queue.sh; then + + if test -f ${current_filename_path}.projectrc; then + source ${current_filename_path}.projectrc + fi + + if [ -z "$APP_QUEUE" ]; then + echo 'APP_QUEUE not defined' + exit 1 + else + echo "Creating the queue: $APP_QUEUE" + ${current_filename_path}/scripts/localstack/sqs/create-queue.sh $APP_QUEUE + fi +fi + diff --git a/examples/lambda_api/scripts/boot.sh b/examples/lambda_api/scripts/boot.sh index 1fc9488..34f6fb0 100755 --- a/examples/lambda_api/scripts/boot.sh +++ b/examples/lambda_api/scripts/boot.sh @@ -1,22 +1,80 @@ #!/bin/bash -echo 'Validating jd installation..' - /usr/bin/jq --help > /dev/null 2>&1 - if [ $? -ne 0 ]; then - echo 'Installing' - # download directly into ~/bin_compciv - sudo curl http://stedolan.github.io/jq/download/linux64/jq -o /usr/bin/jq - # give it executable permissions - sudo chmod a+x /usr/bin/jq - fi - -echo 'Validate connection' -./scripts/boot-validate-connection.sh - -echo 'Booting db...' -./scripts/boot-db.sh - -echo 'Create the queues...' -./scripts/boot-queues.sh - -echo 'Create the lambdas...' -./scripts/boot-lambdas.sh \ No newline at end of file +# ----------------------------------------------------------------------------- +# Current file variables +# ----------------------------------------------------------------------------- +debug=false +parent_folder="../" +current_path=$(pwd)/ +current_path_basename=$(basename $(pwd)) +current_file_full_path=$0 +# echo $current_filepath +current_file_name=$(basename -- "$0") +# echo $current_filename +if [ $current_file_full_path = $current_file_name ] || [ $current_file_full_path = "./$current_file_name" ]; then + current_file_full_path="./${current_file_full_path}" + current_file_path="./" +else + current_file_path="${current_file_full_path/$current_file_name/''}" +fi +if [ debug ]; then + echo '----------------------------------------' + echo "$0 - Script variables" + echo '----------------------------------------' + echo "current_path: $current_path" + echo "current_path_basename: $current_path_basename" + echo "current_file_full_path: $current_file_full_path" + echo "current_file_name: $current_file_name" + echo "current_file_path: $current_file_path" + echo '----------------------------------------' +fi + + +echo '----------------------------------------' +echo "$0 - jq check" +echo '----------------------------------------' +echo 'Validating jq installation...' +/usr/bin/jq --version > /dev/null 2>&1 +if [ $? -ne 0 ]; then + echo 'Installing jq...' + # download directly into ~/bin_compciv + sudo curl http://stedolan.github.io/jq/download/linux64/jq -o /usr/bin/jq + # give it executable permissions + sudo chmod a+x /usr/bin/jq +else + echo 'jq installed...' +fi + +read -p "Press enter to continue..." + +echo '----------------------------------------' +echo "$0 - Localstack connection check" +echo '----------------------------------------' +# valida se o Localstack está rodando +if test -f ${current_file_path}boot-validate-connection.sh; then + echo 'Validate connection...' + ${current_file_path}boot-validate-connection.sh +else + echo 'There is no connection check file' +fi + +read -p "Press enter to continue..." + +echo '----------------------------------------' +echo "$0 - Queues boot" +echo '----------------------------------------' +if test -f ${current_file_path}boot-queues.sh; then + ${current_file_path}boot-queues.sh +else + echo 'There is no queues to be booted' +fi + +read -p "Press enter to continue..." + +echo '----------------------------------------' +echo "$0 - Lambda boot" +echo '----------------------------------------' +if test -f ${current_file_path}boot-lambda.sh; then + ${current_file_path}boot-lambda.sh +else + echo 'There is no lambda to be booted' +fi \ No newline at end of file diff --git a/examples/lambda_api/scripts/docker/create-network.sh b/examples/lambda_api/scripts/docker/create-network.sh index 155c553..73627f5 100755 --- a/examples/lambda_api/scripts/docker/create-network.sh +++ b/examples/lambda_api/scripts/docker/create-network.sh @@ -1,7 +1,7 @@ if test -f .projectrc; then source .projectrc -elif test -f ./bin/.projectrc; then - source ./bin/.projectrc +elif test -f ./scripts/.projectrc; then + source ./scripts/.projectrc fi if [ -z "$NETWORK_NAME" ]; then diff --git a/examples/lambda_sqs/scripts/localstack/lambda/create-event-source-mapping.sh b/examples/lambda_api/scripts/localstack/lambda/create-event-source-mapping.sh similarity index 100% rename from examples/lambda_sqs/scripts/localstack/lambda/create-event-source-mapping.sh rename to examples/lambda_api/scripts/localstack/lambda/create-event-source-mapping.sh diff --git a/examples/lambda_api/scripts/localstack/lambda/create-function-from-s3.sh b/examples/lambda_api/scripts/localstack/lambda/create-function-from-s3.sh new file mode 100755 index 0000000..20c6066 --- /dev/null +++ b/examples/lambda_api/scripts/localstack/lambda/create-function-from-s3.sh @@ -0,0 +1,175 @@ +# ----------------------------------------------------------------------------- +# Current file variables +# ----------------------------------------------------------------------------- +debug=false +parent_folder="../" +current_path=$(pwd)/ +current_path_basename=$(basename $(pwd)) +current_file_full_path=$0 +# echo $current_filepath +current_file_name=$(basename -- "$0") +# echo $current_filename +if [ $current_file_full_path = $current_file_name ] || [ $current_file_full_path = "./$current_file_name" ]; then + current_file_full_path="./${current_file_full_path}" + current_file_path="./" +else + current_file_path="${current_file_full_path/$current_file_name/''}" +fi + + +current_file_path_basename=$(basename -- "$current_file_path") + +if [ -z "$current_file_path_basename" ] || [ $current_file_path = "./" ]; then +# echo 'aq' + current_parent_folder="../" +else +# echo 'naq' + current_file_path_basename=$current_file_path_basename/ + current_parent_folder="${current_file_path/$current_file_path_basename/''}" +fi + +if [ debug ]; then + echo '----------------------------------------' + echo "$0 - Script variables" + echo '----------------------------------------' + echo "current_path: $current_path" + echo "current_path_basename: $current_path_basename" + echo "current_file_full_path: $current_file_full_path" + echo "current_file_name: $current_file_name" + echo "current_file_path: $current_file_path" + echo "current_parent_folder: $current_parent_folder" + echo '----------------------------------------' +fi + +if [ -z "$1" ]; then + echo 'Function name must be informed' + exit 1 +else + + HOST=0.0.0.0 + FUNCTION_PATH=$1 + FUNCTION_NAME=$1 + HANDLER=$2 + REGION=us-east-1 + if [ -z "$2" ]; then + HANDLER="app.index" + fi + + if [ ! -z "$3" ]; then + FUNCTION_PATH=$1 + FUNCTION_NAME=$2 + HANDLER=$3 + fi + + echo '----------------------------------------' + echo "$0 - Checking lambda function path" + echo '----------------------------------------' + if test "${current_path_basename}" = "${FUNCTION_PATH}"; then + echo 'current folder is the same of the function' + FUNCTION_PATH=$current_path + else + echo 'current folder is not the same of the function' + FUNCTION_PATH="${current_parent_folder/scripts\/localstack\//''}" + fi + + read -p "Press enter to continue..." + + echo '----------------------------------------' + echo "$0 - Checking previous installation" + echo '----------------------------------------' + # zip full code + if test -f ${FUNCTION_PATH}lambda-full.zip; then + echo 'Removing old zip file...' + rm ${FUNCTION_PATH}lambda-full.zip + else + echo 'There is no previous installation' + fi + + read -p "Press enter to continue..." + + echo '----------------------------------------' + echo "$0 - Script Function variables" + echo '----------------------------------------' + echo "Function name: $FUNCTION_NAME" + echo "Function path: $FUNCTION_PATH" + echo "Function handler: $HANDLER" + echo '----------------------------------------' + + echo '----------------------------------------' + echo "$0 - Zipping lambda data from ${FUNCTION_PATH}" + echo '----------------------------------------' + LAST_PWD=$(pwd) + cd ${FUNCTION_PATH} + zip -r ./lambda-full.zip ./ -x '*.git*' -x "./zip.sh*" -x "./venv/*" -x "./.idea/*" -x "./lambda-full.zip" + echo "zip file created in ${FUNCTION_PATH}lambda-full.zip" + cd ${LAST_PWD} + + read -p "Press enter to continue..." + + echo '----------------------------------------' + echo "$0 - Preparing bucket operations" + echo '----------------------------------------' + echo 'Try to list' + echo "aws --endpoint-url=http://$HOST:4566 s3api list-objects --bucket test > /dev/null 2>&1" + aws --endpoint-url=http://$HOST:4566 s3api list-objects --bucket test > /dev/null 2>&1 + + if [ $? -ne 0 ]; then + echo 'Create the bucket' + echo "aws --endpoint-url=http://$HOST:4566 s3 mb s3://test" + aws --endpoint-url=http://$HOST:4566 s3 mb s3://test + fi + + echo '----------------------------------------' + echo "$0 - Copy lambda zip file to S3" + echo '----------------------------------------' + echo "aws --endpoint-url=http://$HOST:4566 s3 cp ${FUNCTION_PATH}lambda-full.zip s3://test" + aws --endpoint-url=http://$HOST:4566 s3 cp ${FUNCTION_PATH}lambda-full.zip s3://test + + read -p "Press enter to continue..." + + echo '----------------------------------------' + echo "$0 - Check if the lambda function exits" + echo '----------------------------------------' + echo "aws --endpoint-url=http://$HOST:4566 lambda get-function --function-name $FUNCTION_NAME --region $REGION > /dev/null 2>&1" + aws --endpoint-url=http://$HOST:4566 lambda get-function --function-name $FUNCTION_NAME --region $REGION > /dev/null 2>&1 + + if [ $? -eq 0 ]; then + echo 'Delete the last lambda' + echo "aws --endpoint-url=http://$HOST:4566 lambda delete-function --function-name $FUNCTION_NAME --region $REGION" + aws --endpoint-url=http://$HOST:4566 lambda delete-function --function-name $FUNCTION_NAME --region $REGION + fi + + echo '----------------------------------------' + echo "$0 - Creating the environment variables" + echo '----------------------------------------' + + if test -d ${FUNCTION_PATH}.chalice; then + ENVIRONMENT_VARIABLES=$(jq '.stages.dev.environment_variables' ${FUNCTION_PATH}.chalice/config.json -c) + else + ENVIRONMENT_VARIABLES=$(python3 ${FUNCTION_PATH}scripts/tools/python/env-to-json.py ${FUNCTION_PATH}env/development.env) + fi + + echo "ENVIRONMENT_VARIABLES: ${ENVIRONMENT_VARIABLES}" + # echo "{\"Variables\": $ENVIRONMENT_VARIABLES }" + # echo {"Variables": $ENVIRONMENT_VARIABLES} > environment.json + + read -p "Press enter to continue..." + + echo '----------------------------------------' + echo "$0 - Creating the lambda function" + echo '----------------------------------------' + echo "aws --endpoint-url=http://$HOST:4566 lambda create-function \ + --function-name arn:aws:lambda:$REGION:000000000000:function:$FUNCTION_NAME \ + --runtime python3.6 --handler $HANDLER --memory-size 128 \ + --code S3Bucket=test,S3Key=lambda-full.zip --role arn:aws:iam:awslocal \ + --environment \"{\"Variables\": $ENVIRONMENT_VARIABLES}\"" + + aws --endpoint-url=http://$HOST:4566 lambda create-function \ + --function-name arn:aws:lambda:$REGION:000000000000:function:$FUNCTION_NAME \ + --runtime python3.6 --handler $HANDLER --memory-size 128 \ + --code S3Bucket=test,S3Key=lambda-full.zip --role arn:aws:iam:awslocal \ + --environment "{\"Variables\": $ENVIRONMENT_VARIABLES }" + #--environment Variables="{ENVIRONMENT_NAME=development}" + # --environment file://environment.json + +fi \ No newline at end of file diff --git a/examples/lambda_sqs/scripts/localstack/lambda/create-layer-from-vendor.sh b/examples/lambda_api/scripts/localstack/lambda/create-layer-from-vendor.sh similarity index 100% rename from examples/lambda_sqs/scripts/localstack/lambda/create-layer-from-vendor.sh rename to examples/lambda_api/scripts/localstack/lambda/create-layer-from-vendor.sh diff --git a/examples/lambda_sqs/scripts/localstack/lambda/create-layer.sh b/examples/lambda_api/scripts/localstack/lambda/create-layer.sh similarity index 100% rename from examples/lambda_sqs/scripts/localstack/lambda/create-layer.sh rename to examples/lambda_api/scripts/localstack/lambda/create-layer.sh diff --git a/examples/lambda_sqs/scripts/localstack/lambda/invoke-function.sh b/examples/lambda_api/scripts/localstack/lambda/invoke-function.sh similarity index 100% rename from examples/lambda_sqs/scripts/localstack/lambda/invoke-function.sh rename to examples/lambda_api/scripts/localstack/lambda/invoke-function.sh diff --git a/examples/lambda_sqs/scripts/localstack/lambda/invoke-sqs-function.sh b/examples/lambda_api/scripts/localstack/lambda/invoke-sqs-function.sh similarity index 100% rename from examples/lambda_sqs/scripts/localstack/lambda/invoke-sqs-function.sh rename to examples/lambda_api/scripts/localstack/lambda/invoke-sqs-function.sh diff --git a/examples/lambda_sqs/scripts/localstack/lambda/list-function.sh b/examples/lambda_api/scripts/localstack/lambda/list-function.sh similarity index 100% rename from examples/lambda_sqs/scripts/localstack/lambda/list-function.sh rename to examples/lambda_api/scripts/localstack/lambda/list-function.sh diff --git a/examples/lambda_sqs/scripts/tools/package.sh b/examples/lambda_api/scripts/tools/package.sh similarity index 100% rename from examples/lambda_sqs/scripts/tools/package.sh rename to examples/lambda_api/scripts/tools/package.sh diff --git a/examples/lambda_sqs/scripts/tools/python/env-to-json.py b/examples/lambda_api/scripts/tools/python/env-to-json.py similarity index 100% rename from examples/lambda_sqs/scripts/tools/python/env-to-json.py rename to examples/lambda_api/scripts/tools/python/env-to-json.py diff --git a/examples/lambda_sqs/scripts/tools/python/zip.py b/examples/lambda_api/scripts/tools/python/zip.py similarity index 100% rename from examples/lambda_sqs/scripts/tools/python/zip.py rename to examples/lambda_api/scripts/tools/python/zip.py diff --git a/examples/lambda_api/server.py b/examples/lambda_api/server.py deleted file mode 100644 index 836535b..0000000 --- a/examples/lambda_api/server.py +++ /dev/null @@ -1,5 +0,0 @@ -from gevent.pywsgi import WSGIServer -from app import app - -http_server = WSGIServer(('0.0.0.0', 5000), app) -http_server.serve_forever() diff --git a/examples/lambda_sqs/scripts/boot-db.sh b/examples/lambda_sqs/scriptsx/boot-db.sh similarity index 100% rename from examples/lambda_sqs/scripts/boot-db.sh rename to examples/lambda_sqs/scriptsx/boot-db.sh diff --git a/examples/lambda_sqs/scripts/boot-lambdas.sh b/examples/lambda_sqs/scriptsx/boot-lambdas.sh similarity index 100% rename from examples/lambda_sqs/scripts/boot-lambdas.sh rename to examples/lambda_sqs/scriptsx/boot-lambdas.sh diff --git a/examples/lambda_sqs/scripts/boot-queues.sh b/examples/lambda_sqs/scriptsx/boot-queues.sh similarity index 100% rename from examples/lambda_sqs/scripts/boot-queues.sh rename to examples/lambda_sqs/scriptsx/boot-queues.sh diff --git a/examples/lambda_sqs/scripts/boot-validate-connection.sh b/examples/lambda_sqs/scriptsx/boot-validate-connection.sh similarity index 100% rename from examples/lambda_sqs/scripts/boot-validate-connection.sh rename to examples/lambda_sqs/scriptsx/boot-validate-connection.sh diff --git a/examples/lambda_sqs/scripts/boot.sh b/examples/lambda_sqs/scriptsx/boot.sh similarity index 100% rename from examples/lambda_sqs/scripts/boot.sh rename to examples/lambda_sqs/scriptsx/boot.sh diff --git a/examples/lambda_sqs/scripts/docker/create-network.sh b/examples/lambda_sqs/scriptsx/docker/create-network.sh similarity index 100% rename from examples/lambda_sqs/scripts/docker/create-network.sh rename to examples/lambda_sqs/scriptsx/docker/create-network.sh diff --git a/examples/lambda_sqs/scripts/flask/run-local.sh b/examples/lambda_sqs/scriptsx/flask/run-local.sh similarity index 100% rename from examples/lambda_sqs/scripts/flask/run-local.sh rename to examples/lambda_sqs/scriptsx/flask/run-local.sh diff --git a/examples/lambda_sqs/scripts/install.sh b/examples/lambda_sqs/scriptsx/install.sh similarity index 100% rename from examples/lambda_sqs/scripts/install.sh rename to examples/lambda_sqs/scriptsx/install.sh diff --git a/examples/lambda_sqs/scriptsx/localstack/lambda/create-event-source-mapping.sh b/examples/lambda_sqs/scriptsx/localstack/lambda/create-event-source-mapping.sh new file mode 100755 index 0000000..307db64 --- /dev/null +++ b/examples/lambda_sqs/scriptsx/localstack/lambda/create-event-source-mapping.sh @@ -0,0 +1,21 @@ +if [ -z "$1" ]; then + echo 'Function name must be informed' + exit 1 +else + if [ -z "$2" ]; then + echo 'Queue name must be informed' + exit 1 + else + HOST=0.0.0.0 + REGION=us-east-1 + echo "aws --endpoint-url=http://$HOST:4566 lambda create-event-source-mapping \ + --function-name arn:aws:lambda:$REGION:000000000000:function:$1 \ + --event-source-arn arn:aws:sqs:$REGION:000000000000:$2" + + aws --endpoint-url=http://$HOST:4566 lambda create-event-source-mapping \ + --function-name arn:aws:lambda:$REGION:000000000000:function:$1 \ + --event-source-arn arn:aws:sqs:$REGION:000000000000:$2 + + #--event-source-arn arn:aws:sqs:elasticmq:000000000000:$2 + fi +fi diff --git a/examples/lambda_sqs/scripts/localstack/lambda/create-function-from-s3.sh b/examples/lambda_sqs/scriptsx/localstack/lambda/create-function-from-s3.sh similarity index 67% rename from examples/lambda_sqs/scripts/localstack/lambda/create-function-from-s3.sh rename to examples/lambda_sqs/scriptsx/localstack/lambda/create-function-from-s3.sh index 1f47e9a..de5a431 100755 --- a/examples/lambda_sqs/scripts/localstack/lambda/create-function-from-s3.sh +++ b/examples/lambda_sqs/scriptsx/localstack/lambda/create-function-from-s3.sh @@ -1,3 +1,46 @@ +# ----------------------------------------------------------------------------- +# Current file variables +# ----------------------------------------------------------------------------- +debug=false +parent_folder="../" +current_path=$(pwd)/ +current_path_basename=$(basename $(pwd)) +current_file_full_path=$0 +# echo $current_filepath +current_file_name=$(basename -- "$0") +# echo $current_filename +if [ $current_file_full_path = $current_file_name ] || [ $current_file_full_path = "./$current_file_name" ]; then + current_file_full_path="./${current_file_full_path}" + current_file_path="./" +else + current_file_path="${current_file_full_path/$current_file_name/''}" +fi + + +current_file_path_basename=$(basename -- "$current_file_path") + +if [ -z "$current_file_path_basename" ] || [ $current_file_path = "./" ]; then +# echo 'aq' + current_parent_folder="../" +else +# echo 'naq' + current_file_path_basename=$current_file_path_basename/ + current_parent_folder="${current_file_path/$current_file_path_basename/''}" +fi + +if [ debug ]; then + echo '----------------------------------------' + echo "$0 - Script variables" + echo '----------------------------------------' + echo "current_path: $current_path" + echo "current_path_basename: $current_path_basename" + echo "current_file_full_path: $current_file_full_path" + echo "current_file_name: $current_file_name" + echo "current_file_path: $current_file_path" + echo "current_parent_folder: $current_parent_folder" + echo '----------------------------------------' +fi + if [ -z "$1" ]; then echo 'Function name must be informed' exit 1 @@ -18,16 +61,32 @@ else HANDLER=$3 fi + echo '----------------------------------------' + echo "$0 - Script Function variables" + echo '----------------------------------------' echo "Function name: $FUNCTION_NAME" echo "Function path: $FUNCTION_PATH" echo "Function handler: $HANDLER" + echo '----------------------------------------' + + echo '----------------------------------------' + echo "$0 - Checking previous installation" + echo '----------------------------------------' # zip full code - if test -f lambda-full.zip; then - rm lambda-full.zip + if test -f ${FUNCTION_PATH}lambda-full.zip; then + echo 'Removing old zip file...' + rm ${FUNCTION_PATH}lambda-full.zip + else + echo 'There is no previous installation' fi + echo '---- exit' + exit + + + #check path if test -d ./$FUNCTION_PATH; then echo "Changing dir ./$(FUNCTION_PATH)" diff --git a/examples/lambda_sqs/scriptsx/localstack/lambda/create-layer-from-vendor.sh b/examples/lambda_sqs/scriptsx/localstack/lambda/create-layer-from-vendor.sh new file mode 100755 index 0000000..9d7b883 --- /dev/null +++ b/examples/lambda_sqs/scriptsx/localstack/lambda/create-layer-from-vendor.sh @@ -0,0 +1,51 @@ +# https://aws.amazon.com/premiumsupport/knowledge-center/lambda-layer-simulated-docker/ +if [ -z "$1" ]; then + echo 'Function path must be informed' + exit 1 +else + HOST=0.0.0.0 + FUNCTION_PATH=$1 + FUNCTION_NAME=$1 + LAYER_NAME=$2 + LAYER_DESCRIPTION=$3 + +# if [ ! -z "$3" ]; then +# FUNCTION_PATH=$1 +# FUNCTION_NAME=$2 +# HANDLER=$3 +# fi + + if [ -z "$LAYER_NAME" ]; then + LAYER_NAME="$FUNCTION_PATH-layer" + fi + if [ -z "$LAYER_DESCRIPTION" ]; then + LAYER_DESCRIPTION="$FUNCTION_PATH-layer" + fi + + + echo "Function name: $FUNCTION_NAME" + echo "Function path: $FUNCTION_PATH" + echo "Layer name: $LAYER_NAME" + echo "Layer description: $LAYER_DESCRIPTION" + + + # zip only code + cd ./$FUNCTION_PATH + python3 -m pip install -r requirements-vendor.txt -t ./layer + zip ../layer.zip -r ./layer + rm -Rf ./layer + cd ../ + + echo "aws --endpoint-url=http://$HOST:4566 lambda publish-layer-version --layer-name $LAYER_NAME \ + --description $LAYER_DESCRIPTION --zip-file fileb://layer.zip --compatible-runtimes \"python3.6\" \"python3.8\"" + + aws --endpoint-url=http://$HOST:4566 lambda publish-layer-version --layer-name $LAYER_NAME \ + --description $LAYER_DESCRIPTION --zip-file fileb://layer.zip --compatible-runtimes "python3.6" "python3.8" + + echo "aws --endpoint-url=http://$HOST:4566 lambda update-function-configuration \ + --layers arn:aws:lambda:us-east-1:000000000000:layer:$LAYER_NAME:1 --function-name $FUNCTION_NAME" + + aws --endpoint-url=http://$HOST:4566 lambda update-function-configuration \ + --layers arn:aws:lambda:us-east-1:000000000000:layer:$LAYER_NAME:1 --function-name $FUNCTION_NAME + +fi diff --git a/examples/lambda_sqs/scriptsx/localstack/lambda/create-layer.sh b/examples/lambda_sqs/scriptsx/localstack/lambda/create-layer.sh new file mode 100755 index 0000000..42a3488 --- /dev/null +++ b/examples/lambda_sqs/scriptsx/localstack/lambda/create-layer.sh @@ -0,0 +1,33 @@ +# https://aws.amazon.com/premiumsupport/knowledge-center/lambda-layer-simulated-docker/ +if [ -z "$1" ]; then + echo 'Function path must be informed' + exit 1 +else + HOST=0.0.0.0 + FUNCTION_PATH=$1 + FUNCTION_NAME=$1 + LAYER_NAME=$2 + LAYER_DESCRIPTION=$3 + + + if [ -z "$LAYER_NAME" ]; then + LAYER_NAME="$FUNCTION_PATH-layer" + fi + if [ -z "$LAYER_DESCRIPTION" ]; then + LAYER_DESCRIPTION="$FUNCTION_PATH-layer" + fi + + + if test -f "$FUNCTION_PATH/requirements-layers.txt"; then + input="$FUNCTION_PATH/requirements-layers.txt" + while IFS= read -r arn + do + echo "current arn: $arn" + echo "aws --endpoint-url=http://$HOST:4566 lambda update-function-configuration \ + --layers $arn --function-name $FUNCTION_NAME" + + aws --endpoint-url=http://$HOST:4566 lambda update-function-configuration \ + --layers $arn --function-name $FUNCTION_NAME + done < "$input" + fi +fi diff --git a/examples/lambda_sqs/scriptsx/localstack/lambda/invoke-function.sh b/examples/lambda_sqs/scriptsx/localstack/lambda/invoke-function.sh new file mode 100755 index 0000000..d64fcb2 --- /dev/null +++ b/examples/lambda_sqs/scriptsx/localstack/lambda/invoke-function.sh @@ -0,0 +1,30 @@ +if [ -z "$1" ]; then + echo 'Function name must be informed' + exit 1 +else + + HOST=0.0.0.0 + FUNCTION_PATH=$1 + FUNCTION_NAME=$1 + PAYLOAD=$2 + + if [ -z "$2" ]; then + PAYLOAD='{ "key": "value" }' + fi + + echo "Function name: $FUNCTION_NAME" + echo "Function path: $FUNCTION_PATH" + echo "Function ARN arn:aws:lambda:us-east-1:000000000000:$FUNCTION_NAME" + + echo "aws --endpoint-url=http://$HOST:4566 lambda invoke \ + --function-name arn:aws:lambda:us-east-1:000000000000:function:$FUNCTION_NAME \ + --payload $PAYLOAD ./output/response.json \ + --log-type Tail --query 'LogResult' --output text | base64 -d" + + aws --endpoint-url=http://$HOST:4566 lambda invoke \ + --function-name arn:aws:lambda:us-east-1:000000000000:function:$FUNCTION_NAME \ + --payload "$PAYLOAD" ./output/response.json \ + --log-type Tail --query 'LogResult' --output text | base64 -d + +fi + diff --git a/examples/lambda_sqs/scriptsx/localstack/lambda/invoke-sqs-function.sh b/examples/lambda_sqs/scriptsx/localstack/lambda/invoke-sqs-function.sh new file mode 100755 index 0000000..4d7e6e4 --- /dev/null +++ b/examples/lambda_sqs/scriptsx/localstack/lambda/invoke-sqs-function.sh @@ -0,0 +1,30 @@ +if [ -z "$1" ]; then + echo 'Function name must be informed' + exit 1 +else + + HOST=0.0.0.0 + FUNCTION_PATH=$1 + FUNCTION_NAME=$1 + if test -d ./$FUNCTION_PATH; then + PAYLOAD=./$FUNCTION_PATH/samples/localstack/SQSTest.json + else + PAYLOAD=./samples/localstack/SQSTest.json + fi + + + echo "Function name: $FUNCTION_NAME" + echo "Function path: $FUNCTION_PATH" + echo "Function ARN arn:aws:lambda:us-east-1:000000000000:$FUNCTION_NAME" + + echo "aws --endpoint-url=http://$HOST:4566 lambda invoke \ + --function-name arn:aws:lambda:us-east-1:000000000000:function:$FUNCTION_NAME \ + --payload file://$PAYLOAD ./output/response.json \ + --log-type Tail --query 'LogResult' --output text | base64 -d" + + aws --endpoint-url=http://$HOST:4566 lambda invoke \ + --function-name arn:aws:lambda:us-east-1:000000000000:function:$FUNCTION_NAME \ + --payload file://$PAYLOAD ./output/response.json \ + --log-type Tail --query 'LogResult' --output text | base64 -d +fi + diff --git a/examples/lambda_sqs/scriptsx/localstack/lambda/list-function.sh b/examples/lambda_sqs/scriptsx/localstack/lambda/list-function.sh new file mode 100755 index 0000000..c2c8118 --- /dev/null +++ b/examples/lambda_sqs/scriptsx/localstack/lambda/list-function.sh @@ -0,0 +1,3 @@ +HOST=0.0.0.0 +aws --endpoint-url=http://$HOST:4566 lambda list-functions --master-region us-east-1 +aws --endpoint-url=http://localhost:4566 lambda list-functions --master-region us-east-2 diff --git a/examples/lambda_sqs/scripts/localstack/sqs/create-queue.sh b/examples/lambda_sqs/scriptsx/localstack/sqs/create-queue.sh similarity index 100% rename from examples/lambda_sqs/scripts/localstack/sqs/create-queue.sh rename to examples/lambda_sqs/scriptsx/localstack/sqs/create-queue.sh diff --git a/examples/lambda_sqs/scripts/localstack/sqs/delete-queue.sh b/examples/lambda_sqs/scriptsx/localstack/sqs/delete-queue.sh similarity index 100% rename from examples/lambda_sqs/scripts/localstack/sqs/delete-queue.sh rename to examples/lambda_sqs/scriptsx/localstack/sqs/delete-queue.sh diff --git a/examples/lambda_sqs/scripts/localstack/sqs/list-queues.sh b/examples/lambda_sqs/scriptsx/localstack/sqs/list-queues.sh similarity index 100% rename from examples/lambda_sqs/scripts/localstack/sqs/list-queues.sh rename to examples/lambda_sqs/scriptsx/localstack/sqs/list-queues.sh diff --git a/examples/lambda_sqs/scripts/localstack/sqs/receive-message.sh b/examples/lambda_sqs/scriptsx/localstack/sqs/receive-message.sh similarity index 100% rename from examples/lambda_sqs/scripts/localstack/sqs/receive-message.sh rename to examples/lambda_sqs/scriptsx/localstack/sqs/receive-message.sh diff --git a/examples/lambda_sqs/scripts/localstack/sqs/send-message.sh b/examples/lambda_sqs/scriptsx/localstack/sqs/send-message.sh similarity index 100% rename from examples/lambda_sqs/scripts/localstack/sqs/send-message.sh rename to examples/lambda_sqs/scriptsx/localstack/sqs/send-message.sh diff --git a/examples/lambda_sqs/scripts/migrations/mysql/migrate.py b/examples/lambda_sqs/scriptsx/migrations/mysql/migrate.py similarity index 100% rename from examples/lambda_sqs/scripts/migrations/mysql/migrate.py rename to examples/lambda_sqs/scriptsx/migrations/mysql/migrate.py diff --git a/examples/lambda_sqs/scripts/runenv.sh b/examples/lambda_sqs/scriptsx/runenv.sh similarity index 100% rename from examples/lambda_sqs/scripts/runenv.sh rename to examples/lambda_sqs/scriptsx/runenv.sh diff --git a/examples/lambda_sqs/scripts/tests/component-coverage.sh b/examples/lambda_sqs/scriptsx/tests/component-coverage.sh similarity index 100% rename from examples/lambda_sqs/scripts/tests/component-coverage.sh rename to examples/lambda_sqs/scriptsx/tests/component-coverage.sh diff --git a/examples/lambda_sqs/scripts/tests/component-tests.sh b/examples/lambda_sqs/scriptsx/tests/component-tests.sh similarity index 100% rename from examples/lambda_sqs/scripts/tests/component-tests.sh rename to examples/lambda_sqs/scriptsx/tests/component-tests.sh diff --git a/examples/lambda_sqs/scripts/tests/install-tests.sh b/examples/lambda_sqs/scriptsx/tests/install-tests.sh similarity index 100% rename from examples/lambda_sqs/scripts/tests/install-tests.sh rename to examples/lambda_sqs/scriptsx/tests/install-tests.sh diff --git a/examples/lambda_sqs/scripts/tests/integration-coverage.sh b/examples/lambda_sqs/scriptsx/tests/integration-coverage.sh similarity index 100% rename from examples/lambda_sqs/scripts/tests/integration-coverage.sh rename to examples/lambda_sqs/scriptsx/tests/integration-coverage.sh diff --git a/examples/lambda_sqs/scripts/tests/integration-tests.sh b/examples/lambda_sqs/scriptsx/tests/integration-tests.sh similarity index 100% rename from examples/lambda_sqs/scripts/tests/integration-tests.sh rename to examples/lambda_sqs/scriptsx/tests/integration-tests.sh diff --git a/examples/lambda_sqs/scripts/tests/tests.sh b/examples/lambda_sqs/scriptsx/tests/tests.sh similarity index 100% rename from examples/lambda_sqs/scripts/tests/tests.sh rename to examples/lambda_sqs/scriptsx/tests/tests.sh diff --git a/examples/lambda_sqs/scripts/tests/unit-coverage-no-report-html.sh b/examples/lambda_sqs/scriptsx/tests/unit-coverage-no-report-html.sh similarity index 100% rename from examples/lambda_sqs/scripts/tests/unit-coverage-no-report-html.sh rename to examples/lambda_sqs/scriptsx/tests/unit-coverage-no-report-html.sh diff --git a/examples/lambda_sqs/scripts/tests/unit-coverage.sh b/examples/lambda_sqs/scriptsx/tests/unit-coverage.sh similarity index 100% rename from examples/lambda_sqs/scripts/tests/unit-coverage.sh rename to examples/lambda_sqs/scriptsx/tests/unit-coverage.sh diff --git a/examples/lambda_sqs/scripts/tests/unit-tests.sh b/examples/lambda_sqs/scriptsx/tests/unit-tests.sh similarity index 100% rename from examples/lambda_sqs/scripts/tests/unit-tests.sh rename to examples/lambda_sqs/scriptsx/tests/unit-tests.sh diff --git a/examples/lambda_sqs/scriptsx/tools/package.sh b/examples/lambda_sqs/scriptsx/tools/package.sh new file mode 100755 index 0000000..7241d7d --- /dev/null +++ b/examples/lambda_sqs/scriptsx/tools/package.sh @@ -0,0 +1,107 @@ +# Program variables +program="Package" +program_flag="${program}:" + +if [ "$1" = "--project_name" ] +then + project_name=$2 +fi +if [ -z "$project_name" ] +then + echo 'Error: the project name must be informed' + exit +fi + +# variables +current_dir=$(pwd) +tmp_folder="/tmp/${project_name:?}" +dist_folder="${current_dir:?}/dist" + +echo "${program_flag} Current project: ${project_name:?}" +echo "${program_flag} Current dir: ${current_dir:?}" +echo "${program_flag} Temporary folder: ${tmp_folder:?}" + +# cria um diretorio temporario +if test ! -d $tmp_folder; then + echo "${program_flag} Creating the tmp folder" + mkdir $tmp_folder +else + echo "${program_flag} The tmp folder already exists" +fi + +# cria o dist +if test ! -d $dist_folder; then + echo "${program_flag} Creating the dist folder" + mkdir $dist_folder +else + echo "${program_flag} The dist folder already exists" +fi + +# Remove old tmp files +echo "${program_flag} Removing old tmp files" +if test -d $tmp_folder; then + rm -Rf ${tmp_folder:?}/* +fi + +# Remove old dists +echo "${program_flag} Removing the old dists from ${dist_folder:?}/*" +if test -d $tmp_folder; then + rm -Rf ${dist_folder:?}/* +fi + +# Remove old dists of chalice +echo "${program_flag} Removing the old dists from ${current_dir:?}/.chalice/deployments/*" +if test -d ${current_dir:?}/.chalice/deployments/; then + rm -Rf ${current_dir:?}/.chalice/deployments/* +fi + +# copy the project content +echo "${program_flag} Copying the project content" + +#if ! [ -x "$(command -v rsync)" ]; then + cp -r ./ ${tmp_folder:?}/ + + ## remove unnecessary files + rm -Rf "${tmp_folder:?}/__pycache__" + rm -Rf "${tmp_folder:?}/venv" + rm -Rf "${tmp_folder:?}/.git" + rm -Rf "${tmp_folder:?}/.chalice" + rm -Rf "${tmp_folder:?}/.devops" + rm -Rf "${tmp_folder:?}/.idea" + rm -Rf "${tmp_folder:?}/-" + rm -Rf "${tmp_folder:?}/target" + rm -Rf "${tmp_folder:?}/dist" + rm -Rf "${tmp_folder:?}/bin" + + # deployer + rm -Rf "${tmp_folder:?}/lambda-deploy" +#else +# rsync -av --progress ./ $tmp_folder --exclude .git --exclude .idea --exclude bin --exclude venv --exclude .chalice \ +#--exclude dist --exclude target --exclude .devops +#fi + + +echo "${program_flag} Changing dir to tmp dir: ${tmp_folder}" +cd ${tmp_folder} || exit + +echo "${program_flag} Zipping files in ../${project_name:?}.zip" +if test -f ${current_dir:?}/bin/tools/python/zip.py; then + python3 ${current_dir:?}/bin/tools/python/zip.py --zip_file=../${project_name:?}.zip --source_dir=. +elif test -f ${current_dir:?}/../bin/tools/python/zip.py; then + python3 ${current_dir:?}/../bin/tools/python/zip.py --zip_file=../${project_name:?}.zip --source_dir=. +fi + +# move file to the parent directory +echo "${program_flag} Moving file (../${project_name:?}.zip) to the dist directory (${dist_folder:?}/${project_name:?}.zip)" +mv ../${project_name:?}.zip ${dist_folder:?}/${project_name:?}.zip + + +if test ! -f ${dist_folder:?}/${project_name:?}.zip; then + echo "${program_flag} Error, zip file not created" +else + echo "${program_flag} Zip file created with success" + echo "${program_flag} Listing contents of the dist directory:" + ls -al $dist_folder +fi + +echo "${program_flag} Exiting" \ No newline at end of file diff --git a/examples/lambda_sqs/scriptsx/tools/python/env-to-json.py b/examples/lambda_sqs/scriptsx/tools/python/env-to-json.py new file mode 100644 index 0000000..63fa9b8 --- /dev/null +++ b/examples/lambda_sqs/scriptsx/tools/python/env-to-json.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +import json +import sys + +try: + dotenv = sys.argv[1] +except IndexError as e: + dotenv = '.env' + +with open(dotenv, 'r') as f: + content = f.readlines() + +# removes whitespace chars like '\n' at the end of each line +content = [x.strip().split('=') for x in content if '=' in x] +print(json.dumps(dict(content))) \ No newline at end of file diff --git a/examples/lambda_sqs/scriptsx/tools/python/zip.py b/examples/lambda_sqs/scriptsx/tools/python/zip.py new file mode 100644 index 0000000..1e1b148 --- /dev/null +++ b/examples/lambda_sqs/scriptsx/tools/python/zip.py @@ -0,0 +1,39 @@ +import argparse +import os +import logging +from zipfile import ZipFile + +logger = logging.getLogger("zip") + + +def add_to_zip(): + pass + + +def create_zip(zip_file, source_dir, verbose): + try: + with ZipFile(zip_file, 'w') as zip_obj: + # Iterate over all the files in directory + for folder_name, sub_folders, file_names in os.walk(source_dir): + for file_name in file_names: + # create complete filepath of file in directory + file_path = os.path.join(folder_name, file_name) + # Add file to zip + zip_obj.write(file_path) + if verbose: + logger.info("Added: {}".format(file_path)) + except Exception as err: + logger.info("Added: {}".format(file_path)) + finally: + if zip_obj: + zip_obj.close() + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('-v', action='count') + parser.add_argument("--zip_file", help="Name of the file to be created") + parser.add_argument("--source_dir", help="Source directory") + + args = parser.parse_args() + create_zip(args.zip_file, args.source_dir, args.v) diff --git a/examples/lambda_sqs/scripts/venv-exec.sh b/examples/lambda_sqs/scriptsx/venv-exec.sh similarity index 100% rename from examples/lambda_sqs/scripts/venv-exec.sh rename to examples/lambda_sqs/scriptsx/venv-exec.sh diff --git a/examples/lambda_sqs/scripts/venv.sh b/examples/lambda_sqs/scriptsx/venv.sh similarity index 100% rename from examples/lambda_sqs/scripts/venv.sh rename to examples/lambda_sqs/scriptsx/venv.sh diff --git a/examples/lambda_sqs/scripts/zip.sh b/examples/lambda_sqs/scriptsx/zip.sh similarity index 100% rename from examples/lambda_sqs/scripts/zip.sh rename to examples/lambda_sqs/scriptsx/zip.sh diff --git a/scripts/boot-lambdas.sh b/scripts/boot-lambdas.sh index 4260d00..73d53d4 100755 --- a/scripts/boot-lambdas.sh +++ b/scripts/boot-lambdas.sh @@ -1,3 +1,51 @@ -# a api não precisa ser bootada -#./examples/lambda_api/scripts/boot.sh -./examples/lambda_sqs/scripts/boot.sh +# ----------------------------------------------------------------------------- +# Current file variables +# ----------------------------------------------------------------------------- +debug=false +parent_folder="../" +current_path=$(pwd) +current_path_basename=$(basename $(pwd)) +current_file_full_path=$0 +# echo $current_filepath +current_file_name=$(basename -- "$0") +#echo $current_file_full_path +#echo $current_file_name +if [ $current_file_full_path = $current_file_name ] || [ $current_file_full_path = "./$current_file_name" ]; then + current_file_full_path="./${current_file_full_path}" + current_file_path="./" +else + current_file_path="${current_file_full_path/$current_file_name/''}" +fi +if [ debug ]; then + echo '----------------------------------------' + echo "$0 - Script variables" + echo '----------------------------------------' + echo "current_path: $current_path" + echo "current_path_basename: $current_path_basename" + echo "current_file_full_path: $current_file_full_path" + echo "current_file_name: $current_file_name" + echo "current_file_path: $current_file_path" + echo '----------------------------------------' +fi + + +## variables +echo '----------------------------------------' +echo "$0 - Sourcing file: ${current_file_path}variables.sh" +echo '----------------------------------------' +source ${current_file_path}variables.sh + +## now loop through the above array +for example_path in "${arr[@]}" +do + echo "Booting ${example_path} ..." + if test -f "${parent_folder}${example_path}scripts/boot.sh"; then + ${parent_folder}${example_path}scripts/boot.sh + elif test -f "${example_path}scripts/boot.sh"; then + ${example_path}scripts/boot.sh + else + echo 'There is no boot file' + fi +done + +# You can access them using echo "${arr[0]}", "${arr[1]}" also \ No newline at end of file diff --git a/scripts/boot-validate-connection.sh b/scripts/boot-validate-connection.sh index b5dbe43..ca1d138 100755 --- a/scripts/boot-validate-connection.sh +++ b/scripts/boot-validate-connection.sh @@ -8,7 +8,7 @@ do_request () { elif [ $response -ne "500" ]; then # success return 0 - el + else # error return 1 fi diff --git a/scripts/boot.sh b/scripts/boot.sh index ce8aada..33dbbe2 100755 --- a/scripts/boot.sh +++ b/scripts/boot.sh @@ -1,16 +1,91 @@ #!/bin/bash -echo 'Validating jd installation..' - /usr/bin/jq --help > /dev/null 2>&1 - if [ $? -ne 0 ]; then - echo 'Installing' - # download directly into ~/bin_compciv - sudo curl http://stedolan.github.io/jq/download/linux64/jq -o /usr/bin/jq - # give it executable permissions - sudo chmod a+x /usr/bin/jq - fi - -echo 'Validate connection' -./scripts/boot-validate-connection.sh - -echo 'Create the lambdas...' -./scripts/boot-lambdas.sh \ No newline at end of file +# ----------------------------------------------------------------------------- +# Current file variables +# ----------------------------------------------------------------------------- +debug=false +parent_folder="../" +current_path=$(pwd)/ +current_path_basename=$(basename $(pwd)) +current_file_full_path=$0 +# echo $current_filepath +current_file_name=$(basename -- "$0") +# echo $current_filename +if [ $current_file_full_path = $current_file_name ] || [ $current_file_full_path = "./$current_file_name" ]; then + current_file_full_path="./${current_file_full_path}" + current_file_path="./" +else + current_file_path="${current_file_full_path/$current_file_name/''}" +fi +if [ debug ]; then + echo '----------------------------------------' + echo "$0 - Script variables" + echo '----------------------------------------' + echo "current_path: $current_path" + echo "current_path_basename: $current_path_basename" + echo "current_file_full_path: $current_file_full_path" + echo "current_file_name: $current_file_name" + echo "current_file_path: $current_file_path" + echo '----------------------------------------' +fi + +echo '----------------------------------------' +echo "$0 - jq check" +echo '----------------------------------------' +echo 'Validating jq installation...' +/usr/bin/jq --version > /dev/null 2>&1 +if [ $? -ne 0 ]; then + echo 'Installing jq...' + # download directly into ~/bin_compciv + sudo curl http://stedolan.github.io/jq/download/linux64/jq -o /usr/bin/jq + # give it executable permissions + sudo chmod a+x /usr/bin/jq +else + echo 'jq installed...' +fi + +read -p "Press enter to continue..." + + +echo '----------------------------------------' +echo "$0 - Localstack connection check" +echo '----------------------------------------' +# valida se o Localstack está rodando +if test -f ${current_file_path}boot-validate-connection.sh; then + echo 'Validate connection...' + ${current_file_path}boot-validate-connection.sh +else + echo 'There is no connection check file' +fi + +read -p "Press enter to continue..." + +echo '----------------------------------------' +echo "$0 - Database boot" +echo '----------------------------------------' +if test -f ${current_file_path}boot-db.sh; then + ${current_file_path}boot-db.sh +else + echo 'There is no database to be booted' +fi + +read -p "Press enter to continue..." + +echo '----------------------------------------' +echo "$0 - Queues boot" +echo '----------------------------------------' +if test -f ${current_file_path}boot-queues.sh; then + ${current_file_path}boot-queues.sh +else + echo 'There is no queues to be booted' +fi + +read -p "Press enter to continue..." + +echo '----------------------------------------' +echo "$0 - Lambdas boot" +echo '----------------------------------------' +if test -f ${current_file_path}boot-lambdas.sh; then + ${current_file_path}boot-lambdas.sh +else + echo 'There is no lambdas to be booted' +fi diff --git a/scripts/install.sh b/scripts/install.sh index b5f9d13..66fd346 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -1,7 +1,7 @@ #!/bin/bash -# declare an array variable -declare -a arr=("./examples/lambda_api/" "./examples/lambda_cron/" "./examples/lambda_s3/" "./examples/lambda_sns/" "./examples/lambda_sqs/") +## variables +source ${current_filename_path}scripts/variables.sh ## now loop through the above array for example_path in "${arr[@]}" diff --git a/scripts/variables.sh b/scripts/variables.sh new file mode 100755 index 0000000..d037358 --- /dev/null +++ b/scripts/variables.sh @@ -0,0 +1,3 @@ +#!/bin/bash +# declare an array variable +declare -a arr=("./examples/lambda_api/" "./examples/lambda_cron/" "./examples/lambda_s3/" "./examples/lambda_sns/" "./examples/lambda_sqs/") \ No newline at end of file diff --git a/scripts/venv.sh b/scripts/venv.sh index c1689fe..81d61e9 100755 --- a/scripts/venv.sh +++ b/scripts/venv.sh @@ -1,7 +1,28 @@ #!/bin/bash -python3 -m venv venv -source ./venv/bin/activate +current_path=$(basename $(pwd)) +current_filename=$(basename -- "$0") +current_filename_path=$0 +# echo $current_filename_path +current_filename_path="${current_filename_path/$current_filename/''}" +# echo $current_filename_path +current_filename_path="${current_filename_path/scripts\//''}" +# echo $current_filename_path +current_filename_path_basename=$(basename -- "$current_filename_path") -if test -f "./scripts/install.sh"; then - bash ./scripts/install.sh +echo "current_path: $current_path" +echo "current_filename: $current_filename" +echo "current_filename_path: $current_filename_path" +echo "current_filename_path_basename: $current_filename_path_basename" + + +if test -f "${current_filename_path}venv/bin/activate"; then + python3 -m venv venv + source ${current_filename_path}venv/bin/activate +else + echo "Unable to find ${current_filename_path}venv/bin/activate" + exit 1 +fi + +if test -f "${current_filename_path}scripts/install.sh"; then + bash ${current_filename_path}scripts/install.sh fi