go install在作为cloudinit userdata脚本运行时,不会将二进制文件添加到GOBIN下。

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

go install does not add the binary under GOBIN when running as cloudinit userdata script

问题

我正在尝试安装这个包:github.com/czerwonk/bird_exporter
在安装了golang之后,我这样操作:

  1. GO_VERSION=1.17.1
  2. curl -L https://golang.org/dl/go${GO_VERSION}.linux-amd64.tar.gz -o go${GO_VERSION}.linux-amd64.tar.gz
  3. tar -C /usr/local -xzf go1.17.1.linux-amd64.tar.gz
  4. mkdir /root/go
  5. export GOPATH=/root/go
  6. export PATH=${PATH}:/usr/local/go/bin:${GOPATH}/bin
  7. echo "export GOPATH=${GOPATH}" >> /root/.bashrc
  8. echo "export PATH=${PATH}:/usr/local/go/bin:${GOPATH}/bin" >> /root/.bashrc

我这样安装包:

  1. BIRD_EXPORTER_VERSION=1.2.6
  2. go install github.com/czerwonk/bird_exporter@${BIRD_EXPORTER_VERSION}

当我在EC2实例上手动运行所有这些命令时,最终在默认的GOBIN位置下安装了该包:

  1. file /root/go/bin/bird_exporter
  2. /root/go/bin/bird_exporter: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), not stripped

但是当相同的命令在userdata脚本中由cloudinit执行时,包会被安装在/root/go/pkg/下,但是/root/go/bin/下没有任何内容。
日志显示所有包的下载都正常:

  1. go: downloading github.com/czerwonk/bird_exporter v0.0.0-20210702094413-c9985a3895a4
  2. go: downloading github.com/prometheus/client_golang v1.11.0
  3. go: downloading github.com/sirupsen/logrus v1.6.0
  4. go: downloading github.com/czerwonk/bird_socket v0.0.0-20190111125930-6c39d61f8853
  5. go: downloading github.com/prometheus/client_model v0.2.0
  6. go: downloading github.com/prometheus/common v0.29.0
  7. go: downloading github.com/beorn7/perks v1.0.1
  8. go: downloading github.com/cespare/xxhash/v2 v2.1.1
  9. go: downloading github.com/golang/protobuf v1.4.3
  10. go: downloading github.com/prometheus/procfs v0.6.0
  11. go: downloading golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40
  12. go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.1
  13. go: downloading google.golang.org/protobuf v1.26.0-rc.1

我想了解在这两种情况下go install的区别,以便我可以正确控制安装该包。

附注:已弃用的go get也有相同的结果。

英文:

I am trying to install this package: github.com/czerwonk/bird_exporter
After installing golang like so:

  1. GO_VERSION=1.17.1
  2. curl -L https://golang.org/dl/go${GO_VERSION}.linux-amd64.tar.gz -o go${GO_VERSION}.linux-amd64.tar.gz
  3. tar -C /usr/local -xzf go1.17.1.linux-amd64.tar.gz
  4. mkdir /root/go
  5. export GOPATH=/root/go
  6. export PATH=${PATH}:/usr/local/go/bin:${GOPATH}/bin
  7. echo "export GOPATH=${GOPATH}" >> /root/.bashrc
  8. echo "export PATH=${PATH}:/usr/local/go/bin:${GOPATH}/bin" >> /root/.bashrc

I install the package like so:

  1. BIRD_EXPORTER_VERSION=1.2.6
  2. go install github.com/czerwonk/bird_exporter@${BIRD_EXPORTER_VERSION}

When running all these commands on the EC2 instance manually, I end up with the package under the default GOBIN location:

  1. file /root/go/bin/bird_exporter
  2. /root/go/bin/bird_exporter: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), not stripped

When the same commands get executed by cloudinit in the userdata script, the packages get installed fine under /root/go/pkg/ but there is nothing under /root/go/bin/.
The logs show the downloading of all packages fine:

  1. go: downloading github.com/czerwonk/bird_exporter v0.0.0-20210702094413-c9985a3895a4
  2. go: downloading github.com/prometheus/client_golang v1.11.0
  3. go: downloading github.com/sirupsen/logrus v1.6.0
  4. go: downloading github.com/czerwonk/bird_socket v0.0.0-20190111125930-6c39d61f8853
  5. go: downloading github.com/prometheus/client_model v0.2.0
  6. go: downloading github.com/prometheus/common v0.29.0
  7. go: downloading github.com/beorn7/perks v1.0.1
  8. go: downloading github.com/cespare/xxhash/v2 v2.1.1
  9. go: downloading github.com/golang/protobuf v1.4.3
  10. go: downloading github.com/prometheus/procfs v0.6.0
  11. go: downloading golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40
  12. go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.1
  13. go: downloading google.golang.org/protobuf v1.26.0-rc.1

I would like to understand what the difference is for go install in these 2 scenarios so that I can have proper control over installing the package

P.S. go get, which is deprecated, has the same result.

答案1

得分: 3

这个问题已经解决了。
混淆的原因是日志输出很难读取,但最终我找到了这个:

  1. 构建缓存是必需的,但无法找到:未定义GOCACHE,也未定义$XDG_CACHE_HOME$HOME

当cloudinit运行时,HOME未设置。显然,Golang需要它来确定GOCACHE。我希望它在GOPATH下。

解决方案是同时导出GOCACHE

  1. export GOCACHE=/root/go/cache
英文:

This issue has been resolved.
The reason for the confusion was the log output as it was hard to read, but eventually I found this:

  1. build cache is required, but could not be located: GOCACHE is not defined and neither $XDG_CACHE_HOME nor $HOME are defined

HOME is not set when cloudinit runs. Golang apparently requires it to determine GOCACHE. I would expect this to be under GOPATH.

The solution is to also export GOCACHE:

  1. export GOCACHE=/root/go/cache

huangapple
  • 本文由 发表于 2021年9月11日 16:59:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/69141515.html
匿名

发表评论

匿名网友

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

确定