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