1
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
import sys |
5
|
|
|
from db_sync_tool.utility import mode, system, output, helper |
6
|
|
|
from db_sync_tool.remote import client as remote_client, utility as remote_utility |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class Framework: |
|
|
|
|
10
|
|
|
TYPO3 = 'TYPO3' |
11
|
|
|
SYMFONY = 'Symfony' |
12
|
|
|
DRUPAL = 'Drupal' |
13
|
|
|
WORDPRESS = 'Wordpress' |
14
|
|
|
MANUAL = 'Manual' |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
def get_database_configuration(client): |
18
|
|
|
""" |
19
|
|
|
Getting database configuration of given client and defined sync base (framework type) |
20
|
|
|
:param client: String |
21
|
|
|
:return: |
22
|
|
|
""" |
23
|
|
|
system.config['db'] = {} |
24
|
|
|
|
25
|
|
|
# check framework type |
26
|
|
|
_base = '' |
27
|
|
|
if 'type' in system.config: |
28
|
|
|
_type = system.config['type'].lower() |
29
|
|
|
if _type == 'typo3': |
30
|
|
|
# TYPO3 sync base |
31
|
|
|
_base = Framework.TYPO3 |
32
|
|
|
elif _type == 'symfony': |
33
|
|
|
# Symfony sync base |
34
|
|
|
_base = Framework.SYMFONY |
35
|
|
|
elif _type == 'drupal': |
36
|
|
|
# Symfony sync base |
37
|
|
|
_base = Framework.DRUPAL |
38
|
|
|
elif _type == 'wordpress': |
39
|
|
|
# Symfony sync base |
40
|
|
|
_base = Framework.WORDPRESS |
41
|
|
|
else: |
42
|
|
|
sys.exit( |
43
|
|
|
output.message( |
44
|
|
|
output.Subject.ERROR, |
45
|
|
|
f'Framework type not supported: {_type}', |
46
|
|
|
False |
47
|
|
|
) |
48
|
|
|
) |
49
|
|
|
elif 'db' in system.config['origin'] or 'db' in system.config['target']: |
50
|
|
|
_base = Framework.MANUAL |
51
|
|
|
else: |
52
|
|
|
# Default is TYPO3 sync base |
53
|
|
|
_base = Framework.TYPO3 |
54
|
|
|
|
55
|
|
|
sys.path.append('../recipes') |
56
|
|
|
if _base == Framework.TYPO3: |
57
|
|
|
# Import TYPO3 parser |
58
|
|
|
from ..recipes import typo3 |
|
|
|
|
59
|
|
|
_parser = typo3 |
60
|
|
|
|
61
|
|
|
elif _base == Framework.SYMFONY: |
62
|
|
|
# Import Symfony parser |
63
|
|
|
from ..recipes import symfony |
|
|
|
|
64
|
|
|
_parser = symfony |
65
|
|
|
|
66
|
|
|
elif _base == Framework.DRUPAL: |
67
|
|
|
# Import Symfony parser |
68
|
|
|
from ..recipes import drupal |
|
|
|
|
69
|
|
|
_parser = drupal |
70
|
|
|
|
71
|
|
|
elif _base == Framework.WORDPRESS: |
72
|
|
|
# Import Symfony parser |
73
|
|
|
from ..recipes import wordpress |
|
|
|
|
74
|
|
|
_parser = wordpress |
75
|
|
|
|
76
|
|
|
if client == mode.Client.ORIGIN: |
77
|
|
|
output.message( |
78
|
|
|
output.Subject.INFO, |
79
|
|
|
'Sync base: ' + _base, |
80
|
|
|
True |
81
|
|
|
) |
82
|
|
|
|
83
|
|
|
if _base != Framework.MANUAL: |
84
|
|
|
load_parser(client, _parser) |
|
|
|
|
85
|
|
|
else: |
86
|
|
|
if client == mode.Client.ORIGIN and mode.is_origin_remote(): |
87
|
|
|
remote_client.load_ssh_client_origin() |
88
|
|
|
elif client == mode.Client.TARGET and mode.is_target_remote(): |
89
|
|
|
remote_client.load_ssh_client_target() |
90
|
|
|
|
91
|
|
|
validate_database_credentials(client) |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
def load_parser(client, parser): |
95
|
|
|
""" |
96
|
|
|
Loading parser and checking database configuration |
97
|
|
|
:param client: |
98
|
|
|
:param parser: |
99
|
|
|
:return: |
100
|
|
|
""" |
101
|
|
|
output.message( |
102
|
|
|
output.host_to_subject(client), |
103
|
|
|
'Checking database configuration', |
104
|
|
|
True |
105
|
|
|
) |
106
|
|
|
if client == mode.Client.ORIGIN: |
107
|
|
|
if mode.is_origin_remote(): |
108
|
|
|
remote_client.load_ssh_client_origin() |
109
|
|
|
else: |
110
|
|
|
remote_utility.run_before_script(client) |
111
|
|
|
else: |
112
|
|
|
if mode.is_target_remote(): |
113
|
|
|
remote_client.load_ssh_client_target() |
114
|
|
|
else: |
115
|
|
|
remote_utility.run_before_script(client) |
116
|
|
|
|
117
|
|
|
_path = system.config[client]['path'] |
118
|
|
|
if not helper.check_file_exists(client, _path): |
119
|
|
|
sys.exit( |
120
|
|
|
output.message( |
121
|
|
|
output.Subject.ERROR, |
122
|
|
|
f'Database configuration for {client} not found: {_path}', |
123
|
|
|
False |
124
|
|
|
) |
125
|
|
|
) |
126
|
|
|
parser.check_configuration(client) |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
def validate_database_credentials(client): |
130
|
|
|
""" |
131
|
|
|
Validate the parsed database credentials |
132
|
|
|
:param client: String |
133
|
|
|
:return: |
134
|
|
|
""" |
135
|
|
|
output.message( |
136
|
|
|
output.host_to_subject(client), |
137
|
|
|
'Validating database credentials', |
138
|
|
|
True |
139
|
|
|
) |
140
|
|
|
_db_credential_keys = ['name', 'host', 'password', 'port', 'user'] |
141
|
|
|
|
142
|
|
|
for _key in _db_credential_keys: |
143
|
|
|
if _key not in system.config[client]['db']: |
144
|
|
|
sys.exit( |
145
|
|
|
output.message( |
146
|
|
|
output.Subject.ERROR, |
147
|
|
|
f'Missing database credential "{_key}" for {client} client', |
148
|
|
|
False |
149
|
|
|
) |
150
|
|
|
) |
151
|
|
|
if system.config[client]['db'][_key] is None or system.config[client]['db'][_key] == '': |
152
|
|
|
sys.exit( |
153
|
|
|
output.message( |
154
|
|
|
output.Subject.ERROR, |
155
|
|
|
f'Missing database credential "{_key}" for {client} client', |
156
|
|
|
False |
157
|
|
|
) |
158
|
|
|
) |
159
|
|
|
|