| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| tierno | d125caf | 2018-11-22 16:05:54 +0000 | [diff] [blame] | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | # implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 16 | import logging |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame^] | 17 | import yaml |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 18 | from osm_common import dbmongo, dbmemory, fslocal, msglocal, msgkafka, version as common_version |
| 19 | from osm_common.dbbase import DbException |
| tierno | a8d6363 | 2018-05-10 13:12:32 +0200 | [diff] [blame] | 20 | from osm_common.fsbase import FsException |
| 21 | from osm_common.msgbase import MsgException |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 22 | from http import HTTPStatus |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame^] | 23 | |
| 24 | from authconn_keystone import AuthconnKeystone |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 25 | from base_topic import EngineException, versiontuple |
| tierno | 55ba2e6 | 2018-12-11 17:22:22 +0000 | [diff] [blame] | 26 | from admin_topics import UserTopic, ProjectTopic, VimAccountTopic, WimAccountTopic, SdnTopic |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame^] | 27 | from admin_topics import UserTopicAuth, ProjectTopicAuth, RoleTopicAuth |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 28 | from descriptor_topics import VnfdTopic, NsdTopic, PduTopic, NstTopic |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 29 | from instance_topics import NsrTopic, VnfrTopic, NsLcmOpTopic, NsiTopic, NsiLcmOpTopic |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 30 | from base64 import b64encode |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame^] | 31 | from os import urandom, path |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 32 | from threading import Lock |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 33 | |
| 34 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 35 | min_common_version = "0.1.16" |
| tierno | 441dbbf | 2018-07-10 12:52:48 +0200 | [diff] [blame] | 36 | |
| 37 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 38 | class Engine(object): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 39 | map_from_topic_to_class = { |
| 40 | "vnfds": VnfdTopic, |
| 41 | "nsds": NsdTopic, |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 42 | "nsts": NstTopic, |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 43 | "pdus": PduTopic, |
| 44 | "nsrs": NsrTopic, |
| 45 | "vnfrs": VnfrTopic, |
| 46 | "nslcmops": NsLcmOpTopic, |
| 47 | "vim_accounts": VimAccountTopic, |
| tierno | 55ba2e6 | 2018-12-11 17:22:22 +0000 | [diff] [blame] | 48 | "wim_accounts": WimAccountTopic, |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 49 | "sdns": SdnTopic, |
| 50 | "users": UserTopic, |
| 51 | "projects": ProjectTopic, |
| Felipe Vicens | b57758d | 2018-10-16 16:00:20 +0200 | [diff] [blame] | 52 | "nsis": NsiTopic, |
| Felipe Vicens | 07f3172 | 2018-10-29 15:16:44 +0100 | [diff] [blame] | 53 | "nsilcmops": NsiLcmOpTopic |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 54 | # [NEW_TOPIC]: add an entry here |
| 55 | } |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 56 | |
| 57 | def __init__(self): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 58 | self.db = None |
| 59 | self.fs = None |
| 60 | self.msg = None |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame^] | 61 | self.auth = None |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 62 | self.config = None |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame^] | 63 | self.operations = None |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 64 | self.logger = logging.getLogger("nbi.engine") |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 65 | self.map_topic = {} |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 66 | self.write_lock = None |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 67 | |
| 68 | def start(self, config): |
| 69 | """ |
| 70 | Connect to database, filesystem storage, and messaging |
| 71 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 72 | :return: None |
| 73 | """ |
| 74 | self.config = config |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 75 | # check right version of common |
| 76 | if versiontuple(common_version) < versiontuple(min_common_version): |
| 77 | raise EngineException("Not compatible osm/common version '{}'. Needed '{}' or higher".format( |
| 78 | common_version, min_common_version)) |
| 79 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 80 | try: |
| 81 | if not self.db: |
| 82 | if config["database"]["driver"] == "mongo": |
| 83 | self.db = dbmongo.DbMongo() |
| 84 | self.db.db_connect(config["database"]) |
| 85 | elif config["database"]["driver"] == "memory": |
| 86 | self.db = dbmemory.DbMemory() |
| 87 | self.db.db_connect(config["database"]) |
| 88 | else: |
| 89 | raise EngineException("Invalid configuration param '{}' at '[database]':'driver'".format( |
| 90 | config["database"]["driver"])) |
| 91 | if not self.fs: |
| 92 | if config["storage"]["driver"] == "local": |
| 93 | self.fs = fslocal.FsLocal() |
| 94 | self.fs.fs_connect(config["storage"]) |
| 95 | else: |
| 96 | raise EngineException("Invalid configuration param '{}' at '[storage]':'driver'".format( |
| 97 | config["storage"]["driver"])) |
| 98 | if not self.msg: |
| 99 | if config["message"]["driver"] == "local": |
| 100 | self.msg = msglocal.MsgLocal() |
| 101 | self.msg.connect(config["message"]) |
| 102 | elif config["message"]["driver"] == "kafka": |
| 103 | self.msg = msgkafka.MsgKafka() |
| 104 | self.msg.connect(config["message"]) |
| 105 | else: |
| 106 | raise EngineException("Invalid configuration param '{}' at '[message]':'driver'".format( |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 107 | config["message"]["driver"])) |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame^] | 108 | if not self.auth: |
| 109 | if config["authentication"]["backend"] == "keystone": |
| 110 | self.auth = AuthconnKeystone(config["authentication"]) |
| 111 | if not self.operations: |
| 112 | if "resources_to_operations" in config["rbac"]: |
| 113 | resources_to_operations_file = config["rbac"]["resources_to_operations"] |
| 114 | else: |
| 115 | possible_paths = ( |
| 116 | __file__[:__file__.rfind("engine.py")] + "resources_to_operations.yml", |
| 117 | "./resources_to_operations.yml" |
| 118 | ) |
| 119 | for config_file in possible_paths: |
| 120 | if path.isfile(config_file): |
| 121 | resources_to_operations_file = config_file |
| 122 | break |
| 123 | if not resources_to_operations_file: |
| 124 | raise EngineException("Invalid permission configuration: resources_to_operations file missing") |
| 125 | |
| 126 | with open(resources_to_operations_file, 'r') as f: |
| 127 | resources_to_operations = yaml.load(f) |
| 128 | |
| 129 | self.operations = [] |
| 130 | |
| 131 | for _, value in resources_to_operations["resources_to_operations"].items(): |
| 132 | if value not in self.operations: |
| 133 | self.operations += value |
| 134 | |
| 135 | if config["authentication"]["backend"] == "keystone": |
| 136 | self.map_from_topic_to_class["users"] = UserTopicAuth |
| 137 | self.map_from_topic_to_class["projects"] = ProjectTopicAuth |
| 138 | self.map_from_topic_to_class["roles"] = RoleTopicAuth |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 139 | |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 140 | self.write_lock = Lock() |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 141 | # create one class per topic |
| 142 | for topic, topic_class in self.map_from_topic_to_class.items(): |
| Eduardo Sousa | 5c01e19 | 2019-05-08 02:35:47 +0100 | [diff] [blame^] | 143 | if self.auth and topic_class in (UserTopicAuth, ProjectTopicAuth): |
| 144 | self.map_topic[topic] = topic_class(self.db, self.fs, self.msg, self.auth) |
| 145 | elif self.auth and topic_class == RoleTopicAuth: |
| 146 | self.map_topic[topic] = topic_class(self.db, self.fs, self.msg, self.auth, |
| 147 | self.operations) |
| 148 | else: |
| 149 | self.map_topic[topic] = topic_class(self.db, self.fs, self.msg) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 150 | except (DbException, FsException, MsgException) as e: |
| 151 | raise EngineException(str(e), http_code=e.http_code) |
| 152 | |
| 153 | def stop(self): |
| 154 | try: |
| 155 | if self.db: |
| 156 | self.db.db_disconnect() |
| 157 | if self.fs: |
| 158 | self.fs.fs_disconnect() |
| tierno | 932499c | 2019-01-28 17:28:10 +0000 | [diff] [blame] | 159 | if self.msg: |
| 160 | self.msg.disconnect() |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 161 | self.write_lock = None |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 162 | except (DbException, FsException, MsgException) as e: |
| 163 | raise EngineException(str(e), http_code=e.http_code) |
| 164 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 165 | def new_item(self, rollback, session, topic, indata=None, kwargs=None, headers=None, force=False): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 166 | """ |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 167 | Creates a new entry into database. For nsds and vnfds it creates an almost empty DISABLED entry, |
| 168 | that must be completed with a call to method upload_content |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 169 | :param rollback: list to append created items at database in case a rollback must to be done |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 170 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 171 | :param topic: it can be: users, projects, vim_accounts, sdns, nsrs, nsds, vnfds |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 172 | :param indata: data to be inserted |
| 173 | :param kwargs: used to override the indata descriptor |
| 174 | :param headers: http request headers |
| tierno | b92094f | 2018-05-11 13:44:22 +0200 | [diff] [blame] | 175 | :param force: If True avoid some dependence checks |
| tierno | 0ffaa99 | 2018-05-09 13:21:56 +0200 | [diff] [blame] | 176 | :return: _id: identity of the inserted data. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 177 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 178 | if topic not in self.map_topic: |
| 179 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 180 | with self.write_lock: |
| 181 | return self.map_topic[topic].new(rollback, session, indata, kwargs, headers, force) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 182 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 183 | def upload_content(self, session, topic, _id, indata, kwargs, headers, force=False): |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 184 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 185 | Upload content for an already created entry (_id) |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 186 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 187 | :param topic: it can be: users, projects, vnfds, nsds, |
| 188 | :param _id: server id of the item |
| 189 | :param indata: data to be inserted |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 190 | :param kwargs: used to override the indata descriptor |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 191 | :param headers: http request headers |
| 192 | :param force: If True avoid some dependence checks |
| 193 | :return: _id: identity of the inserted data. |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 194 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 195 | if topic not in self.map_topic: |
| 196 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 197 | with self.write_lock: |
| 198 | return self.map_topic[topic].upload_content(session, _id, indata, kwargs, headers, force) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 199 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 200 | def get_item_list(self, session, topic, filter_q=None): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 201 | """ |
| 202 | Get a list of items |
| 203 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 204 | :param topic: it can be: users, projects, vnfds, nsds, ... |
| 205 | :param filter_q: filter of data to be applied |
| 206 | :return: The list, it can be empty if no one match the filter_q. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 207 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 208 | if topic not in self.map_topic: |
| 209 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| 210 | return self.map_topic[topic].list(session, filter_q) |
| tierno | f27c79b | 2018-03-12 17:08:42 +0100 | [diff] [blame] | 211 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 212 | def get_item(self, session, topic, _id): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 213 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 214 | Get complete information on an item |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 215 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 216 | :param topic: it can be: users, projects, vnfds, nsds, |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 217 | :param _id: server id of the item |
| 218 | :return: dictionary, raise exception if not found. |
| 219 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 220 | if topic not in self.map_topic: |
| 221 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| 222 | return self.map_topic[topic].show(session, _id) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 223 | |
| tierno | 8700604 | 2018-10-24 12:50:20 +0200 | [diff] [blame] | 224 | def get_file(self, session, topic, _id, path=None, accept_header=None): |
| 225 | """ |
| 226 | Get descriptor package or artifact file content |
| 227 | :param session: contains the used login username and working project |
| 228 | :param topic: it can be: users, projects, vnfds, nsds, |
| 229 | :param _id: server id of the item |
| 230 | :param path: artifact path or "$DESCRIPTOR" or None |
| 231 | :param accept_header: Content of Accept header. Must contain applition/zip or/and text/plain |
| 232 | :return: opened file plus Accept format or raises an exception |
| 233 | """ |
| 234 | if topic not in self.map_topic: |
| 235 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| 236 | return self.map_topic[topic].get_file(session, _id, path, accept_header) |
| 237 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 238 | def del_item_list(self, session, topic, _filter=None): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 239 | """ |
| 240 | Delete a list of items |
| 241 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 242 | :param topic: it can be: users, projects, vnfds, nsds, ... |
| 243 | :param _filter: filter of data to be applied |
| 244 | :return: The deleted list, it can be empty if no one match the _filter. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 245 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 246 | if topic not in self.map_topic: |
| 247 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 248 | with self.write_lock: |
| 249 | return self.map_topic[topic].delete_list(session, _filter) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 250 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 251 | def del_item(self, session, topic, _id, force=False): |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 252 | """ |
| tierno | b92094f | 2018-05-11 13:44:22 +0200 | [diff] [blame] | 253 | Delete item by its internal id |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 254 | :param session: contains the used login username and working project |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 255 | :param topic: it can be: users, projects, vnfds, nsds, ... |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 256 | :param _id: server id of the item |
| tierno | 65acb4d | 2018-04-06 16:42:40 +0200 | [diff] [blame] | 257 | :param force: indicates if deletion must be forced in case of conflict |
| tierno | 09c073e | 2018-04-26 13:36:48 +0200 | [diff] [blame] | 258 | :return: dictionary with deleted item _id. It raises exception if not found. |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 259 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 260 | if topic not in self.map_topic: |
| 261 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 262 | with self.write_lock: |
| 263 | return self.map_topic[topic].delete(session, _id, force) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 264 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 265 | def edit_item(self, session, topic, _id, indata=None, kwargs=None, force=False): |
| 266 | """ |
| 267 | Update an existing entry at database |
| 268 | :param session: contains the used login username and working project |
| 269 | :param topic: it can be: users, projects, vnfds, nsds, ... |
| 270 | :param _id: identifier to be updated |
| 271 | :param indata: data to be inserted |
| 272 | :param kwargs: used to override the indata descriptor |
| 273 | :param force: If True avoid some dependence checks |
| 274 | :return: dictionary, raise exception if not found. |
| 275 | """ |
| 276 | if topic not in self.map_topic: |
| 277 | raise EngineException("Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR) |
| tierno | 04dbb0e | 2019-01-09 16:00:24 +0000 | [diff] [blame] | 278 | with self.write_lock: |
| 279 | return self.map_topic[topic].edit(session, _id, indata, kwargs, force) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 280 | |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 281 | def create_admin(self): |
| 282 | """ |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 283 | Creates a new user admin/admin into database if database is empty. Useful for initialization |
| 284 | :return: _id identity of the inserted data, or None |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 285 | """ |
| 286 | users = self.db.get_one("users", fail_on_empty=False, fail_on_more=False) |
| 287 | if users: |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 288 | return None |
| 289 | # raise EngineException("Unauthorized. Database users is not empty", HTTPStatus.UNAUTHORIZED) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 290 | user_desc = {"username": "admin", "password": "admin", "projects": ["admin"]} |
| 291 | fake_session = {"project_id": "admin", "username": "admin", "admin": True} |
| 292 | roolback_list = [] |
| 293 | _id = self.map_topic["users"].new(roolback_list, fake_session, user_desc, force=True) |
| tierno | c94c3df | 2018-02-09 15:38:54 +0100 | [diff] [blame] | 294 | return _id |
| 295 | |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 296 | def upgrade_db(self, current_version, target_version): |
| 297 | if not target_version or current_version == target_version: |
| 298 | return |
| 299 | if target_version == '1.0': |
| 300 | if not current_version: |
| 301 | # create database version |
| 302 | serial = urandom(32) |
| 303 | version_data = { |
| 304 | "_id": 'version', # Always 'version' |
| 305 | "version_int": 1000, # version number |
| 306 | "version": '1.0', # version text |
| 307 | "date": "2018-10-25", # version date |
| 308 | "description": "added serial", # changes in this version |
| 309 | 'status': 'ENABLED', # ENABLED, DISABLED (migration in process), ERROR, |
| 310 | 'serial': b64encode(serial) |
| 311 | } |
| 312 | self.db.create("admin", version_data) |
| 313 | self.db.set_secret_key(serial) |
| tierno | bee085c | 2018-12-12 17:03:04 +0000 | [diff] [blame] | 314 | return |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 315 | # TODO add future migrations here |
| 316 | |
| 317 | raise EngineException("Wrong database version '{}'. Expected '{}'" |
| 318 | ". It cannot be up/down-grade".format(current_version, target_version), |
| 319 | http_code=HTTPStatus.INTERNAL_SERVER_ERROR) |
| 320 | |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 321 | def init_db(self, target_version='1.0'): |
| 322 | """ |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 323 | Init database if empty. If not empty it checks that database version and migrates if needed |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 324 | If empty, it creates a new user admin/admin at 'users' and a new entry at 'version' |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 325 | :param target_version: check desired database version. Migrate to it if possible or raises exception |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 326 | :return: None if ok, exception if error or if the version is different. |
| 327 | """ |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 328 | |
| 329 | version_data = self.db.get_one("admin", {"_id": "version"}, fail_on_empty=False, fail_on_more=True) |
| 330 | # check database status is ok |
| 331 | if version_data and version_data.get("status") != 'ENABLED': |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 332 | raise EngineException("Wrong database status '{}'".format( |
| tierno | d985a8d | 2018-10-19 14:12:28 +0200 | [diff] [blame] | 333 | version_data["status"]), HTTPStatus.INTERNAL_SERVER_ERROR) |
| 334 | |
| 335 | # check version |
| 336 | db_version = None if not version_data else version_data.get("version") |
| 337 | if db_version != target_version: |
| 338 | self.upgrade_db(db_version, target_version) |
| 339 | |
| 340 | # create user admin if not exist |
| 341 | self.create_admin() |
| tierno | 4a946e4 | 2018-04-12 17:48:49 +0200 | [diff] [blame] | 342 | return |