Skip to content
Open
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
25 changes: 25 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,31 @@ resource "aws_s3_bucket_public_access_block" "codepipeline_bucket" {
]
}

resource "aws_s3_bucket_cors_configuration" "inframap_cors_rules" {
bucket = aws_s3_bucket.codepipeline_bucket.id
cors_rule {
allowed_headers = ["*"]
allowed_methods = ["GET", "HEAD"]
allowed_origins = ["*"]
expose_headers = []
max_age_seconds = 3000
}
}

resource "aws_s3_bucket_object" "infrmap_cli" {
bucket = aws_s3_bucket.codepipeline_bucket.id
key = "inframap/inframap-linux-amd64.tar.gz"
source = "${path.module}/scripts/inframap-linux-amd64.tar.gz"
etag = "${filemd5("${path.module}/scripts/inframap-linux-amd64.tar.gz")}"
}

resource "aws_s3_bucket_object" "infrmap_generator" {
bucket = aws_s3_bucket.codepipeline_bucket.id
key = "inframap/generator.sh"
source = "${path.module}/scripts/generator.sh"
etag = "${filemd5("${path.module}/scripts/generator.sh")}"
}

data "aws_caller_identity" "current" {

}
Expand Down
46 changes: 46 additions & 0 deletions scripts/generator.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
APP_NAME="$(echo $1)"
ENV_NAME="$(echo $2)"
ENV_NAME_COLOR=$(echo $3)
if [ $ENV_NAME_COLOR == "blue" ] || [ $ENV_NAME_COLOR == "green" ]; then
ENV_NAME_COLOR=$(echo $2-$3)
fi
export CONSUL_URL="$(echo $4)"
export CONSUL_HTTP_ADDR=https://$CONSUL_URL
export CONSUL_HTTP_TOKEN="$(echo $5)"

tar -zxvf inframap-linux-amd64.tar.gz
declare -a STATE_LAYERS=("app" "data" "shared")
for s in "${STATE_LAYERS[@]}"; do
if [ $s == "app" ]; then
DATA_WORKSPACE=$(consul kv get "terraform/${APP_NAME}/app-env.json" | jq -r ".${ENV_NAME}.data_workspace")
SHARED_LAYER=$(consul kv get "terraform/${APP_NAME}/app-env.json" | jq -r ".${ENV_NAME}.env_type")
echo $DATA_WORKSPACE;
echo $SHARED_LAYER;
fi
if [ $s == "data" ]; then
ENV_NAME_COLOR=$DATA_WORKSPACE
elif [ $s == "shared" ]; then
ENV_NAME_COLOR="shared-"$SHARED_LAYER
fi
CHECK=$(consul kv get "terraform/${APP_NAME}/$s/state-env:${ENV_NAME_COLOR}" | jq -r '."current-hash"')
echo $CHECK
if [ -z "$CHECK" ] || [ $CHECK == "null" ]; then
STATE=$(consul kv get "terraform/${APP_NAME}/$s/state-env:${ENV_NAME_COLOR}")
echo $STATE | jq | tee -a "$s".tfstate >/dev/null
else
STATE=$(curl -sS "$CONSUL_HTTP_ADDR/v1/kv/terraform/${APP_NAME}/$s/state-env:${ENV_NAME_COLOR}/?keys&token=$CONSUL_HTTP_TOKEN")
KEYS=$(echo $STATE | jq -r '.' | tr -d '[],"')
KEYS=($(echo $STATE | jq -r '.' | tr -d '[]," '))
LAST_ELEMENT=$(echo ${KEYS[${#KEYS[@]}-1]})
FETCH_STATE=""
for row in "${KEYS[@]}"; do
FETCH_STATE=$FETCH_STATE$(consul kv get "$row")
if [ "$LAST_ELEMENT" = "$row" ]; then
echo $FETCH_STATE | jq | tee -a "$s".tfstate >/dev/null
echo $FETCH_STATE;
fi
done
fi
./inframap-linux-amd64 generate "$s".tfstate | dot -Tpng > "$s"_graph.png
aws s3 cp "$s"_graph.png s3://s3-codepipeline-${APP_NAME}-$SHARED_LAYER/inframap/
done
Binary file added scripts/inframap-linux-amd64.tar.gz
Binary file not shown.