Skip to content

enabling retry function to resolve failures due to lock on apt #155

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

Merged
merged 2 commits into from
May 29, 2024
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
2 changes: 1 addition & 1 deletion modules/runtime_container_engine_config/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,6 @@ variable "vault_secret_id" {
}

variable "vault_token_renew" {
type = number
type = number
description = "Vault token renewal period in seconds. Required when TFE_VAULT_USE_EXTERNAL is true."
}
5 changes: 5 additions & 0 deletions modules/tfe_init/functions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ locals {
distribution = var.distribution
enable_monitoring = var.enable_monitoring != null ? var.enable_monitoring : false
})

quadlet_unit = templatefile("${path.module}/templates/terraform-enterprise.kube.tpl", {})

retry = templatefile("${path.module}/templates/retry.func", {
cloud = var.cloud
})
}
1 change: 1 addition & 0 deletions modules/tfe_init/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ locals {
get_base64_secrets = local.get_base64_secrets
install_packages = local.install_packages
install_monitoring_agents = local.install_monitoring_agents
retry = local.retry
quadlet_unit = local.quadlet_unit

active_active = var.operational_mode == "active-active"
Expand Down
6 changes: 3 additions & 3 deletions modules/tfe_init/templates/install_packages.func
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function install_packages {
systemctl start firewalld
%{ else ~}
echo "[$(date +"%FT%T")] [Terraform Enterprise] Install unzip with apt-get" | tee -a $log_pathname
apt-get update -y
apt-get install -y unzip
retry 5 apt-get update -y
retry 5 apt-get install -y unzip
%{ endif ~}

echo "[$(date +"%FT%T")] [Terraform Enterprise] Install AWS CLI" | tee -a $log_pathname
Expand All @@ -39,4 +39,4 @@ function install_packages {
install_packages () {
:
}
%{ endif ~}
%{ endif ~}
25 changes: 25 additions & 0 deletions modules/tfe_init/templates/retry.func
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Retry a command up to a specific numer of times until it exits successfully,
# with exponential back off.
#
# $ retry 5 echo Hello

function retry {
local retries=$1
shift

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

}
2 changes: 1 addition & 1 deletion modules/tfe_init/templates/tfe.sh.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
set -eu pipefail

${retry}
${get_base64_secrets}
${install_packages}
%{ if enable_monitoring ~}
Expand Down
4 changes: 4 additions & 0 deletions modules/tfe_init_replicated/functions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ locals {
distribution = var.distribution
enable_monitoring = var.enable_monitoring != null ? var.enable_monitoring : false
})

retry = templatefile("${path.module}/templates/retry.func", {
cloud = var.cloud
})
}
1 change: 1 addition & 0 deletions modules/tfe_init_replicated/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ locals {
get_base64_secrets = local.get_base64_secrets
install_packages = local.install_packages
install_monitoring_agents = local.install_monitoring_agents
retry = local.retry

# Configuration data
active_active = var.tfe_configuration != null ? var.tfe_configuration.enable_active_active.value == "1" ? true : false : null
Expand Down
4 changes: 2 additions & 2 deletions modules/tfe_init_replicated/templates/install_packages.func
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ function install_packages {
systemctl start firewalld
%{ else ~}
echo "[$(date +"%FT%T")] [Terraform Enterprise] Install unzip with apt-get" | tee -a $log_pathname
apt-get update -y
apt-get install -y unzip
retry 5 apt-get update -y
retry 5 apt-get install -y unzip
%{ endif ~}

echo "[$(date +"%FT%T")] [Terraform Enterprise] Install AWS CLI" | tee -a $log_pathname
Expand Down
25 changes: 25 additions & 0 deletions modules/tfe_init_replicated/templates/retry.func
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Retry a command up to a specific numer of times until it exits successfully,
# with exponential back off.
#
# $ retry 5 echo Hello

function retry {
local retries=$1
shift

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

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env bash
set -euo pipefail

${retry}
${get_base64_secrets}
${install_packages}
${install_monitoring_agents}
Expand Down
Loading