destroy_cdk_applications.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/bash
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. # Deploy the CDK applications for AWS gems (Linux only)
  8. # Prerequisites:
  9. # 1) Node.js is installed
  10. # 2) Node.js version >= 10.13.0, except for versions 13.0.0 - 13.6.0. A version in active long-term support is recommended.
  11. SOURCE_DIRECTORY=$PWD
  12. PATH=$SOURCE_DIRECTORY/python/runtime/$PYTHON_RUNTIME/python/bin:$PATH
  13. GEM_DIRECTORY=$SOURCE_DIRECTORY/Gems
  14. DestroyCDKApplication()
  15. {
  16. # Destroy the CDK application for a specific AWS gem
  17. GEM_NAME=$1
  18. echo [cdk_destruction] Destroy the CDK application for the $GEM_NAME gem
  19. pushd $GEM_DIRECTORY/$GEM_NAME/cdk
  20. # Revert the CDK application code to a stable state using the provided commit ID
  21. if ! git checkout $COMMIT_ID -- .;
  22. then
  23. echo [git_checkout] Failed to checkout the CDK application for the $GEM_NAME gem using commit ID $COMMIT_ID
  24. popd
  25. return 1
  26. fi
  27. # Install required packages for the CDK application
  28. if ! python -m pip install -r requirements.txt;
  29. then
  30. echo [cdk_destruction] Failed to install required packages for the $GEM_NAME gem
  31. popd
  32. return 1
  33. fi
  34. # Destroy the CDK application
  35. if ! cdk destroy --all -f;
  36. then
  37. echo [cdk_destruction] Failed to destroy the CDK application for the $GEM_NAME gem
  38. popd
  39. return 1
  40. fi
  41. popd
  42. return 0
  43. }
  44. echo [cdk_installation] Install nvm $NVM_VERSION
  45. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/$NVM_VERSION/install.sh | bash
  46. export NVM_DIR="$HOME/.nvm"
  47. [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
  48. [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
  49. echo [cdk_installation] Install the current version of nodejs
  50. nvm install node
  51. echo [cdk_installation] Install aws-cdk@$CDK_VERSION
  52. if ! npm uninstall -g aws-cdk;
  53. then
  54. echo [cdk_bootstrap] Failed to uninstall the current version of CDK
  55. exit 1
  56. fi
  57. if ! npm install -g aws-cdk@$CDK_VERSION;
  58. then
  59. echo [cdk_bootstrap] Failed to install aws-cdk@$CDK_VERSION
  60. exit 1
  61. fi
  62. # Set temporary AWS credentials from the assume role
  63. credentials=$(aws sts assume-role --query Credentials.[SecretAccessKey,SessionToken,AccessKeyId] --output text --role-arn $ASSUME_ROLE_ARN --role-session-name o3de-Automation-session)
  64. export AWS_SECRET_ACCESS_KEY=$(echo $credentials | cut -d' ' -f1)
  65. export AWS_SESSION_TOKEN=$(echo $credentials | cut -d' ' -f2)
  66. export AWS_ACCESS_KEY_ID=$(echo $credentials | cut -d' ' -f3)
  67. if [[ -z "$O3DE_AWS_PROJECT_NAME" ]]; then
  68. # To avoid resource name length issues, potentially verbose pipeline names are capped at 25 chars.
  69. # TODO: consolidate project name formulation for tests and deploy/destroy scripts to same place.
  70. pipeline_short=${PIPELINE_NAME:0:25}
  71. echo Truncated pipeline name is: $pipeline_short
  72. export O3DE_AWS_PROJECT_NAME=$BRANCH_NAME-$pipeline_short-Linux
  73. export O3DE_AWS_PROJECT_NAME=${O3DE_AWS_PROJECT_NAME///} # remove occurances of "/" b/c not allowed in AWS CFN stack names
  74. fi
  75. ERROR_EXISTS=0
  76. DestroyCDKApplication AWSCore
  77. ERROR_EXISTS=$?
  78. DestroyCDKApplication AWSClientAuth
  79. ERROR_EXISTS=$?
  80. DestroyCDKApplication AWSMetrics
  81. ERROR_EXISTS=$?
  82. if [ $ERROR_EXISTS -eq 1 ]
  83. then
  84. exit 1
  85. fi
  86. exit 0