Scott Baker | e53b6b2 | 2014-08-11 19:21:14 -0700 | [diff] [blame^] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | import json |
| 4 | import os |
| 5 | import requests |
| 6 | import sys |
| 7 | |
| 8 | from operator import itemgetter, attrgetter |
| 9 | |
| 10 | REST_API="http://alpha.opencloud.us:8000/plstackapi/" |
| 11 | |
| 12 | NODES_API = REST_API + "nodes/" |
| 13 | SLICES_API = REST_API + "slices/" |
| 14 | SLIVERS_API = REST_API + "slivers/" |
| 15 | |
| 16 | opencloud_auth=("demo@onlab.us", "demo") |
| 17 | |
| 18 | def get_slice_id(slice_name): |
| 19 | r = requests.get(SLICES_API + "?name=%s" % slice_name, auth=opencloud_auth) |
| 20 | return r.json()[0]["id"] |
| 21 | |
| 22 | def get_node_id(host_name): |
| 23 | r = requests.get(NODES_API + "?name=%s" % host_name, auth=opencloud_auth) |
| 24 | return r.json()[0]["id"] |
| 25 | |
| 26 | def get_slivers(slice_id=None, node_id=None): |
| 27 | queries = [] |
| 28 | if slice_id: |
| 29 | queries.append("slice=%s" % str(slice_id)) |
| 30 | if node_id: |
| 31 | queries.append("node=%s" % str(node_id)) |
| 32 | |
| 33 | if queries: |
| 34 | query_string = "?" + "&".join(queries) |
| 35 | else: |
| 36 | query_string = "" |
| 37 | |
| 38 | r = requests.get(SLIVERS_API + query_string, auth=opencloud_auth) |
| 39 | return r.json() |
| 40 | |
| 41 | def main(): |
| 42 | global opencloud_auth |
| 43 | |
| 44 | if len(sys.argv)!=5: |
| 45 | print >> sys.stderr, "syntax: get_instance_name.py <username>, <password>, <hostname> <slicename>" |
| 46 | sys.exit(-1) |
| 47 | |
| 48 | username = sys.argv[1] |
| 49 | password = sys.argv[2] |
| 50 | hostname = sys.argv[3] |
| 51 | slice_name = sys.argv[4] |
| 52 | |
| 53 | opencloud_auth=(username, password) |
| 54 | |
| 55 | slice_id = get_slice_id(slice_name) |
| 56 | node_id = get_node_id(hostname) |
| 57 | slivers = get_slivers(slice_id, node_id) |
| 58 | |
| 59 | # get (instance_name, ip) pairs for instances with names and ips |
| 60 | |
| 61 | slivers = [x for x in slivers if x["instance_name"] and x["ip"]] |
| 62 | slivers = sorted(slivers, key = lambda sliver: sliver["instance_name"]) |
| 63 | |
| 64 | # return the last one in the list (i.e. the newest one) |
| 65 | |
| 66 | print slivers[-1]["ip"] |
| 67 | |
| 68 | if __name__ == "__main__": |
| 69 | main() |
| 70 | |