-
-
Notifications
You must be signed in to change notification settings - Fork 765
Expand file tree
/
Copy pathresource_manager.py
More file actions
91 lines (81 loc) · 3.66 KB
/
Copy pathresource_manager.py
File metadata and controls
91 lines (81 loc) · 3.66 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
import fedml
import requests
from fedml.core.common.singleton import Singleton
from fedml.computing.scheduler.master.server_constants import ServerConstants
from fedml.core.mlops.mlops_configs import MLOpsConfigs
class FedMLResourceManager(Singleton):
def __init__(self):
self.config_version = fedml.get_env_version()
@staticmethod
def get_instance():
return FedMLResourceManager()
def check_heartbeat(self, api_key, encrypted_api_key_flag=False):
heartbeat_url = ServerConstants.get_heartbeat_url()
heartbeat_api_headers = {'Content-Type': 'application/json', 'Connection': 'close',
'Encrypted': str(encrypted_api_key_flag)}
heartbeat_json = {
"apiKey": api_key
}
args = {"config_version": self.config_version}
cert_path = MLOpsConfigs.get_cert_path_with_version()
if cert_path is not None:
try:
requests.session().verify = cert_path
response = requests.post(
heartbeat_url, verify=True, headers=heartbeat_api_headers, json=heartbeat_json
)
except requests.exceptions.SSLError as err:
MLOpsConfigs.install_root_ca_file()
response = requests.post(
heartbeat_url, verify=True, headers=heartbeat_api_headers, json=heartbeat_json
)
else:
response = requests.post(heartbeat_url, headers=heartbeat_api_headers, json=heartbeat_json)
if response.status_code != 200:
print(f"Check heartbeat with response.status_code = {response.status_code}, "
f"response.content: {response.content}")
return False
else:
resp_data = response.json()
code = resp_data.get("code", "")
message = resp_data.get("message", "")
data = resp_data.get("data", False)
if code == "SUCCESS" and data is True:
return True
return False
def show_resource_type(self):
resource_url = ServerConstants.get_resource_url()
args = {"config_version": self.config_version}
cert_path = MLOpsConfigs.get_cert_path_with_version()
if cert_path is not None:
try:
requests.session().verify = cert_path
response = requests.get(
resource_url, verify=True)
except requests.exceptions.SSLError as err:
MLOpsConfigs.install_root_ca_file()
response = requests.get(
resource_url, verify=True)
else:
# the server only allows GET
response = requests.get(resource_url)
if response.status_code != 200:
print(f"Get resource type with response.status_code = {response.status_code}, "
f"response.content: {response.content}")
pass
else:
resp_data = response.json()
code = resp_data.get("code", "")
message = resp_data.get("message", "")
data = resp_data.get("data", None)
if code == "SUCCESS" and data is not None:
resource_list = list()
for resource_item in data:
gpu_type = resource_item.get("gpuType", None)
resource_type = resource_item.get("resourceType", None)
resource_list.append((resource_type, gpu_type))
return resource_list
else:
print(f"Get resource type with response.status_code = {response.status_code}, "
f"response.content: {response.content}")
return None