| 1 |
from builtins import str |
| 2 |
from builtins import object |
| 3 |
import logging |
| 4 |
import logging.config |
| 5 |
import os |
| 6 |
import time |
| 7 |
import sys |
| 8 |
|
| 9 |
from biomaj_core.utils import Utils |
| 10 |
from biomaj_core.bmajindex import BmajIndex |
| 11 |
|
| 12 |
import configparser |
| 13 |
|
| 14 |
|
| 15 |
class BiomajConfig(object): |
| 16 |
""" |
| 17 |
Manage Biomaj configuration |
| 18 |
""" |
| 19 |
|
| 20 |
DEFAULTS = { |
| 21 |
'http.parse.dir.line': r'<img[\s]+src="[\S]+"[\s]+alt="\[DIR\]"[\s]*/?>[\s]*<a[\s]+href="([\S]+)/"[\s]*>.*([\d]{2}-[\w\d]{2,5}-[\d]{4}\s[\d]{2}:[\d]{2})', |
| 22 |
'http.parse.file.line': r'<img[\s]+src="[\S]+"[\s]+alt="\[[\s]+\]"[\s]*/?>[\s]<a[\s]+href="([\S]+)".*([\d]{2}-[\w\d]{2,5}-[\d]{4}\s[\d]{2}:[\d]{2})[\s]+([\d\.]+[MKG]{0,1})', |
| 23 |
'http.group.dir.name': 1, |
| 24 |
'http.group.dir.date': 2, |
| 25 |
'http.group.file.name': 1, |
| 26 |
'http.group.file.date': 2, |
| 27 |
'http.group.file.size': 3, |
| 28 |
'visibility.default': 'public', |
| 29 |
'historic.logfile.level': 'INFO', |
| 30 |
'bank.num.threads': 2, |
| 31 |
'files.num.threads': 4, |
| 32 |
'use_elastic': 0, |
| 33 |
'use_drmaa': 0, |
| 34 |
'db.type': '', |
| 35 |
'db.formats': '', |
| 36 |
'keep.old.version': 1, |
| 37 |
'keep.old.sessions': 0, |
| 38 |
'docker.sudo': '1', |
| 39 |
'auto_publish': 0, |
| 40 |
'use_hardlinks': 0, |
| 41 |
'wait_policy': 'wait_fixed(3)', |
| 42 |
'stop_condition': 'stop_after_attempt(3)', |
| 43 |
'ssh_hosts_file': os.path.expanduser('~/.ssh/known_hosts'), |
| 44 |
'ssh_new_host': 'reject', |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
LOGLEVEL = { |
| 49 |
'DEBUG': logging.DEBUG, |
| 50 |
'VERBOSE': logging.INFO, |
| 51 |
'INFO': logging.INFO, |
| 52 |
'WARN': logging.WARNING, |
| 53 |
'ERR': logging.ERROR |
| 54 |
} |
| 55 |
|
| 56 |
""" |
| 57 |
Global configuration file |
| 58 |
""" |
| 59 |
global_config = None |
| 60 |
|
| 61 |
""" |
| 62 |
Per use global configuration file, overriding global_config |
| 63 |
""" |
| 64 |
user_config = None |
| 65 |
allow_user_config = False |
| 66 |
|
| 67 |
@staticmethod |
| 68 |
def load_config(config_file=None, allow_user_config=True): |
| 69 |
""" |
| 70 |
Loads general config |
| 71 |
|
| 72 |
:param config_file: global.properties file path |
| 73 |
:type config_file: str |
| 74 |
:param allow_user_config: use ~/.biomaj.cfg if present |
| 75 |
:type allow_user_config: bool |
| 76 |
""" |
| 77 |
BiomajConfig.allow_user_config = allow_user_config |
| 78 |
|
| 79 |
if config_file is None: |
| 80 |
env_file = os.environ.get('BIOMAJ_CONF') |
| 81 |
if env_file is not None and os.path.exists(env_file): |
| 82 |
config_file = env_file |
| 83 |
else: |
| 84 |
env_file = 'global.properties' |
| 85 |
if os.path.exists(env_file): |
| 86 |
config_file = env_file |
| 87 |
|
| 88 |
if config_file is None or not os.path.exists(config_file): |
| 89 |
raise Exception('Missing global configuration file') |
| 90 |
|
| 91 |
BiomajConfig.config_file = os.path.abspath(config_file) |
| 92 |
|
| 93 |
BiomajConfig.global_config = configparser.ConfigParser() |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
BiomajConfig.global_config.read([config_file]) |
| 103 |
|
| 104 |
|
| 105 |
do_index = False |
| 106 |
if BiomajConfig.global_config.get('GENERAL', 'use_elastic') and \ |
| 107 |
BiomajConfig.global_config.get('GENERAL', 'use_elastic') == "1": |
| 108 |
do_index = True |
| 109 |
if do_index: |
| 110 |
if BiomajConfig.global_config.get('GENERAL', 'elastic_nodes'): |
| 111 |
elastic_hosts = BiomajConfig.global_config.get('GENERAL', |
| 112 |
'elastic_nodes')\ |
| 113 |
.split(',') |
| 114 |
else: |
| 115 |
elastic_hosts = ['localhost'] |
| 116 |
elastic_index = BiomajConfig.global_config.get('GENERAL', 'elastic_index') |
| 117 |
if elastic_index is None: |
| 118 |
elastic_index = 'biomaj' |
| 119 |
|
| 120 |
if BiomajConfig.global_config.has_option('GENERAL', 'test') and \ |
| 121 |
BiomajConfig.global_config.get('GENERAL', 'test') == "1": |
| 122 |
|
| 123 |
BmajIndex.skip_if_failure = True |
| 124 |
|
| 125 |
BmajIndex.load(index=elastic_index, |
| 126 |
hosts=elastic_hosts, |
| 127 |
do_index=do_index) |
| 128 |
|
| 129 |
def __init__(self, bank, options=None): |
| 130 |
""" |
| 131 |
Loads bank configuration |
| 132 |
|
| 133 |
:param bank: bank name |
| 134 |
:type bank: str |
| 135 |
:param options: bank options |
| 136 |
:type options: argparse |
| 137 |
""" |
| 138 |
self.log_level = logging.INFO |
| 139 |
self.name = bank |
| 140 |
if BiomajConfig.global_config is None: |
| 141 |
BiomajConfig.load_config() |
| 142 |
self.config_bank = configparser.ConfigParser() |
| 143 |
conf_dir = BiomajConfig.global_config.get('GENERAL', 'conf.dir') |
| 144 |
if not os.path.exists(os.path.join(conf_dir, bank + '.properties')): |
| 145 |
logging.error('Bank configuration file does not exists') |
| 146 |
raise Exception('Configuration file ' + bank + |
| 147 |
'.properties does not exists') |
| 148 |
try: |
| 149 |
config_files = [BiomajConfig.config_file] |
| 150 |
self.user_config_file = None |
| 151 |
if self.allow_user_config and hasattr(options, 'user')\ |
| 152 |
and options.user\ |
| 153 |
and os.path.exists(os.path.expanduser('~' + options.user + '/.biomaj.cfg')): |
| 154 |
self.user_config_file = os.path.expanduser('~/' + options.user + |
| 155 |
'.biomaj.cfg') |
| 156 |
self.user_config = configparser.ConfigParser() |
| 157 |
self.user_config.read([os.path.expanduser('~/' + options.user + |
| 158 |
'.biomaj.cfg')]) |
| 159 |
|
| 160 |
if self.user_config_file is not None: |
| 161 |
config_files.append(self.user_config_file) |
| 162 |
config_files.append(os.path.join(conf_dir, bank + '.properties')) |
| 163 |
self.config_bank.read(config_files) |
| 164 |
except Exception as e: |
| 165 |
logging.error("Configuration file error " + str(e)) |
| 166 |
raise Exception("Configuration file error " + str(e)) |
| 167 |
|
| 168 |
self.last_modified = int(os.stat(os.path.join(conf_dir, bank + |
| 169 |
'.properties')).st_mtime) |
| 170 |
|
| 171 |
do_log = False |
| 172 |
if options is None: |
| 173 |
do_log = True |
| 174 |
elif hasattr(options, 'no_log') and not options.no_log: |
| 175 |
do_log = True |
| 176 |
elif type(options) is dict and 'no_log' in options and not options['no_log']: |
| 177 |
do_log = True |
| 178 |
|
| 179 |
if do_log: |
| 180 |
if os.path.exists(os.path.expanduser('~/.biomaj.cfg')): |
| 181 |
logging.config.fileConfig(os.path.expanduser('~/.biomaj.cfg')) |
| 182 |
else: |
| 183 |
logging.config.fileConfig(BiomajConfig.config_file) |
| 184 |
|
| 185 |
logger = logging.getLogger() |
| 186 |
bank_log_dir = os.path.join(self.get('log.dir'), bank, str(time.time())) |
| 187 |
if not os.path.exists(bank_log_dir): |
| 188 |
os.makedirs(bank_log_dir) |
| 189 |
hdlr = logging.FileHandler(os.path.join(bank_log_dir, bank + '.log')) |
| 190 |
self.log_file = os.path.join(bank_log_dir, bank + '.log') |
| 191 |
if options is not None and options.get_option('log') is not None: |
| 192 |
hdlr.setLevel(BiomajConfig.LOGLEVEL[options.get_option('log')]) |
| 193 |
self.log_level = BiomajConfig.LOGLEVEL[options.get_option('log')] |
| 194 |
logger.setLevel(self.log_level) |
| 195 |
else: |
| 196 |
hdlr.setLevel(BiomajConfig.LOGLEVEL[self.get('historic.logfile.level')]) |
| 197 |
self.log_level = BiomajConfig.LOGLEVEL[self.get('historic.logfile.level')] |
| 198 |
logger.setLevel(self.log_level) |
| 199 |
formatter = logging.Formatter('%(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s') |
| 200 |
hdlr.setFormatter(formatter) |
| 201 |
logger.addHandler(hdlr) |
| 202 |
''' |
| 203 |
# For monolithic, update default logger/handlers? |
| 204 |
existing_logger_names = list(logging.getLogger().manager.loggerDict.keys()) |
| 205 |
for existing_logger in existing_logger_names: |
| 206 |
if existing_logger.startswith('biomaj'): |
| 207 |
tmp_logger = logging.getLogger(existing_logger) |
| 208 |
tmp_logger.setLevel(self.log_level) |
| 209 |
for handler in tmp_logger.handlers: |
| 210 |
handler.setLevel(self.log_level) |
| 211 |
''' |
| 212 |
else: |
| 213 |
self.log_file = 'none' |
| 214 |
|
| 215 |
cache_dir = self.get('cache.dir') |
| 216 |
if cache_dir is None: |
| 217 |
print("Configuration file error: cache.dir empty") |
| 218 |
logging.error("cache.dir is not defined") |
| 219 |
sys.exit(1) |
| 220 |
|
| 221 |
if not os.path.exists(cache_dir): |
| 222 |
os.makedirs(cache_dir) |
| 223 |
|
| 224 |
process_dir = self.get('process.dir') |
| 225 |
if process_dir is None: |
| 226 |
print("Configuration file error: process.dir empty") |
| 227 |
logging.error("process.dir is not defined") |
| 228 |
sys.exit(1) |
| 229 |
|
| 230 |
if not os.path.exists(process_dir): |
| 231 |
os.makedirs(process_dir) |
| 232 |
|
| 233 |
data_dir = self.get('data.dir') |
| 234 |
if data_dir is None: |
| 235 |
print("Configuration file error: data.dir empty") |
| 236 |
logging.error("data.dir is not defined") |
| 237 |
sys.exit(1) |
| 238 |
|
| 239 |
if not os.path.exists(data_dir): |
| 240 |
os.makedirs(data_dir) |
| 241 |
|
| 242 |
lock_dir = self.get('lock.dir') |
| 243 |
if lock_dir is None: |
| 244 |
print("Configuration file error: lock.dir empty") |
| 245 |
logging.error("lock.dir is not defined") |
| 246 |
sys.exit(1) |
| 247 |
|
| 248 |
if not os.path.exists(lock_dir): |
| 249 |
os.makedirs(lock_dir) |
| 250 |
|
| 251 |
def reset_logger(self): |
| 252 |
''' |
| 253 |
Reconfigure logging |
| 254 |
''' |
| 255 |
if self.log_file == 'none': |
| 256 |
return |
| 257 |
if os.path.exists(os.path.expanduser('~/.biomaj.cfg')): |
| 258 |
logging.config.fileConfig(os.path.expanduser('~/.biomaj.cfg')) |
| 259 |
else: |
| 260 |
logging.config.fileConfig(BiomajConfig.config_file) |
| 261 |
|
| 262 |
logger = logging.getLogger() |
| 263 |
hdlr = logging.FileHandler(self.log_file) |
| 264 |
hdlr.setLevel(self.log_level) |
| 265 |
formatter = logging.Formatter('%(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s') |
| 266 |
hdlr.setFormatter(formatter) |
| 267 |
logger.addHandler(hdlr) |
| 268 |
|
| 269 |
def set(self, prop, value, section='GENERAL'): |
| 270 |
self.config_bank.set(section, prop, value) |
| 271 |
|
| 272 |
def _in_env(self, prop): |
| 273 |
''' |
| 274 |
Search for env variable BIOMAJ_X_Y_Z for property X.Y.Z |
| 275 |
''' |
| 276 |
env_prop = 'BIOMAJ_' + prop.upper().replace('.', '_') |
| 277 |
if env_prop in os.environ: |
| 278 |
return os.environ[env_prop] |
| 279 |
else: |
| 280 |
return None |
| 281 |
|
| 282 |
def get_bool(self, prop, section='GENERAL', escape=True, default=None): |
| 283 |
""" |
| 284 |
Get a boolean property from bank or general configration. Optionally in section. |
| 285 |
""" |
| 286 |
value = self.get(prop, section, escape, default) |
| 287 |
|
| 288 |
return Utils.to_bool(value) |
| 289 |
|
| 290 |
def get(self, prop, section='GENERAL', escape=True, default=None): |
| 291 |
""" |
| 292 |
Get a property from bank or general configration. Optionally in section. |
| 293 |
""" |
| 294 |
if self._in_env(prop): |
| 295 |
return self._in_env(prop) |
| 296 |
|
| 297 |
if prop == 'depends': |
| 298 |
depend = self.get('db.source', section, escape, None) |
| 299 |
if depend: |
| 300 |
return depend |
| 301 |
|
| 302 |
if self.config_bank.has_option(section, prop): |
| 303 |
val = self.config_bank.get(section, prop) |
| 304 |
if prop == 'remote.dir' and not val.endswith('/'): |
| 305 |
val = val + '/' |
| 306 |
|
| 307 |
if escape and (prop == 'local.files' or prop == 'remote.files' or |
| 308 |
prop == 'http.parse.dir.line' or |
| 309 |
prop == 'http.parse.file.line'): |
| 310 |
val = val.replace('\\\\', '\\') |
| 311 |
return val |
| 312 |
|
| 313 |
if self.user_config is not None: |
| 314 |
if self.user_config.has_option(section, prop): |
| 315 |
return self.user_config.get(section, prop) |
| 316 |
|
| 317 |
if BiomajConfig.global_config.has_option(section, prop): |
| 318 |
return BiomajConfig.global_config.get(section, prop) |
| 319 |
|
| 320 |
if prop in BiomajConfig.DEFAULTS: |
| 321 |
return BiomajConfig.DEFAULTS[prop] |
| 322 |
|
| 323 |
return default |
| 324 |
|
| 325 |
def get_time(self): |
| 326 |
""" |
| 327 |
Return last modification time of config files |
| 328 |
""" |
| 329 |
return self.last_modified |
| 330 |
|
| 331 |
def check(self): |
| 332 |
""" |
| 333 |
Check configuration |
| 334 |
""" |
| 335 |
self.set('localrelease', '') |
| 336 |
self.set('remoterelease', '') |
| 337 |
|
| 338 |
status = True |
| 339 |
if not self.get('data.dir'): |
| 340 |
logging.error('data.dir is not set') |
| 341 |
status = False |
| 342 |
if not self.get('conf.dir'): |
| 343 |
logging.error('conf.dir is not set') |
| 344 |
status = False |
| 345 |
if not self.get('log.dir'): |
| 346 |
logging.error('log.dir is not set') |
| 347 |
status = False |
| 348 |
if not self.get('process.dir'): |
| 349 |
logging.error('process.dir is not set') |
| 350 |
status = False |
| 351 |
if not self.get('lock.dir'): |
| 352 |
logging.error('lock.dir is not set') |
| 353 |
status = False |
| 354 |
if not self.get('cache.dir'): |
| 355 |
logging.error('cache.dir is not set') |
| 356 |
status = False |
| 357 |
if not self.get('db.fullname'): |
| 358 |
logging.warn('db.fullname is not set') |
| 359 |
if not self.get('db.formats'): |
| 360 |
logging.warn('db.formats is not set') |
| 361 |
if self.get_bool('use_ldap'): |
| 362 |
if not self.get('ldap.host') or not self.get('ldap.port') or \ |
| 363 |
not self.get('ldap.dn'): |
| 364 |
logging.error('use_ldap set to 1 but missing configuration') |
| 365 |
status = False |
| 366 |
if self.get_bool('use_elastic'): |
| 367 |
if not self.get('elastic_nodes') or not self.get('elastic_index'): |
| 368 |
logging.error('use_elastic set to 1 but missing configuration') |
| 369 |
status = False |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
if not self.get('mail.smtp.host'): |
| 375 |
logging.warn('SMTP mail config not set, you will not be able to send emails') |
| 376 |
if self.get('mail.smtp.host') and not self.get('mail.from'): |
| 377 |
logging.error('Mail origin mail.from not set') |
| 378 |
status = False |
| 379 |
|
| 380 |
if not self.get('offline.dir.name'): |
| 381 |
logging.error('offline.dir.name is not set') |
| 382 |
status = False |
| 383 |
elif self.get('offline.dir.name').startswith('/'): |
| 384 |
logging.error('offline dir must be relative to data.dir and should not start with a /') |
| 385 |
status = False |
| 386 |
if not self.get('dir.version'): |
| 387 |
logging.error('dir.version is not set') |
| 388 |
status = False |
| 389 |
elif self.get('dir.version').startswith('/'): |
| 390 |
logging.error('dir.version must be relative to data.dir and should not start with a /') |
| 391 |
status = False |
| 392 |
if not self.get('protocol'): |
| 393 |
logging.error('protocol is not set') |
| 394 |
status = False |
| 395 |
else: |
| 396 |
protocol = self.get('protocol') |
| 397 |
allowed_protocols = ['none', 'multi', 'local', 'ftp', 'ftps', 'http', 'https', |
| 398 |
'directftp', 'directftps', 'directhttp', 'directhttps', 'rsync', 'irods'] |
| 399 |
if protocol not in allowed_protocols: |
| 400 |
logging.error('Protocol not supported: ' + protocol) |
| 401 |
status = False |
| 402 |
if protocol not in ['multi', 'none']: |
| 403 |
if protocol != 'local' and not self.get('server'): |
| 404 |
logging.error('server not set') |
| 405 |
status = False |
| 406 |
if not self.get('remote.dir'): |
| 407 |
logging.error('remote.dir not set') |
| 408 |
status = False |
| 409 |
elif not self.get('remote.dir').endswith('/'): |
| 410 |
logging.error('remote.dir must end with a /') |
| 411 |
return False |
| 412 |
if protocol not in ['directftp', 'directftps', 'directhttp', 'directhttps'] and\ |
| 413 |
not self.get('remote.files') and\ |
| 414 |
not self.get('remote.list'): |
| 415 |
logging.error('remote.files not set') |
| 416 |
status = False |
| 417 |
if 'irods' in protocol: |
| 418 |
if not self.get('irods.user'): |
| 419 |
logging.error('irods user is not set') |
| 420 |
status = False |
| 421 |
if not self.get('irods.password'): |
| 422 |
logging.error('irods password is not set') |
| 423 |
status = False |
| 424 |
if not self.get('irods.port'): |
| 425 |
logging.error('irods port is not set') |
| 426 |
status = False |
| 427 |
if not self.get('irods.protocol'): |
| 428 |
logging.error('irods protocol is not set') |
| 429 |
status = False |
| 430 |
if not self.get('local.files'): |
| 431 |
logging.error('local.files is not set') |
| 432 |
status = False |
| 433 |
|
| 434 |
processes = ['db.remove.process', 'db.pre.process'] |
| 435 |
for process in processes: |
| 436 |
if self.get(process): |
| 437 |
metas = self.get(process).split(',') |
| 438 |
for meta in metas: |
| 439 |
if not self.get(meta): |
| 440 |
logging.error('Metaprocess ' + meta + ' not defined') |
| 441 |
status = False |
| 442 |
else: |
| 443 |
procs = self.get(meta).split(',') |
| 444 |
for proc in procs: |
| 445 |
if not self.get(proc + '.name'): |
| 446 |
logging.error('Process ' + proc + ' not defined') |
| 447 |
status = False |
| 448 |
else: |
| 449 |
if not self.get(proc + '.exe'): |
| 450 |
logging.error('Process exe for ' + proc + |
| 451 |
' not defined') |
| 452 |
status = False |
| 453 |
if self.get(proc + '.args') is None: |
| 454 |
logging.error( |
| 455 |
'Process args for ' + proc + |
| 456 |
' not defined') |
| 457 |
status = False |
| 458 |
|
| 459 |
if self.get('BLOCKS'): |
| 460 |
blocks = self.get('BLOCKS').split(',') |
| 461 |
for block in blocks: |
| 462 |
if not self.get(block + '.db.post.process'): |
| 463 |
logging.error('Block ' + block + ' not defined') |
| 464 |
status = False |
| 465 |
else: |
| 466 |
metas = self.get(block + '.db.post.process').split(',') |
| 467 |
for meta in metas: |
| 468 |
if not self.get(meta): |
| 469 |
logging.error('Metaprocess ' + meta + ' not defined') |
| 470 |
status = False |
| 471 |
else: |
| 472 |
procs = self.get(meta).split(',') |
| 473 |
for proc in procs: |
| 474 |
if not self.get(proc + '.name'): |
| 475 |
logging.error('Process ' + proc + |
| 476 |
' not defined') |
| 477 |
status = False |
| 478 |
else: |
| 479 |
if not self.get(proc + '.exe'): |
| 480 |
logging.error('Process exe for ' + |
| 481 |
proc + ' not defined') |
| 482 |
status = False |
| 483 |
if self.get(proc + '.args') is None: |
| 484 |
logging.error('Process args for ' + |
| 485 |
proc + ' not defined') |
| 486 |
status = False |
| 487 |
return status |