Skip to content

Commit f9612ff

Browse files
authored
enabling retry function to resolve failures due to lock on apt (#155)
* enabling retry
1 parent 8e5d617 commit f9612ff

File tree

11 files changed

+69
-7
lines changed

11 files changed

+69
-7
lines changed

modules/runtime_container_engine_config/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,6 @@ variable "vault_secret_id" {
345345
}
346346

347347
variable "vault_token_renew" {
348-
type = number
348+
type = number
349349
description = "Vault token renewal period in seconds. Required when TFE_VAULT_USE_EXTERNAL is true."
350350
}

modules/tfe_init/functions.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,10 @@ locals {
1616
distribution = var.distribution
1717
enable_monitoring = var.enable_monitoring != null ? var.enable_monitoring : false
1818
})
19+
1920
quadlet_unit = templatefile("${path.module}/templates/terraform-enterprise.kube.tpl", {})
21+
22+
retry = templatefile("${path.module}/templates/retry.func", {
23+
cloud = var.cloud
24+
})
2025
}

modules/tfe_init/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ locals {
4545
get_base64_secrets = local.get_base64_secrets
4646
install_packages = local.install_packages
4747
install_monitoring_agents = local.install_monitoring_agents
48+
retry = local.retry
4849
quadlet_unit = local.quadlet_unit
4950

5051
active_active = var.operational_mode == "active-active"

modules/tfe_init/templates/install_packages.func

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ function install_packages {
1616
systemctl start firewalld
1717
%{ else ~}
1818
echo "[$(date +"%FT%T")] [Terraform Enterprise] Install unzip with apt-get" | tee -a $log_pathname
19-
apt-get update -y
20-
apt-get install -y unzip
19+
retry 5 apt-get update -y
20+
retry 5 apt-get install -y unzip
2121
%{ endif ~}
2222

2323
echo "[$(date +"%FT%T")] [Terraform Enterprise] Install AWS CLI" | tee -a $log_pathname
@@ -39,4 +39,4 @@ function install_packages {
3939
install_packages () {
4040
:
4141
}
42-
%{ endif ~}
42+
%{ endif ~}

modules/tfe_init/templates/retry.func

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Retry a command up to a specific numer of times until it exits successfully,
2+
# with exponential back off.
3+
#
4+
# $ retry 5 echo Hello
5+
6+
function retry {
7+
local retries=$1
8+
shift
9+
10+
local count=0
11+
until "$@"; do
12+
exit=$?
13+
wait=$((2 ** $count))
14+
count=$(($count + 1))
15+
if [ $count -lt $retries ]; then
16+
echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
17+
sleep $wait
18+
else
19+
echo "Retry $count/$retries exited $exit, no more retries left."
20+
return $exit
21+
fi
22+
done
23+
return 0
24+
25+
}

modules/tfe_init/templates/tfe.sh.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22
set -eu pipefail
3-
3+
${retry}
44
${get_base64_secrets}
55
${install_packages}
66
%{ if enable_monitoring ~}

modules/tfe_init_replicated/functions.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ locals {
1616
distribution = var.distribution
1717
enable_monitoring = var.enable_monitoring != null ? var.enable_monitoring : false
1818
})
19+
20+
retry = templatefile("${path.module}/templates/retry.func", {
21+
cloud = var.cloud
22+
})
1923
}

modules/tfe_init_replicated/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ locals {
1111
get_base64_secrets = local.get_base64_secrets
1212
install_packages = local.install_packages
1313
install_monitoring_agents = local.install_monitoring_agents
14+
retry = local.retry
1415

1516
# Configuration data
1617
active_active = var.tfe_configuration != null ? var.tfe_configuration.enable_active_active.value == "1" ? true : false : null

modules/tfe_init_replicated/templates/install_packages.func

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ function install_packages {
2929
systemctl start firewalld
3030
%{ else ~}
3131
echo "[$(date +"%FT%T")] [Terraform Enterprise] Install unzip with apt-get" | tee -a $log_pathname
32-
apt-get update -y
33-
apt-get install -y unzip
32+
retry 5 apt-get update -y
33+
retry 5 apt-get install -y unzip
3434
%{ endif ~}
3535

3636
echo "[$(date +"%FT%T")] [Terraform Enterprise] Install AWS CLI" | tee -a $log_pathname
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Retry a command up to a specific numer of times until it exits successfully,
2+
# with exponential back off.
3+
#
4+
# $ retry 5 echo Hello
5+
6+
function retry {
7+
local retries=$1
8+
shift
9+
10+
local count=0
11+
until "$@"; do
12+
exit=$?
13+
wait=$((2 ** $count))
14+
count=$(($count + 1))
15+
if [ $count -lt $retries ]; then
16+
echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
17+
sleep $wait
18+
else
19+
echo "Retry $count/$retries exited $exit, no more retries left."
20+
return $exit
21+
fi
22+
done
23+
return 0
24+
25+
}

modules/tfe_init_replicated/templates/tfe_replicated.sh.tpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env bash
22
set -euo pipefail
33

4+
${retry}
45
${get_base64_secrets}
56
${install_packages}
67
${install_monitoring_agents}

0 commit comments

Comments
 (0)