0

I have a fairly simple install of chef and go (golang). If I use a go install command in my chef recipe I get an error of go: command not found but when I do the exact same command manually using the same directory.

Here is my chef code:

remote_file consul_path + '/go1.11.2.linux-amd64.tar.gz' do
  source 'https://dl.google.com/go/go1.11.2.linux-amd64.tar.gz'
  owner consul_user
  group consul_user
  mode '0755'
  action :create
end

bash 'golang_setup' do
  user 'root'
  cwd consul_path
  code <<-EOH
  tar xvf #{consul_path}/go1.11.2.linux-amd64.tar.gz
  chown -R #{consul_user}:#{consul_user} ./go
  mv go /usr/local
  echo 'export GOROOT=/usr/local/go' >> /etc/profile
  echo 'export GOPATH=$HOME/go' >> /etc/profile
  echo 'export PATH=$GOPATH/bin:$GOROOT/bin:$PATH' >> /etc/profile
  source /etc/profile
  EOH
  action :run
end

directory '/home/' + consul_user + '/go' do
  owner consul_user
  group consul_user
  mode '0755'
  action :create
end

bash 'install_consul-alerts' do
  user consul_user
  cwd '/home/' + consul_user
  code <<-EOH
  go install github.com/AcalephStorage/consul-alerts
  EOH
  action :run
end

I am not too sure why it would work manually but not when it is run in a bash block in chef. In theory it should be the exact same if I am the correct user as well as the correct directory.

Ideas or suggestions to try?

  • You're adding variables to /Etc/profile but not sourcing it in your second bash script, your second bash script runs with what Chef has as environment. Either specify GOROOT and GOPATH in the environment property of the bash resource or add source /etc/profile before the go install – Tensibai Dec 7 '18 at 8:27
  • @Tensibai So I have to source that /etc/profile in every bash block? There has to be a better way than that surely? For the mean time I change GOPATH to /home/consul/go because I'm pretty sure that will go my only user using a go program. – joshk132 Dec 8 '18 at 14:45
  • No you have to understand how environments variables works with your shell, there's no magic here and chef can't guess what you want to do. BTW to install consul there's already a cookbook here which you may take inspiration from – Tensibai Dec 10 '18 at 8:38
  • @Tensibai I have been looking at that cookbook but am not using it because of the bloat that seems to come with community cookbooks. – joshk132 Dec 10 '18 at 23:03

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.