117 lines
3.6 KiB
Python
117 lines
3.6 KiB
Python
# 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 json
|
|
import posixpath
|
|
import urllib2
|
|
import urlparse
|
|
|
|
from oslo.config import cfg
|
|
|
|
from cloudbaseinit.metadata.services import base
|
|
from cloudbaseinit.openstack.common import log as logging
|
|
from cloudbaseinit.osutils import factory as osutils_factory
|
|
from cloudbaseinit.utils.windows import x509
|
|
|
|
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 BaseOpenStackService(base.BaseMetadataService):
|
|
|
|
def get_content(self, name):
|
|
path = posixpath.normpath(
|
|
posixpath.join('openstack', 'content', name))
|
|
return self._get_cache_data(path)
|
|
|
|
def get_user_data(self):
|
|
path = posixpath.normpath(
|
|
posixpath.join('openstack', 'latest', 'user_data'))
|
|
return self._get_cache_data(path)
|
|
|
|
def _get_meta_data(self, version='latest'):
|
|
path = posixpath.normpath(
|
|
posixpath.join('openstack', version, 'meta_data.json'))
|
|
data = self._get_cache_data(path)
|
|
if type(data) is str:
|
|
return json.loads(self._get_cache_data(path))
|
|
else:
|
|
return data
|
|
|
|
def get_instance_id(self):
|
|
return self._get_meta_data().get('uuid')
|
|
|
|
def get_host_name(self):
|
|
return self._get_meta_data().get('hostname')
|
|
|
|
def get_public_keys(self):
|
|
public_keys = self._get_meta_data().get('public_keys')
|
|
if public_keys:
|
|
return public_keys.values()
|
|
|
|
def get_network_config(self):
|
|
return self._get_meta_data().get('network_config')
|
|
|
|
def get_admin_password(self):
|
|
meta_data = self._get_meta_data()
|
|
meta = meta_data.get('meta')
|
|
|
|
if meta and 'admin_pass' in meta:
|
|
password = meta['admin_pass']
|
|
elif 'admin_pass' in meta_data:
|
|
password = meta_data['admin_pass']
|
|
else:
|
|
password = None
|
|
|
|
return password
|
|
|
|
def get_client_auth_certs(self):
|
|
cert_data = None
|
|
|
|
meta_data = self._get_meta_data()
|
|
meta = meta_data.get('meta')
|
|
|
|
if meta:
|
|
i = 0
|
|
while True:
|
|
# Chunking is necessary as metadata items can be
|
|
# max. 255 chars long
|
|
cert_chunk = meta.get('admin_cert%d' % i)
|
|
if not cert_chunk:
|
|
break
|
|
if not cert_data:
|
|
cert_data = cert_chunk
|
|
else:
|
|
cert_data += cert_chunk
|
|
i += 1
|
|
|
|
if not cert_data:
|
|
# Look if the user_data contains a PEM certificate
|
|
try:
|
|
user_data = self.get_user_data()
|
|
if user_data.startswith(x509.PEM_HEADER):
|
|
cert_data = user_data
|
|
except base.NotExistingMetadataException:
|
|
LOG.debug("user_data metadata not present")
|
|
|
|
if cert_data:
|
|
return [cert_data]
|