consul watch and handler only work with a stdin in external script

Created on 26 Nov 2014  路  7Comments  路  Source: hashicorp/consul

consul 0.4.1

3 consul server
2 consul agent

config watch on consul agent 1 .

agent 1:

cat /etc/consul.d/watch.json
{
"watches": [
{
"type": "keyprefix",
"prefix": "web/",
"handler": "/root/test.sh"
}
]
}

handler script :

cat /root/test.sh

!/bin/bash

date >> /root/watch.log

start consul agent as below:

/root/gocode/bin/consul agent -data-dir /tmp/consul -config-dir /etc/consul.d/ -client 192.168.2.62 -join 192.168.2.73

And Error as below:
2014/11/26 10:05:08 [INFO] consul: adding server consul-server2 (Addr: 192.168.2.72:8300) (DC: dc1)
2014/11/26 10:05:08 [INFO] consul: adding server consul-server3 (Addr: 192.168.2.73:8300) (DC: dc1)
2014/11/26 10:05:08 [INFO] consul: adding server consul-server1 (Addr: 192.168.2.71:8300) (DC: dc1)
2014/11/26 10:05:08 [ERR] agent: Failed to invoke watch handler '/root/test.sh': write |1: broken pipe

then we change the handler script and it works
cat /root/test.sh

!/bin/bash

read a
date >> /root/watch.log

We have tested update the kv and the watch and handler works without problem.

The question is why handler script need such an input ? even it don't need one?

The consul docs about watch (http://www.consul.io/docs/agent/watches.html) and said :

A handler should read it's input from stdin, and expect to read JSON formatted data. The format of the data depends on the type of the watch. Each watch type documents the format type, and because they map directly to an HTTP API, handlers should expect the input to match the format of the API.

What is the correct way to make a handler to meet the above requirement?

Most helpful comment

Any equivalent fix for a Powershell handler ?

I tried to use this without success:

Read-Host | Out-Null

All 7 comments

For now, there is no way to disable the watch from providing the data. You can always just add a "cat > /dev/null" to read and discard the stdin data.

Closing this per the workaround in https://github.com/hashicorp/consul/issues/501#issuecomment-64699620.

Hi everyone, Hi @slackpad I'm affraid I'll have to ask you to re-open this bug as I'm facing the exact same issue with python handlers (Python 3.4) even if I manage to use the stdin/json stream.

Using the command line tool is almost working but wait for a stdin EOF which never come and so fill the handler with an infinite stdin data stream.

If we keyboard interrupt the signal, the command success and the data (just a testing string) is repeating itself countlessly on the destination file of the handler.

Using the external script is failing immediately with the error: agent: Failed to invoke watch handler

PS: At the opposite of the original issue report I'm willing to use this stdin stream on my handler and so expect it, but the stdin stream is infinite where it should not.

@slackpad ok, so, I've found out what's going on exactly with the agent: Failed to invoke error, indeed, you may end up with this kind of error if your unix user do not have available terminal or if the user do not have home directory.

Using the standard input is really handy, but indeed that introduce a bunch of other pitfalls.

I ran into the same issue with some Ruby scripts that I was using as watch handlers since I run Consul as a system user with no home directory or shell. I ended up writing a small bash wrapper script that sets up the necessary PATH and shell variables for the scripts to execute properly.

Any equivalent fix for a Powershell handler ?

I tried to use this without success:

Read-Host | Out-Null

Hi,
How can we i get updated values in my handler file.?
What will be the code of lines for Handler file (.sh) file.?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pritam97 picture pritam97  路  3Comments

matteoturra picture matteoturra  路  4Comments

sandstrom picture sandstrom  路  3Comments

lmb picture lmb  路  4Comments

runswithd6s picture runswithd6s  路  3Comments