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 |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
def extend_config(args): |
117
|
|
|
""" |
118
|
|
|
Extending optional config |
119
|
|
|
:param args: |
120
|
|
|
:return: |
121
|
|
|
""" |
122
|
|
|
config = system.config |
123
|
|
|
|
124
|
|
|
if args is None or not args: |
125
|
|
|
return config |
126
|
|
|
|
127
|
|
|
if not args.target_host is None: |
128
|
|
|
config['target']['host'] = args.target_host |
129
|
|
|
|
130
|
|
|
if not args.target_user is None: |
131
|
|
|
config['target']['user'] = args.target_user |
132
|
|
|
|
133
|
|
|
if not args.target_password is None: |
134
|
|
|
config['target']['password'] = args.target_password |
135
|
|
|
|
136
|
|
|
if not args.target_key is None: |
137
|
|
|
config['target']['ssh_key'] = args.target_key |
138
|
|
|
|
139
|
|
|
if not args.target_port is None: |
140
|
|
|
config['target']['port'] = args.target_port |
141
|
|
|
|
142
|
|
|
if not args.origin_host is None: |
143
|
|
|
config['origin']['host'] = args.origin_host |
144
|
|
|
|
145
|
|
|
if not args.origin_user is None: |
146
|
|
|
config['origin']['user'] = args.origin_user |
147
|
|
|
|
148
|
|
|
if not args.origin_password is None: |
149
|
|
|
config['origin']['password'] = args.origin_password |
150
|
|
|
|
151
|
|
|
if not args.origin_key is None: |
152
|
|
|
config['origin']['ssh_key'] = args.origin_key |
153
|
|
|
|
154
|
|
|
if not args.origin_port is None: |
155
|
|
|
config['origin']['port'] = args.origin_port |
156
|
|
|
|
157
|
|
|
if not args.files_origin is None: |
158
|
|
|
if 'config' not in config['files']: |
159
|
|
|
config['files']['config'] = [] |
160
|
|
|
config['files']['config'].append({}) |
161
|
|
|
config['files']['config'][0]['origin'] = args.files_origin |
162
|
|
|
|
163
|
|
|
if not args.files_target is None: |
164
|
|
|
if 'config' not in config['files']: |
165
|
|
|
config['files']['config'] = [] |
166
|
|
|
config['files']['config'].append({}) |
167
|
|
|
config['files']['config'][0]['target'] = args.files_target |
168
|
|
|
|
169
|
|
|
if not args.files_exclude is None: |
170
|
|
|
config['files']['config'][0]['exclude'] = args.files_exclude.split(',') |
171
|
|
|
|
172
|
|
|
if not args.files_option is None: |
173
|
|
|
config['files']['option'] = args.files_option.split(',') |
174
|
|
|
|
175
|
|
|
return config |
176
|
|
|
|