drsrq26482 2017-03-19 20:42
浏览 35
已采纳

在提供流浪汉期间如何安装golang软件包?

There are similar questions about how to install golang with vagrant provision or how to set $GOPATH environment variable, such that it will be available on boot.

However, this question is about how you can set $GOPATH, etc, such that the variable is available during later steps of the provisioning script (and everything else important about golang is in-place for that matter). I'm using an ubuntu image.

Specifically, I want to run these steps in my_provision.sh:

go get github.com/saymedia/terraform-s3-dir
go install github.com/saymedia/terraform-s3-dir

such that when I run the following command after vagrant ssh, it will just work:

terraform-s3-dir

When the go get/install commands run in my_provision.sh, it outputs one of two things: silence, or complaints that the GOPATH var is not set. Either way, when I boot in, terraform-s3-dir is not a recognised command.

I've tried setting the GOPATH, GOROOT and PATH variables in several different ways:

Setting them in my_provision.sh directly (with and without export), echoing export commands into .bashrc. echoing exporting commands to /etc/profile.d/gopath.sh. These all results in "$GOPATH not set".

running exports in an inline provisioning script, prior to my_provision.sh. This resulted in silent failure, unless I try to run the go get/install commands with sudo, in which case it has the $GOPATH error, too.

If I echo the GOPATH variable in my_provision.sh immediately before the go get/install commands, the variables show that they are set as expected.

If I run the go get/install commands manually from the shell after vagrant ssh, then it installs successfully.

What is different about the vagrant ssh context, vs the provisioning script context, where the go get/install commands get the desired result in the former, but not the latter?

  • 写回答

1条回答 默认 最新

  • duan5731 2017-03-19 22:54
    关注

    The problem I was having is that in the context of the provisioning script, $HOME refers to the /root/ folder, rather than /home/ubuntu/

    It pays to NOT rely on the $HOME variable when setting environment variables during provisioning, if you want to use those variables later in the provisioning scripts.

    Here is my solution (from VagrantFile)

    config.vm.provision "shell", inline: <<-SHELL
        echo -n                                          >  /etc/profile.d/gopath.sh
        echo 'export GOROOT=/usr/lib/go'                 >> /etc/profile.d/gopath.sh
        echo 'export GOPATH=/home/ubuntu/go'             >> /etc/profile.d/gopath.sh
        echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin' >> /etc/profile.d/gopath.sh
    SHELL
    devbox.vm.provision "shell", path: "scripts/my_provision.sh"
    

    After this, this GOPATH is picked up correctly by go get and go install

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里