Hi,
Something from my experience playing with Golang and Puppet code this morning.
I wrote a very very simple script to restart a service that you can find here
Today i wanted to put it on the machine and run it with puppet, so i wrote a very small class that looked like this:
class profiles_test::updatekafka {
package { 'golang':
ensure => installed,
name => 'golang',
}
file {"/root/servicerestart.go":
source => 'puppet:///modules/profiles_test/servicerestart.go',
mode => '0644',
replace => true,
}
exec { 'go build servicerestart.go':
cwd => '/root',
creates => '/root/servicerestart',
path => ['/usr/bin', '/usr/sbin'],
} ->
exec { '/root/servicerestart':
cwd => '/root',
path => ['/usr/bin', '/usr/sbin','/root'],
onlyif => 'ps -u kafka',
}
}
On execution, surprise, it kept throw this feedback:
08:19:44 Notice: /Stage[main]/Profiles_test::Updatekafka/File[/root/check_cluster_state.go]/ensure: defined content as '{md5}0817dbf82b74072e125f8b71ee914150' 08:19:44 Notice: /Stage[main]/Profiles_test::Updatekafka/Exec[go run servicerestart.go]/returns: panic: exit status 127 08:19:44 Notice: /Stage[main]/Profiles_test::Updatekafka/Exec[go run servicerestart.go]/returns: 08:19:44 Notice: /Stage[main]/Profiles_test::Updatekafka/Exec[go run servicerestart.go]/returns: goroutine 1 [running]: 08:19:44 Notice: /Stage[main]/Profiles_test::Updatekafka/Exec[go run servicerestart.go]/returns: runtime.panic(0x4c2100, 0xc2100000b8) 08:19:44 Notice: /Stage[main]/Profiles_test::Updatekafka/Exec[go run servicerestart.go]/returns: /usr/lib/go/src/pkg/runtime/panic.c:266 +0xb6 08:19:44 Notice: /Stage[main]/Profiles_test::Updatekafka/Exec[go run servicerestart.go]/returns: main.check(0x7f45b08ed150, 0xc2100000b8) 08:19:44 Notice: /Stage[main]/Profiles_test::Updatekafka/Exec[go run servicerestart.go]/returns: /root/servicerestart.go:94 +0x4f 08:19:44 Notice: /Stage[main]/Profiles_test::Updatekafka/Exec[go run servicerestart.go]/returns: main.RunCommand(0x4ea7f0, 0x2c, 0x7f4500000000, 0x409928, 0x5d9f38) 08:19:44 Notice: /Stage[main]/Profiles_test::Updatekafka/Exec[go run servicerestart.go]/returns: /root/servicerestart.go:87 +0x112 08:19:44 Notice: /Stage[main]/Profiles_test::Updatekafka/Exec[go run servicerestart.go]/returns: main.GetBrokerList(0x7f45b08ecf00, 0xc21003fa00, 0xe) 08:19:44 Notice: /Stage[main]/Profiles_test::Updatekafka/Exec[go run servicerestart.go]/returns: /root/servicerestart.go:66 +0x3c 08:19:44 Notice: /Stage[main]/Profiles_test::Updatekafka/Exec[go run servicerestart.go]/returns: main.GetStatus(0xc21000a170) 08:19:44 Notice: /Stage[main]/Profiles_test::Updatekafka/Exec[go run servicerestart.go]/returns: /root/servicerestart.go:33 +0x1e 08:19:44 Notice: /Stage[main]/Profiles_test::Updatekafka/Exec[go run servicerestart.go]/returns: main.StopBroker() 08:19:44 Notice: /Stage[main]/Profiles_test::Updatekafka/Exec[go run servicerestart.go]/returns: /root/servicerestart.go:39 +0x21 08:19:44 Notice: /Stage[main]/Profiles_test::Updatekafka/Exec[go run servicerestart.go]/returns: main.main() 08:19:44 Notice: /Stage[main]/Profiles_test::Updatekafka/Exec[go run servicerestart.go]/returns: /root/servicerestart.go:15 +0x1e 08:19:44 Notice: /Stage[main]/Profiles_test::Updatekafka/Exec[go run servicerestart.go]/returns: exit status 2 08:19:44 Error: go run servicerestart.go returned 1 instead of one of [0]
The secret is in panic: exit status 127 and it seems that it’s not related to neither golang or puppet, but shell.
In my script they way to get the broker list is by
output1 := RunCommand("echo dump | nc localhost 2181 | grep brokers")
and the error is related to not having the required binaries in you path. For example if you run
whereis echo echo: /bin/echo /usr/share/man/man1/echo.1.gz
So the right way to for the exec block is actually:
exec { '/root/servicerestart':
cwd => '/root',
path => ['/usr/bin', '/usr/sbin','/root','/bin','/sbin'],
onlyif => 'ps -u kafka',
}
And then it will work.
Cheers!