tfutils / tfenv

Terraform version manager
MIT License
4.51k stars 454 forks source link

`tfenv install` fails if Terraform is already installed, but Hashicorp's CDN is unavailable #395

Closed don-code closed 9 months ago

don-code commented 1 year ago

The releases.hashicorp.com site was down briefly this morning. We pre-bake needed Terraform versions into our CI/CD executors, but still run a tfenv install ... command to make local dev easier.

Since Hashicorp releases are immutable, I'd expect that if a given revision of Terraform is already installed, tfenv wouldn't have any need to reach out to their CDN. Instead, it would detect that this version is already installed, and pass.

If releases.hashicorp.com is down, and a requested version of Terraform is already installed, tfenv will fail instead:

bash-5.2# tfenv list
  1.4.6
No default set. Set with 'tfenv use <version>'

bash-5.2# tfenv install 1.4.6
curl: (7) Failed to connect to releases.hashicorp.com port 443 after 0 ms: Couldn't connect to server
Failed to download remote versions from https://releases.hashicorp.com/terraform/
No versions matching '1.4.6' found in remote
bash-5.2# echo $?
1

Here's a test case with Docker to reproduce:

# on local machine
$ docker run -it --rm bash

# inside the container - set up the environment
$ apk add curl git
$ git clone --depth=1 https://github.com/tfutils/tfenv.git ~/.tfenv
$ export PATH=~/.tfenv/bin:$PATH

# install Terraform
$ tfenv install 1.4.6

# make releases.hashicorp.com resolve to localhost, to fake an outage
$ echo "127.0.0.1 releases.hashicorp.com" > /etc/hosts

# try to install again - this step should succeed, but currently fails
$ tfenv install 1.4.6