1
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
2
|
|
|
# -*- coding: future_fstrings -*- |
3
|
|
|
|
4
|
|
|
import sys |
|
|
|
|
5
|
|
|
import os |
|
|
|
|
6
|
|
|
import re |
7
|
|
|
from db_sync_tool.utility import mode, system, output |
8
|
|
|
from file_sync_tool.transfer import utility |
|
|
|
|
9
|
|
|
|
10
|
|
|
|
11
|
|
|
def check_rsync_version(): |
12
|
|
|
""" |
13
|
|
|
Check rsync version |
14
|
|
|
:return: |
15
|
|
|
""" |
16
|
|
|
_raw_version = mode.run_command( |
17
|
|
|
'rsync --version', |
18
|
|
|
mode.Client.LOCAL, |
19
|
|
|
True |
20
|
|
|
) |
21
|
|
|
_version = parse_version(_raw_version) |
22
|
|
|
output.message( |
23
|
|
|
output.Subject.LOCAL, |
24
|
|
|
f'rsync version {_version}' |
25
|
|
|
) |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
def check_sshpass_version(): |
|
|
|
|
29
|
|
|
""" |
30
|
|
|
Check sshpass version |
31
|
|
|
:return: |
32
|
|
|
""" |
33
|
|
|
system.config['use_sshpass'] = False |
34
|
|
|
_raw_version = mode.run_command( |
35
|
|
|
'sshpass -V', |
36
|
|
|
mode.Client.LOCAL, |
37
|
|
|
force_output=True, |
38
|
|
|
allow_fail=True |
39
|
|
|
) |
40
|
|
|
_version = parse_version(_raw_version) |
41
|
|
|
|
42
|
|
|
if _version: |
43
|
|
|
output.message( |
44
|
|
|
output.Subject.LOCAL, |
45
|
|
|
f'sshpass version {_version}' |
46
|
|
|
) |
47
|
|
|
system.config['use_sshpass'] = True |
48
|
|
|
return True |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
def parse_version(output): |
|
|
|
|
52
|
|
|
""" |
53
|
|
|
Parse version out of console output |
54
|
|
|
https://stackoverflow.com/a/60730346 |
55
|
|
|
:param output: String |
56
|
|
|
:return: |
57
|
|
|
""" |
58
|
|
|
_version_pattern = r'\d+(=?\.(\d+(=?\.(\d+)*)*)*)*' |
59
|
|
|
_regex_matcher = re.compile(_version_pattern) |
60
|
|
|
_version = _regex_matcher.search(output) |
61
|
|
|
if _version: |
|
|
|
|
62
|
|
|
return _version.group(0) |
63
|
|
|
else: |
64
|
|
|
return None |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
def check_authorizations(): |
68
|
|
|
""" |
69
|
|
|
Check authorization for clients |
70
|
|
|
:return: |
71
|
|
|
""" |
72
|
|
|
if system.config['use_sshpass']: |
73
|
|
|
# When using sshpass, check for passwords |
74
|
|
|
system.check_authorization(mode.Client.ORIGIN) |
75
|
|
|
system.check_authorization(mode.Client.TARGET) |
76
|
|
|
elif not 'ssh_key' in system.config[mode.Client.ORIGIN] and \ |
77
|
|
|
not 'ssh_key' in system.config[mode.Client.TARGET] and \ |
78
|
|
|
(mode.get_sync_mode() == mode.SyncMode.PROXY or len(system.config['files']['config']) > 1): |
|
|
|
|
79
|
|
|
# Suggest to install sshpass |
80
|
|
|
output.message( |
81
|
|
|
output.Subject.INFO, |
82
|
|
|
f'Suggestion: Install {output.CliFormat.BOLD}sshpass{output.CliFormat.ENDC} to avoid multiple input of ssh passwords' |
|
|
|
|
83
|
|
|
) |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
def dict_to_args(dict): |
|
|
|
|
87
|
|
|
""" |
88
|
|
|
Convert an dictionary to a args list |
89
|
|
|
:param dict: Dictionary |
90
|
|
|
:return: List |
91
|
|
|
""" |
92
|
|
|
_args = [] |
93
|
|
|
for key, val in dict.items(): |
94
|
|
|
if isinstance(val, bool): |
95
|
|
|
if val: |
96
|
|
|
_args.append(f'--{key}') |
97
|
|
|
else: |
98
|
|
|
_args.append(f'--{key}') |
99
|
|
|
_args.append(str(val)) |
100
|
|
|
if len(_args) == 0: |
101
|
|
|
return None |
102
|
|
|
return _args |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
def adjust_sync_mode(): |
106
|
|
|
""" |
107
|
|
|
|
108
|
|
|
:return: |
109
|
|
|
""" |
110
|
|
|
if mode.get_sync_mode() == mode.SyncMode.DUMP_LOCAL: |
111
|
|
|
mode.sync_mode = mode.SyncMode.SYNC_LOCAL |
112
|
|
|
if mode.get_sync_mode() == mode.SyncMode.DUMP_REMOTE: |
113
|
|
|
mode.sync_mode = mode.SyncMode.SYNC_REMOTE |
|
|
|
|