forked from databrickslabs/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_db.py
276 lines (247 loc) · 12.7 KB
/
import_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from dbclient import *
from timeit import default_timer as timer
from datetime import timedelta, datetime
from os import makedirs
from checkpoint_service import CheckpointService
import logging_utils
import os
# python 3.6
def main():
# define a parser to identify what component to import / export
my_parser = get_import_parser()
# parse the args
args = my_parser.parse_args()
# parse the path location of the Databricks CLI configuration
login_args = get_login_credentials(profile=args.profile)
if is_azure_creds(login_args) and (not args.azure):
raise ValueError('Login credentials do not match args. Please provide --azure flag for azure environments.')
# cant use netrc credentials because requests module tries to load the credentials into http basic auth headers
url = login_args['host']
token = login_args['token']
client_config = build_client_config(args.profile, url, token, args)
session = args.session if args.session else ""
client_config['session'] = session
client_config['export_dir'] = os.path.join(client_config['export_dir'], session) + '/'
makedirs(client_config['export_dir'], exist_ok=True)
logging_utils.set_default_logging(client_config['export_dir'])
checkpoint_service = CheckpointService(client_config)
if client_config['debug']:
print(url)
now = str(datetime.now())
if args.users:
print("Import all users and groups at {0}".format(now))
scim_c = ScimClient(client_config, checkpoint_service)
if client_config['is_aws']:
print("Start import of instance profiles first to ensure they exist...")
cl_c = ClustersClient(client_config, checkpoint_service)
start = timer()
cl_c.import_instance_profiles()
end = timer()
print("Complete Instance Profile Import Time: " + str(timedelta(seconds=end - start)))
start = timer()
scim_c.import_all_users_and_groups()
end = timer()
print("Complete Users and Groups Import Time: " + str(timedelta(seconds=end - start)))
if args.workspace:
print("Import the complete workspace at {0}".format(now))
print("Import on {0}".format(url))
ws_c = WorkspaceClient(client_config, checkpoint_service)
start = timer()
if ws_c.is_overwrite_notebooks():
# if OVERWRITE is configured, check that the SOURCE format option is used. Otherwise fail
if not ws_c.is_source_file_format():
raise ValueError('Overwrite notebooks only supports the SOURCE format. See Rest API docs for details')
# log notebooks and libraries
ws_c.import_all_workspace_items(archive_missing=args.archive_missing, num_parallel=args.num_parallel)
end = timer()
print("Complete Workspace Import Time: " + str(timedelta(seconds=end - start)))
if args.workspace_top_level:
print("Import the top level workspace items at {0}".format(now))
print("Import on {0}".format(url))
ws_c = WorkspaceClient(client_config, checkpoint_service)
start = timer()
if ws_c.is_overwrite_notebooks():
# if OVERWRITE is configured, check that the SOURCE format option is used. Otherwise fail
if not ws_c.is_source_file_format():
raise ValueError('Overwrite notebooks only supports the SOURCE format. See Rest API docs for details')
# log notebooks and libraries
ws_c.import_current_workspace_items()
end = timer()
print("Complete Workspace Import Time: " + str(timedelta(seconds=end - start)))
if args.workspace_acls:
print("Import workspace ACLs at {0}".format(now))
print("Import on {0}".format(url))
ws_c = WorkspaceClient(client_config, checkpoint_service)
start = timer()
# log notebooks and libraries
# Workspace Acl Import cannot handle parallel APIs due to the heavy loads.
ws_c.import_workspace_acls(num_parallel=1)
end = timer()
print("Complete Workspace acl Import Time: " + str(timedelta(seconds=end - start)))
if args.clusters:
print("Import all cluster configs at {0}".format(now))
cl_c = ClustersClient(client_config, checkpoint_service)
if client_config['is_aws']:
print("Start import of instance profiles ...")
start = timer()
cl_c.import_instance_profiles()
end = timer()
print("Complete Instance Profile Import Time: " + str(timedelta(seconds=end - start)))
print("Start import of cluster policies ...")
start = timer()
cl_c.import_cluster_policies()
end = timer()
print("Complete Cluster Policies Creation Time: " + str(timedelta(seconds=end - start)))
print("Start import of instance pool configurations ...")
start = timer()
cl_c.import_instance_pools()
end = timer()
print("Complete Instance Pools Creation Time: " + str(timedelta(seconds=end - start)))
print("Start import of cluster configurations ...")
start = timer()
cl_c.import_cluster_configs()
end = timer()
print("Complete Cluster Import Time: " + str(timedelta(seconds=end - start)))
if args.jobs:
print("Importing the jobs configs at {0}".format(now))
start = timer()
jobs_c = JobsClient(client_config, checkpoint_service)
jobs_c.import_job_configs()
end = timer()
print("Complete Jobs Export Time: " + str(timedelta(seconds=end - start)))
if args.metastore or args.metastore_unicode:
print("Importing the metastore configs at {0}".format(now))
start = timer()
hive_c = HiveClient(client_config, checkpoint_service)
# log job configs
hive_c.import_hive_metastore(cluster_name=args.cluster_name, has_unicode=args.metastore_unicode,
should_repair_table=args.repair_metastore_tables)
end = timer()
print("Complete Metastore Import Time: " + str(timedelta(seconds=end - start)))
if args.repair_metastore_tables:
print("Repairing metastore table")
start = timer()
hive_c = HiveClient(client_config, checkpoint_service)
hive_c.repair_legacy_tables(cluster_name=args.cluster_name)
end = timer()
print("Complete Metastore Repair Time: " + str(timedelta(seconds=end - start)))
if args.table_acls:
print("Importing table acls configs at {0}".format(now))
start = timer()
table_acls_c = TableACLsClient(client_config, checkpoint_service)
# log table ACLS configs
notebook_exit_value = table_acls_c.import_table_acls()
end = timer()
print(f'Complete Table ACLs with exit value: {json.dumps(notebook_exit_value)}, Import Time: {timedelta(seconds=end - start)}')
if args.secrets:
print("Import secret scopes configs at {0}".format(now))
start = timer()
sc = SecretsClient(client_config, checkpoint_service)
sc.import_all_secrets()
end = timer()
print("Complete Secrets Import Time: " + str(timedelta(seconds=end - start)))
if args.pause_all_jobs:
print("Pause all current jobs {0}".format(now))
start = timer()
jobs_c = JobsClient(client_config, checkpoint_service)
# log job configs
jobs_c.pause_all_jobs()
end = timer()
print("Paused all jobs time: " + str(timedelta(seconds=end - start)))
if args.unpause_all_jobs:
print("Unpause all current jobs {0}".format(now))
start = timer()
jobs_c = JobsClient(client_config, checkpoint_service)
# log job configs
jobs_c.pause_all_jobs(False)
end = timer()
print("Unpaused all jobs time: " + str(timedelta(seconds=end - start)))
if args.delete_all_jobs:
print("Delete all current jobs {0}".format(now))
start = timer()
jobs_c = JobsClient(client_config, checkpoint_service)
url = jobs_c.get_url()
response = prompt_for_input(f'\nPlease confirm that you would like to delete jobs from {url} [yes/no]:')
if response:
print("Deleting all job configs ... ")
jobs_c.delete_all_jobs()
end = timer()
print("Delete all jobs time: " + str(timedelta(seconds=end - start)))
if args.single_user:
user_email = args.single_user
print(f"Import user {user_email} at {now}")
scim_c = ScimClient(client_config, checkpoint_service)
start = timer()
# log all users
scim_c.import_single_user(user_email)
end = timer()
print("Complete single user import: " + str(timedelta(seconds=end - start)))
if args.import_home:
username = args.import_home
print("Importing home directory: {0}".format(username))
ws_c = WorkspaceClient(client_config, checkpoint_service)
start = timer()
# log notebooks and libraries
if ws_c.is_overwrite_notebooks():
# if OVERWRITE is configured, check that the SOURCE format option is used. Otherwise fail
if not ws_c.is_source_file_format():
raise ValueError('Overwrite notebooks only supports the SOURCE format. See Rest API docs for details')
ws_c.import_user_home(username, 'user_exports')
end = timer()
print("Complete Single User Import Time: " + str(timedelta(seconds=end - start)))
if args.import_groups:
print("Importing Groups from logs")
start = timer()
scim_c = ScimClient(client_config, checkpoint_service)
scim_c.import_all_users_and_groups()
user_names = scim_c.get_users_from_log()
print('Export users notebooks:', user_names)
ws_c = WorkspaceClient(client_config, WorkspaceClient)
if ws_c.is_overwrite_notebooks():
# if OVERWRITE is configured, check that the SOURCE format option is used. Otherwise fail
if not ws_c.is_source_file_format():
raise ValueError('Overwrite notebooks only supports the SOURCE format. See Rest API docs for details')
for username in user_names:
ws_c.import_user_home(username, 'user_exports')
jobs_c = JobsClient(client_config, checkpoint_service)
# this will only import the groups jobs since we're filtering the jobs during the export process
print('Importing the groups members jobs:')
jobs_c.import_job_configs()
end = timer()
print("Complete User Export Time: " + str(timedelta(seconds=end - start)))
if args.libs:
start = timer()
print("Not supported today")
end = timer()
# print("Complete Library Import Time: " + str(timedelta(seconds=end - start)))
if args.mlflow_experiments:
print("Importing MLflow experiments.")
mlflow_c = MLFlowClient(client_config, checkpoint_service)
mlflow_c.import_mlflow_experiments(num_parallel=args.num_parallel)
failed_task_log = logging_utils.get_error_log_file(wmconstants.WM_IMPORT, wmconstants.MLFLOW_EXPERIMENT_OBJECT, client_config['export_dir'])
logging_utils.raise_if_failed_task_file_exists(failed_task_log, "MLflow Runs Import.")
if args.mlflow_experiments_permissions:
print("Importing MLflow experiment permissions.")
mlflow_c = MLFlowClient(client_config, checkpoint_service)
mlflow_c.import_mlflow_experiments_acls(num_parallel=args.num_parallel)
failed_task_log = logging_utils.get_error_log_file(wmconstants.WM_IMPORT, wmconstants.MLFLOW_EXPERIMENT_PERMISSION_OBJECT, client_config['export_dir'])
logging_utils.raise_if_failed_task_file_exists(failed_task_log, "MLflow Experiments Permissions Import.")
if args.mlflow_runs:
print("Importing MLflow runs.")
mlflow_c = MLFlowClient(client_config, checkpoint_service)
assert args.src_profile is not None, "Import MLflow runs requires --src-profile flag."
src_login_args = get_login_credentials(profile=args.src_profile)
src_client_config = build_client_config(args.src_profile, src_login_args['host'], src_login_args['token'], args)
mlflow_c.import_mlflow_runs(src_client_config, num_parallel=args.num_parallel)
failed_task_log = logging_utils.get_error_log_file(wmconstants.WM_IMPORT, wmconstants.MLFLOW_RUN_OBJECT, client_config['export_dir'])
logging_utils.raise_if_failed_task_file_exists(failed_task_log, "MLflow Runs Import.")
if args.get_repair_log:
print("Finding partitioned tables to repair at {0}".format(now))
start = timer()
hive_c = HiveClient(client_config, checkpoint_service)
# log job configs
hive_c.repair_legacy_tables()
end = timer()
print("Complete Report Time: " + str(timedelta(seconds=end - start)))
if __name__ == '__main__':
main()