| 1 |
import string |
| 2 |
import random |
| 3 |
import os |
| 4 |
import sys |
| 5 |
from biomaj.bank import Bank |
| 6 |
from biomaj.mongo_connector import MongoConnector |
| 7 |
from biomaj_core.config import BiomajConfig |
| 8 |
from biomaj_core.utils import Utils |
| 9 |
|
| 10 |
import logging |
| 11 |
|
| 12 |
|
| 13 |
class SchemaVersion: |
| 14 |
|
| 15 |
""" |
| 16 |
BioMAJ database schema version. This package can be used to make some schema modification if needed during |
| 17 |
incremental software version. |
| 18 |
""" |
| 19 |
|
| 20 |
VERSION = None |
| 21 |
|
| 22 |
@staticmethod |
| 23 |
def migrate_pendings(): |
| 24 |
""" |
| 25 |
Migrate database |
| 26 |
|
| 27 |
3.0.18: Check the actual BioMAJ version and if older than 3.0.17, do the 'pending' key migration |
| 28 |
""" |
| 29 |
if BiomajConfig.global_config is None: |
| 30 |
try: |
| 31 |
BiomajConfig.load_config() |
| 32 |
except Exception as err: |
| 33 |
print("* SchemaVersion: Can't find config file: " + str(err)) |
| 34 |
return None |
| 35 |
if MongoConnector.db is None: |
| 36 |
MongoConnector(BiomajConfig.global_config.get('GENERAL', 'db.url'), |
| 37 |
BiomajConfig.global_config.get('GENERAL', 'db.name')) |
| 38 |
|
| 39 |
schema = MongoConnector.db_schema |
| 40 |
banks = MongoConnector.banks |
| 41 |
users = MongoConnector.users |
| 42 |
schema_version = SchemaVersion.get_dbschema_version(schema) |
| 43 |
moderate = int(schema_version.split('.')[1]) |
| 44 |
minor = int(schema_version.split('.')[2]) |
| 45 |
|
| 46 |
if moderate == 0 and minor <= 17: |
| 47 |
print("Migrate from release: %s" % schema_version) |
| 48 |
# Update pending releases |
| 49 |
bank_list = banks.find() |
| 50 |
updated = 0 |
| 51 |
for bank in bank_list: |
| 52 |
if 'pending' in bank: |
| 53 |
# Check we have an old pending type |
| 54 |
if type(bank['pending']) == dict: |
| 55 |
updated += 1 |
| 56 |
pendings = [] |
| 57 |
for release in sorted(bank['pending'], key=lambda r: bank['pending'][r]): |
| 58 |
pendings.append({'release': str(release), 'id': bank['pending'][str(release)]}) |
| 59 |
if len(pendings) > 0: |
| 60 |
banks.update({'name': bank['name']}, |
| 61 |
{'$set': {'pending': pendings}}) |
| 62 |
else: |
| 63 |
# We remove old type for 'pending' |
| 64 |
banks.update({'name': bank['name']}, |
| 65 |
{'$unset': {'pending': ""}}) |
| 66 |
|
| 67 |
print("Migration: %d bank(s) updated" % updated) |
| 68 |
if moderate < 1: |
| 69 |
updated = 0 |
| 70 |
user_list = users.find() |
| 71 |
for user in user_list: |
| 72 |
if 'apikey' not in user: |
| 73 |
updated += 1 |
| 74 |
api_key = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(10)) |
| 75 |
users.update({'_id': user['_id']}, {'$set': {'apikey': api_key}}) |
| 76 |
print("Migration: %d user(s) updated" % updated) |
| 77 |
# production size |
| 78 |
bank_list = banks.find() |
| 79 |
updated = 0 |
| 80 |
for bank in bank_list: |
| 81 |
for prod in bank['production']: |
| 82 |
''' |
| 83 |
{ "_id" : ObjectId("54edb10856e8bb11340b5f51"), "production" : [ |
| 84 |
{ "freeze" : false, "remoterelease" : "2003-11-26", "session" : 1427809848.560108, |
| 85 |
"data_dir" : "/db", "formats" : [ ], "release" : "2003-11-26", |
| 86 |
"dir_version" : "ncbi/blast/alu", |
| 87 |
"prod_dir" : "alu-2003-11-26", "types" : [ ], "size" : 319432 } ] } |
| 88 |
''' |
| 89 |
if 'size' not in prod or prod['size'] == 0: |
| 90 |
logging.info('Calculate size for bank %s' % (bank['name'])) |
| 91 |
if 'data_dir' not in prod or not prod['data_dir'] or 'prod_dir' not in prod or not prod['prod_dir'] or 'dir_version' not in prod or not prod['dir_version']: |
| 92 |
logging.warn('no production directory information for %s, skipping...' % (bank['name'])) |
| 93 |
continue |
| 94 |
prod_dir = os.path.join(prod['data_dir'], prod['dir_version'], prod['prod_dir']) |
| 95 |
if not os.path.exists(prod_dir): |
| 96 |
logging.warn('production directory %s does not exists for %s, skipping...' % (prod_dir, bank['name'])) |
| 97 |
continue |
| 98 |
dir_size = Utils.get_folder_size(prod_dir) |
| 99 |
banks.update({'name': bank['name'], 'production.release': prod['release']}, {'$set': {'production.$.size': dir_size}}) |
| 100 |
updated += 1 |
| 101 |
print("Migration: %d bank production info updated" % updated) |
| 102 |
|
| 103 |
@staticmethod |
| 104 |
def add_property(bank=None, prop=None, value=None, cfg=None): |
| 105 |
""" |
| 106 |
Update properties field for banks. |
| 107 |
|
| 108 |
:param bank: Bank name to update, default all |
| 109 |
:type bank: str |
| 110 |
:param prop: New property to add |
| 111 |
:type prop: str |
| 112 |
:param value: Property value, if cfg set, value taken |
| 113 |
from bank configuration cfg key |
| 114 |
:type value: str |
| 115 |
:param cfg: Bank configuration key value is taken from |
| 116 |
:type cfg: str |
| 117 |
|
| 118 |
:raise Exception: If not configuration file found |
| 119 |
:returns: True/False |
| 120 |
:rtype: bool |
| 121 |
""" |
| 122 |
if BiomajConfig.global_config is None: |
| 123 |
try: |
| 124 |
BiomajConfig.load_config() |
| 125 |
except Exception as err: |
| 126 |
print("* SchemaVersion: Can't find config file: " + str(err)) |
| 127 |
return False |
| 128 |
if prop is None: |
| 129 |
print("Property key is required", file=sys.stderr) |
| 130 |
return False |
| 131 |
|
| 132 |
if MongoConnector.db is None: |
| 133 |
MongoConnector(BiomajConfig.global_config.get('GENERAL', 'db.url'), |
| 134 |
BiomajConfig.global_config.get('GENERAL', 'db.name')) |
| 135 |
|
| 136 |
schema = MongoConnector.db_schema |
| 137 |
banks = MongoConnector.banks |
| 138 |
schema_version = SchemaVersion.get_dbschema_version(schema) |
| 139 |
moderate = int(schema_version.split('.')[1]) |
| 140 |
minor = int(schema_version.split('.')[2]) |
| 141 |
|
| 142 |
if moderate <= 1 and minor <= 0: |
| 143 |
bank_list = [] |
| 144 |
if bank is None: |
| 145 |
bank_list = banks.find() |
| 146 |
else: |
| 147 |
bank_list = [banks.find_one({'name': bank})] |
| 148 |
updated = 0 |
| 149 |
for bank in bank_list: |
| 150 |
if 'properties' in bank: |
| 151 |
b = Bank(bank['name'], no_log=True) |
| 152 |
new_prop = 'properties.' + prop |
| 153 |
new_value = value |
| 154 |
if new_value is None: |
| 155 |
if cfg is not None: |
| 156 |
new_value = b.config.get(cfg) |
| 157 |
else: |
| 158 |
print("[%s] With value set to None, you must set cfg to get " |
| 159 |
"corresponding value" % str(bank['name']), file=sys.stderr) |
| 160 |
continue |
| 161 |
banks.update({'name': bank['name']}, |
| 162 |
{'$set': {new_prop: new_value}}) |
| 163 |
updated += 1 |
| 164 |
else: |
| 165 |
logging.warn("Bank %s does not have 'properties' field!" % str(bank['name'])) |
| 166 |
|
| 167 |
print("Add property: %d bank(s) updated" % updated) |
| 168 |
|
| 169 |
@staticmethod |
| 170 |
def get_dbschema_version(schema): |
| 171 |
""" |
| 172 |
Get the version of the actual schema version stored in database |
| 173 |
|
| 174 |
:param schema: Mongo schema info |
| 175 |
:type schema: MongoDB collection |
| 176 |
:returns: moderate, minor |
| 177 |
:rtype: int, int |
| 178 |
|
| 179 |
""" |
| 180 |
schema_version = schema.find_one({'id': 1}) |
| 181 |
if schema_version is None: |
| 182 |
schema_version = {'id': 1, 'version': '3.0.0'} |
| 183 |
schema.insert(schema_version) |
| 184 |
return schema_version['version'] |
| 185 |
|
| 186 |
@staticmethod |
| 187 |
def set_version(version=None): |
| 188 |
""" |
| 189 |
Set BioMAJ current installed version in db_schema collection if version is None |
| 190 |
|
| 191 |
:param version: db_schema collection version to set |
| 192 |
:type version: str |
| 193 |
""" |
| 194 |
installed_version = version |
| 195 |
if installed_version is None: |
| •196 |
installed_version = '3.1.24' |
| 197 |
if BiomajConfig.global_config is None: |
| 198 |
try: |
| 199 |
BiomajConfig.load_config() |
| 200 |
except Exception as err: |
| 201 |
print("* SchemaVersion: Can't find config file: " + str(err)) |
| 202 |
return None |
| 203 |
if MongoConnector.db is None: |
| 204 |
MongoConnector(BiomajConfig.global_config.get('GENERAL', 'db.url'), |
| 205 |
BiomajConfig.global_config.get('GENERAL', 'db.name')) |
| 206 |
schema = MongoConnector.db_schema |
| 207 |
schema.update_one({'id': 1}, {'$set': {'version': installed_version}}) |
| 208 |
print("Schema version set to %s" % str(installed_version)) |