Skip to content

Add debug to terraform script - DO NOT MERGE #2722

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

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ENV GCLOUD_SDK_VERSION=467.0.0-0
ENV TERRAFORM_VERSION=1.9.6

RUN apt-get -qq update \
&& apt-get install -yq curl apt-transport-https ca-certificates gnupg \
&& apt-get install -yq curl apt-transport-https ca-certificates gnupg unzip \
&& apt-get clean

RUN echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list \
Expand All @@ -18,6 +18,11 @@ RUN curl -fsSL https://apt.releases.hashicorp.com/gpg | gpg --dearmor -o /usr/sh
&& apt-get install -yq terraform=${TERRAFORM_VERSION}-1 \
&& apt-get clean

RUN curl -s "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" \
&& unzip -q awscliv2.zip \
&& ./aws/install \
&& rm -rf awscliv2.zip aws

HEALTHCHECK --timeout=3s CMD sh -c "[ -f /tmp/tf-applied ]"

ENV TF_IN_AUTOMATION=true
Expand Down
53 changes: 52 additions & 1 deletion internal/servicedeployer/_static/terraform_deployer_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,64 @@ cleanup() {
set -x
terraform destroy -auto-approve

if [[ "${running_on_aws}" == 1 ]]; then
echo "After Terraform destroy command"
aws s3api list-buckets --query "Buckets[].Name" --output text | tr '\t' '\n'
fi

exit $r
}
trap cleanup EXIT INT TERM

retry() {
local retries=$1
shift
local count=0
until "$@"; do
exit=$?
wait=$((2 ** count))
count=$((count + 1))
if [ $count -lt "$retries" ]; then
>&2 echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
sleep $wait
else
>&2 echo "Retry $count/$retries exited $exit, no more retries left."
return $exit
fi
done
return 0
}

terraform init
terraform plan
terraform apply -auto-approve

running_on_aws=0
if [[ "${AWS_SECRET_ACCESS_KEY:-""}" != "" ]]; then
running_on_aws=1
echo "Before Terraform Apply command"
aws s3api list-buckets --query "Buckets[].Name" --output text | tr '\t' '\n'

buckets=(
"elastic-package-canva-bucket-64363"
"elastic-package-canva-bucket-51662"
"elastic-package-sublime-security-bucket-35776"
"elastic-package-symantec-endpoint-security-bucket-65009"
"elastic-package-symantec-endpoint-security-bucket-78346"
)
for b in "${buckets[@]}"; do
echo "Check buckets: ${b}"
aws s3api head-bucket --bucket "${b}" || true
echo ""
done
fi


retry 2 terraform apply -auto-approve

if [[ "${running_on_aws}" == 1 ]]; then
echo "After Terraform Apply command"
aws s3api list-buckets --query "Buckets[].Name" --output text | tr '\t' '\n'
fi

terraform output -json > /output/tfOutputValues.json

Expand Down
15 changes: 15 additions & 0 deletions internal/servicedeployer/terraform_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package servicedeployer
import (
"errors"
"fmt"
"os"
"strings"

"github.com/elastic/elastic-package/internal/compose"
Expand All @@ -27,6 +28,20 @@ func (tsd TerraformServiceDeployer) buildTerraformExecutorEnvironment(info Servi
vars[tfDir] = tsd.definitionsDir
vars[tfOutputDir] = info.OutputDir

// canva
// vars[tfTestRunID] = "64363"
vars[tfTestRunID] = "51662"
// sublime_security
// vars[tfTestRunID] = "35776"

if v, found := os.LookupEnv("ELASTIC_PACKAGE_PREFIX_TERRAFORM_RUN_ID"); found && v != "" {
vars[tfTestRunID] = fmt.Sprintf("%s%s", v, info.Test.RunID)
}

if v, found := os.LookupEnv("ELASTIC_PACKAGE_SET_TERRAFORM_RUN_ID"); found && v != "" {
vars[tfTestRunID] = v
}

var pairs []string
for k, v := range vars {
pairs = append(pairs, fmt.Sprintf("%s=%s", k, v))
Expand Down