I'm writing a Puppet module to deploy Hyperic, with the final part being executing hq-agent.sh start if the service is not running. To determine if the service is running, I can execute hq-agent.sh status which includes the text "HQ Agent is running".
The Puppet documentation states onlyif would work, however needs to return an exit code of 1 if running and 0 if not running; what's the applicable bash(?) command to do the conversion?
Pseudo code => if ('hq-agent.sh status' contains "running") return 1; else return 0;
This sounds very much like something that should be modeled via a Service
resource, not an Exec
. Doing so does not require the service to be managed via the system's regular service-control subsystem (initscripts, systemd, ...), though I would certainly recommend arranging for that, even if you have to write the appropriate scripts or configuration files yourself. In this case, however, the hq-agent.sh
script sounds like it has an interface similar, if not identical, to a traditional initscript. If so, then it would probably be pretty easy to set up as an ordinary system service. If you did that, then managing it could be as easy as
service { 'hq-agent':
ensure => 'running',
enable => true,
}
But if you just want to use ad hoc scripts to manage the service, Puppet can support that. Specifically, the Service
resource has start
, restart
, status
, and stop
attributes with which you can specify arbitrary commands for managing the service. For example,
service { 'hq-agent':
ensure => 'running',
provider => 'service',
hasstatus => false,
hasrestart => false,
status => 'hq-agent.sh status',
start => 'hq-agent.sh start',
stop => 'hq-agent.sh stop',
path => '/path/to/hyperic/bin',
# no 'enable' attribute specified
}
That particular example makes some assumptions about the exit codes of the hq-agent.sh
script, based on its superficial similarity to a standard SysV initscript. Specifically, it supposes that they conform with the LSB specifications. If in fact they don't, so that you need to test the script's output instead of its exit codes, then a typical approach would be to pipe the output into grep
. For example,
status => 'hq-agent.sh status | grep -q running'
Beware, however, that you may need to test the script's standard error instead of its standard output.