diff --git a/metalsmith/deploy.py b/metalsmith/deploy.py index b1330c8..37d097e 100644 --- a/metalsmith/deploy.py +++ b/metalsmith/deploy.py @@ -15,40 +15,97 @@ import logging -import glanceclient -from keystoneclient.v2_0 import client as ks_client -from neutronclient.neutron import client as neu_client +from oslo_utils import excutils + +from metalsmith import os_api LOG = logging.getLogger(__name__) -class API(object): - """Various OpenStack API's.""" - - GLANCE_VERSION = '1' - NEUTRON_VERSION = '2.0' - - def __init__(self, **kwargs): - LOG.debug('creating Keystone client') - self.keystone = ks_client.Client(**kwargs) - self.auth_token = self.keystone.auth_token - LOG.debug('creating service clients') - self.glance = glanceclient.Client( - self.GLANCE_VERSION, endpoint=self.get_endpoint('image'), - token=self.auth_token) - self.neutron = neu_client.Client( - self.NEUTRON_VERSION, endpoint_url=self.get_endpoint('network'), - token=self.auth_token) - - def get_endpoint(self, service_type, endpoint_type='internalurl'): - service_id = self.keystone.services.find(type=service_type).id - endpoint = self.keystone.endpoints.find(service_id=service_id) - return getattr(endpoint, endpoint_type) +def _log_node(node): + if node.name: + return '%s (UUID %s)' % (node.name, node.uuid) + else: + return node.uuid -def deploy(profile, image): +def _get_capabilities(node): + return dict(x.split(':', 1) for x in + node.properties.get('capabilities', '').split(',') if x) + + +def reserve(api, nodes, profile): + suitable_nodes = [] + for node in nodes: + caps = _get_capabilities(node) + LOG.debug('Capabilities for node %(node)s: %(cap)s', + {'node': _log_node(node), 'cap': caps}) + if caps.get('profile') == profile: + suitable_nodes.append(node) + + if not suitable_nodes: + raise RuntimeError('No nodes found with profile %s' % profile) + + for node in suitable_nodes: + try: + api.update_node(node.uuid, instance_uuid=node.uuid) + except os_api.ir_exc.Conflict: + LOG.info('Node %s was occupied, proceeding with the next', + _log_node(node)) + else: + return node + + raise RuntimeError('Unable to reserve any node') + + +def clean_up(api, node): + try: + api.update_node(node.uuid, instance_uuid=os_api.REMOVE) + except Exception: + LOG.debug('Failed to remove instance_uuid, assuming already removed') + + +def prepare(api, node, network, image): + raise NotImplementedError('Not implemented') + + +def provision(api, node): + raise NotImplementedError('Not implemented') + + +def deploy(profile, image_id, network_id, auth_args): """Deploy an image on a given profile.""" - LOG.debug('deploying image %(image)s on node with profile %(profile)s', - {'image': image, 'profile': profile}) - API() + LOG.debug('Deploying image %(image)s on node with profile %(profile)s ' + 'on network %(net)s', + {'image': image_id, 'profile': profile, 'net': network_id}) + api = os_api.API(**auth_args) + + image = api.get_image_info(image_id) + if image is None: + raise RuntimeError('Image %s does not exist' % image_id) + LOG.debug('Image: %s', image) + network = api.get_network(network_id) + if network is None: + raise RuntimeError('Network %s does not exist' % network_id) + LOG.debug('Network: %s', network) + + nodes = api.list_nodes() + LOG.debug('Ironic nodes: %s', nodes) + if not len(nodes): + raise RuntimeError('No available nodes found') + LOG.info('Got list of %d available nodes from Ironic', len(nodes)) + + node = reserve(api, nodes, profile) + LOG.info('Reserved node %s', _log_node(node)) + + try: + prepare(api, node, network, image) + provision(api, node) + except Exception: + with excutils.save_and_reraise_exception(): + LOG.error('Deploy failed, cleaning up') + try: + clean_up(api, node) + except Exception: + LOG.exception('Clean up also failed') diff --git a/metalsmith/main.py b/metalsmith/main.py index ecd330b..57dde11 100644 --- a/metalsmith/main.py +++ b/metalsmith/main.py @@ -15,6 +15,7 @@ import argparse import logging +import os import sys from metalsmith import deploy @@ -30,6 +31,13 @@ def main(): help='output more logging') parser.add_argument('-i', '--image', help='image to use (name or UUID)', required=True) + parser.add_argument('-n', '--network', + help='network to use (name or UUID)', required=True), + parser.add_argument('--os-username', default=os.environ.get('OS_USERNAME')) + parser.add_argument('--os-password', default=os.environ.get('OS_PASSWORD')) + parser.add_argument('--os-tenant-name', + default=os.environ.get('OS_TENANT_NAME')) + parser.add_argument('--os-auth-url', default=os.environ.get('OS_AUTH_URL')) parser.add_argument('profile', help='node profile to deploy') args = parser.parse_args() @@ -38,9 +46,18 @@ def main(): logging.basicConfig( level=logging.DEBUG if args.debug else logging.INFO, format=log_fmt) + if not args.debug: + logging.getLogger('requests.packages.urllib3.connectionpool').setLevel( + logging.CRITICAL) + + auth_args = {'auth_url': args.os_auth_url, + 'username': args.os_username, + 'tenant_name': args.os_tenant_name, + 'password': args.os_password} try: - deploy.deploy(profile=args.profile, image=args.image) + deploy.deploy(profile=args.profile, image_id=args.image, + network_id=args.network, auth_args=auth_args) except Exception as exc: LOG.critical('%s', exc, exc_info=args.debug) sys.exit(1) diff --git a/metalsmith/os_api.py b/metalsmith/os_api.py new file mode 100644 index 0000000..822e45a --- /dev/null +++ b/metalsmith/os_api.py @@ -0,0 +1,110 @@ +# Copyright 2015 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import logging + +import glanceclient +from ironicclient import client as ir_client +from ironicclient import exc as ir_exc # noqa +from keystoneclient.v2_0 import client as ks_client +from neutronclient.neutron import client as neu_client + + +LOG = logging.getLogger(__name__) +DEFAULT_ENDPOINTS = { + 'image': 'http://127.0.0.1:9292/', + 'network': 'http://127.0.0.1:9696/', + 'baremetal': 'http://127.0.0.1:6385/', +} +REMOVE = object() + + +class DictWithAttrs(dict): + __slots__ = () + + def __getattr__(self, attr): + try: + return self[attr] + except KeyError: + super(DictWithAttrs, self).__getattr__(attr) + + +class API(object): + """Various OpenStack API's.""" + + GLANCE_VERSION = '1' + NEUTRON_VERSION = '2.0' + IRONIC_VERSION = 1 + + def __init__(self, **kwargs): + LOG.debug('Creating Keystone client') + self.keystone = ks_client.Client(**kwargs) + self.auth_token = self.keystone.auth_token + LOG.debug('Creating service clients') + self.glance = glanceclient.Client( + self.GLANCE_VERSION, endpoint=self.get_endpoint('image'), + token=self.auth_token) + self.neutron = neu_client.Client( + self.NEUTRON_VERSION, endpoint_url=self.get_endpoint('network'), + token=self.auth_token) + self.ironic = ir_client.get_client( + self.IRONIC_VERSION, ironic_url=self.get_endpoint('baremetal'), + os_auth_token=self.auth_token) + + def get_endpoint(self, service_type, endpoint_type='internalurl'): + service_id = self.keystone.services.find(type=service_type).id + try: + endpoint = self.keystone.endpoints.find(service_id=service_id) + except Exception as exc: + default = DEFAULT_ENDPOINTS.get(service_type) + LOG.warn('Failed to detect %(srv)s service endpoint, using ' + 'the default of %(def)s: %(err)s', + {'srv': service_type, 'def': default, 'err': exc}) + return default + return getattr(endpoint, endpoint_type) + + def get_image_info(self, image_id): + for img in self.glance.images.list(): + if img.name == image_id or img.id == image_id: + return img + + def get_network(self, network_id): + for net in self.neutron.list_networks()['networks']: + if net['name'] == network_id or net['id'] == network_id: + return DictWithAttrs(net) + + def list_nodes(self, maintenance=False, associated=False, + provision_state='available', detail=True): + nodes = self.ironic.node.list(limit=0, maintenance=maintenance, + associated=associated, detail=detail) + if provision_state: + # TODO(dtantsur): use Liberty API for filtring by state + nodes = [n for n in nodes + if n.provision_state.lower() == provision_state.lower()] + + return nodes + + def update_node(self, node_id, **attrs): + patches = [] + for key, value in attrs.items(): + if not key.startswith('/'): + key = '/' + key + + if value is REMOVE: + patches.append({'op': 'remove', 'path': key}) + else: + patches.append({'op': 'add', 'path': key, 'value': value}) + + return self.ironic.node.update(node_id, patches) diff --git a/requirements.txt b/requirements.txt index 2377b58..5d40594 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ pbr>=1.6,<2.0 +oslo.utils>=2.0.0 # Apache-2.0 python-glanceclient>=0.18.0 python-ironicclient>=0.6.0 python-keystoneclient>=1.6.0 diff --git a/tox.ini b/tox.ini index b28339a..c26de9b 100644 --- a/tox.ini +++ b/tox.ini @@ -9,7 +9,8 @@ commands = coverage run --branch --include "metalsmith*" -m unittest discover metalsmith.test coverage report -m setenv = PYTHONDONTWRITEBYTECODE=1 -passenv = http_proxy HTTP_PROXY https_proxy HTTPS_PROXY no_proxy NO_PROXY +passenv = http_proxy HTTP_PROXY https_proxy HTTPS_PROXY no_proxy NO_PROXY \ + OS_USERNAME OS_PASSWORD OS_TENANT_NAME OS_AUTH_URL [testenv:venv] commands = {posargs}