Skip to content

Instantly share code, notes, and snippets.

@divideby0
Created January 10, 2015 04:36
Show Gist options
  • Save divideby0/165cd1d764665843d381 to your computer and use it in GitHub Desktop.
Save divideby0/165cd1d764665843d381 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Retrieves a comma-delimited list of unicasts hosts for Elasticsearch instances
# running on marathon, based on the application name.
import sys, getopt, json, requests
def exit_invalid_args():
print 'usage: marathon-es-hosts.py -m <marathon_url> -a <app_name>'
sys.exit(2)
def main(argv):
marathon_url = ''
app_name = ''
try:
opts, args = getopt.getopt(argv,"m:a:",["marathon=","app="])
except getopt.GetoptError:
exit_invalid_args()
for opt, arg in opts:
if opt in ("-m", "--marathon"):
marathon_url = arg
elif opt in ("-a", "--app"):
app_name = arg
if not marathon_url or not app_name:
exit_invalid_args()
resp = requests.get(url="{0}/v2/apps/{1}".format(marathon_url, app_name))
data = json.loads(resp.text)
unicast_hosts = []
for task in data['app']['tasks']:
# get the host name and the second exposed port (this should be the transport port)
unicast_hosts.append("{0}:{1}".format(task['host'], task['ports'][1]))
print ",".join(unicast_hosts)
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment