Skip to content
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

feat(logging): implements standardized logging #16

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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=""
Expand All @@ -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=""
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@


*.gem
.env
10 changes: 6 additions & 4 deletions scripts/aws/ec/redis/replication-group/create
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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."
13 changes: 11 additions & 2 deletions scripts/aws/ec/redis/replication-group/delete
Original file line number Diff line number Diff line change
@@ -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."
11 changes: 11 additions & 0 deletions scripts/aws/ec/redis/replication-group/describe
Original file line number Diff line number Diff line change
@@ -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" \
Expand Down
11 changes: 11 additions & 0 deletions scripts/aws/ec/redis/replication-group/status
Original file line number Diff line number Diff line change
@@ -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" \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\""
Expand All @@ -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."
10 changes: 6 additions & 4 deletions scripts/aws/ec/subnet-groups/delete
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
64 changes: 49 additions & 15 deletions scripts/aws/utils
Original file line number Diff line number Diff line change
@@ -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="$@"
}