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