diff --git a/cachemonkey/auth.py b/cachemonkey/auth.py new file mode 100644 index 0000000..aec0ec0 --- /dev/null +++ b/cachemonkey/auth.py @@ -0,0 +1,75 @@ +# Copyright 2014 Rackspace Hosting +# All Rights Reserved. +# +# 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. + +from keystoneclient.v2_0 import client +from oslo.config import cfg + +from cachemonkey.openstack.common import log as logging + +LOG = logging.getLogger(__name__) +CONF = cfg.CONF + +opts = [ + + cfg.StrOpt('url', help='Auth system endpoint URL'), + cfg.StrOpt('username', help='Username for auth'), + cfg.StrOpt('password', help='Password for auth'), + cfg.StrOpt('region', help='Region'), +] + +CONF.register_opts(opts, group='auth') + + +class Client(object): + + def __init__(self): + url, username, password, region = self._config() + self.keystoneclient = client.Client(auth_url=url, username=username, + password=password, + region_name=region) + + def auth(self): + if not self.keystoneclient.authenticate(): + raise Exception('Failed to auth with keystoneclient') + + @property + def catalog(self): + return self.keystoneclient.service_catalog + + @property + def token(self): + return self.keystoneclient.auth_token + + def _config(self): + # check for presence of required config options + url = CONF.auth.url + if not url: + raise ValueError('Missing required url for auth') + + username = CONF.auth.username + if not username: + raise ValueError('Missing required username for auth') + + password = CONF.auth.password + if not password: + raise ValueError('Missing required password for auth') + + # the region scopes the catalog object for easier endpoint + # identification + region = CONF.auth.region + if not region: + raise ValueError('Missing required region for auth') + + return url, username, password, region diff --git a/cachemonkey/cacher.py b/cachemonkey/cacher.py index b9e74af..7210821 100644 --- a/cachemonkey/cacher.py +++ b/cachemonkey/cacher.py @@ -37,3 +37,5 @@ class Cacher(object): def cache(self): images = self.lister.images() + for image in images: + LOG.debug(image) diff --git a/cachemonkey/lister/glance.py b/cachemonkey/lister/glance.py index 37ad320..a1e078b 100644 --- a/cachemonkey/lister/glance.py +++ b/cachemonkey/lister/glance.py @@ -13,9 +13,48 @@ # License for the specific language governing permissions and limitations # under the License. +import glanceclient +from oslo.config import cfg + +from cachemonkey import auth +from cachemonkey.openstack.common import log as logging + +LOG = logging.getLogger(__name__) +CONF = cfg.CONF + +opt = cfg.StrOpt('endpoint', help='Glance endpoint (override catalog)') +CONF.register_opt(opt, group='glance') + class GlanceLister(object): + def __init__(self): + self.authclient = auth.Client() + self.authclient.auth() + endpoint = self._endpoint() + + api_version = 2 + token = self.authclient.token + self.client = glanceclient.Client(api_version, endpoint, token=token) + def images(self): + LOG.debug('Fetching image list') # list images to be cached. - pass + images = self.client.images.list() + return images + + def _endpoint(self): + # if an endpoint is provided via config, prefer that + if CONF.glance.endpoint: + return CONF.glance.endpoint + + # fallback to getting the glance endpoint from the service catalog + catalog = self.authclient.catalog + image_endpoints = catalog.get_endpoints()['image'] + + LOG.debug('Image endpoints: %s' % image_endpoints) + if len(image_endpoints) > 1: + LOG.warn('Expected a single image endpoint, but there are %d' % + len(image_endpoints)) + + return image_endpoints[0]['publicURL'] diff --git a/requirements.txt b/requirements.txt index 6f8c804..b47b7d4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,5 @@ iso8601>=0.1.9 oslo.config>=1.2.0 pbr>=0.6,!=0.7,<1.0 posix_ipc +python-glanceclient>=0.9.0 +python-keystoneclient>=0.9.0