Consul-template: Can't run command in template

Created on 2 Jun 2017  路  2Comments  路  Source: hashicorp/consul-template

Consul Template version

consul-template v0.18.5 (9902dd5)

Configuration

template = {
    source = "haproxy.conf.ctmpl"
    destination = "haproxy.conf"
    command = "/usr/local/bin/haproxy -f /root/Documents/consulProto/haproxy.conf -sf $(pidof haproxy)"
}
global
    maxconn 15
    log 127.0.0.1 local1 notice

defaults
    mode http
    timeout connect 5000
    timeout client  50000
    timeout server  50000

listen http-in
    bind *:8000{{range service "web"}}
    server {{.Node}} {{.Address}}:{{.Port}}{{end}}

Command

/usr/local/bin/haproxy -f /root/Documents/consulProto/haproxy.conf -sf $(pidof haproxy)

Debug output

https://gist.github.com/thomas-oo/dcd2858a949340e50a5c2b1c1070a204
FYI: When I did this command manually and checking the exit code, it is indeed 0 as expected by consul-template

Expected behavior

The command should have been run, which restarts a new haproxy process with the new conf file, and stops the old haproxy process.

Actual behavior

It shows an improper usage message from haproxy, suggesting that the pidof command did not work (this is my guess). Thus the old haproxy process continues and is configured with the old conf file.

Steps to reproduce

  1. Save the template to some file, save the config to some file, run consul-template with -config= -consul-addr=
  2. Stop some service node, which marks it as unhealthy
  3. This triggers consul-template to re-render the conf file according to the template. This works BUT the haproxy command fails to execute.

Most helpful comment

Found a quick workaround which is to pass the entire command into "/bin/bash -c '[problematic command]'". In my example, my command became "/bin/bash -c '/usr/local/bin/haproxy -f /root/Documents/consulProto/haproxy.conf -sf $(pidof haproxy) &'"
Not sure if this is useful but..

All 2 comments

This is a known issue with haproxy. The first time that CT runs, pidof will return`(nothing), which haproxy interprets as a missing argument. You'll need to write a wrapper script to first check if haproxy is running and then conditionally use the-f` flag.

Found a quick workaround which is to pass the entire command into "/bin/bash -c '[problematic command]'". In my example, my command became "/bin/bash -c '/usr/local/bin/haproxy -f /root/Documents/consulProto/haproxy.conf -sf $(pidof haproxy) &'"
Not sure if this is useful but..

Was this page helpful?
0 / 5 - 0 ratings