|
| 1 | +# Copyright 2010 United States Government as represented by the |
| 2 | +# Administrator of the National Aeronautics and Space Administration. |
| 3 | +# All Rights Reserved. |
| 4 | +# |
| 5 | +# Copyright 2013 OpenStack Foundation |
| 6 | +# Copyright 2013 Intel Corporation |
| 7 | +# |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | +# not use this file except in compliance with the License. You may obtain |
| 10 | +# a copy of the License at |
| 11 | +# |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +# |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 17 | +# License for the specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | + |
| 20 | +import json |
| 21 | +import os |
| 22 | +from os.path import isfile |
| 23 | +from os.path import join |
| 24 | +import re |
| 25 | + |
| 26 | +from oslo.config import cfg |
| 27 | +import sqlalchemy |
| 28 | +from sqlalchemy.schema import MetaData |
| 29 | + |
| 30 | +from glance.common import utils |
| 31 | +from glance import i18n |
| 32 | +import glance.openstack.common.log as logging |
| 33 | +from glance.openstack.common import timeutils |
| 34 | + |
| 35 | +LOG = logging.getLogger(__name__) |
| 36 | +_LE = i18n._LE |
| 37 | +_LW = i18n._LW |
| 38 | +_LI = i18n._LI |
| 39 | + |
| 40 | +metadata_opts = [ |
| 41 | + cfg.StrOpt('metadata_source_path', default='/etc/glance/metadefs/', |
| 42 | + help=_('Path to the directory where json metadata ' |
| 43 | + 'files are stored')) |
| 44 | +] |
| 45 | + |
| 46 | +CONF = cfg.CONF |
| 47 | +CONF.register_opts(metadata_opts) |
| 48 | + |
| 49 | + |
| 50 | +def get_metadef_namespaces_table(meta): |
| 51 | + return sqlalchemy.Table('metadef_namespaces', meta, autoload=True) |
| 52 | + |
| 53 | + |
| 54 | +def get_metadef_resource_types_table(meta): |
| 55 | + return sqlalchemy.Table('metadef_resource_types', meta, autoload=True) |
| 56 | + |
| 57 | + |
| 58 | +def get_metadef_namespace_resource_types_table(meta): |
| 59 | + return sqlalchemy.Table('metadef_namespace_resource_types', meta, |
| 60 | + autoload=True) |
| 61 | + |
| 62 | + |
| 63 | +def get_metadef_properties_table(meta): |
| 64 | + return sqlalchemy.Table('metadef_properties', meta, autoload=True) |
| 65 | + |
| 66 | + |
| 67 | +def get_metadef_objects_table(meta): |
| 68 | + return sqlalchemy.Table('metadef_objects', meta, autoload=True) |
| 69 | + |
| 70 | + |
| 71 | +def _get_resource_type_id(meta, name): |
| 72 | + resource_types_table = get_metadef_resource_types_table(meta) |
| 73 | + return resource_types_table.select().\ |
| 74 | + where(resource_types_table.c.name == name).execute().fetchone().id |
| 75 | + |
| 76 | + |
| 77 | +def _get_resource_type(meta, resource_type_id): |
| 78 | + resource_types_table = get_metadef_resource_types_table(meta) |
| 79 | + return resource_types_table.select().\ |
| 80 | + where(resource_types_table.c.id == resource_type_id).\ |
| 81 | + execute().fetchone() |
| 82 | + |
| 83 | + |
| 84 | +def _get_namespace_resource_types(meta, namespace_id): |
| 85 | + namespace_resource_types_table =\ |
| 86 | + get_metadef_namespace_resource_types_table(meta) |
| 87 | + return namespace_resource_types_table.select().\ |
| 88 | + where(namespace_resource_types_table.c.namespace_id == namespace_id).\ |
| 89 | + execute().fetchall() |
| 90 | + |
| 91 | + |
| 92 | +def _get_properties(meta, namespace_id): |
| 93 | + properties_table = get_metadef_properties_table(meta) |
| 94 | + return properties_table.select().\ |
| 95 | + where(properties_table.c.namespace_id == namespace_id).\ |
| 96 | + execute().fetchall() |
| 97 | + |
| 98 | + |
| 99 | +def _get_objects(meta, namespace_id): |
| 100 | + objects_table = get_metadef_objects_table(meta) |
| 101 | + return objects_table.select().\ |
| 102 | + where(objects_table.c.namespace_id == namespace_id).\ |
| 103 | + execute().fetchall() |
| 104 | + |
| 105 | + |
| 106 | +def _populate_metadata(meta, metadata_path=None): |
| 107 | + if not metadata_path: |
| 108 | + metadata_path = CONF.metadata_source_path |
| 109 | + |
| 110 | + try: |
| 111 | + json_schema_files = [f for f in os.listdir(metadata_path) |
| 112 | + if isfile(join(metadata_path, f)) |
| 113 | + and f.endswith('.json')] |
| 114 | + except OSError as e: |
| 115 | + LOG.error(utils.exception_to_str(e)) |
| 116 | + return |
| 117 | + |
| 118 | + metadef_namespaces_table = get_metadef_namespaces_table(meta) |
| 119 | + metadef_namespace_resource_types_tables =\ |
| 120 | + get_metadef_namespace_resource_types_table(meta) |
| 121 | + metadef_objects_table = get_metadef_objects_table(meta) |
| 122 | + metadef_properties_table = get_metadef_properties_table(meta) |
| 123 | + metadef_resource_types_table = get_metadef_resource_types_table(meta) |
| 124 | + |
| 125 | + if not json_schema_files: |
| 126 | + LOG.error(_LE("Json schema files not found in %s. Aborting."), |
| 127 | + metadata_path) |
| 128 | + return |
| 129 | + |
| 130 | + for namespace_id, json_schema_file in enumerate(json_schema_files, |
| 131 | + start=1): |
| 132 | + try: |
| 133 | + file = join(metadata_path, json_schema_file) |
| 134 | + json_metadata = open(file) |
| 135 | + metadata = json.load(json_metadata) |
| 136 | + json_metadata.close() |
| 137 | + except Exception as e: |
| 138 | + LOG.error(utils.exception_to_str(e)) |
| 139 | + continue |
| 140 | + |
| 141 | + values = { |
| 142 | + 'id': namespace_id, |
| 143 | + 'namespace': metadata.get('namespace', None), |
| 144 | + 'display_name': metadata.get('display_name', None), |
| 145 | + 'description': metadata.get('description', None), |
| 146 | + 'visibility': metadata.get('visibility', None), |
| 147 | + 'protected': metadata.get('protected', None), |
| 148 | + 'owner': metadata.get('owner', 'admin'), |
| 149 | + 'created_at': timeutils.utcnow() |
| 150 | + } |
| 151 | + _insert_data_to_db(metadef_namespaces_table, values) |
| 152 | + |
| 153 | + for resource_type in metadata.get('resource_type_associations', []): |
| 154 | + try: |
| 155 | + resource_type_id = \ |
| 156 | + _get_resource_type_id(meta, resource_type['name']) |
| 157 | + except AttributeError: |
| 158 | + values = { |
| 159 | + 'name': resource_type['name'], |
| 160 | + 'protected': True, |
| 161 | + 'created_at': timeutils.utcnow() |
| 162 | + } |
| 163 | + _insert_data_to_db(metadef_resource_types_table, |
| 164 | + values) |
| 165 | + resource_type_id =\ |
| 166 | + _get_resource_type_id(meta, resource_type['name']) |
| 167 | + |
| 168 | + values = { |
| 169 | + 'resource_type_id': resource_type_id, |
| 170 | + 'namespace_id': namespace_id, |
| 171 | + 'created_at': timeutils.utcnow(), |
| 172 | + 'properties_target': resource_type.get('properties_target'), |
| 173 | + 'prefix': resource_type.get('prefix', None) |
| 174 | + } |
| 175 | + _insert_data_to_db(metadef_namespace_resource_types_tables, |
| 176 | + values) |
| 177 | + |
| 178 | + for property, schema in metadata.get('properties', {}).iteritems(): |
| 179 | + values = { |
| 180 | + 'name': property, |
| 181 | + 'namespace_id': namespace_id, |
| 182 | + 'schema': json.dumps(schema), |
| 183 | + 'created_at': timeutils.utcnow() |
| 184 | + } |
| 185 | + _insert_data_to_db(metadef_properties_table, values) |
| 186 | + |
| 187 | + for object in metadata.get('objects', []): |
| 188 | + values = { |
| 189 | + 'name': object.get('name', None), |
| 190 | + 'description': object.get('description', None), |
| 191 | + 'namespace_id': namespace_id, |
| 192 | + 'schema': json.dumps(object.get('properties', None)), |
| 193 | + 'created_at': timeutils.utcnow() |
| 194 | + } |
| 195 | + _insert_data_to_db(metadef_objects_table, values) |
| 196 | + |
| 197 | + LOG.info(_LI("File %s loaded to database."), file) |
| 198 | + |
| 199 | + LOG.info(_LI("Metadata loading finished")) |
| 200 | + |
| 201 | + |
| 202 | +def _clear_metadata(meta): |
| 203 | + metadef_tables = [get_metadef_properties_table(meta), |
| 204 | + get_metadef_objects_table(meta), |
| 205 | + get_metadef_namespace_resource_types_table(meta), |
| 206 | + get_metadef_namespaces_table(meta)] |
| 207 | + |
| 208 | + for table in metadef_tables: |
| 209 | + table.delete().execute() |
| 210 | + LOG.info(_LI("Table %s has been cleared"), table) |
| 211 | + |
| 212 | + |
| 213 | +def _insert_data_to_db(table, values, log_exception=True): |
| 214 | + try: |
| 215 | + table.insert(values=values).execute() |
| 216 | + except sqlalchemy.exc.IntegrityError: |
| 217 | + if log_exception: |
| 218 | + LOG.warning(_LW("Duplicate entry for values: %s"), values) |
| 219 | + |
| 220 | + |
| 221 | +def _export_data_to_file(meta, path): |
| 222 | + if not path: |
| 223 | + path = CONF.metadata_source_path |
| 224 | + |
| 225 | + namespace_table = get_metadef_namespaces_table(meta) |
| 226 | + namespaces = namespace_table.select().execute().fetchall() |
| 227 | + |
| 228 | + pattern = re.compile('[\W_]+', re.UNICODE) |
| 229 | + |
| 230 | + for id, namespace in enumerate(namespaces, start=1): |
| 231 | + namespace_id = namespace['id'] |
| 232 | + namespace_file_name = pattern.sub('', namespace['display_name']) |
| 233 | + |
| 234 | + values = { |
| 235 | + 'namespace': namespace['namespace'], |
| 236 | + 'display_name': namespace['display_name'], |
| 237 | + 'description': namespace['description'], |
| 238 | + 'visibility': namespace['visibility'], |
| 239 | + 'protected': namespace['protected'], |
| 240 | + 'owner': namespace['owner'], |
| 241 | + 'resource_type_associations': [], |
| 242 | + 'properties': {}, |
| 243 | + 'objects': [] |
| 244 | + } |
| 245 | + |
| 246 | + namespace_resource_types = _get_namespace_resource_types(meta, |
| 247 | + namespace_id) |
| 248 | + db_objects = _get_objects(meta, namespace_id) |
| 249 | + db_properties = _get_properties(meta, namespace_id) |
| 250 | + |
| 251 | + resource_types = [] |
| 252 | + for namespace_resource_type in namespace_resource_types: |
| 253 | + resource_type =\ |
| 254 | + _get_resource_type(meta, |
| 255 | + namespace_resource_type['resource_type_id']) |
| 256 | + resource_types.append({ |
| 257 | + 'name': resource_type['name'], |
| 258 | + 'protected': resource_type['protected'] |
| 259 | + }) |
| 260 | + values.update({ |
| 261 | + 'resource_type_associations': resource_types |
| 262 | + }) |
| 263 | + |
| 264 | + objects = [] |
| 265 | + for object in db_objects: |
| 266 | + objects.append({ |
| 267 | + "name": object['name'], |
| 268 | + "description": object['description'], |
| 269 | + "properties": json.loads(object['schema']) |
| 270 | + }) |
| 271 | + values.update({ |
| 272 | + 'objects': objects |
| 273 | + }) |
| 274 | + |
| 275 | + properties = {} |
| 276 | + for property in db_properties: |
| 277 | + properties.update({ |
| 278 | + property['name']: json.loads(property['schema']) |
| 279 | + }) |
| 280 | + values.update({ |
| 281 | + 'properties': properties |
| 282 | + }) |
| 283 | + |
| 284 | + try: |
| 285 | + file_name = ''.join([path, namespace_file_name, '.json']) |
| 286 | + json_file = open(file_name, 'w+') |
| 287 | + json_file.write(json.dumps(values)) |
| 288 | + json_file.close() |
| 289 | + |
| 290 | + except Exception as e: |
| 291 | + LOG.exception(utils.exception_to_str(e)) |
| 292 | + LOG.info(_LI("Namespace %s saved in %s"), |
| 293 | + namespace_file_name, file_name) |
| 294 | + |
| 295 | + |
| 296 | +def db_load_metadefs(engine, metadata_path=None): |
| 297 | + meta = MetaData() |
| 298 | + meta.bind = engine |
| 299 | + |
| 300 | + _populate_metadata(meta, metadata_path) |
| 301 | + |
| 302 | + |
| 303 | +def db_unload_metadefs(engine): |
| 304 | + meta = MetaData() |
| 305 | + meta.bind = engine |
| 306 | + |
| 307 | + _clear_metadata(meta) |
| 308 | + |
| 309 | + |
| 310 | +def db_export_metadefs(engine, metadata_path=None): |
| 311 | + meta = MetaData() |
| 312 | + meta.bind = engine |
| 313 | + |
| 314 | + _export_data_to_file(meta, metadata_path) |
0 commit comments