blob: 61cc5780104a8f23562a13ff4415bb5b84a554a8 [file] [log] [blame]
Scott Bakere53b6b22014-08-11 19:21:14 -07001#! /usr/bin/python
2
3import json
4import os
5import requests
6import sys
7
8from operator import itemgetter, attrgetter
9
10REST_API="http://alpha.opencloud.us:8000/plstackapi/"
11
12NODES_API = REST_API + "nodes/"
13SLICES_API = REST_API + "slices/"
14SLIVERS_API = REST_API + "slivers/"
15
16opencloud_auth=("demo@onlab.us", "demo")
17
18def 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
22def 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
26def 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
41def 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
68if __name__ == "__main__":
69 main()
70