-
Notifications
You must be signed in to change notification settings - Fork 239
/
__init__.py
88 lines (72 loc) · 2.46 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Copyright 2018 Iguazio
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from urllib.parse import urlparse
from ..config import config
from ..platforms import add_or_refresh_credentials
from .base import RunDBError, RunDBInterface # noqa
from .filedb import FileRunDB
from .sqldb import SQLDB
from os import environ
def get_or_set_dburl(default=""):
if not config.dbpath and default:
config.dbpath = default
environ["MLRUN_DBPATH"] = default
return config.dbpath
def get_httpdb_kwargs(host, username, password):
username = username or config.httpdb.user
password = password or config.httpdb.password
username, password, token = add_or_refresh_credentials(
host, username, password, config.httpdb.token
)
return {
"user": username,
"password": password,
"token": token,
}
_run_db = None
_last_db_url = None
def get_run_db(url="", secrets=None, force_reconnect=False):
"""Returns the runtime database"""
global _run_db, _last_db_url
if not url:
url = get_or_set_dburl("./")
if (
_last_db_url is not None
and url == _last_db_url
and _run_db
and not force_reconnect
):
return _run_db
_last_db_url = url
parsed_url = urlparse(url)
scheme = parsed_url.scheme.lower()
kwargs = {}
if "://" not in url or scheme in ["file", "s3", "v3io", "v3ios"]:
cls = FileRunDB
elif scheme in ("http", "https"):
# import here to avoid circular imports
from .httpdb import HTTPRunDB
cls = HTTPRunDB
kwargs = get_httpdb_kwargs(
parsed_url.hostname, parsed_url.username, parsed_url.password
)
endpoint = parsed_url.hostname
if parsed_url.port:
endpoint += ":{}".format(parsed_url.port)
url = f"{parsed_url.scheme}://{endpoint}{parsed_url.path}"
else:
cls = SQLDB
_run_db = cls(url, **kwargs)
_run_db.connect(secrets=secrets)
return _run_db