destroy_cdk_applications.cmd 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. @ECHO OFF
  2. REM
  3. REM Copyright (c) Contributors to the Open 3D Engine Project.
  4. REM For complete copyright and license terms please see the LICENSE at the root of this distribution.
  5. REM
  6. REM SPDX-License-Identifier: Apache-2.0 OR MIT
  7. REM
  8. REM
  9. REM Destroy the CDK applcations for AWS gems (Windows only)
  10. REM Prerequisites:
  11. REM 1) Node.js is installed
  12. REM 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.
  13. SETLOCAL EnableDelayedExpansion
  14. SET SOURCE_DIRECTORY=%CD%
  15. SET PATH=%SOURCE_DIRECTORY%\python;%PATH%
  16. SET GEM_DIRECTORY=%SOURCE_DIRECTORY%\Gems
  17. ECHO [cdk_installation] Install aws-cdk@%CDK_VERSION%
  18. CALL npm uninstall -g aws-cdk
  19. IF ERRORLEVEL 1 (
  20. ECHO [cdk_bootstrap] Failed to uninstall the current version of CDK
  21. exit /b 1
  22. )
  23. CALL npm install -g aws-cdk@%CDK_VERSION%
  24. IF ERRORLEVEL 1 (
  25. ECHO [cdk_bootstrap] Failed to install aws-cdk@%CDK_VERSION%
  26. exit /b 1
  27. )
  28. REM Set temporary AWS credentials from the assume role
  29. FOR /f "tokens=1,2,3" %%a IN ('CALL aws sts assume-role --query Credentials.[SecretAccessKey^,SessionToken^,AccessKeyId] --output text --role-arn %ASSUME_ROLE_ARN% --role-session-name o3de-Automation-session') DO (
  30. SET AWS_SECRET_ACCESS_KEY=%%a
  31. SET AWS_SESSION_TOKEN=%%b
  32. SET AWS_ACCESS_KEY_ID=%%c
  33. )
  34. FOR /F "tokens=4 delims=:" %%a IN ("%ASSUME_ROLE_ARN%") DO SET O3DE_AWS_DEPLOY_ACCOUNT=%%a
  35. IF "%O3DE_AWS_PROJECT_NAME%"=="" (
  36. REM To avoid resource name length issues, potentially verbose pipeline names are capped at 25 chars.
  37. REM TODO: consolidate project name formulation for tests and deploy/destroy scripts to same place.
  38. ECHO Truncated pipeline name is: %PIPELINE_NAME:~0,25%
  39. SET O3DE_AWS_PROJECT_NAME=%BRANCH_NAME%-%PIPELINE_NAME:~0,25%-Windows
  40. SET slashreplace=
  41. call SET O3DE_AWS_PROJECT_NAME=%%O3DE_AWS_PROJECT_NAME:/=%slashreplace%%%
  42. )
  43. SET ERROR_EXISTS=0
  44. CALL :DestroyCDKApplication AWSCore,ERROR_EXISTS
  45. CALL :DestroyCDKApplication AWSClientAuth,ERROR_EXISTS
  46. CALL :DestroyCDKApplication AWSMetrics,ERROR_EXISTS
  47. IF %ERROR_EXISTS% EQU 1 (
  48. EXIT /b 1
  49. )
  50. EXIT /b 0
  51. :DestroyCDKApplication
  52. REM Destroy the CDK application for a specific AWS gem
  53. SET GEM_NAME=%~1
  54. ECHO [cdk_destruction] Destroy the CDK application for the %GEM_NAME% gem
  55. PUSHD %GEM_DIRECTORY%\%GEM_NAME%\cdk
  56. REM Revert the CDK application code to a stable state using the provided commit ID
  57. CALL git checkout %COMMIT_ID% -- .
  58. IF ERRORLEVEL 1 (
  59. ECHO [git_checkout] Failed to checkout the CDK application for the %GEM_NAME% gem using commit ID %COMMIT_ID%
  60. POPD
  61. SET %~2=1
  62. )
  63. REM Install required packages for the CDK application
  64. CALL python -m pip install -r requirements.txt
  65. IF ERRORLEVEL 1 (
  66. ECHO [cdk_destruction] Failed to install required packages for the %GEM_NAME% gem
  67. POPD
  68. SET %~2=1
  69. )
  70. REM Destroy the CDK application
  71. CALL cdk destroy --all -f
  72. IF ERRORLEVEL 1 (
  73. ECHO [cdk_destruction] Failed to destroy the CDK application for the %GEM_NAME% gem
  74. POPD
  75. SET %~2=1
  76. )
  77. POPD