-
-
Notifications
You must be signed in to change notification settings - Fork 765
Expand file tree
/
Copy pathutils.py
More file actions
168 lines (143 loc) · 6.04 KB
/
Copy pathutils.py
File metadata and controls
168 lines (143 loc) · 6.04 KB
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
import os
import shutil
import click
from fedml.computing.scheduler.scheduler_entry.resource_manager import FedMLResourceManager
from fedml.computing.scheduler.comm_utils.security_utils import get_api_key, save_api_key
FEDML_MLOPS_BUILD_PRE_IGNORE_LIST = 'dist-packages,client-package.zip,server-package.zip,__pycache__,*.pyc,*.git'
def fedml_login(api_key, encrypted_api_key_flag=False):
api_key_is_valid, api_key = _check_api_key(api_key=api_key, encrypted_api_key_flag=encrypted_api_key_flag)
if api_key_is_valid:
return 0, api_key
return -1, api_key
def _check_api_key(api_key=None, encrypted_api_key_flag=False):
if api_key is None or api_key == "":
saved_api_key = get_api_key()
if saved_api_key is None or saved_api_key == "":
api_key = click.prompt("FedML® Launch API Key is not set yet, please input your API key")
else:
api_key = saved_api_key
is_valid_heartbeat = FedMLResourceManager.get_instance().check_heartbeat(api_key, encrypted_api_key_flag)
if not is_valid_heartbeat:
return False, api_key
else:
save_api_key(api_key)
return True, api_key
def authenticate(api_key, encrypted_api_key_flag=False):
error_code, api_key = fedml_login(api_key, encrypted_api_key_flag)
# Exit if not able to authenticate successfully
if error_code:
raise SystemExit(f"Failed to authenticate with apikey: '{api_key}'. Please make sure your login "
f"credentials are valid.")
return api_key
def build_mlops_package(
ignore,
source_folder,
entry_point,
config_folder,
dest_folder,
mlops_build_path,
mlops_package_parent_dir,
mlops_package_name,
rank,
package_type="default"
):
if not os.path.exists(source_folder):
click.echo("source folder is not exist: " + source_folder)
return -1
if not os.path.exists(os.path.join(source_folder, entry_point)):
click.echo(
"entry file: "
+ entry_point
+ " is not exist in the source folder: "
+ source_folder
)
return -1
if not os.path.exists(config_folder):
click.echo("config folder is not exist: " + source_folder)
return -1
mlops_src = source_folder
mlops_src_entry = entry_point
mlops_conf = config_folder
cur_dir = mlops_build_path
mlops_package_base_dir = os.path.join(
cur_dir, "mlops-core", mlops_package_parent_dir
)
package_dir = os.path.join(mlops_package_base_dir, mlops_package_name)
fedml_dir = os.path.join(package_dir, "fedml")
mlops_dest = fedml_dir
mlops_dest_conf = os.path.join(fedml_dir, "config")
mlops_pkg_conf = os.path.join(package_dir, "conf", "fedml.yaml")
mlops_dest_entry = os.path.join("fedml", mlops_src_entry)
mlops_package_file_name = mlops_package_name + ".zip"
dist_package_dir = os.path.join(dest_folder, "dist-packages")
dist_package_file = os.path.join(dist_package_dir, mlops_package_file_name)
ignore_list = tuple(ignore.split(','))
shutil.rmtree(mlops_dest, ignore_errors=True)
try:
os.rmdir(mlops_dest)
except Exception as e:
pass
try:
shutil.copytree(mlops_src, mlops_dest, copy_function=shutil.copy,
ignore_dangling_symlinks=True, ignore=shutil.ignore_patterns(*ignore_list))
except Exception as e:
pass
shutil.rmtree(mlops_dest_conf, ignore_errors=True)
try:
os.rmdir(mlops_dest_conf)
except Exception as e:
pass
try:
shutil.copytree(mlops_conf, mlops_dest_conf, copy_function=shutil.copy,
ignore_dangling_symlinks=True, ignore=shutil.ignore_patterns(*ignore_list))
except Exception as e:
pass
try:
os.remove(os.path.join(mlops_dest_conf, "mqtt_config.yaml"))
os.remove(os.path.join(mlops_dest_conf, "s3_config.yaml"))
except Exception as e:
pass
mlops_pkg_conf_file = open(mlops_pkg_conf, mode="w")
mlops_pkg_conf_file.writelines(
[
"entry_config: \n",
" entry_file: " + mlops_dest_entry + "\n",
" conf_file: config/fedml_config.yaml\n",
" package_type: " + package_type + "\n",
"dynamic_args:\n",
" rank: " + rank + "\n",
" run_id: ${FEDSYS.RUN_ID}\n",
# " data_cache_dir: ${FEDSYS.PRIVATE_LOCAL_DATA}\n",
# " data_cache_dir: /fedml/fedml-package/fedml/data\n",
" mqtt_config_path: /fedml/fedml_config/mqtt_config.yaml\n",
" s3_config_path: /fedml/fedml_config/s3_config.yaml\n",
" log_file_dir: /fedml/fedml-package/fedml/data\n",
" log_server_url: ${FEDSYS.LOG_SERVER_URL}\n",
" client_id_list: ${FEDSYS.CLIENT_ID_LIST}\n",
" client_objects: ${FEDSYS.CLIENT_OBJECT_LIST}\n",
" is_using_local_data: ${FEDSYS.IS_USING_LOCAL_DATA}\n",
" synthetic_data_url: ${FEDSYS.SYNTHETIC_DATA_URL}\n",
" client_num_in_total: ${FEDSYS.CLIENT_NUM}\n",
]
)
mlops_pkg_conf_file.flush()
mlops_pkg_conf_file.close()
local_mlops_package = os.path.join(mlops_package_base_dir, mlops_package_file_name)
if os.path.exists(local_mlops_package):
os.remove(os.path.join(mlops_package_base_dir, mlops_package_file_name))
mlops_archive_name = os.path.join(mlops_package_base_dir, mlops_package_name)
shutil.make_archive(
mlops_archive_name,
"zip",
root_dir=mlops_package_base_dir,
base_dir=mlops_package_name,
)
if not os.path.exists(dist_package_dir):
os.makedirs(dist_package_dir, exist_ok=True)
if os.path.exists(dist_package_file) and not os.path.isdir(dist_package_file):
os.remove(dist_package_file)
mlops_archive_zip_file = mlops_archive_name + ".zip"
if os.path.exists(mlops_archive_zip_file):
shutil.move(mlops_archive_zip_file, dist_package_file)
shutil.rmtree(mlops_build_path, ignore_errors=True)
return 0