Skip to content
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

tfswitch to enable querying current binary install location and list installed binaries #270

Open
krish7919 opened this issue Sep 7, 2022 · 2 comments
Assignees
Labels
enhancement Refactor existing code for better performance and quality

Comments

@krish7919
Copy link

Recently, in our automated CI, when using a new hardened docker image to test things, I see the log:

[source] Executing: tfswitch 1.1.2
++++++ tfswitch 1.1.2
Creating directory for terraform binary at: /var/lib/postgresql/.terraform.versions
Unable to write to: /usr/local/bin/terraform
Creating bin directory at: /var/lib/postgresql/bin
Creating directory for terraform binary at: /var/lib/postgresql/bin
RUN `export PATH=$PATH:/var/lib/postgresql/bin` to append bin to $PATH
Downloading to: /var/lib/postgresql/.terraform.versions
18687805 bytes downloaded
Switched terraform to version "1.1.2" 

Obviously, tfswitch is smart enough to know that it cannot write to /usr/local/bin/terraform and then falls back to using the logged in user's $HOME/bin directory to create the symlink to the Terraform version.
However, after that it expects a manual run of the command on the console like so RUN export PATH=$PATH:/var/lib/postgresql/bin to append bin to $PATH, and does indeed prompt the user to do so.

Describe the solution you'd like

Proposal

Add more sub-command options to tfswitch to:

  1. list the current directory where .terraform.versions is created. I would propose tfswitch --local-binary-dir or something similar.
  2. manipulate the PATH variable to run set PATH=/var/lib/postgresql/bin:$PATH in case this permission denied error is hit. Note the binary/symlink PATH is prefixed to the PATH variable and not suffixed.

Describe alternatives you've considered
Currently, we have code to check the dir location manually and do all this in a dirty shell script, which frankly can be improved by a lot if the above proposal is accepted.

@krish7919 krish7919 added the enhancement Refactor existing code for better performance and quality label Sep 7, 2022
@MatthewJohn
Copy link
Collaborator

MatthewJohn commented May 24, 2024

Hey @krish7919,

From my understanding, you're wanting to run tfswitch, which ends up placing a binary in directory that is not part of PATH and then want the PATH environment variable of the shell to be updated?

If so, as far as my understanding goes, this isn't possible.
When you execute a command on linux, it performs a "fork" , which spawns a sub-process that copies (among other things) the environment variables from the parent process.
However, any changes to the environment variable are not persisted to the parent process.

This can be shown in this example:

$ cat main.go 
package main

import "os"

func main() {
	os.Setenv("TEST", "HI")
}
$ env | grep -i test
$ go run ./main.go
$ env | grep -i test

The easiest way to achieve this, I think, would be to:

  • create your own directory first (e.g. ~/.bin),
  • run tfswitch with the --bin flag ^1,
  • set PATH environment variable with your chosen directory

e.g.

# Define and create bin directory for Terraform
TERRAFORM_BIN_DIR=/tmp/terraform_bin
mkdir $TERRAFORM_BIN_DIR

# Run tfswitch specifying the bin directory
tfswitch --bin=$TERRAFORM_BIN_DIR

# Set environment variable to allow execution of 'terraform'
export PATH=$PATH:$TERRAFORM_BIN_DIR

terraform --version

^1

 -b, --bin=value    Custom binary path. Ex: tfswitch -b
                    /Users/username/bin/terraform

It's worth noting that in your environment /var/lib/postgresql is the HOME of the current user, which is why it's using this directory.

Hope this helps!

Matt

@yermulnik
Copy link
Collaborator

  • list the current directory where .terraform.versions is created. I would propose tfswitch --local-binary-dir or something similar.

Relates to #424

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Refactor existing code for better performance and quality
Projects
None yet
Development

No branches or pull requests

4 participants