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 | |
Scott Baker | f476a97 | 2015-02-02 15:56:31 -0800 | [diff] [blame] | 10 | REST_API="http://alpha.opencloud.us:8000/xos/" |
Scott Baker | e53b6b2 | 2014-08-11 19:21:14 -0700 | [diff] [blame] | 11 | |
| 12 | NODES_API = REST_API + "nodes/" |
| 13 | SLICES_API = REST_API + "slices/" |
| 14 | SLIVERS_API = REST_API + "slivers/" |
Scott Baker | f2ddddf | 2014-08-12 18:14:34 -0700 | [diff] [blame] | 15 | NETWORKSLIVERS_API = REST_API + "networkslivers/" |
Scott Baker | e53b6b2 | 2014-08-11 19:21:14 -0700 | [diff] [blame] | 16 | |
| 17 | opencloud_auth=("demo@onlab.us", "demo") |
| 18 | |
| 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"] |
| 22 | |
| 23 | def get_node_id(host_name): |
Scott Baker | 7fbf1db | 2014-09-05 15:02:43 -0700 | [diff] [blame] | 24 | 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 Baker | e53b6b2 | 2014-08-11 19:21:14 -0700 | [diff] [blame] | 31 | |
| 32 | def get_slivers(slice_id=None, node_id=None): |
| 33 | 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 | |
| 44 | r = requests.get(SLIVERS_API + query_string, auth=opencloud_auth) |
| 45 | return r.json() |
| 46 | |
| 47 | def 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) |
| 63 | slivers = get_slivers(slice_id, node_id) |
| 64 | |
| 65 | # get (instance_name, ip) pairs for instances with names and ips |
| 66 | |
Scott Baker | 4a69ab9 | 2014-08-20 12:33:26 -0700 | [diff] [blame] | 67 | slivers = [x for x in slivers if x["instance_name"]] |
Scott Baker | e53b6b2 | 2014-08-11 19:21:14 -0700 | [diff] [blame] | 68 | slivers = sorted(slivers, key = lambda sliver: sliver["instance_name"]) |
| 69 | |
| 70 | # return the last one in the list (i.e. the newest one) |
| 71 | |
Scott Baker | f2ddddf | 2014-08-12 18:14:34 -0700 | [diff] [blame] | 72 | sliver_id = slivers[-1]["id"] |
| 73 | |
| 74 | r = requests.get(NETWORKSLIVERS_API + "?sliver=%s" % sliver_id, auth=opencloud_auth) |
| 75 | |
| 76 | networkSlivers = r.json() |
| 77 | ips = [x["ip"] for x in networkSlivers] |
| 78 | |
| 79 | # XXX kinda hackish -- assumes private ips start with "10." and nat start with "172." |
| 80 | |
| 81 | # print a public IP if there is one |
| 82 | for ip in ips: |
| 83 | if (not ip.startswith("10")) and (not ip.startswith("172")): |
| 84 | print ip |
| 85 | return |
| 86 | |
| 87 | # otherwise print a privat ip |
| 88 | for ip in ips: |
| 89 | if (not ip.startswith("172")): |
| 90 | print ip |
| 91 | return |
| 92 | |
| 93 | # otherwise just print the first one |
| 94 | print ips[0] |
Scott Baker | e53b6b2 | 2014-08-11 19:21:14 -0700 | [diff] [blame] | 95 | |
| 96 | if __name__ == "__main__": |
| 97 | main() |
| 98 | |