blob: 1783b25c5395bf3625fc49cf9e5febb59ab837dc [file] [log] [blame]
S.Çağlar Onur3e92b4d2015-02-09 13:34:11 -05001#! /usr/bin/env python
Scott Bakere53b6b22014-08-11 19:21:14 -07002
3import json
4import os
5import requests
6import sys
7
8from operator import itemgetter, attrgetter
9
Scott Baker55e0ebe2015-02-02 15:56:31 -080010REST_API="http://alpha.opencloud.us:8000/xos/"
Scott Bakere53b6b22014-08-11 19:21:14 -070011
12NODES_API = REST_API + "nodes/"
13SLICES_API = REST_API + "slices/"
Tony Mack26ea0eb2015-09-01 16:06:52 +000014INSTANCES_API = REST_API + "instances/"
Scott Baker11fe2e92015-08-28 11:55:19 -070015PORTS_API = REST_API + "ports/"
Scott Bakere53b6b22014-08-11 19:21:14 -070016
17opencloud_auth=("demo@onlab.us", "demo")
18
19def get_slice_id(slice_name):
20 r = requests.get(SLICES_API + "?name=%s" % slice_name, auth=opencloud_auth)
21 return r.json()[0]["id"]
22
23def get_node_id(host_name):
Scott Baker46075ac2014-09-05 15:02:43 -070024 r = requests.get(NODES_API)
25 nodes = r.json()
26 for node in nodes:
27 if node["name"].lower() == host_name.lower():
28 return node["id"]
29 print >> sys.stderr, "Error: failed to find node %s" % host_name
30 sys.exit(-1)
Scott Bakere53b6b22014-08-11 19:21:14 -070031
Tony Mackd8515472015-08-19 11:58:18 -040032def get_instances(slice_id=None, node_id=None):
Scott Bakere53b6b22014-08-11 19:21:14 -070033 queries = []
34 if slice_id:
35 queries.append("slice=%s" % str(slice_id))
36 if node_id:
37 queries.append("node=%s" % str(node_id))
38
39 if queries:
40 query_string = "?" + "&".join(queries)
41 else:
42 query_string = ""
43
Tony Mack26ea0eb2015-09-01 16:06:52 +000044 r = requests.get(INSTANCES_API + query_string, auth=opencloud_auth)
Scott Bakere53b6b22014-08-11 19:21:14 -070045 return r.json()
46
47def main():
48 global opencloud_auth
49
50 if len(sys.argv)!=5:
51 print >> sys.stderr, "syntax: get_instance_name.py <username>, <password>, <hostname> <slicename>"
52 sys.exit(-1)
53
54 username = sys.argv[1]
55 password = sys.argv[2]
56 hostname = sys.argv[3]
57 slice_name = sys.argv[4]
58
59 opencloud_auth=(username, password)
60
61 slice_id = get_slice_id(slice_name)
62 node_id = get_node_id(hostname)
Tony Mackd8515472015-08-19 11:58:18 -040063 instances = get_instances(slice_id, node_id)
Scott Bakere53b6b22014-08-11 19:21:14 -070064
65 # get (instance_name, ip) pairs for instances with names and ips
66
Tony Mackd8515472015-08-19 11:58:18 -040067 instances = [x for x in instances if x["instance_name"]]
68 instances = sorted(instances, key = lambda instance: instance["instance_name"])
Scott Bakere53b6b22014-08-11 19:21:14 -070069
70 # return the last one in the list (i.e. the newest one)
71
Tony Mackd8515472015-08-19 11:58:18 -040072 instance_id = instances[-1]["id"]
Scott Bakerf2ddddf2014-08-12 18:14:34 -070073
Scott Baker2a5fe802015-09-14 16:02:21 -070074 r = requests.get(PORTS_API + "?instance=%s" % instance_id, auth=opencloud_auth)
Scott Baker11fe2e92015-08-28 11:55:19 -070075 ports = r.json()
76 ips = [x["ip"] for x in ports]
Scott Bakerf2ddddf2014-08-12 18:14:34 -070077
78 # XXX kinda hackish -- assumes private ips start with "10." and nat start with "172."
79
80 # print a public IP if there is one
81 for ip in ips:
82 if (not ip.startswith("10")) and (not ip.startswith("172")):
83 print ip
84 return
85
86 # otherwise print a privat ip
87 for ip in ips:
88 if (not ip.startswith("172")):
89 print ip
90 return
91
92 # otherwise just print the first one
93 print ips[0]
Scott Bakere53b6b22014-08-11 19:21:14 -070094
95if __name__ == "__main__":
96 main()
97