|
1
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
|
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
|
|
4
|
|
|
import sys |
|
5
|
|
|
import json |
|
6
|
|
|
import os |
|
7
|
|
|
import getpass |
|
8
|
|
|
from db_sync_tool.utility import log, parser, mode, helper, output |
|
9
|
|
|
|
|
10
|
|
|
# |
|
11
|
|
|
# GLOBALS |
|
12
|
|
|
# |
|
13
|
|
|
|
|
14
|
|
|
config = {} |
|
15
|
|
|
option = { |
|
16
|
|
|
'verbose': False, |
|
17
|
|
|
'mute': False, |
|
18
|
|
|
'use_origin_ssh_key': False, |
|
19
|
|
|
'use_target_ssh_key': False, |
|
20
|
|
|
'keep_dump': False, |
|
21
|
|
|
'dump_name': '', |
|
22
|
|
|
'import': '', |
|
23
|
|
|
'link_hosts': '', |
|
24
|
|
|
'default_origin_dump_dir': True, |
|
25
|
|
|
'default_target_dump_dir': True, |
|
26
|
|
|
'check_dump': True, |
|
27
|
|
|
'is_same_client': False, |
|
28
|
|
|
'config_file_path': None, |
|
29
|
|
|
'ssh_password': { |
|
30
|
|
|
'origin': None, |
|
31
|
|
|
'target': None |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
# |
|
36
|
|
|
# DEFAULTS |
|
37
|
|
|
# |
|
38
|
|
|
|
|
39
|
|
|
default_local_sync_path = '/tmp/' |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
# |
|
43
|
|
|
# FUNCTIONS |
|
44
|
|
|
# |
|
45
|
|
|
|
|
46
|
|
|
def check_target_configuration(): |
|
47
|
|
|
""" |
|
48
|
|
|
Checking target database configuration |
|
49
|
|
|
:return: |
|
50
|
|
|
""" |
|
51
|
|
|
parser.get_database_configuration(mode.Client.TARGET) |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
def get_configuration(host_config): |
|
55
|
|
|
""" |
|
56
|
|
|
Checking configuration information by file or dictionary |
|
57
|
|
|
:param host_config: Dictionary |
|
58
|
|
|
:return: |
|
59
|
|
|
""" |
|
60
|
|
|
if not option['config_file_path'] is None: |
|
61
|
|
|
if os.path.isfile(option['config_file_path']): |
|
62
|
|
|
with open(option['config_file_path'], 'r') as read_file: |
|
63
|
|
|
config['host'] = json.load(read_file) |
|
64
|
|
|
output.message( |
|
65
|
|
|
output.Subject.LOCAL, |
|
66
|
|
|
'Loading host configuration', |
|
67
|
|
|
True |
|
68
|
|
|
) |
|
69
|
|
|
|
|
70
|
|
|
check_options() |
|
71
|
|
|
else: |
|
72
|
|
|
sys.exit( |
|
73
|
|
|
output.message( |
|
74
|
|
|
output.Subject.ERROR, |
|
75
|
|
|
f'Local configuration not found: {option["config_file_path"]}', |
|
76
|
|
|
False |
|
77
|
|
|
) |
|
78
|
|
|
) |
|
79
|
|
|
|
|
80
|
|
|
if host_config: |
|
81
|
|
|
config['host'] = host_config |
|
82
|
|
|
|
|
83
|
|
|
log.get_logger().info('Starting db_sync_tool') |
|
84
|
|
|
if option['config_file_path']: |
|
85
|
|
|
output.message( |
|
86
|
|
|
output.Subject.INFO, |
|
87
|
|
|
'Configuration: ' + option['config_file_path'], |
|
88
|
|
|
True, |
|
89
|
|
|
True |
|
90
|
|
|
) |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
def check_options(): |
|
94
|
|
|
""" |
|
95
|
|
|
Checking configuration provided file |
|
96
|
|
|
:return: |
|
97
|
|
|
""" |
|
98
|
|
|
if 'dump_dir' in config['host']['origin']: |
|
99
|
|
|
option['default_origin_dump_dir'] = False |
|
100
|
|
|
|
|
101
|
|
|
if 'dump_dir' in config['host']['target']: |
|
102
|
|
|
option['default_target_dump_dir'] = False |
|
103
|
|
|
|
|
104
|
|
|
if 'check_dump' in config['host']: |
|
105
|
|
|
option['check_dump'] = config['host']['check_dump'] |
|
106
|
|
|
|
|
107
|
|
|
link_configuration_with_hosts() |
|
108
|
|
|
mode.check_sync_mode() |
|
109
|
|
|
check_authorization(mode.Client.ORIGIN) |
|
110
|
|
|
check_authorization(mode.Client.TARGET) |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
def check_authorization(client): |
|
114
|
|
|
""" |
|
115
|
|
|
Checking arguments and fill options array |
|
116
|
|
|
:param client: String |
|
117
|
|
|
:return: |
|
118
|
|
|
""" |
|
119
|
|
|
# only need authorization if client is remote |
|
120
|
|
|
if mode.is_remote(client): |
|
121
|
|
|
# Workaround |
|
122
|
|
|
if (mode.get_sync_mode() == mode.SyncMode.DUMP_REMOTE and client == mode.Client.TARGET) or ( |
|
|
|
|
|
|
123
|
|
|
mode.get_sync_mode() == mode.SyncMode.DUMP_LOCAL and client == mode.Client.ORIGIN) or ( |
|
|
|
|
|
|
124
|
|
|
mode.get_sync_mode() == mode.SyncMode.IMPORT_REMOTE and client == mode.Client.ORIGIN): |
|
|
|
|
|
|
125
|
|
|
return |
|
126
|
|
|
|
|
127
|
|
|
# ssh key authorization |
|
128
|
|
|
if 'ssh_key' in config['host'][client]: |
|
|
|
|
|
|
129
|
|
|
_ssh_key = config['host'][client]['ssh_key'] |
|
130
|
|
|
if os.path.isfile(_ssh_key): |
|
131
|
|
|
option[f'use_{client}_ssh_key'] = True |
|
132
|
|
|
else: |
|
133
|
|
|
sys.exit( |
|
134
|
|
|
output.message( |
|
135
|
|
|
output.Subject.ERROR, |
|
136
|
|
|
f'SSH {client} private key not found: {_ssh_key}', |
|
137
|
|
|
False |
|
138
|
|
|
) |
|
139
|
|
|
) |
|
140
|
|
|
elif 'password' in config['host'][client]: |
|
141
|
|
|
config['host'][client]['password'] = config['host'][client]['password'] |
|
142
|
|
|
else: |
|
143
|
|
|
# user input authorization |
|
144
|
|
|
config['host'][client]['password'] = get_password_by_user(client) |
|
145
|
|
|
|
|
146
|
|
|
if mode.get_sync_mode() == mode.SyncMode.DUMP_REMOTE and client == mode.Client.ORIGIN and 'password' in config['host'][mode.Client.ORIGIN]: |
|
|
|
|
|
|
147
|
|
|
config['host'][mode.Client.TARGET]['password'] = config['host'][mode.Client.ORIGIN]['password'] |
|
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
def get_password_by_user(client): |
|
151
|
|
|
""" |
|
152
|
|
|
Getting password by user input |
|
153
|
|
|
:param client: String |
|
154
|
|
|
:return: String password |
|
155
|
|
|
""" |
|
156
|
|
|
_password = getpass.getpass( |
|
157
|
|
|
output.message( |
|
158
|
|
|
output.Subject.INFO, |
|
159
|
|
|
'SSH password ' + helper.get_ssh_host_name(client, True) + ': ', |
|
160
|
|
|
False |
|
161
|
|
|
) |
|
162
|
|
|
) |
|
163
|
|
|
|
|
164
|
|
|
while _password.strip() == '': |
|
165
|
|
|
output.message( |
|
166
|
|
|
output.Subject.WARNING, |
|
167
|
|
|
'Password seems to be empty. Please enter a valid password.', |
|
168
|
|
|
True |
|
169
|
|
|
) |
|
170
|
|
|
|
|
171
|
|
|
_password = getpass.getpass( |
|
172
|
|
|
output.message( |
|
173
|
|
|
output.Subject.INFO, |
|
174
|
|
|
'SSH password ' + helper.get_ssh_host_name(client, True) + ': ', |
|
175
|
|
|
False |
|
176
|
|
|
) |
|
177
|
|
|
) |
|
178
|
|
|
|
|
179
|
|
|
return _password |
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
def check_args_options(args): |
|
183
|
|
|
""" |
|
184
|
|
|
Checking arguments and fill options array |
|
185
|
|
|
:param args: |
|
186
|
|
|
:return: |
|
187
|
|
|
""" |
|
188
|
|
|
global option |
|
|
|
|
|
|
189
|
|
|
global default_local_sync_path |
|
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
if not args.file is None: |
|
192
|
|
|
option['config_file_path'] = args.file |
|
193
|
|
|
|
|
194
|
|
|
if not args.verbose is None: |
|
195
|
|
|
option['verbose'] = args.verbose |
|
196
|
|
|
|
|
197
|
|
|
if not args.mute is None: |
|
198
|
|
|
option['mute'] = args.mute |
|
199
|
|
|
|
|
200
|
|
|
if not args.importfile is None: |
|
201
|
|
|
option['import'] = args.importfile |
|
202
|
|
|
|
|
203
|
|
|
if not args.dumpname is None: |
|
204
|
|
|
option['dump_name'] = args.dumpname |
|
205
|
|
|
|
|
206
|
|
|
if not args.hosts is None: |
|
207
|
|
|
option['link_hosts'] = args.hosts |
|
208
|
|
|
|
|
209
|
|
|
if not args.keepdump is None: |
|
210
|
|
|
default_local_sync_path = args.keepdump |
|
211
|
|
|
|
|
212
|
|
|
# Adding trailing slash if necessary |
|
213
|
|
|
if default_local_sync_path[-1] != '/': |
|
214
|
|
|
default_local_sync_path += '/' |
|
215
|
|
|
|
|
216
|
|
|
option['keep_dump'] = True |
|
217
|
|
|
output.message( |
|
218
|
|
|
output.Subject.INFO, |
|
219
|
|
|
'"Keep dump" option chosen', |
|
220
|
|
|
True |
|
221
|
|
|
) |
|
222
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
def link_configuration_with_hosts(): |
|
225
|
|
|
""" |
|
226
|
|
|
Merging the hosts definition with the given configuration file |
|
227
|
|
|
:return: |
|
228
|
|
|
""" |
|
229
|
|
|
if ('link' in config['host']['origin'] or 'link' in config['host']['target']) and option['link_hosts'] == '': |
|
|
|
|
|
|
230
|
|
|
# Try to find default hosts.json file in same directory |
|
231
|
|
|
sys.exit( |
|
232
|
|
|
output.message( |
|
233
|
|
|
output.Subject.ERROR, |
|
234
|
|
|
f'Missing hosts file for linking hosts with configuration. ' |
|
|
|
|
|
|
235
|
|
|
f'Use the "-o" / "--hosts" argument to define the filepath for the hosts file, when using a link parameter within the configuration.', |
|
|
|
|
|
|
236
|
|
|
False |
|
237
|
|
|
) |
|
238
|
|
|
) |
|
239
|
|
|
|
|
240
|
|
|
if option['link_hosts'] != '': |
|
241
|
|
|
if os.path.isfile(option['link_hosts']): |
|
242
|
|
|
with open(option['link_hosts'], 'r') as read_file: |
|
243
|
|
|
_hosts = json.load(read_file) |
|
244
|
|
|
output.message( |
|
245
|
|
|
output.Subject.INFO, |
|
246
|
|
|
'Linking configuration with hosts', |
|
247
|
|
|
True |
|
248
|
|
|
) |
|
249
|
|
|
if 'link' in config['host']['origin']: |
|
250
|
|
|
_host_name = str(config['host']['origin']['link']).replace('@','') |
|
|
|
|
|
|
251
|
|
|
if _host_name in _hosts: |
|
252
|
|
|
config['host']['origin'] = {**config['host']['origin'], **_hosts[_host_name]} |
|
|
|
|
|
|
253
|
|
|
|
|
254
|
|
|
if 'link' in config['host']['target']: |
|
255
|
|
|
_host_name = str(config['host']['target']['link']).replace('@','') |
|
|
|
|
|
|
256
|
|
|
if _host_name in _hosts: |
|
257
|
|
|
config['host']['target'] = {**config['host']['target'], **_hosts[_host_name]} |
|
|
|
|
|
|
258
|
|
|
else: |
|
259
|
|
|
sys.exit( |
|
260
|
|
|
output.message( |
|
261
|
|
|
output.Subject.ERROR, |
|
262
|
|
|
f'Local host file not found: {option["link_hosts"]}', |
|
263
|
|
|
False |
|
264
|
|
|
) |
|
265
|
|
|
) |
|
266
|
|
|
|
|
267
|
|
|
|
|
268
|
|
|
|
|
|
|
|
|
|
269
|
|
|
|