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)
27 if node["name"].lower() == host_name.lower():
29 print >> sys.stderr, "Error: failed to find node %s" % host_name
32 def get_slivers(slice_id=None, node_id=None):
35 queries.append("slice=%s" % str(slice_id))
37 queries.append("node=%s" % str(node_id))
40 query_string = "?" + "&".join(queries)
44 r = requests.get(SLIVERS_API + query_string, auth=opencloud_auth)
51 print >> sys.stderr, "syntax: get_instance_name.py <username>, <password>, <hostname> <slicename>"
54 username = sys.argv[1]
55 password = sys.argv[2]
56 hostname = sys.argv[3]
57 slice_name = sys.argv[4]
59 opencloud_auth=(username, password)
61 slice_id = get_slice_id(slice_name)
62 node_id = get_node_id(hostname)
63 slivers = get_slivers(slice_id, node_id)
65 # get (instance_name, ip) pairs for instances with names and ips
67 slivers = [x for x in slivers if x["instance_name"]]
68 slivers = sorted(slivers, key = lambda sliver: sliver["instance_name"])
70 # return the last one in the list (i.e. the newest one)
72 sliver_id = slivers[-1]["id"]
74 r = requests.get(NETWORKSLIVERS_API + "?sliver=%s" % sliver_id, auth=opencloud_auth)
76 networkSlivers = r.json()
77 ips = [x["ip"] for x in networkSlivers]
79 # XXX kinda hackish -- assumes private ips start with "10." and nat start with "172."
81 # print a public IP if there is one
83 if (not ip.startswith("10")) and (not ip.startswith("172")):
87 # otherwise print a privat ip
89 if (not ip.startswith("172")):
93 # otherwise just print the first one
96 if __name__ == "__main__":