diff --git a/.env.example b/.env.example index 51f6330..1bfa77e 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,9 @@ +# Define relative path to aws scripts and runners +AWS_SCRIPTS_BASE_DIR="./bin" + +# VPC +AWS_VPC_SUBNET_IDS="" + # Redis AWS_REPLICATION_GROUP_ID="" AWS_REPLICATION_GROUP_DESCRIPTION="" @@ -6,6 +12,8 @@ AWS_REPLICATION_ENGINE="" AWS_REPLICATION_CACHE_PARAMETER_GROUP_NAME="" AWS_REPLICATION_CACHE_NUMBER_OF_CLUSTERS="" AWS_ELASTICACHE_SUBNET_GROUP_ID="" +AWS_EC_SUBNET_GROUP_NAME="" +AWS_EC_SUBNET_GROUP_DESCRIPTION="" # Database AWS_DB_INSTANCE_ID="" diff --git a/.gitignore b/.gitignore index 01c025d..18146d3 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ *.gem +.env diff --git a/scripts/aws/ec/redis/replication-group/create b/scripts/aws/ec/redis/replication-group/create index 4be2bb8..6751220 100755 --- a/scripts/aws/ec/redis/replication-group/create +++ b/scripts/aws/ec/redis/replication-group/create @@ -3,13 +3,15 @@ set -e set -o pipefail -. ./bin/aws/utils +. .env + +. $AWS_SCRIPTS_BASE_DIR/aws/utils check_aws_cli_installed -. .env +set_log_context "[UTILS] [EC]" -echo "[UTILS] [EKS] Creating elasticache replication group." >&2 +info "Creating elasticache replication group." cmd="aws elasticache create-replication-group" @@ -36,4 +38,4 @@ cmd+=" --no-cli-pager" eval $cmd -echo "[UTILS] [EKS] Completed creating elasticache replication group." >&2 +info "Completed creating elasticache replication group." diff --git a/scripts/aws/ec/redis/replication-group/delete b/scripts/aws/ec/redis/replication-group/delete index 51707bc..847047c 100755 --- a/scripts/aws/ec/redis/replication-group/delete +++ b/scripts/aws/ec/redis/replication-group/delete @@ -1,11 +1,20 @@ #!/bin/sh +set -e +set -o pipefail + . .env -echo "[UTILS] [EC] [REPLICATION GROUP] Deleting elasticache replication group." >&2 +. $AWS_SCRIPTS_BASE_DIR/aws/utils + +check_aws_cli_installed + +set_log_context "[UTILS] [EC] [REPLICATION GROUP]" + +info "Deleting elasticache replication group." aws elasticache delete-replication-group \ --replication-group-id "$AWS_REPLICATION_GROUP_ID" \ --no-cli-pager -echo "[UTILS] [EC] [REPLICATION GROUP] Scheduled deleting elasticache replication group." >&2 +info "[UTILS] [EC] [REPLICATION GROUP] Scheduled deleting elasticache replication group." diff --git a/scripts/aws/ec/redis/replication-group/describe b/scripts/aws/ec/redis/replication-group/describe index 9a08705..8ea97b0 100755 --- a/scripts/aws/ec/redis/replication-group/describe +++ b/scripts/aws/ec/redis/replication-group/describe @@ -1,7 +1,18 @@ #!/bin/sh +set -e +set -o pipefail + . .env +. $AWS_SCRIPTS_BASE_DIR/aws/utils + +check_aws_cli_installed + +set_log_context "[UTILS] [EC]" + +info "Describing elasticache replication group." + aws elasticache describe-replication-groups \ --replication-group-id "$AWS_REPLICATION_GROUP_ID" \ --query "ReplicationGroups[0].ConfigurationEndpoint.Address" \ diff --git a/scripts/aws/ec/redis/replication-group/status b/scripts/aws/ec/redis/replication-group/status index 50114ff..3ab1f1e 100755 --- a/scripts/aws/ec/redis/replication-group/status +++ b/scripts/aws/ec/redis/replication-group/status @@ -1,7 +1,18 @@ #!/bin/sh +set -e +set -o pipefail + . .env +. $AWS_SCRIPTS_BASE_DIR/aws/utils + +check_aws_cli_installed + +set_log_context "[UTILS] [EC]" + +info "Fetching status of elasticache replication group." + aws elasticache describe-replication-groups \ --replication-group-id "$AWS_REPLICATION_GROUP_ID" \ --query "ReplicationGroups[0].Status" \ diff --git a/scripts/aws/eks/subnet-group/create b/scripts/aws/ec/subnet-groups/create similarity index 57% rename from scripts/aws/eks/subnet-group/create rename to scripts/aws/ec/subnet-groups/create index 6aa5f03..5c06461 100755 --- a/scripts/aws/eks/subnet-group/create +++ b/scripts/aws/ec/subnet-groups/create @@ -3,19 +3,21 @@ set -e set -o pipefail -. ./bin/aws/utils +. .env + +. $AWS_SCRIPTS_BASE_DIR/aws/utils check_aws_cli_installed -. .env +set_log_context "[UTILS] [EC]" -echo "[UTILS] [EKS] Creating elasticache subnet group." >&2 +info "Creating elasticache subnet group." cmd="aws elasticache create-cache-subnet-group" -cache_subnet_group_name="${EC_SUBNET_GROUP_NAME:-}" -cache_subnet_group_description="${EC_SUBNET_GROUP_DESCRIPTION:-}" -subnet_ids="${SUBNET_IDS:-}" +cache_subnet_group_name="${AWS_EC_SUBNET_GROUP_NAME:-}" +cache_subnet_group_description="${AWS_EC_SUBNET_GROUP_DESCRIPTION:-}" +subnet_ids="${AWS_VPC_SUBNET_IDS:-}" [[ -n "$cache_subnet_group_name" ]] && cmd+=" --cache-subnet-group-name \"$cache_subnet_group_name\"" [[ -n "$cache_subnet_group_description" ]] && cmd+=" --cache-subnet-group-description \"$cache_subnet_group_description\"" @@ -25,4 +27,4 @@ cmd+=" --no-cli-pager" eval $cmd -echo "[UTILS] [EKS] Completed creating elasticache subnet group." >&2 +info "Completed creating elasticache subnet group." diff --git a/scripts/aws/ec/subnet-groups/delete b/scripts/aws/ec/subnet-groups/delete index 68d205c..9505906 100755 --- a/scripts/aws/ec/subnet-groups/delete +++ b/scripts/aws/ec/subnet-groups/delete @@ -3,16 +3,18 @@ set -e set -o pipefail -. ./bin/aws/utils +. .env + +. $AWS_SCRIPTS_BASE_DIR/aws/utils check_aws_cli_installed -. .env +set_log_context "[UTILS] [EC] [SUBNET GROUP]" -echo "[UTILS] [EC] [SUBNET GROUP] Delete elasticache subnet group. ${AWS_ELASTICACHE_SUBNET_GROUP_ID}" +info "Delete elasticache subnet group. ${AWS_ELASTICACHE_SUBNET_GROUP_ID}" aws elasticache delete-cache-subnet-group \ --cache-subnet-group-name "$AWS_ELASTICACHE_SUBNET_GROUP_ID" \ --no-cli-pager -echo "[UTILS] [EC] [SUBNET GROUP] Deleted elasticache subnet group. ${AWS_ELASTICACHE_SUBNET_GROUP_ID}" +info "Deleted elasticache subnet group. ${AWS_ELASTICACHE_SUBNET_GROUP_ID}" diff --git a/scripts/aws/utils b/scripts/aws/utils index 5a77081..79b4468 100755 --- a/scripts/aws/utils +++ b/scripts/aws/utils @@ -1,56 +1,90 @@ #!/bin/bash function check_aws_cli_installed { - echo "[UTILS] Checking AWS CLI is installed." >&2 + set_log_context "[UTILS]" + + info "Checking AWS CLI is installed." if ! command -v aws &> /dev/null; then - echo "AWS CLI is not installed. Please install it first. https://aws.amazon.com/cli/" >&2 + error "AWS CLI is not installed. Please install it first. https://aws.amazon.com/cli/" exit 1 fi - echo "[UTILS] Confirmed AWS CLI is installed." >&2 + info "Confirmed AWS CLI is installed." } function check_helm_installed { - echo "[UTILS] Checking Helm is installed." >&2 + set_log_context "[UTILS]" + + info "Checking Helm is installed." if ! command -v helm &> /dev/null; then - echo "hl could not be found. Please install it first. https://helm.sh/" >&2 + error "hl could not be found. Please install it first. https://helm.sh/" exit 1 fi - echo "[UTILS] Confirmed Helm is installed." >&2 + info "Confirmed Helm is installed." } function check_kubectl_installed { - echo "[UTILS] Checking kubectl is installed." >&2 + set_log_context "[UTILS]" + + info "Checking kubectl is installed." if ! command -v helm &> /dev/null; then - echo "kubectl could not be found. Please install it first. https://kubernetes.io/docs/reference/kubectl/" >&2 + error "kubectl could not be found. Please install it first. https://kubernetes.io/docs/reference/kubectl/" exit 1 fi - echo "[UTILS] Confirmed kubectl is installed." >&2 + info "Confirmed kubectl is installed." } function check_eksctl_installed { - echo "[UTILS] Checking eksctl is installed." >&2 + set_log_context "[UTILS]" + + info "Checking eksctl is installed." if ! command -v eksctl &> /dev/null; then - echo "eksctl could not be found. Please install it first. https://eksctl.io/" >&2 + error "eksctl could not be found. Please install it first. https://eksctl.io/" exit 1 fi - echo "[UTILS] Confirmed eksctl is installed." >&2 + info "Confirmed eksctl is installed." } function check_docker_installed { - echo "[UTILS] Checking docker is installed." >&2 + set_log_context "[UTILS]" + + info "Checking docker is installed." if ! command -v docker &> /dev/null; then - echo "Docker could not be found. Please install it first. https://www.docker.com/" >&2 + error "Docker could not be found. Please install it first. https://www.docker.com/" exit 1 fi - echo "[UTILS] Confirmed docker is installed." >&2 + info "Confirmed docker is installed." +} + +LOG_CONTEXT="" + +log() { + local level="$1" + shift + + local datetime=$(date +"%Y-%m-%d %H:%M:%S") + local message="$@" + + echo "[$datetime] [$level] $LOG_CONTEXT $message" >&2 +} + +info() { + log "INFO" "$@" +} + +error() { + log "ERROR" "$@" +} + +set_log_context() { + LOG_CONTEXT="$@" }