#!/usr/bin/env python import subprocess import os import tempfile import pprint import json from socket import getaddrinfo # all changes mean we should regen the file memcached_ips = [] rl = subprocess.Popen("relation-list",stdout=subprocess.PIPE) for memcached_unit in rl.stdout: p = subprocess.Popen(["relation-get", "--format", "json", "-", memcached_unit.strip()], stdout=subprocess.PIPE, close_fds=True) settings = json.loads(p.stdout.read().strip()) try: if 'private-address' in settings and 'port' in settings: memcached_ips.append("'%s:%d'" % getaddrinfo(settings['private-address'],int(settings['port']))[0][4][0:2]) except KeyError: print memcached_unit + " has no settings. ["+str(settings)+"]" rl.wait() pprint.pprint(memcached_ips) memcache_template = """