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
14 changes: 14 additions & 0 deletions platform/infra/terraform/cluster/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ main() {
# Initialize Terraform with S3 backend
initialize_terraform "clusters" "$DEPLOY_SCRIPTDIR"

# Check for and clear any stale locks
cd "$DEPLOY_SCRIPTDIR"
if terraform state list &>/dev/null; then
log "State accessible, no lock issues"
else
log_warning "State lock detected, attempting to force unlock"
LOCK_ID=$(terraform force-unlock -force 2>&1 | grep -oP 'Lock ID: \K[a-f0-9-]+' || echo "")
if [[ -n "$LOCK_ID" ]]; then
log "Force unlocking with ID: $LOCK_ID"
terraform force-unlock -force "$LOCK_ID" || log_warning "Force unlock failed, continuing anyway"
fi
fi
cd -

# Apply Terraform configuration
log "Applying clusters stack..."
if ! terraform -chdir=$DEPLOY_SCRIPTDIR apply \
Expand Down
27 changes: 26 additions & 1 deletion platform/infra/terraform/cluster/destroy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ main() {
# Initialize Terraform with S3 backend
initialize_terraform "clusters" "$DEPLOY_SCRIPTDIR"

# Check for and clear any stale locks
cd "$DEPLOY_SCRIPTDIR"
if terraform state list &>/dev/null; then
log "State accessible, no lock issues"
else
log_warning "State lock detected, attempting to force unlock"
LOCK_ID=$(terraform force-unlock -force 2>&1 | grep -oP 'Lock ID: \K[a-f0-9-]+' || echo "")
if [[ -n "$LOCK_ID" ]]; then
log "Force unlocking with ID: $LOCK_ID"
terraform force-unlock -force "$LOCK_ID" || log_warning "Force unlock failed, continuing anyway"
fi
fi
cd -

# Destroy Terraform resources
log "Destroying clusters stack..."
if ! terraform -chdir=$DEPLOY_SCRIPTDIR destroy \
Expand All @@ -40,7 +54,18 @@ main() {
-var="resource_prefix=${RESOURCE_PREFIX}" \
-var="workshop_participant_role_arn=${WS_PARTICIPANT_ROLE_ARN}" \
-auto-approve; then
log_warning "Clusters stack destroy failed, trying again..."
log_warning "Clusters stack destroy failed, checking for lock issues"

# Extract lock ID from error if present
cd "$DEPLOY_SCRIPTDIR"
LOCK_ID=$(terraform plan 2>&1 | grep -oP 'ID:\s+\K[a-f0-9-]+' | head -1 || echo "")
if [[ -n "$LOCK_ID" ]]; then
log "Forcing unlock with ID: $LOCK_ID"
terraform force-unlock -force "$LOCK_ID" || true
fi
cd -

log_warning "Retrying destroy after lock handling"
if ! terraform -chdir=$DEPLOY_SCRIPTDIR destroy \
-var-file="${GENERATED_TFVAR_FILE}" \
-var="hub_vpc_id=${HUB_VPC_ID}" \
Expand Down
28 changes: 28 additions & 0 deletions platform/infra/terraform/common/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ main() {
# Initialize Terraform with S3 backend
initialize_terraform "gitlab infra" "$DEPLOY_SCRIPTDIR/gitlab_infra"

# Check for and clear any stale locks
cd "$DEPLOY_SCRIPTDIR/gitlab_infra"
if terraform state list &>/dev/null; then
log "State accessible, no lock issues"
else
log_warning "State lock detected, attempting to force unlock"
LOCK_ID=$(terraform force-unlock -force 2>&1 | grep -oP 'Lock ID: \K[a-f0-9-]+' || echo "")
if [[ -n "$LOCK_ID" ]]; then
log "Force unlocking with ID: $LOCK_ID"
terraform force-unlock -force "$LOCK_ID" || log_warning "Force unlock failed, continuing anyway"
fi
fi
cd -

# Apply Terraform configuration with retry logic
log "Applying gitlab infra resources..."

Expand Down Expand Up @@ -99,6 +113,20 @@ main() {
# Initialize Terraform with S3 backend
initialize_terraform "bootstrap" "$DEPLOY_SCRIPTDIR"

# Check for and clear any stale locks
cd "$DEPLOY_SCRIPTDIR"
if terraform state list &>/dev/null; then
log "State accessible, no lock issues"
else
log_warning "State lock detected, attempting to force unlock"
LOCK_ID=$(terraform force-unlock -force 2>&1 | grep -oP 'Lock ID: \K[a-f0-9-]+' || echo "")
if [[ -n "$LOCK_ID" ]]; then
log "Force unlocking with ID: $LOCK_ID"
terraform force-unlock -force "$LOCK_ID" || log_warning "Force unlock failed, continuing anyway"
fi
fi
cd -

# Apply Terraform configuration with retry mechanism
deploy_bootstrap_stack() {
local max_attempts=3
Expand Down
26 changes: 25 additions & 1 deletion platform/infra/terraform/common/destroy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ main() {
initialize_terraform "boostrap" "$DEPLOY_SCRIPTDIR"

cd "$DEPLOY_SCRIPTDIR" # Get into common stack directory

# Check for and clear any stale locks
if terraform state list &>/dev/null; then
log "State accessible, no lock issues"
else
log_warning "State lock detected, attempting to force unlock"
LOCK_ID=$(terraform force-unlock -force 2>&1 | grep -oP 'Lock ID: \K[a-f0-9-]+' || echo "")
if [[ -n "$LOCK_ID" ]]; then
log "Force unlocking with ID: $LOCK_ID"
terraform force-unlock -force "$LOCK_ID" || log_warning "Force unlock failed, continuing anyway"
fi
fi

# Remove GitLab resources from state, if they exist
if ! terraform state rm gitlab_personal_access_token.workshop || ! terraform state rm data.gitlab_user.workshop; then
log_warning "GitLab resources not found in state"
Expand All @@ -68,7 +81,18 @@ main() {
-var="git_password=${USER1_PASSWORD}" \
-var="working_repo=${WORKING_REPO}" \
-auto-approve -refresh=false; then
log_warning "Bootstrap stack destroy failed, trying again"
log_warning "Bootstrap stack destroy failed, checking for lock issues"

# Extract lock ID from error if present
cd "$DEPLOY_SCRIPTDIR"
LOCK_ID=$(terraform plan 2>&1 | grep -oP 'ID:\s+\K[a-f0-9-]+' | head -1 || echo "")
if [[ -n "$LOCK_ID" ]]; then
log "Forcing unlock with ID: $LOCK_ID"
terraform force-unlock -force "$LOCK_ID" || true
fi
cd -

log_warning "Retrying destroy after lock handling"
if ! terraform -chdir=$DEPLOY_SCRIPTDIR destroy \
-var-file="${GENERATED_TFVAR_FILE}" \
-var="gitlab_domain_name=${GITLAB_DOMAIN:-""}" \
Expand Down
Loading