/
deploy.py
228 lines (201 loc) · 8.33 KB
/
deploy.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
import os
import yaml
import tempfile
import shutil
from datmo.core.controller.base import BaseController
from datmo.core.util.misc_functions import Commands
from datmo.core.controller.deploy.driver.datmo_microservice import DatmoMicroserviceDeployDriver
from datmo.config import Config
from datmo.core.util.spinner import Spinner
class DeployController(BaseController):
"""
A controller for deploying a model
Parameters
----------
service_container_manager : bool
representing to use service's container management system
Methods
-------
system_setup()
Setup the system with provider and their key and secret token
cluster_deploy(cluster_name=None, server_type=None, size=None)
Deploy the Servers in the cluster with the defined setup
cluster_update(cluster_name, size)
Scale up/down the number of servers in the cluster
cluster_stop(cluster_name)
Stop and remove the cluster
cluster_ls(cluster_name='*')
List all containers in the cluster
system_info()
To return the information about the System and Logging portal
system_cost()
To return the information about the cost due to datmo deploy from cloud systems
model_deploy(cluster_name)
Deploy the model after building it from the docker compose file
service_iologs(service_path, date)
Extract io logs for a particular service
"""
def __init__(self, service_container_management=False):
"""Initialize the Orchestrator service"""
super(DeployController, self).__init__()
self.commands = Commands()
self.config = Config()
self.master_server_ip, self.datmo_api_key, self.datmo_end_point = self.config.remote_credentials
self.service_container_management = service_container_management
self.driver = DatmoMicroserviceDeployDriver(
end_point=self.datmo_end_point, api_key=self.datmo_api_key)
self.spinner = Spinner()
def cluster_deploy(self, cluster_name=None, server_type=None, size=None):
"""
Deploy the Servers in the cluster with the defined setup
"""
# Validate deployment
bool_deploy_validate, response = self.driver.validate_deploy(self.home)
if not bool_deploy_validate:
return response
self.spinner.start()
response = self.driver.create_cluster(
cluster_name, server_type, count=size)
self.spinner.stop()
return response
def cluster_update(self, cluster_name, size):
"""
Scale up/down the number of servers in the cluster
Parameters
----------
cluster_name : str
Name of cluster
size : str
Number of servers
"""
# Validate deployment
bool_deploy_validate, response = self.driver.validate_deploy(self.home)
if not bool_deploy_validate:
return response
self.spinner.start()
response = self.driver.update_cluster(
count=size, cluster_name=cluster_name)
self.spinner.stop()
return response
def cluster_stop(self, cluster_name):
"""
Stop and remove the cluster
Parameters
----------
cluster_name : str
name of the cluster
"""
self.spinner.start()
response = self.driver.update_cluster(
count=0, cluster_name=cluster_name)
self.spinner.stop()
return response
def cluster_ls(self, cluster_name='*'):
"""
List all containers in the cluster
Parameters
----------
cluster_name : str
name of the cluster
"""
self.spinner.start()
response = self.driver.get_cluster_info(cluster_name)
self.spinner.stop()
return response
def system_info(self):
"""
To return the information about the System and Logging portal
"""
response = self.driver.get_system_info()
return response
def system_cost(self):
"""
To return the information about the cost due to datmo deploy from cloud systems
"""
response = self.driver.get_system_cost()
return response
def model_deploy(self, cluster_name):
"""
Deploy the model after building it from the docker compose file
Parameters
----------
cluster_name : str
Name of the cluster
"""
# Validate deployment
bool_deploy_validate, response = self.driver.validate_deploy(self.home)
if not bool_deploy_validate:
return response
# Specific for datmo service logic
tmp_dirpath = tempfile.mkdtemp()
# copy the content for project directory to tmp folder and the environment to root location in tmp folder
try:
self.commands.copy(self.home, tmp_dirpath)
environment_dirpath = os.path.join(tmp_dirpath,
'datmo_environment')
files_dirpath = os.path.join(tmp_dirpath, 'datmo_files')
if os.path.exists(environment_dirpath):
shutil.rmtree(os.path.join(tmp_dirpath, '.datmo'))
self.commands.copy(environment_dirpath, tmp_dirpath)
shutil.rmtree(os.path.join(tmp_dirpath, 'datmo_environment'))
if os.path.exists(files_dirpath):
shutil.rmtree(os.path.join(files_dirpath))
# Exclude any files based on datmo deploy config file
if os.path.exists(os.path.join(tmp_dirpath, 'datmo-deploy.yml')):
datmo_deploy_config_path = os.path.join(
tmp_dirpath, 'datmo-deploy.yml')
elif os.path.exists(
os.path.join(tmp_dirpath, 'datmo-deploy.yaml')):
datmo_deploy_config_path = os.path.join(
tmp_dirpath, 'datmo-deploy.yaml')
else:
datmo_deploy_config_path = None
list_dir = os.listdir(tmp_dirpath)
if datmo_deploy_config_path:
with open(datmo_deploy_config_path, 'r') as stream:
try:
datmo_deploy = yaml.safe_load(stream)
if datmo_deploy is not None:
files_exclude = datmo_deploy['deploy'][
'files_exclude']
for item in list_dir:
if item in files_exclude:
if os.path.isfile(
os.path.join(tmp_dirpath, item)):
os.remove(
os.path.join(tmp_dirpath, item))
elif os.path.isdir(
os.path.join(tmp_dirpath, item)):
shutil.rmtree(
os.path.join(tmp_dirpath, item))
if item.startswith('.') and \
os.path.isdir(os.path.join(tmp_dirpath, item)):
shutil.rmtree(
os.path.join(tmp_dirpath, item))
elif item.startswith('.') and \
os.path.isfile(os.path.join(tmp_dirpath, item)):
os.remove(os.path.join(tmp_dirpath, item))
except yaml.YAMLError as exc:
print(exc)
except Exception as e:
print(e)
model_zipfile_path = os.path.join(tmp_dirpath, 'datmo_model.zip')
self.spinner.start()
self.commands.zip_folder(tmp_dirpath, model_zipfile_path)
response = self.driver.model_deploy(cluster_name, model_zipfile_path)
# remove the temp directory
shutil.rmtree(tmp_dirpath)
self.spinner.stop()
return response
def service_iologs(self, service_path, date):
"""
Extract io logs for a particular service
Parameters
----------
service_path : str
service route for the algorithm
date : str
Date for which you want to get the logs
"""
response = self.driver.get_service_iologs(service_path, date)
return response