From 7a2c21f89802380b239c31e012848b955007e905 Mon Sep 17 00:00:00 2001 From: Giacomo Marciani Date: Wed, 22 Nov 2023 15:13:45 +0100 Subject: [PATCH] [Tools] Add script 'deploy.sh' to have a 1-click deployment of PCUI and script 'setup-env.sh' to create config files for personal environments. --- scripts/README.md | 18 ++++++- scripts/common.sh | 13 +++++ scripts/deploy.sh | 125 +++++++++++++++++++++++++++++++++++++++++++ scripts/setup-env.sh | 45 ++++++++++++++++ 4 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 scripts/common.sh create mode 100755 scripts/deploy.sh create mode 100755 scripts/setup-env.sh diff --git a/scripts/README.md b/scripts/README.md index bf9d6d68..91005032 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -21,8 +21,24 @@ bash ./rollforward_awslambda_image.sh **NOTE:** The rollforward script should be used only if the rollback script is launched by mistake, the normal procedure to publish a new release after a rollback is by launching the `build_and_release_image.sh --tag YYYY.MM.REVISION` after fixing the code -# How to deploy PCUI image in personal account +# How to deploy PCUI in personal account +Create configurations files for your personal environment (una tantum): +``` +bash scripts/setup-env.sh [ENVIRONMENT_NAME] +``` + +where *ENVIRONMENT_NAME* could be your login. + +Adapt the configurations files `[ENVIRONMENT_NAME]-*` created in `infrastructure/environments` with your data. + +Deploy the local PCUI to your environment: + +``` +bash scripts/deploy.sh [ENVIRONMENT_NAME] +``` + +# How to deploy the PCUI image to a private ECR repository Create an ECR *private* repository in your personal account, where PCUI Docker images will be stored for testing: ``` diff --git a/scripts/common.sh b/scripts/common.sh new file mode 100644 index 00000000..edb4da8a --- /dev/null +++ b/scripts/common.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +function info() { + echo "[INFO] $1" +} + +function warn() { + echo "[WARN] $1" +} + +function fail() { + echo "[ERROR] $1" && exit 1 +} \ No newline at end of file diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100755 index 00000000..c3178d79 --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,125 @@ +#!/bin/bash +set -e +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance +# with the License. A copy of the License is located at +# +# http://aws.amazon.com/apache2.0/ +# +# or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES +# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and +# limitations under the License. + +# This script is used to update the infrastructure of a given PCM environment. +# An environment is composed of a list of variables with the entrypoints of the environment +# and a CloudFormation request file where the stack update can be customized, +# for example by changing the parameters provided to the previous version of the stack +# +# Usage: ./scripts/deploy.sh [ENVIRONMENT] +# Example: ./scripts/deploy.sh demo + +CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)" + +INFRASTRUCTURE_DIR="$CURRENT_DIR/../infrastructure" + +source "$CURRENT_DIR/common.sh" + +ENVIRONMENT=$1 + +[[ -z $ENVIRONMENT ]] && fail "Missing required argument: ENVIRONMENT" + +info "Selected environment: $ENVIRONMENT" + +source "$INFRASTRUCTURE_DIR/environments/$ENVIRONMENT-variables.sh" + +# STEP: Upload templates +info "Retrieving infrastructure bucket" +if [[ -n $INFRA_BUCKET_NAME ]]; then + BUCKET=$INFRA_BUCKET_NAME +else + BUCKET=$(aws cloudformation describe-stack-resources \ + --region "$REGION" \ + --stack-name "$INFRA_BUCKET_STACK_NAME" \ + --logical-resource-id InfrastructureBucket \ + --output text \ + --query 'StackResources[0].PhysicalResourceId') +fi +info "Using infrastructure bucket $BUCKET" + +info "Uploading templates to infrastructure bucket $BUCKET" +FILES=(parallelcluster-ui-cognito.yaml parallelcluster-ui.yaml) +for FILE in "${FILES[@]}"; do + aws s3 cp --region "$REGION" "$INFRASTRUCTURE_DIR/$FILE" "s3://$BUCKET/$FILE" +done +# ----------------------------- + +# STEP: Setup private ECR repository +ECR_REPOSITORY_NAME="parallelcluster-ui" +info "Settings up private ECR repository $ECR_REPOSITORY_NAME" +if [[ ! $(aws ecr describe-repositories --repository-name $ECR_REPOSITORY_NAME --region "$REGION" 2>/dev/null) ]]; then + info "The private ECR repository $ECR_REPOSITORY_NAME does not exist, creating ..." + aws ecr create-repository \ + --region "$REGION" \ + --repository-name $ECR_REPOSITORY_NAME \ + --image-tag-mutability MUTABLE + +else + info "The private ECR repository $ECR_REPOSITORY_NAME already exists" +fi +REPOSITORY_INFO=$(aws ecr describe-repositories \ +--region "$REGION" \ +--repository-name $ECR_REPOSITORY_NAME \ +--query "repositories[0].{arn:repositoryArn,uri:repositoryUri}" \ +--output json) +REPOSITORY_ARN=$(echo "$REPOSITORY_INFO" | jq -cr '.arn') +REPOSITORY_ENDPOINT=$(echo "$REPOSITORY_INFO" | jq -cr '.uri' | cut -d '/' -f 1) +info "Using private ECR repository $REPOSITORY_ARN at ECR endpoint $REPOSITORY_ENDPOINT" +# ----------------------------- + +# STEP: Build image locally and publish to private ECR repository +info "Building image locally and publishing it to private ECR repository" +ECR_IMAGE_TAG=$(date +%Y.%m.%d) +bash "$CURRENT_DIR/build_and_release_image.sh" --ecr-region "$REGION" --ecr-endpoint "$REPOSITORY_ENDPOINT" --tag "$ECR_IMAGE_TAG" +info "Image built with tag '$ECR_IMAGE_TAG' and 'latest'" +# ----------------------------- + +# STEP: Deploy stack +info "Deploying stack $STACK_NAME" +BUCKET_URL="https://$BUCKET.s3.$REGION.amazonaws.com" + +if [[ ! $(aws cloudformation describe-stacks --stack-name "$STACK_NAME" --region "$REGION" 2>/dev/null) ]]; then + info "The stack $STACK_NAME does not exist, creating ..." + CFN_DEPLOY_COMMAND="create-stack" + CFN_WAIT_COMMAND="stack-create-complete" + CFN_CLI_INPUT_YAML_FILE="$INFRASTRUCTURE_DIR/environments/$ENVIRONMENT-cfn-create-args.yaml" + +else + info "The stack $STACK_NAME exists, updating ..." + CFN_DEPLOY_COMMAND="update-stack" + CFN_WAIT_COMMAND="stack-update-complete" + CFN_CLI_INPUT_YAML_FILE="$INFRASTRUCTURE_DIR/environments/$ENVIRONMENT-cfn-update-args.yaml" +fi + +CLI_INPUT_YAML=$(sed "s#BUCKET_URL_PLACEHOLDER#$BUCKET_URL#g" "$CFN_CLI_INPUT_YAML_FILE") + +AWS_PAGER="cat" aws cloudformation $CFN_DEPLOY_COMMAND \ + --cli-input-yaml "$CLI_INPUT_YAML" \ + --stack-name "$STACK_NAME" \ + --region "$REGION" + +aws cloudformation wait $CFN_WAIT_COMMAND \ + --stack-name "$STACK_NAME" \ + --region "$REGION" + +info "Deployment completed!" +# ----------------------------- + +# STEP: Retrieve stack details +PCUI_ENDPOINT=$(aws cloudformation describe-stacks \ + --stack-name "$STACK_NAME" \ + --region "$REGION" \ + --query "Stacks[0].Outputs[?OutputKey == 'ParallelClusterUIUrl'].OutputValue" \ + --output text) + +info "PCUI endpoint is $PCUI_ENDPOINT" \ No newline at end of file diff --git a/scripts/setup-env.sh b/scripts/setup-env.sh new file mode 100755 index 00000000..eccec01c --- /dev/null +++ b/scripts/setup-env.sh @@ -0,0 +1,45 @@ +#!/bin/bash +set -e +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance +# with the License. A copy of the License is located at +# +# http://aws.amazon.com/apache2.0/ +# +# or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES +# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and +# limitations under the License. + +# This script is used to update the infrastructure of a given PCM environment. +# An environment is composed of a list of variables with the entrypoints of the environment +# and a CloudFormation request file where the stack update can be customized, +# for example by changing the parameters provided to the previous version of the stack +# +# Usage: ./scripts/setup-env.sh [ENVIRONMENT] +# Example: ./scripts/setup-env.sh demo + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" + +ENVIRONMENTS_DIR=$(realpath "$CURRENT_DIR/../infrastructure/environments") + +source "$CURRENT_DIR/common.sh" + +ENVIRONMENT=$1 + +[[ -z $ENVIRONMENT ]] && fail "Missing required argument: ENVIRONMENT" + +info "Selected environment: $ENVIRONMENT" + +if [[ $(ls $ENVIRONMENTS_DIR/$ENVIRONMENT-*) ]]; then + warn "Configuration files for the environment $ENVIRONMENT already exist. Nothing to do." + exit 0 +fi + +info "Creating environment files" +cp "$ENVIRONMENTS_DIR/demo-variables.sh" "$ENVIRONMENTS_DIR/$ENVIRONMENT-variables.sh" +chmod +x "$ENVIRONMENTS_DIR/$ENVIRONMENT-variables.sh" +cp "$ENVIRONMENTS_DIR/demo-cfn-update-args.yaml" "$ENVIRONMENTS_DIR/$ENVIRONMENT-cfn-create-args.sh" +cp "$ENVIRONMENTS_DIR/demo-cfn-update-args.yaml" "$ENVIRONMENTS_DIR/$ENVIRONMENT-cfn-update-args.sh" + +info "Environment files created! Check out $ENVIRONMENTS_DIR" \ No newline at end of file