Adds support for MaaS metadata
Adds a MaaS metadata service
This commit is contained in:
parent
c03ea53bcc
commit
525b9f41a9
|
@ -26,6 +26,7 @@ opts = [
|
|||
'cloudbaseinit.metadata.services.httpservice.HttpService',
|
||||
'cloudbaseinit.metadata.services.configdrive.configdrive.'
|
||||
'ConfigDriveService',
|
||||
'cloudbaseinit.metadata.services.maasservice.MaaSHttpService'
|
||||
],
|
||||
help='List of enabled metadata service classes, '
|
||||
'to be tested fro availability in the provided order. '
|
||||
|
|
|
@ -0,0 +1,124 @@
|
|||
# Copyright 2014 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 posixpath
|
||||
import time
|
||||
import urllib2
|
||||
|
||||
from oauth import oauth
|
||||
from oslo.config import cfg
|
||||
|
||||
from cloudbaseinit.metadata.services import base
|
||||
from cloudbaseinit.openstack.common import log as logging
|
||||
from cloudbaseinit.plugins.windows import x509
|
||||
|
||||
opts = [
|
||||
cfg.StrOpt('maas_metadata_url', default=None,
|
||||
help='The base URL for MaaS metadata'),
|
||||
cfg.StrOpt('maas_oauth_consumer_key', default="",
|
||||
help='The MaaS OAuth consumer key'),
|
||||
cfg.StrOpt('maas_oauth_consumer_secret', default="",
|
||||
help='The MaaS OAuth consumer secret'),
|
||||
cfg.StrOpt('maas_oauth_token_key', default="",
|
||||
help='The MaaS OAuth token key'),
|
||||
cfg.StrOpt('maas_oauth_token_secret', default="",
|
||||
help='The MaaS OAuth token secret'),
|
||||
]
|
||||
|
||||
CONF = cfg.CONF
|
||||
CONF.register_opts(opts)
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MaaSHttpService(base.BaseMetadataService):
|
||||
_METADATA_LATEST = 'latest'
|
||||
|
||||
def __init__(self):
|
||||
super(MaaSHttpService, self).__init__()
|
||||
self._enable_retry = True
|
||||
self._metadata_version = self._METADATA_LATEST
|
||||
|
||||
def load(self):
|
||||
super(MaaSHttpService, self).load()
|
||||
|
||||
if not CONF.maas_metadata_url:
|
||||
LOG.debug('MaaS metadata url not set')
|
||||
else:
|
||||
try:
|
||||
self._get_data('latest/meta-data/')
|
||||
return True
|
||||
except Exception, ex:
|
||||
LOG.exception(ex)
|
||||
LOG.debug('Metadata not found at URL \'%s\'' %
|
||||
CONF.maas_metadata_url)
|
||||
return False
|
||||
|
||||
def _get_response(self, req):
|
||||
try:
|
||||
return urllib2.urlopen(req)
|
||||
except urllib2.HTTPError as ex:
|
||||
if ex.code == 404:
|
||||
raise base.NotExistingMetadataException()
|
||||
else:
|
||||
raise
|
||||
|
||||
def _get_oauth_headers(self, url):
|
||||
consumer = oauth.OAuthConsumer(CONF.maas_oauth_consumer_key,
|
||||
CONF.maas_oauth_consumer_secret)
|
||||
token = oauth.OAuthToken(CONF.maas_oauth_token_key,
|
||||
CONF.maas_oauth_token_secret)
|
||||
|
||||
parameters = {'oauth_version': "1.0",
|
||||
'oauth_nonce': oauth.generate_nonce(),
|
||||
'oauth_timestamp': int(time.time()),
|
||||
'oauth_token': token.key,
|
||||
'oauth_consumer_key': consumer.key}
|
||||
|
||||
req = oauth.OAuthRequest(http_url=url, parameters=parameters)
|
||||
req.sign_request(oauth.OAuthSignatureMethod_PLAINTEXT(), consumer,
|
||||
token)
|
||||
|
||||
return req.to_header()
|
||||
|
||||
def _get_data(self, path):
|
||||
norm_path = posixpath.join(CONF.maas_metadata_url, path)
|
||||
oauth_headers = self._get_oauth_headers(norm_path)
|
||||
|
||||
LOG.debug('Getting metadata from: %(norm_path)s',
|
||||
{'norm_path': norm_path})
|
||||
req = urllib2.Request(norm_path, headers=oauth_headers)
|
||||
response = self._get_response(req)
|
||||
return response.read()
|
||||
|
||||
def get_host_name(self):
|
||||
return self._get_cache_data('%s/meta-data/local-hostname' %
|
||||
self._metadata_version)
|
||||
|
||||
def get_instance_id(self):
|
||||
return self._get_cache_data('%s/meta-data/instance-id' %
|
||||
self._metadata_version)
|
||||
|
||||
def _get_list_from_text(self, text, delimiter):
|
||||
return [v + delimiter for v in text.split(delimiter)]
|
||||
|
||||
def get_public_keys(self):
|
||||
return self._get_list_from_text(
|
||||
self._get_cache_data('%s/meta-data/public-keys' %
|
||||
self._metadata_version), "\n")
|
||||
|
||||
def get_client_auth_certs(self):
|
||||
return self._get_list_from_text(
|
||||
self._get_cache_data('%s/meta-data/x509' % self._metadata_version),
|
||||
"%s\n" % x509.PEM_FOOTER)
|
|
@ -7,3 +7,4 @@ pyserial
|
|||
oslo.config
|
||||
six>=1.4.1
|
||||
Babel>=1.3
|
||||
oauth
|
||||
|
|
Loading…
Reference in New Issue