HTTP metadata service support

This commit is contained in:
Alessandro Pilotti
2012-12-04 10:47:14 -05:00
parent 41bdcce044
commit 5cfd03e420
3 changed files with 62 additions and 4 deletions

View File

@@ -15,12 +15,14 @@
# under the License.
from cloudbaseinit.openstack.common import cfg
from cloudbaseinit.openstack.common import log as logging
from cloudbaseinit.utils import *
opts = [
cfg.ListOpt('metadata_services', default=[
'cloudbaseinit.metadata.services.configdrive.configdrive.'
'ConfigDriveService'
'ConfigDriveService',
'cloudbaseinit.metadata.services.httpservice.HttpService',
],
help='List of enabled metadata service classes, '
'to be tested fro availability in the provided order. '
@@ -29,6 +31,7 @@ opts = [
CONF = cfg.CONF
CONF.register_opts(opts)
LOG = logging.getLogger(__name__)
class MetadataServiceFactory(object):
@@ -37,6 +40,11 @@ class MetadataServiceFactory(object):
utils = Utils()
for class_path in CONF.metadata_services:
service = utils.load_class(class_path)()
try:
if service.load():
return service
except Exception, ex:
LOG.error('Failed to load metadata service \'%(class_path)s\' '
'with error: %(ex)s'%
locals())
raise Exception("No available service found")

View File

@@ -48,7 +48,7 @@ class ConfigDriveManager(object):
q1 = conn.query('SELECT Label FROM Win32_Volume WHERE Name = \'%(drive)s\'' % locals())
for r1 in q1:
if r1.Label == "config-2" and \
os.path.exists(os.path.join(drive, 'openstack\\latest\meta_data.json')):
os.path.exists(os.path.join(drive, 'openstack\\latest\\meta_data.json')):
return drive
return None

View File

@@ -0,0 +1,50 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2012 Cloudbase Solutions Srl
#
# 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 posixpath
import urllib2
from cloudbaseinit.metadata.services.base import *
from cloudbaseinit.openstack.common import cfg
opts = [
cfg.StrOpt('metadata_base_url', default='http://169.254.169.254/',
help='The base URL where the service looks for metadata'),
]
CONF = cfg.CONF
CONF.register_opts(opts)
LOG = logging.getLogger(__name__)
class HttpService(BaseMetadataService):
def load(self):
super(HttpService, self).load()
try:
self.get_meta_data('openstack')
return True
except:
LOG.debug('Metadata not found at URL \'%s\'' %
CONF.metadata_base_url)
return False
def _get_data(self, path):
norm_path = posixpath.join(CONF.metadata_base_url, path)
LOG.debug('Getting metadata from: %(norm_path)s' % locals())
req = urllib2.Request(norm_path)
response = urllib2.urlopen(req)
return response.read()