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