Skip to content

Instantly share code, notes, and snippets.

@inokappa
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inokappa/7189764d2d2e583be3f2 to your computer and use it in GitHub Desktop.
Save inokappa/7189764d2d2e583be3f2 to your computer and use it in GitHub Desktop.
Consul KVS に登録された Consul ノードで実行したスクリプトの結果をフックして標準出力にログっぽく出力するスクリプト
#!/usr/bin/env python
import json, urllib2, sys, base64, time, datetime
# check nodes
node_url = 'http://127.0.0.1:8500/v1/catalog/nodes'
r = urllib2.urlopen(node_url)
j = json.loads(r.read())
nodes = []
for attr in j:
if 'consul_0' in attr['Node']:
nodes.append(attr['Node'])
# check X-Consul-Index
chk_url = 'http://localhost:8500/v1/kv/deploy/?recurse'
try:
r = urllib2.urlopen(chk_url)
headers = r.info()
current_index = int(headers.getheader("X-Consul-Index"))
except urllib2.HTTPError, e:
time.sleep(10)
print 'Got Error Code:' + str(e.code)
# check result
for node in nodes:
kvs_url = 'http://localhost:8500/v1/kv/deploy/' + node + '/result?recurse&index=' + str(current_index)
r = urllib2.urlopen(kvs_url)
j = json.loads(r.read())
for attr in j:
timestamp = datetime.datetime.fromtimestamp(float(attr['Flags']))
mod_index = attr['ModifyIndex']
message = base64.b64decode(attr['Value'])
node = attr['Key'].split("/")
result = ('%s %d %s %s' % (timestamp, mod_index, node[1], message))
print result
r.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment