|
14 | 14 | # See the License for the specific language governing permissions and |
15 | 15 | # limitations under the License. |
16 | 16 |
|
17 | | -set -ex |
| 17 | +set -eux |
18 | 18 |
|
19 | 19 | # XXX: In general, accepting so many flags is a bad design. However this |
20 | 20 | # script will only be called by automation. |
|
41 | 41 | exit 1 |
42 | 42 | fi |
43 | 43 |
|
44 | | -# Create/Update the deployment |
45 | | -retry_count=3 |
46 | | -n=0 |
47 | | -until [ "$n" -ge ${retry_count} ]; do |
48 | | - terraform init -upgrade && \ |
49 | | - script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" |
50 | | - terraform init -upgrade -chdir="${script_dir}" && \ |
51 | | - terraform apply -chdir="${script_dir}" \ |
52 | | - -var="project_id=${project_id}" \ |
53 | | - -var="deployment_name=${cluster}" \ |
54 | | - -var="zone=${zone}" \ |
55 | | - -var="network=${network}" \ |
56 | | - -var="initial_node_count=${node_count}" \ |
57 | | - -var="machine_type=${machine_type}" \ |
58 | | - -var="image_type=${image_type}" \ |
59 | | - -var="release_channel=${release_channel}" \ |
60 | | - -auto-approve && \ |
61 | | - break |
| 44 | +function retry() { |
| 45 | + local retries=$1 |
| 46 | + local wait=$2 |
62 | 47 |
|
63 | | - n=$((n + 1)) |
64 | | - echo -e "import random\n\nimport time\ntime.sleep(random.randint(30,90))" | python3 |
65 | | -done |
| 48 | + # Remove the first two arguments (retries and wait) to isolate the command |
| 49 | + shift 2 |
66 | 50 |
|
67 | | -if [ ${n} = ${retry_count} ]; then |
68 | | - echo "create deployment failed too many times (${retry_count})" |
69 | | - exit 1 |
70 | | -fi |
| 51 | + local count=0 |
| 52 | + until "$@"; do |
| 53 | + exit_code=$? |
| 54 | + count=$((count + 1)) |
| 55 | + if [ $count -ge $retries ]; then |
| 56 | + echo "Command \"$1\" failed after $retries attempts." |
| 57 | + return $exit_code |
| 58 | + fi |
| 59 | + echo "Command failed. Retrying in $wait seconds... (Attempt $count/$retries)" |
| 60 | + sleep $wait |
| 61 | + done |
| 62 | + return 0 |
| 63 | +} |
| 64 | + |
| 65 | +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" |
| 66 | + |
| 67 | +echo "Initializing Terraform..." |
| 68 | +terraform -chdir="${script_dir}" init -upgrade |
| 69 | + |
| 70 | +echo "Applying Terraform configuration..." |
| 71 | +retry 15 60 terraform -chdir="${script_dir}" apply \ |
| 72 | + -var="project_id=${project_id}" \ |
| 73 | + -var="deployment_name=${cluster}" \ |
| 74 | + -var="zone=${zone}" \ |
| 75 | + -var="network=${network}" \ |
| 76 | + -var="initial_node_count=${node_count}" \ |
| 77 | + -var="machine_type=${machine_type}" \ |
| 78 | + -var="image_type=${image_type}" \ |
| 79 | + -var="release_channel=${release_channel}" \ |
| 80 | + -auto-approve |
| 81 | +echo "Terraform apply completed successfully." |
0 commit comments