How to install a golang package during vagrant provision?

huangapple go评论77阅读模式
英文:

How to install a golang package during vagrant provision?

问题

有类似的问题关于如何使用vagrant provision安装golang或如何设置$GOPATH环境变量,以便在启动时可用。

然而,这个问题是关于如何设置$GOPATH等,以便在后续的provisioning脚本步骤中该变量是可用的(以及关于golang的其他重要设置)。我正在使用一个ubuntu镜像。

具体来说,我想在my_provision.sh中运行以下步骤:

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

这样,当我在vagrant ssh之后运行以下命令时,它将正常工作:

terraform-s3-dir

my_provision.sh中的go get/install命令运行时,它会输出以下两种结果之一:静默或者投诉GOPATH变量未设置。无论哪种情况,当我启动后,terraform-s3-dir都不是一个被识别的命令。

我尝试了几种不同的方式来设置GOPATHGOROOTPATH变量:

直接在my_provision.sh中设置它们(带或不带export),将export命令输出到.bashrc文件中,将export命令输出到/etc/profile.d/gopath.sh文件中。所有这些都导致"$GOPATH未设置"的错误。

在inline provisioning脚本中运行export命令,在运行my_provision.sh之前。这导致静默失败,除非我尝试使用sudo运行go get/install命令,此时也会出现$GOPATH错误。

如果我在my_provision.sh中的go get/install命令之前立即输出GOPATH变量,变量显示它们按预期设置。

如果我在vagrant ssh之后手动从shell运行go get/install命令,那么它会成功安装。

vagrant ssh上下文与provisioning脚本上下文有何不同,导致go get/install命令在前者中获得期望的结果,而在后者中没有呢?

英文:

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

得分: 0

我遇到的问题是,在配置脚本的上下文中,$HOME 指的是 /root/ 文件夹,而不是 /home/ubuntu/

如果你想在配置脚本中稍后使用这些变量,最好不要依赖 $HOME 变量来设置环境变量。

以下是我的解决方案(来自 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"

在这之后,go getgo install 正确地使用了这个 GOPATH 变量。

英文:

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 &quot;shell&quot;, inline: &lt;&lt;-SHELL
    echo -n                                          &gt;  /etc/profile.d/gopath.sh
    echo &#39;export GOROOT=/usr/lib/go&#39;                 &gt;&gt; /etc/profile.d/gopath.sh
    echo &#39;export GOPATH=/home/ubuntu/go&#39;             &gt;&gt; /etc/profile.d/gopath.sh
    echo &#39;export PATH=$PATH:$GOROOT/bin:$GOPATH/bin&#39; &gt;&gt; /etc/profile.d/gopath.sh
SHELL
devbox.vm.provision &quot;shell&quot;, path: &quot;scripts/my_provision.sh&quot;

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

huangapple
  • 本文由 发表于 2017年3月20日 04:42:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/42892030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定