-
Notifications
You must be signed in to change notification settings - Fork 0
fix: pause resume and delete old infra #156
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Autodetect text files and forces unix eols, so Windows does not break them | ||
* text=auto eol=lf | ||
|
||
# Force images/fonts to be handled as binaries | ||
*.jpg binary | ||
*.jpeg binary | ||
*.gif binary | ||
*.png binary |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
#!/bin/bash | ||
# This script resumes AWS resources (ECS service and RDS Aurora cluster) in the specified AWS account. | ||
# Made idempotent - safe to run multiple times, checks resource existence before acting. | ||
|
||
set -e # Exit on error | ||
set -euo pipefail # Exit on error, undefined variables, and pipe failures | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] The script uses Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
|
||
# Error handling function | ||
error_handler() { | ||
|
@@ -11,7 +12,7 @@ error_handler() { | |
exit 1 | ||
} | ||
|
||
# Set trap for error handling | ||
# Set trap for error handling (but not for resource not found errors) | ||
trap 'error_handler ${LINENO} ${FUNCNAME[0]}' ERR | ||
|
||
# Function to check if required parameters are provided | ||
|
@@ -31,7 +32,11 @@ check_db_cluster() { | |
local prefix=$1 | ||
local env=$2 | ||
local cluster_id="${prefix}-aurora-${env}" | ||
local status=$(aws rds describe-db-clusters --db-cluster-identifier ${cluster_id} --query 'DBClusters[0].Status' --output text 2>/dev/null || echo "not-found") | ||
|
||
# Use || true to handle expected failures gracefully | ||
local status | ||
status=$(aws rds describe-db-clusters --db-cluster-identifier "${cluster_id}" --query 'DBClusters[0].Status' --output text 2>/dev/null || echo "not-found") | ||
|
||
echo "$status" | ||
} | ||
|
||
|
@@ -42,56 +47,109 @@ start_db_cluster() { | |
local cluster_id="${prefix}-aurora-${env}" | ||
|
||
echo "Starting DB cluster ${cluster_id}..." | ||
aws rds start-db-cluster --db-cluster-identifier ${cluster_id} --no-cli-pager --output json | ||
|
||
# Start the cluster and capture result | ||
if ! aws rds start-db-cluster --db-cluster-identifier "${cluster_id}" --no-cli-pager --output json; then | ||
echo "Failed to start DB cluster ${cluster_id}" | ||
return 1 | ||
fi | ||
|
||
echo "Waiting for DB cluster to be available..." | ||
if ! aws rds wait db-cluster-available --db-cluster-identifier ${cluster_id}; then | ||
echo "Timeout waiting for DB cluster to become available" | ||
if ! aws rds wait db-cluster-available --db-cluster-identifier "${cluster_id}"; then | ||
echo "Timeout or error waiting for DB cluster to become available" | ||
return 1 | ||
fi | ||
|
||
echo "DB cluster is now available" | ||
return 0 | ||
} | ||
|
||
# Function to check if ECS cluster exists | ||
check_ecs_cluster() { | ||
local cluster=$1 | ||
|
||
# Use || true to handle expected failures gracefully | ||
local status | ||
status=$(aws ecs describe-clusters --clusters "${cluster}" --query 'clusters[0].status' --output text 2>/dev/null || echo "not-found") | ||
|
||
if [ "$status" = "None" ] || [ -z "$status" ]; then | ||
echo "not-found" | ||
else | ||
echo "$status" | ||
fi | ||
} | ||
|
||
# Function to check if ECS service exists | ||
check_ecs_service() { | ||
local cluster=$1 | ||
local service=$2 | ||
|
||
# Use || true to handle expected failures gracefully | ||
local status | ||
status=$(aws ecs describe-services --cluster "${cluster}" --services "${service}" --query 'services[0].status' --output text 2>/dev/null || echo "not-found") | ||
|
||
if [ "$status" = "None" ] || [ -z "$status" ]; then | ||
echo "not-found" | ||
else | ||
echo "$status" | ||
fi | ||
} | ||
|
||
# Function to resume ECS service | ||
resume_ecs_service() { | ||
local prefix=$1 | ||
local env=$2 | ||
local cluster="${prefix}-node-api-${env}" | ||
local service="${prefix}-node-api-${env}" | ||
|
||
echo "Resuming ECS service ${service} on cluster ${cluster}..." | ||
# Check if the ECS cluster exists | ||
if ! aws ecs describe-clusters --clusters "${cluster}" --query 'clusters[0]' --output text &>/dev/null; then | ||
echo "Checking ECS cluster ${cluster}..." | ||
local cluster_status=$(check_ecs_cluster "${cluster}") | ||
|
||
if [ "$cluster_status" = "not-found" ]; then | ||
echo "ECS cluster ${cluster} does not exist. Skipping service resume." | ||
return 0 | ||
fi | ||
|
||
if [ "$cluster_status" != "ACTIVE" ]; then | ||
echo "ECS cluster ${cluster} is not active (status: ${cluster_status}). Skipping service resume." | ||
return 0 | ||
fi | ||
|
||
# Check if the ECS service exists | ||
if ! aws ecs describe-services --cluster "${cluster}" --services "${service}" --query 'services[0]' --output text &>/dev/null; then | ||
echo "Checking ECS service ${service}..." | ||
local service_status=$(check_ecs_service "${cluster}" "${service}") | ||
|
||
if [ "$service_status" = "not-found" ]; then | ||
echo "ECS service ${service} does not exist in cluster ${cluster}. Skipping service resume." | ||
return 0 | ||
fi | ||
# Update scaling policy | ||
aws application-autoscaling register-scalable-target \ | ||
|
||
echo "Resuming ECS service ${service} on cluster ${cluster}..." | ||
|
||
# Update scaling policy - use || true to handle potential failures | ||
if ! aws application-autoscaling register-scalable-target \ | ||
--service-namespace ecs \ | ||
--resource-id service/${cluster}/${service} \ | ||
--resource-id "service/${cluster}/${service}" \ | ||
--scalable-dimension ecs:service:DesiredCount \ | ||
--min-capacity 1 \ | ||
--max-capacity 2 \ | ||
--no-cli-pager \ | ||
--output json | ||
--output json; then | ||
echo "Warning: Failed to update scaling policy for ECS service ${service}" | ||
fi | ||
|
||
# Update service desired count | ||
aws ecs update-service \ | ||
--cluster ${cluster} \ | ||
--service ${service} \ | ||
if ! aws ecs update-service \ | ||
--cluster "${cluster}" \ | ||
--service "${service}" \ | ||
--desired-count 1 \ | ||
--no-cli-pager \ | ||
--output json | ||
--output json; then | ||
echo "Failed to update ECS service ${service} desired count" | ||
return 1 | ||
fi | ||
|
||
echo "ECS service has been resumed" | ||
return 0 | ||
} | ||
|
||
# Main function | ||
|
@@ -103,20 +161,35 @@ main() { | |
|
||
# Check DB cluster status | ||
local db_status=$(check_db_cluster "$prefix" "$env") | ||
echo "DB cluster status: ${db_status}" | ||
|
||
if [ "$db_status" == "not-found" ]; then | ||
echo "Skipping resume operation, DB cluster does not exist" | ||
return 0 | ||
elif [ "$db_status" == "stopped" ]; then | ||
start_db_cluster "$prefix" "$env" || return 1 | ||
if [ "$db_status" = "not-found" ]; then | ||
echo "DB cluster does not exist - skipping DB operations" | ||
elif [ "$db_status" = "stopped" ]; then | ||
echo "DB cluster is stopped - starting it..." | ||
if start_db_cluster "$prefix" "$env"; then | ||
echo "DB cluster started successfully" | ||
else | ||
echo "Failed to start DB cluster" | ||
return 1 | ||
fi | ||
elif [ "$db_status" = "available" ]; then | ||
echo "DB cluster is already available - no action needed" | ||
elif [ "$db_status" = "starting" ]; then | ||
echo "DB cluster is already starting - no action needed" | ||
else | ||
echo "DB cluster is not in a stopped state. Current state: $db_status" | ||
echo "DB cluster is in state: $db_status - no action taken" | ||
fi | ||
|
||
# Resume ECS service | ||
resume_ecs_service "$prefix" "$env" | ||
if resume_ecs_service "$prefix" "$env"; then | ||
echo "ECS service operations completed successfully" | ||
else | ||
echo "ECS service operations failed" | ||
return 1 | ||
fi | ||
|
||
echo "Resources have been resumed successfully" | ||
echo "Resources resume operations completed successfully" | ||
} | ||
|
||
# Parse and check arguments | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Similar to the resume script, this uses
set -euo pipefail
but frequently toggles error handling withset +e
. Consider using|| true
or command-specific error handling instead of repeatedly changing the global error handling behavior.Copilot uses AI. Check for mistakes.