1
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
2
|
|
|
# -*- coding: future_fstrings -*- |
3
|
|
|
|
4
|
|
|
import sys |
|
|
|
|
5
|
|
|
import getpass |
|
|
|
|
6
|
|
|
import shutil |
7
|
|
|
import os |
8
|
|
|
from db_sync_tool.utility import mode, system, output |
9
|
|
|
from db_sync_tool.remote import utility as remote_utility |
10
|
|
|
|
11
|
|
|
# |
12
|
|
|
# FUNCTIONS |
13
|
|
|
# |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
def clean_up(): |
17
|
|
|
""" |
18
|
|
|
Clean up |
19
|
|
|
:return: |
20
|
|
|
""" |
21
|
|
|
if not mode.is_import(): |
22
|
|
|
remote_utility.remove_target_database_dump() |
23
|
|
|
if mode.get_sync_mode() == mode.SyncMode.PROXY: |
24
|
|
|
remove_temporary_data_dir() |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
def remove_temporary_data_dir(): |
28
|
|
|
""" |
29
|
|
|
Remove temporary data directory for storing database dump files |
30
|
|
|
:return: |
31
|
|
|
""" |
32
|
|
|
if os.path.exists(system.default_local_sync_path): |
33
|
|
|
shutil.rmtree(system.default_local_sync_path) |
34
|
|
|
output.message( |
35
|
|
|
output.Subject.LOCAL, |
36
|
|
|
'Cleaning up', |
37
|
|
|
True |
38
|
|
|
) |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
def clean_up_dump_dir(client, path, num=5): |
42
|
|
|
""" |
43
|
|
|
Clean up the dump directory from old dump files (only affect .sql and .tar.gz files) |
44
|
|
|
:param client: |
45
|
|
|
:param path: |
46
|
|
|
:param num: |
47
|
|
|
:return: |
48
|
|
|
""" |
49
|
|
|
# Distinguish stat command on os system (Darwin|Linux) |
50
|
|
|
if check_os(client).strip() == 'Darwin': |
51
|
|
|
_command = get_command(client, 'stat') + ' -f "%Sm %N" ' + path + ' | ' + get_command(client, |
|
|
|
|
52
|
|
|
'sort') + ' -rn | ' + get_command( |
|
|
|
|
53
|
|
|
client, 'grep') + ' -E ".tar.gz|.sql"' |
|
|
|
|
54
|
|
|
else: |
55
|
|
|
_command = get_command(client, 'stat') + ' -c "%y %n" ' + path + ' | ' + get_command(client, |
56
|
|
|
'sort') + ' -rn | ' + get_command( |
|
|
|
|
57
|
|
|
client, 'grep') + ' -E ".tar.gz|.sql"' |
|
|
|
|
58
|
|
|
|
59
|
|
|
# List files in directory sorted by change date |
60
|
|
|
_files = mode.run_command( |
61
|
|
|
_command, |
62
|
|
|
client, |
63
|
|
|
True |
64
|
|
|
).splitlines() |
65
|
|
|
|
66
|
|
|
for i in range(len(_files)): |
|
|
|
|
67
|
|
|
_filename = _files[i].rsplit(' ', 1)[-1] |
68
|
|
|
|
69
|
|
|
# Remove oldest files chosen by keep_dumps count |
70
|
|
|
if not i < num: |
71
|
|
|
mode.run_command( |
72
|
|
|
'rm ' + _filename, |
73
|
|
|
client |
74
|
|
|
) |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
def check_os(client): |
78
|
|
|
""" |
79
|
|
|
Check which system is running (Linux|Darwin) |
80
|
|
|
:param client: |
81
|
|
|
:return: |
82
|
|
|
""" |
83
|
|
|
return mode.run_command( |
84
|
|
|
get_command(client, 'uname') + ' -s', |
85
|
|
|
client, |
86
|
|
|
True |
87
|
|
|
) |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
def get_command(client, command): |
91
|
|
|
""" |
92
|
|
|
Get command helper for overriding default commands on the given client |
93
|
|
|
:param client: |
94
|
|
|
:param command: |
95
|
|
|
:return: String command |
96
|
|
|
""" |
97
|
|
|
if 'console' in system.config[client]: |
98
|
|
|
if command in system.config[client]['console']: |
99
|
|
|
return system.config[client]['console'][command] |
100
|
|
|
return command |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
def get_dump_dir(client): |
104
|
|
|
""" |
105
|
|
|
Get database dump directory by client |
106
|
|
|
:param client: |
107
|
|
|
:return: String path |
108
|
|
|
""" |
109
|
|
|
if system.config[f'default_{client}_dump_dir']: |
|
|
|
|
110
|
|
|
return '/tmp/' |
111
|
|
|
else: |
112
|
|
|
return system.config[client]['dump_dir'] |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
def check_and_create_dump_dir(client, path): |
116
|
|
|
""" |
117
|
|
|
Check if a path exists on the client system and creates the given path if necessary |
118
|
|
|
:param client: |
119
|
|
|
:param path: |
120
|
|
|
:return: |
121
|
|
|
""" |
122
|
|
|
mode.run_command( |
123
|
|
|
'[ ! -d "' + path + '" ] && mkdir -p "' + path + '"', |
124
|
|
|
client |
125
|
|
|
) |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
def get_ssh_host_name(client, with_user=False): |
129
|
|
|
""" |
130
|
|
|
Format ssh host name depending on existing client name |
131
|
|
|
:param client: |
132
|
|
|
:param with_user: |
133
|
|
|
:return: |
134
|
|
|
""" |
135
|
|
|
if with_user: |
136
|
|
|
_host = system.config[client]['user'] + '@' + system.config[client]['host'] |
137
|
|
|
else: |
138
|
|
|
_host = system.config[client]['host'] |
139
|
|
|
|
140
|
|
|
if 'name' in system.config[client]: |
|
|
|
|
141
|
|
|
return output.CliFormat.BOLD + system.config[client][ |
142
|
|
|
'name'] + output.CliFormat.ENDC + output.CliFormat.BLACK + ' (' + _host + ')' + output.CliFormat.ENDC |
|
|
|
|
143
|
|
|
else: |
144
|
|
|
return _host |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
def create_local_temporary_data_dir(): |
148
|
|
|
""" |
149
|
|
|
Create local temporary data dir |
150
|
|
|
:return: |
151
|
|
|
""" |
152
|
|
|
# @ToDo: Combine with check_and_create_dump_dir() |
153
|
|
|
if not os.path.exists(system.default_local_sync_path): |
154
|
|
|
os.mkdir(system.default_local_sync_path) |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
def dict_to_args(dict): |
|
|
|
|
158
|
|
|
""" |
159
|
|
|
Convert an dictionary to a args list |
160
|
|
|
:param dict: Dictionary |
161
|
|
|
:return: List |
162
|
|
|
""" |
163
|
|
|
_args = [] |
164
|
|
|
for key, val in dict.items(): |
165
|
|
|
if isinstance(val, bool): |
166
|
|
|
if val: |
167
|
|
|
_args.append(f'--{key}') |
168
|
|
|
else: |
169
|
|
|
_args.append(f'--{key}') |
170
|
|
|
_args.append(str(val)) |
171
|
|
|
if len(_args) == 0: |
172
|
|
|
return None |
173
|
|
|
return _args |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
def check_file_exists(client, path): |
177
|
|
|
""" |
178
|
|
|
Check if a file exists |
179
|
|
|
:param client: String |
180
|
|
|
:param path: String file path |
181
|
|
|
:return: Boolean |
182
|
|
|
""" |
183
|
|
|
return mode.run_command(f'[ -f {path} ] && echo "1"', client, True) == '1' |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
def confirm(prompt=None, resp=False): |
187
|
|
|
""" |
188
|
|
|
https://code.activestate.com/recipes/541096-prompt-the-user-for-confirmation/ |
189
|
|
|
|
190
|
|
|
prompts for yes or no response from the user. Returns True for yes and |
191
|
|
|
False for no. |
192
|
|
|
|
193
|
|
|
'resp' should be set to the default value assumed by the caller when |
194
|
|
|
user simply types ENTER. |
195
|
|
|
|
196
|
|
|
>>> confirm(prompt='Create Directory?', resp=True) |
197
|
|
|
Create Directory? [Y|n]: |
198
|
|
|
True |
199
|
|
|
>>> confirm(prompt='Create Directory?', resp=False) |
200
|
|
|
Create Directory? [y|N]: |
201
|
|
|
False |
202
|
|
|
|
203
|
|
|
""" |
204
|
|
|
|
205
|
|
|
if prompt is None: |
206
|
|
|
prompt = 'Confirm' |
207
|
|
|
|
208
|
|
|
if resp: |
209
|
|
|
prompt = '%s [%s|%s]: ' % (prompt, 'Y', 'n') |
210
|
|
|
else: |
211
|
|
|
prompt = '%s [%s|%s]: ' % (prompt, 'y', 'N') |
212
|
|
|
|
213
|
|
|
while True: |
214
|
|
|
ans = input(prompt) |
215
|
|
|
if not ans: |
216
|
|
|
return resp |
217
|
|
|
if ans not in ['y', 'Y', 'n', 'N']: |
218
|
|
|
print |
|
|
|
|
219
|
|
|
'please enter y or n.' |
|
|
|
|
220
|
|
|
continue |
221
|
|
|
if ans == 'y' or ans == 'Y': |
|
|
|
|
222
|
|
|
return True |
223
|
|
|
if ans == 'n' or ans == 'N': |
|
|
|
|
224
|
|
|
return False |
225
|
|
|
|