koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame^] | 1 | import socket |
| 2 | import logging |
| 3 | |
| 4 | |
| 5 | from novaclient.client import Client |
| 6 | |
| 7 | from .node import Node |
| 8 | from wally.utils import parse_creds |
| 9 | |
| 10 | |
| 11 | logger = logging.getLogger("io-perf-tool.discover") |
| 12 | |
| 13 | |
| 14 | def get_floating_ip(vm): |
| 15 | addrs = vm.addresses |
| 16 | for net_name, ifaces in addrs.items(): |
| 17 | for iface in ifaces: |
| 18 | if iface.get('OS-EXT-IPS:type') == "floating": |
| 19 | return iface['addr'] |
| 20 | |
| 21 | |
| 22 | def discover_vms(client, search_opts): |
| 23 | user, password, key = parse_creds(search_opts.pop('auth')) |
| 24 | |
| 25 | servers = client.servers.list(search_opts=search_opts) |
| 26 | logger.debug("Found %s openstack vms" % len(servers)) |
| 27 | return [Node(get_floating_ip(server), ["test_vm"], username=user, |
| 28 | password=password, key_path=key) |
| 29 | for server in servers if get_floating_ip(server)] |
| 30 | |
| 31 | |
| 32 | def discover_services(client, opts): |
| 33 | user, password, key = parse_creds(opts.pop('auth')) |
| 34 | |
| 35 | services = [] |
| 36 | if opts['service'] == "all": |
| 37 | services = client.services.list() |
| 38 | else: |
| 39 | if isinstance(opts['service'], basestring): |
| 40 | opts['service'] = [opts['service']] |
| 41 | |
| 42 | for s in opts['service']: |
| 43 | services.extend(client.services.list(binary=s)) |
| 44 | |
| 45 | host_services_mapping = {} |
| 46 | |
| 47 | for service in services: |
| 48 | ip = socket.gethostbyname(service.host) |
| 49 | host_services_mapping[ip].append(service.binary) |
| 50 | |
| 51 | logger.debug("Found %s openstack service nodes" % |
| 52 | len(host_services_mapping)) |
| 53 | return [Node(host, services, username=user, |
| 54 | password=password, key_path=key) for host, services in |
| 55 | host_services_mapping.items()] |
| 56 | |
| 57 | |
| 58 | def discover_openstack_nodes(conn_details, conf): |
| 59 | """Discover vms running in openstack |
| 60 | :param conn_details - dict with openstack connection details - |
| 61 | auth_url, api_key (password), username |
| 62 | """ |
| 63 | client = Client(version='1.1', **conn_details) |
| 64 | nodes = [] |
| 65 | if conf.get('discover'): |
| 66 | services_to_discover = conf['discover'].get('nodes') |
| 67 | if services_to_discover: |
| 68 | nodes.extend(discover_services(client, services_to_discover)) |
| 69 | |
| 70 | return nodes |