leg100/pug

Auto detect and download terraform

Closed this issue · 12 comments

Automatically detect required terraform version and download.

Food for tough, for the last coupld of years I have been doing this with bash by looking at the terraform -> required_version. It works pretty well and does not require custom file.

Another option, use version manager such as https://github.com/tfutils/tfenv or https://github.com/tofuutils/tofuenv . They are able to read the required_version attributes by design. Additionally these tools can install latest, ENV VAR value, or .terraform-version/.tofu-version if so desired.

Just my .2c, I don't think this feature should be part of pug. I like the Unix philosophy of combining simple tools.
Been using asdf for years now btw.

Maybe what could be useful is an arbitrary command runner in a selected list of directories, with the ability to sequence or parallelize tasks. Would allow tfenv / tgenv / asdf install en masse or terraform init -upgrade (which I am missing too)

What do you think?

Just my .2c, I don't think this feature should be part of pug. I like the Unix philosophy of combining simple tools.
Been using asdf for years now btw.

I completely agree. I've been meaning to write something in the README to this effect and then close this issue.

Maybe what could be useful is an arbitrary command runner in a selected list of directories, with the ability to sequence or parallelize tasks. Would allow tfenv / tgenv / asdf install en masse or terraform init -upgrade (which I am missing too)

I'm not sure what you mean, can you elaborate?

Maybe what could be useful is an arbitrary command runner in a selected list of directories, with the ability to sequence or parallelize tasks. Would allow tfenv / tgenv / asdf install en masse or terraform init -upgrade (which I am missing too)

I'm not sure what you mean, can you elaborate?

I mean that you implemented classic terraform actions like init / plan / apply. What if I want to run something else in the directory of a set of stacks (or root modules in pug) ? It would bypass the --program switch and result in executing a command of my choice.

Example: I want to download and install terraform which version is controlled by a version manager. The simple use case is when there is only one configuration file at the root of the repository. As a developer, I just have to run asdf install terraform or tfenv install or whatever your version manager dictactes. There is not much need for an additional tool here. But there is a more complex scenario where there are many version manager configuration files. For instance, you can override a terraform version configuration in a subdirectory. The version manager does the version resolution based on the working directory. Say you have /.terraform-version = 1.7.5 and /this/old/dev/stack/.terraform-version = 1.0.11. It would be handy to run asdf install terraform or tfenv install... in each directory with pug.

For the init -upgrade command, this feature would also help. It is not part of the keybindings as far as I have seen so upgrading providers and modules on a large number of stacks is not possible with pug.

Makes more sense? Sorry if it doesn't, english is only my second language.

Note that I have worked around this limitation augmenting terragrunt feature with this quick and dirty script: https://gist.github.com/ohmer/40e61ed16afe5d9a46b5de0b7fd6a7d8

I do not have this limitation when using terramate. terramate run <COMMAND> executes <COMMAND> in each stack directories while terragrunt run-all <COMMAND> executes terraform <COMMAND>. For instance I can execute tflint with terramate run tflint

Yup, makes sense. I was under the misapprehension that adsf auto-downloaded tools if the specified version isn't installed. (I've only dabbled with asdf in the past).

Just to need to think how best to implement this.

There could be a key to invoke an abitrary command on the selected modules, e.g. X, which when pressed prompts the user to specify the full command to execute.

Or could allow user to map their own keys in the startup configuration, e.g. in the config file:

keys:
  X: asdf install terraform
  I: terraform init -upgrade

Ability to add key mapping via config and invoke abitrary commands sounds like great features, I don't see them as mutually exclusive. The ability to run with a maximum concurrency of 1 is important for some commands like terraform init. Wish I could help, I can't fkin write go...

Added section to README, explaining use of asdf or mise in tandem with Pug to download and install versions terraform.

@ohmer https://github.com/leg100/pug/releases/tag/v0.5.2 maps x to arbitrary execution, and u to terraform init -upgrade.

I'll implement user-customisable mappings later on.

Just tested both features. Just works, thanks a lot @leg100 !