Skip to content

Commit

Permalink
feat: create runners and organize dirs (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvincrespo authored Sep 15, 2023
1 parent d34b393 commit 309bc1a
Show file tree
Hide file tree
Showing 23 changed files with 174 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/tasks/aws_scripts_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace :aws_scripts do
desc "Copies AWS scripts to bin directory"
task copy_scripts: :environment do
source = File.expand_path("../../../scripts", __FILE__)
destination = Rails.root.join("bin/aws")
destination = Rails.root.join("bin")

FileUtils.mkdir_p(destination)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions scripts/runners/create-eks-cluster
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

set -e
set -o pipefail

. ./bin/aws/utils

check_aws_cli_installed
check_helm_installed

# Function to clean up resources in case of failure
cleanup() {
echo "[UTILS] [EKS] An error occurred."
}

# Trap any error, and call our cleanup function
trap cleanup ERR

# Source .env file
. .env

# 1. Create cluster
./bin/aws/eks/cluster/create

# 2. Setup secrets
./bin/aws/eks/setup-secrets

# 3. Apply deployments
# kubectl apply -f deployment/secrets.yml
# kubectl apply -f deployment/sidekiq.yml
# kubectl apply -f deployment/app.yml
71 changes: 71 additions & 0 deletions scripts/runners/create-rds-instance
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/bash

set -e
set -o pipefail

. ./bin/aws/utils

check_aws_cli_installed

# Function to clean up resources in case of failure
cleanup() {
echo "[UTILS] [RDS] An error occurred. Deleting RDS instance..."
./bin/aws/rds/instance/delete
}

# Trap any error, and call our cleanup function
trap cleanup ERR

# Source .env file
. .env

# ----------------------------------------------------------------------------
# Create RDS instance
# ----------------------------------------------------------------------------

echo "[UTILS] [RDS] Running RDS creation script for $DB_INSTANCE_ID..."

./bin/aws/rds/instance/create

# ----------------------------------------------------------------------------
# Wait for RDS instance to be available
# ----------------------------------------------------------------------------

DB_STATUS=""

while [ "$DB_STATUS" != "available" ]; do
echo "[UTILS] [RDS] Waiting for RDS instance to be available..."

DB_STATUS=$(bin/aws/rds/instance/status)

sleep 10
done

echo "[UTILS] [RDS] The RDS instance is available."

DB_ARN=$(./bin/aws/rds/instance/describe)

echo "[UTILS] [RDS] The ARN of the RDS instance is: $DB_ARN"

# ----------------------------------------------------------------------------
# Update .env file with RDS instance ARN
# ----------------------------------------------------------------------------

ENV_FILE=".env"
KEY_TO_REPLACE="DB_ARN"

# Check if key exists in .env file
if grep -q "$KEY_TO_REPLACE" $ENV_FILE; then
# Key found, so update it
echo "[UTILS] [RDS] Updating .env file with RDS instance ARN..."
sed -i '.bak' "s/^$KEY_TO_REPLACE=.*/$KEY_TO_REPLACE=$DB_ARN/" $ENV_FILE
else
# Key not found, so append it
echo "[UTILS] [RDS] Appending .env file with RDS instance ARN..."
echo "$KEY_TO_REPLACE=$DB_ARN" >> $ENV_FILE
fi

echo "[UTILS] [RDS] Updated .env file with RDS instance ARN: $DB_ARN"

# Remove the trap since everything was successful
trap - ERR
71 changes: 71 additions & 0 deletions scripts/runners/create-redis-instance
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/bash

set -e
set -o pipefail

. ./bin/aws/utils

check_aws_cli_installed

# Function to clean up resources in case of failure
cleanup() {
echo "[UTILS] [REDIS] An error occurred. Deleting RDS instance..."
./bin/aws/ec/redis/replication-group/delete
}

# Trap any error, and call our cleanup function
trap cleanup ERR

# Source .env file
. .env

# ----------------------------------------------------------------------------
# Create RDS instance
# ----------------------------------------------------------------------------

echo "[UTILS] [REDIS] Running Redis creation script for $REPLICATION_GROUP_ID..."

./bin/aws/ec/redis/replication-group/create

# ----------------------------------------------------------------------------
# Wait for Redis instance to be available
# ----------------------------------------------------------------------------

REPLICATION_GROUP_STATUS=""

while [ "$REPLICATION_GROUP_STATUS" != "available" ]; do
echo "[UTILS] [REDIS] Waiting for Redis instance to be available..."

REPLICATION_GROUP_STATUS=$(./bin/aws/ec/redis/replication-group/status)

sleep 10
done

echo "[UTILS] [REDIS] The Redis instance is available."

REPLICATION_GROUP_ADDRESS=$(./bin/aws/ec/redis/replication-group/describe)

echo "[UTILS] [REDIS] The Address of the Redis instance is: $REPLICATION_GROUP_ADDRESS"

# ----------------------------------------------------------------------------
# Update .env file with RDS instance ARN
# ----------------------------------------------------------------------------

ENV_FILE=".env"
KEY_TO_REPLACE="REPLICATION_GROUP_ADDRESS"

# Check if key exists in .env file
if grep -q "$KEY_TO_REPLACE" $ENV_FILE; then
# Key found, so update it
echo "[UTILS] [REDIS] Updating .env file with RDS instance ARN..."
sed -i '.bak' "s/^$KEY_TO_REPLACE=.*/$KEY_TO_REPLACE=$REPLICATION_GROUP_ADDRESS/" $ENV_FILE
else
# Key not found, so append it
echo "[UTILS] [REDIS] Appending .env file with RDS instance ARN..."
echo "$KEY_TO_REPLACE=$REPLICATION_GROUP_ADDRESS" >> $ENV_FILE
fi

echo "[UTILS] [REDIS] Updated .env file with Redis address: $REPLICATION_GROUP_ADDRESS"

# Remove the trap since everything was successful
trap - ERR

0 comments on commit 309bc1a

Please sign in to comment.