terraform-switcher icon indicating copy to clipboard operation
terraform-switcher copied to clipboard

snap package cannot use "~/.terraform.version" and "/usr/local/bin"

Open strowk opened this issue 4 years ago • 8 comments

I've installed package from snap and running tfswitch -b $HOME/bin , it gives me

Error while creating /home/user/.terraform.versions/terraform_0.13.5_linux_amd64.zip - open /home/user/.terraform.versions/terraform_0.13.5_linux_amd64.zip: permission denied

Running just tfswitch --latest complains about /usr/local/bin directory . Both places are not available from usual snap packages.

strowk avatar Dec 07 '20 14:12 strowk

  1. Are you running tfswitch -b $HOME/bin/terraform or tfswitch -b $HOME/bin ?
  2. Do you have permission to write to the $HOME/bin directory (what do you see when you run ls -l on $HOME?

warrensbox avatar Dec 10 '20 20:12 warrensbox

  1. both give same result
  2. I am owner of $HOME/bin:
whoami
tim

ls -la $HOME | grep ' bin'
drwxrwxr-x  3 tim  tim   4096 Dez 14 16:50 bin

Note:

I've uninstalled tfswitch installed using Troubleshooting guilde and installed snap package again to reproduce it again

strowk avatar Dec 14 '20 16:12 strowk

From what I see here https://snapcraft.io/docs/home-interface https://forum.snapcraft.io/t/access-to-specific-hidden-file-path-in-users-home/6948 it looks like

  1. problem is in ~/.terraform.versions
  2. snap does not allow to use hidden (dot-prefixed) dirs in home

So either tfswitch in snap package should automatically use different folder (maybe ~/snap/tfswitch ? I see this folder has been created by snap) or at least allow to customize it.

strowk avatar Dec 14 '20 16:12 strowk

@strowk perhaps I can allow users to customize it. Thank you for bringing this up. I will find some time to do this before the end of the year. Hopefully, I can release this fix in January. In the source code: -

getInstallLocation() //only downloads the terraform bins into the home directory in .terraform.versions
.
.
.
installLocation = usr.HomeDir + installPath //where installPath = "/.terraform.versions/"

I can make this getInstallLocation() customizable where users can provide a location for all terraform binaries. Again, I can only get to this by the end of the year. If you know go and would like to contribute, I would be happy to review and merge your code as well.

I will let you know once I have implemented this. Thank you for bringing this up.

warrensbox avatar Dec 14 '20 16:12 warrensbox

Just want to add one note here - I see it would go better if either there would be note in installation doc and in description here https://snapcraft.io/tfswitch , that snap package requires additional configuration, or binary should somehow understand that it's running as a snap package and change path automatically. Otherwise everyone who install snap package would found themselves in this issue :)

I suspect that detection run from snap should not be very hard. Probably checking any of these env vars would do - https://snapcraft.io/docs/environment-variables

For example SNAP_USER_COMMON seems very convenient for this task -

Directory for user data that is common across revisions of a snap.

Basically it seems that following algo would work nicely -

  1. if custom location is set - use it
  2. else check if SNAP_USER_COMMON is set. If set - use it plus /.terraform.versions/
  3. else use usr.HomeDir plus /.terraform.versions/

I can try to do it, does not look very hard

strowk avatar Dec 14 '20 20:12 strowk

Is there any progress on this? I would actually like to use a snap version of tfswitch :+1:

applike-ss avatar Feb 15 '21 11:02 applike-ss

I am requesting snapcraft for access to user home: https://forum.snapcraft.io/t/request-interface-personal-files-read-write/25277/3

If you all can upvote the issue, it would be great!

warrensbox avatar Jul 13 '21 21:07 warrensbox

Hi, just wanted to leave my 2ct here: Using Gitlab for ci/cd. Gitlab allows specifying a cache from the build-dir (has to be withing the build-dir!). It would be very helpful if there was a cli-parameter to allow changing ~/.terraform.versions/ to e.g. $PWD/.terraform.versions. This way the executables could be cached via Gitlabs mechanism, and there is no/less need to download these from external ressources in every pipeline.

In gitlab-ci.yml this would look sth like this;

.plan:
  stage: plan
  allow_failure: false
  cache:
    key: "${CI_COMMIT_REF_SLUG}"
    paths:
      - ${CI_PROJECT_PATH}/.terraform.versions
  script:
    - tfswitch -cache=$CI_PROJECT_PATH/.terraform.versions
    - terraform init
    - terraform validate
...

strowi avatar Sep 06 '21 08:09 strowi

I also have a need to be able to set a custom the location for the .terraform.versions directory not related to snap.

toast-gear avatar Dec 15 '22 21:12 toast-gear

any progress on this one?

strowi avatar Nov 14 '23 19:11 strowi