8 from operator import itemgetter, attrgetter
10 REST_API="http://alpha.opencloud.us:8000/plstackapi/"
12 NODES_API = REST_API + "nodes/"
13 SLICES_API = REST_API + "slices/"
14 SLIVERS_API = REST_API + "slivers/"
15 NETWORKSLIVERS_API = REST_API + "networkslivers/"
17 opencloud_auth=("demo@onlab.us", "demo")
19 def get_slice_id(slice_name):
20 r = requests.get(SLICES_API + "?name=%s" % slice_name, auth=opencloud_auth)
21 return r.json()[0]["id"]
23 def get_node_id(host_name):
24 r = requests.get(NODES_API + "?name=%s" % host_name, auth=opencloud_auth)
25 return r.json()[0]["id"]
27 def get_slivers(slice_id=None, node_id=None):
30 queries.append("slice=%s" % str(slice_id))
32 queries.append("node=%s" % str(node_id))
35 query_string = "?" + "&".join(queries)
39 r = requests.get(SLIVERS_API + query_string, auth=opencloud_auth)
46 print >> sys.stderr, "syntax: get_instance_name.py <username>, <password>, <hostname> <slicename>"
49 username = sys.argv[1]
50 password = sys.argv[2]
51 hostname = sys.argv[3]
52 slice_name = sys.argv[4]
54 opencloud_auth=(username, password)
56 slice_id = get_slice_id(slice_name)
57 node_id = get_node_id(hostname)
58 slivers = get_slivers(slice_id, node_id)
60 # get (instance_name, ip) pairs for instances with names and ips
62 slivers = [x for x in slivers if x["instance_name"]]
63 slivers = sorted(slivers, key = lambda sliver: sliver["instance_name"])
65 # return the last one in the list (i.e. the newest one)
67 sliver_id = slivers[-1]["id"]
69 r = requests.get(NETWORKSLIVERS_API + "?sliver=%s" % sliver_id, auth=opencloud_auth)
71 networkSlivers = r.json()
72 ips = [x["ip"] for x in networkSlivers]
74 # XXX kinda hackish -- assumes private ips start with "10." and nat start with "172."
76 # print a public IP if there is one
78 if (not ip.startswith("10")) and (not ip.startswith("172")):
82 # otherwise print a privat ip
84 if (not ip.startswith("172")):
88 # otherwise just print the first one
91 if __name__ == "__main__":