|
8 | 8 | import os |
9 | 9 | import re |
10 | 10 | from . import occ_cloud |
11 | | -from .connectors import create_connection, internal_handle_db_connect_exception |
| 11 | +from .connectors import create_connection, internal_handle_db_connect_exception, test_connection |
12 | 12 |
|
13 | 13 |
|
14 | 14 | # @copyright Copyright (c) 2021 Andrey Borysenko <[email protected]> |
@@ -61,52 +61,111 @@ def init_cloud_config(map_schema: dict) -> bool: |
61 | 61 | return result |
62 | 62 |
|
63 | 63 |
|
64 | | -def detect_msql_socket(config: dict, _php_info: str) -> None: |
65 | | - global Warnings |
66 | | - socket_path = config['dbhost'].split(":")[-1] |
67 | | - if os.path.exists(socket_path): |
68 | | - config['usock'] = socket_path |
| 64 | +def get_basic_db_provider(dbtype: str) -> str: |
| 65 | + if dbtype == 'mysql': |
| 66 | + return 'pymysql' |
| 67 | + if dbtype == 'pgsql': |
| 68 | + return 'pg8000' |
| 69 | + return '' |
| 70 | + |
| 71 | + |
| 72 | +def check_db_config(config: dict) -> None: |
| 73 | + """Finding way to connect to database.""" |
| 74 | + if test_connection(config, get_basic_db_provider(config['dbtype'])): |
69 | 75 | return |
70 | | - if not config['dbport'] and _php_info: |
71 | | - m_groups = re.search(r'pdo_mysql\.default_socket\s*=>\s*(.*)\s*=>\s*(.*)', |
72 | | - _php_info, flags=re.MULTILINE + re.IGNORECASE) |
73 | | - if m_groups is None: |
74 | | - Warnings.append("Cant parse php info.") |
| 76 | + # lets try without socket. |
| 77 | + if config['usock']: |
| 78 | + usock = config['usock'] |
| 79 | + config['usock'] = '' |
| 80 | + if test_connection(config, get_basic_db_provider(config['dbtype'])): |
75 | 81 | return |
76 | | - socket_path = m_groups.groups()[-1].strip() |
77 | | - if os.path.exists(socket_path): |
78 | | - config['usock'] = socket_path |
| 82 | + config['usock'] = usock |
79 | 83 |
|
80 | 84 |
|
81 | | -def detect_pgsql_socket(config: dict, _php_info: str) -> None: |
82 | | - socket_path = config['dbhost'].split(":")[-1] |
83 | | - if os.path.exists(socket_path): |
84 | | - config['usock'] = socket_path |
| 85 | +def postprocess_config(config: dict) -> None: |
| 86 | + """Parses 'dbhost' value. Possible values: host:port, host:socket, host, socket, nil""" |
| 87 | + global Warnings |
| 88 | + host_port_socket = config['dbhost'].split(":", maxsplit=1) |
| 89 | + if len(host_port_socket) > 1: |
| 90 | + # when dbhost = host:port or host:socket |
| 91 | + config['dbhost'] = host_port_socket[0] |
| 92 | + if os.path.exists(host_port_socket[1]): |
| 93 | + config['usock'] = host_port_socket[1] |
| 94 | + elif host_port_socket[1].isdigit(): |
| 95 | + config['dbport'] = host_port_socket[1] |
| 96 | + else: |
| 97 | + Warnings.append("Unknown socket or port value.") |
| 98 | + elif os.path.exists(config['dbhost']): |
| 99 | + # when dbhost = socket |
| 100 | + config['usock'] = config['dbhost'] |
| 101 | + config['dbhost'] = '' |
| 102 | + if config['dbtype'] == 'pgsql': |
| 103 | + # Don't know currently how to handle this situation properly. Using default port value for socket name. |
| 104 | + if config['usock']: |
| 105 | + config['usock'] += '.s.PGSQL.5432' |
| 106 | + |
| 107 | + |
| 108 | +def find_db_configuration() -> bool: |
| 109 | + """Finding working way to connect to database, if will found global `Config` will be changed according to it.""" |
| 110 | + global Warnings, Config |
| 111 | + # Trying what we parsed from cloud config. |
| 112 | + if test_connection(Config, get_basic_db_provider(Config['dbtype'])): |
| 113 | + return True |
| 114 | + # Trying without socket. |
| 115 | + if Config['usock']: |
| 116 | + usock = Config['usock'] |
| 117 | + Config['usock'] = '' |
| 118 | + if test_connection(Config, get_basic_db_provider(Config['dbtype'])): |
| 119 | + return True |
| 120 | + Config['usock'] = usock |
| 121 | + if Config['dbtype'] == 'mysql': |
| 122 | + # when no dbport or usock found in cloud config, trying php socket configuration. |
| 123 | + php_ok, php_info = occ_cloud.php_call('-r', "phpinfo();") |
| 124 | + if not php_ok: |
| 125 | + Warnings.append("Cant get php info.") |
| 126 | + else: |
| 127 | + m_groups = re.search(r'pdo_mysql\.default_socket\s*=>\s*(.*)\s*=>\s*(.*)', |
| 128 | + php_info, flags=re.MULTILINE + re.IGNORECASE) |
| 129 | + if m_groups is None: |
| 130 | + Warnings.append("Cant parse php info.") |
| 131 | + else: |
| 132 | + socket_path = m_groups.groups()[-1].strip() |
| 133 | + if os.path.exists(socket_path): |
| 134 | + usock = Config['usock'] |
| 135 | + Config['usock'] = socket_path |
| 136 | + if test_connection(Config, get_basic_db_provider(Config['dbtype'])): |
| 137 | + return True |
| 138 | + Config['usock'] = usock |
| 139 | + # If we got here then all is not so good, as it can be. Last try with default host and port. |
| 140 | + host = Config['dbhost'] |
| 141 | + port = Config['dbport'] |
| 142 | + Config['dbhost'] = '' |
| 143 | + Config['dbport'] = '' |
| 144 | + if test_connection(Config, get_basic_db_provider(Config['dbtype'])): |
| 145 | + return True |
| 146 | + Config['dbhost'] = host |
| 147 | + Config['dbport'] = port |
| 148 | + return False |
85 | 149 |
|
86 | 150 |
|
87 | 151 | def init(): |
88 | | - global Config, DatabaseProvider, Warnings |
| 152 | + global Config, DatabaseProvider |
89 | 153 | map_schema = {'datadir': 'datadirectory', |
90 | 154 | 'dbname': 'dbname', |
91 | 155 | 'dbtprefix': 'dbtableprefix', |
92 | 156 | 'dbuser': 'dbuser', |
93 | 157 | 'dbpassword': 'dbpassword', |
94 | 158 | 'dbhost': 'dbhost', |
95 | | - 'dbport': 'dbport', |
96 | 159 | 'dbtype': 'dbtype'} |
97 | 160 | Config['usock'] = '' |
| 161 | + Config['dbport'] = '' |
98 | 162 | if not init_cloud_config(map_schema): |
99 | 163 | return DbType.UNKNOWN |
100 | | - php_ok, php_info = occ_cloud.php_call('-r', "phpinfo();") |
101 | | - if not php_ok: |
102 | | - php_info = '' |
103 | | - Warnings.append("Cant get php info.") |
| 164 | + postprocess_config(Config) |
104 | 165 | if Config['dbtype'] == 'mysql': |
105 | | - detect_msql_socket(Config, php_info) |
106 | 166 | DatabaseProvider = 'pymysql' |
107 | 167 | return DbType.MYSQL |
108 | 168 | if Config['dbtype'] == 'pgsql': |
109 | | - detect_pgsql_socket(Config, php_info) |
110 | 169 | DatabaseProvider = 'pg8000' |
111 | 170 | return DbType.PGSQL |
112 | 171 | if Config['dbtype'] == 'oci': |
@@ -238,6 +297,7 @@ def check_db() -> list: |
238 | 297 | return Errors |
239 | 298 | if not DatabaseProvider: |
240 | 299 | return [f"Unsupported DB type:`{Config['dbtype']}`"] |
| 300 | + find_db_configuration() |
241 | 301 | ret = [] |
242 | 302 | success = False |
243 | 303 | try: |
|
0 commit comments