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

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

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

问题

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

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

我这样安装包:

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

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

    file /root/go/bin/bird_exporter 
    /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/下没有任何内容。
日志显示所有包的下载都正常:

go: downloading github.com/czerwonk/bird_exporter v0.0.0-20210702094413-c9985a3895a4
go: downloading github.com/prometheus/client_golang v1.11.0
go: downloading github.com/sirupsen/logrus v1.6.0
go: downloading github.com/czerwonk/bird_socket v0.0.0-20190111125930-6c39d61f8853
go: downloading github.com/prometheus/client_model v0.2.0
go: downloading github.com/prometheus/common v0.29.0
go: downloading github.com/beorn7/perks v1.0.1
go: downloading github.com/cespare/xxhash/v2 v2.1.1
go: downloading github.com/golang/protobuf v1.4.3
go: downloading github.com/prometheus/procfs v0.6.0
go: downloading golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40
go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.1
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:

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

I install the package like so:

    BIRD_EXPORTER_VERSION=1.2.6
    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:

    file /root/go/bin/bird_exporter 
    /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:

go: downloading github.com/czerwonk/bird_exporter v0.0.0-20210702094413-c9985a3895a4
go: downloading github.com/prometheus/client_golang v1.11.0
go: downloading github.com/sirupsen/logrus v1.6.0
go: downloading github.com/czerwonk/bird_socket v0.0.0-20190111125930-6c39d61f8853
go: downloading github.com/prometheus/client_model v0.2.0
go: downloading github.com/prometheus/common v0.29.0
go: downloading github.com/beorn7/perks v1.0.1
go: downloading github.com/cespare/xxhash/v2 v2.1.1
go: downloading github.com/golang/protobuf v1.4.3
go: downloading github.com/prometheus/procfs v0.6.0
go: downloading golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40
go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.1
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

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

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

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

解决方案是同时导出GOCACHE

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:

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:

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:

确定