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

Applying update to generators #30

Merged
merged 3 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions operations/_scripts/deploy/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ set -x
echo "In deploy.sh"
GITHUB_REPO_NAME=$(echo $GITHUB_REPOSITORY | sed 's/^.*\///')

# Generate the tf state bucket
export TF_STATE_BUCKET="$(/bin/bash $GITHUB_ACTION_PATH/operations/_scripts/generate/generate_tf_state_bucket.sh | xargs)"
# Generate buckets identifiers and check them agains AWS Rules
export TF_STATE_BUCKET="$(/bin/bash $GITHUB_ACTION_PATH/operations/_scripts/generate/generate_buckets_identifiers.sh tf | xargs)"
/bin/bash $GITHUB_ACTION_PATH/operations/_scripts/deploy/check_bucket_name.sh $TF_STATE_BUCKET
export LB_LOGS_BUCKET="$(/bin/bash $GITHUB_ACTION_PATH/operations/_scripts/generate/generate_buckets_identifiers.sh lb | xargs)"
/bin/bash $GITHUB_ACTION_PATH/operations/_scripts/deploy/check_bucket_name.sh $LB_LOGS_BUCKET

# Generate buckets identifiers
export TF_STATE_BUCKET="$(/bin/bash $GITHUB_ACTION_PATH/operations/_scripts/generate/generate_buckets_identifiers.sh tf | xargs)"
export LB_LOGS_BUCKET="$(/bin/bash $GITHUB_ACTION_PATH/operations/_scripts/generate/generate_buckets_identifiers.sh lb | xargs)"

# Generate subdomain
/bin/bash $GITHUB_ACTION_PATH/operations/_scripts/generate/generate_subdomain.sh
Expand Down
57 changes: 57 additions & 0 deletions operations/_scripts/generate/check_bucket_name.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash
echo "In check_bucket_name.sh"

set -e

### S3 Buckets name must follow AWS rules. Info below.
### https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html

function checkBucket() {
# check length of bucket name
if [[ ${#1} -lt 3 || ${#1} -gt 63 ]]; then
echo "::error::Bucket name must be between 3 and 63 characters long."
exit 1
fi

# check that bucket name consists only of lowercase letters, numbers, dots (.), and hyphens (-)
if [[ ! $1 =~ ^[a-z0-9.-]+$ ]]; then
echo "::error::Bucket name can only consist of lowercase letters, numbers, dots (.), and hyphens (-)."
exit 1
fi

# check that bucket name begins and ends with a letter or number
if [[ ! $1 =~ ^[a-zA-Z0-9] ]]; then
echo "::error::Bucket name must begin with a letter or number."
exit 1
fi
if [[ ! $1 =~ [a-zA-Z0-9]$ ]]; then
echo "::error::Bucket name must end with a letter or number."
exit 1
fi

# check that bucket name does not contain two adjacent periods
if [[ $1 =~ \.\. ]]; then
echo "::error::Bucket name cannot contain two adjacent periods."
exit 1
fi

# check that bucket name is not formatted as an IP address
if [[ $1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
echo "::error::Bucket name cannot be formatted as an IP address."
exit 1
fi

# check that bucket name does not start with the prefix xn--
if [[ $1 =~ ^xn-- ]]; then
echo "::error::Bucket name cannot start with the prefix xn--."
exit 1
fi

# check that bucket name does not end with the suffix -s3alias
if [[ $1 =~ -s3alias$ ]]; then
echo "::error::Bucket name cannot end with the suffix -s3alias."
exit 1
fi
}

checkBucket $1
31 changes: 31 additions & 0 deletions operations/_scripts/generate/generate_buckets_identifiers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

set -e

GITHUB_IDENTIFIER="$(echo $($GITHUB_ACTION_PATH/operations/_scripts/generate/generate_identifier.sh) | tr '[:upper:]' '[:lower:]' | tr '_' '-' )"

case $1 in
tf)
# Generate TF_STATE_BUCKET ID if empty
if [ -z "${TF_STATE_BUCKET}" ]; then
# Add trailing id depending on name length - See AWS S3 bucket naming rules
if [[ ${#GITHUB_IDENTIFIER} < 55 ]]; then
TF_STATE_BUCKET="${GITHUB_IDENTIFIER}-tf-state"
else
TF_STATE_BUCKET="${GITHUB_IDENTIFIER}-tf"
fi
fi
echo "$TF_STATE_BUCKET"

;;
lb)
# Generate LB_LOGS_BUCKET ID
# Add trailing id depending on name length - See AWS S3 bucket naming rules
if [[ ${#GITHUB_IDENTIFIER} < 59 ]]; then
LB_LOGS_BUCKET="${GITHUB_IDENTIFIER}-logs"
else
LB_LOGS_BUCKET="${GITHUB_IDENTIFIER}-lg"
fi
echo "$LB_LOGS_BUCKET"
;;
esac
16 changes: 0 additions & 16 deletions operations/_scripts/generate/generate_tf_state_bucket.sh

This file was deleted.

2 changes: 1 addition & 1 deletion operations/_scripts/generate/generate_tf_vars.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ app_branch_name = \"${GITHUB_BRANCH_NAME}\"
app_install_root = \"/home/ubuntu\"

# Logs
lb_access_bucket_name = \"${TF_STATE_BUCKET}-logs\"
lb_access_bucket_name = \"${LB_LOGS_BUCKET}\"

# Security Group names
security_group_name = \"${GITHUB_IDENTIFIER}\"
Expand Down