简体   繁体   English

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

[英]How to install a golang package during vagrant provision?

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. 关于如何使用无业游民的条款安装golang或如何设置$GOPATH环境变量,以便在启动时可用,存在类似的问题。

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). 但是,这个问题是关于如何设置$GOPATH等的,以便在供应脚本的后续步骤中可以使用该变量(关于golang的其他所有重要事项都已就位)。 I'm using an ubuntu image. 我正在使用Ubuntu图片。

Specifically, I want to run these steps in my_provision.sh : 具体来说,我想在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: 这样,当我在vagrant ssh之后运行以下命令时,它将正常工作:

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. 当go get / install命令在my_provision.sh运行时,它将输出以下两项之一:静音或抱怨未设置GOPATH Either way, when I boot in, terraform-s3-dir is not a recognised command. 无论哪种方式,当我启动时, terraform-s3-dir都不是公认的命令。

I've tried setting the GOPATH , GOROOT and PATH variables in several different ways: 我尝试以几种不同的方式设置GOPATHGOROOTPATH变量:

Setting them in my_provision.sh directly (with and without export ), echoing export commands into .bashrc. 直接在my_provision.sh设置它们(带有和不带有export ),并将导出命令回显到.bashrc中。 echoing exporting commands to /etc/profile.d/gopath.sh. 将导出命令回显到/etc/profile.d/gopath.sh。 These all results in "$GOPATH not set". 所有这些都会导致“未设置$ GOPATH”。

running exports in an inline provisioning script, prior to my_provision.sh . 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. 这导致了静默失败,除非我尝试使用sudo运行go get / install命令,在这种情况下,它$GOPATH出现$GOPATH错误。

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. 如果我在go get / install命令之前立即在my_provision.sh回显GOPATH变量,则该变量表明已按预期设置它们。

If I run the go get/install commands manually from the shell after vagrant ssh , then it installs successfully. 如果我在vagrant ssh之后从外壳手动运行go get / install命令,那么它将成功安装。

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? vagrant ssh上下文和配置脚本上下文有什么不同,在后者中,go get / install命令获得了所需的结果,而后者却没有?

The problem I was having is that in the context of the provisioning script, $HOME refers to the /root/ folder, rather than /home/ubuntu/ 我遇到的问题是,在配置脚本的上下文中, $HOME指向/root/文件夹,而不是/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. 如果要稍后在供应脚本中使用这些变量,则在设置供应过程中设置环境变量时不必依赖$HOME变量。

Here is my solution (from VagrantFile ) 这是我的解决方案(来自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 此后,通过go get and go install正确拾取该GOPATH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM