1
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
import os |
|
|
|
|
5
|
|
|
import re |
6
|
|
|
import subprocess |
|
|
|
|
7
|
|
|
import sys |
8
|
|
|
from subprocess import check_output |
|
|
|
|
9
|
|
|
|
10
|
|
|
from db_sync_tool.utility import mode, system, helper, output |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
def check_configuration(client): |
14
|
|
|
""" |
15
|
|
|
Checking remote Symfony database configuration |
16
|
|
|
:param client: String |
17
|
|
|
:return: |
18
|
|
|
""" |
19
|
|
|
_path = system.config[client]['path'] |
20
|
|
|
|
21
|
|
|
# Check for symfony 2.8 |
22
|
|
|
if 'parameters.yml' in _path: |
23
|
|
|
_db_config = { |
24
|
|
|
'name': get_database_parameter(client, 'database_name', _path), |
25
|
|
|
'host': get_database_parameter(client, 'database_host', _path), |
26
|
|
|
'password': get_database_parameter(client, 'database_password', _path), |
27
|
|
|
'port': get_database_parameter(client, 'database_port', _path), |
28
|
|
|
'user': get_database_parameter(client, 'database_user', _path), |
29
|
|
|
} |
30
|
|
|
# Using for symfony >=3.4 |
31
|
|
|
else: |
32
|
|
|
stdout = mode.run_command( |
33
|
|
|
helper.get_command(client, 'grep') + ' -v "^#" ' + system.config[client][ |
34
|
|
|
'path'] + ' | ' + helper.get_command(client, 'grep') + ' DATABASE_URL', |
35
|
|
|
client, |
36
|
|
|
True |
37
|
|
|
) |
38
|
|
|
_db_config = parse_database_credentials(stdout) |
39
|
|
|
|
40
|
|
|
system.config[client]['db'] = _db_config |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
def parse_database_credentials(db_credentials): |
44
|
|
|
""" |
45
|
|
|
Parsing database credentials to needed format |
46
|
|
|
:param db_credentials: Dictionary |
47
|
|
|
:return: Dictionary |
48
|
|
|
""" |
49
|
|
|
db_credentials = str(db_credentials).replace('\\n\'','') |
|
|
|
|
50
|
|
|
# DATABASE_URL=mysql://db-user:1234@db-host:3306/db-name |
51
|
|
|
db_credentials = re.findall(r"\/{2}(.+):(.+)@(.+):(\d+)\/(.+)", db_credentials)[0] |
52
|
|
|
|
53
|
|
|
if len(db_credentials) != 5: |
54
|
|
|
sys.exit( |
55
|
|
|
output.message( |
56
|
|
|
output.Subject.ERROR, |
57
|
|
|
'Mismatch of expected database credentials', |
58
|
|
|
False |
59
|
|
|
) |
60
|
|
|
) |
61
|
|
|
|
62
|
|
|
_db_config = { |
63
|
|
|
'name': db_credentials[4], |
64
|
|
|
'host': db_credentials[2], |
65
|
|
|
'password': db_credentials[1], |
66
|
|
|
'port': db_credentials[3], |
67
|
|
|
'user': db_credentials[0], |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return _db_config |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
def get_database_parameter(client, name, file): |
74
|
|
|
""" |
75
|
|
|
Parsing a single database variable from the parameters.yml file |
76
|
|
|
hhttps://unix.stackexchange.com/questions/84922/extract-a-part-of-one-line-from-a-file-with-sed |
77
|
|
|
:param client: String |
78
|
|
|
:param name: String |
79
|
|
|
:param file: String |
80
|
|
|
:return: |
81
|
|
|
""" |
82
|
|
|
return mode.run_command( |
83
|
|
|
helper.get_command(client, 'sed') + f' -n -e \'/{name}/ s/.*\\: *//p\' {file}', |
84
|
|
|
client, |
85
|
|
|
True |
86
|
|
|
).replace('\n', '') |
|
|
|
|