-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathapideployer.py
More file actions
561 lines (440 loc) · 20.1 KB
/
apideployer.py
File metadata and controls
561 lines (440 loc) · 20.1 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
import json
from .future import DSSFuture
class DSSAPIDeployer(object):
"""
Handle to interact with the API Deployer.
Do not create this directly, use :meth:`dataikuapi.dss.DSSClient.get_apideployer`
"""
def __init__(self, client):
self.client = client
def list_deployments(self, as_objects=True):
"""
Lists deployments on the API Deployer
:param boolean as_objects: if True, returns a list of :class:`DSSAPIDeployerDeployment`, else returns a list of dict.
Each dict contains at least a field "id" indicating the identifier of this deployment
:returns: a list - see as_objects for more information
:rtype: list
"""
l = self.client._perform_json("GET", "/api-deployer/deployments")
if as_objects:
return [DSSAPIDeployerDeployment(self.client, x["deploymentBasicInfo"]["id"]) for x in l]
else:
return l
def get_deployment(self, deployment_id):
"""
Returns a handle to interact with a single deployment, as a :class:`DSSAPIDeployerDeployment`
:param str deployment_id: Identifier of the deployment to get
:rtype: :class:`DSSAPIDeployerDeployment`
"""
return DSSAPIDeployerDeployment(self.client, deployment_id)
def create_deployment(self, deployment_id, service_id, infra_id, version):
"""
Creates a deployment and returns the handle to interact with it. The returned deployment
is not yet started and you need to call :meth:`~DSSAPIDeployerDeployment.start_update`
:param str deployment_id: Identifier of the deployment to create
:param str service_id: Identifier of the API Service to target
:param str infra_id: Identifier of the deployment infrastructure to use
:param str version_id: Identifier of the API Service version to deploy
:rtype: :class:`DSSAPIDeployerDeployment`
"""
settings = {
"deploymentId" : deployment_id,
"publishedServiceId" : service_id,
"infraId" : infra_id,
"version" : version
}
self.client._perform_json("POST", "/api-deployer/deployments", body=settings)
return self.get_deployment(deployment_id)
def list_stages(self):
"""
Lists infrastructure stages of the API Deployer
:rtype: a list of dict. Each dict contains a field "id" for the stage identifier and "desc" for its description.
:rtype: list
"""
return self.client._perform_json("GET", "/api-deployer/stages")
def list_infras(self, as_objects=True):
"""
Lists deployment infrastructures on the API Deployer
:param boolean as_objects: if True, returns a list of :class:`DSSAPIDeployerInfra`, else returns a list of dict.
Each dict contains at least a field "id" indicating the identifier of this infra
:returns: a list - see as_objects for more information
:rtype: list
"""
l = self.client._perform_json("GET", "/api-deployer/infras")
if as_objects:
return [DSSAPIDeployerInfra(self.client, x["infraBasicInfo"]["id"]) for x in l]
else:
return l
def create_infra(self, infra_id, stage, type):
"""
Creates a new infrastructure on the API Deployer and returns the handle to interact with it.
:param str infra_id: Unique Identifier of the infra to create
:param str stage: Infrastructure stage. Stages are configurable on each API Deployer
:param str type: STATIC or KUBERNETES
:rtype: :class:`DSSAPIDeployerInfra`
"""
settings = {
"id": infra_id,
"stage": stage,
"type": type,
}
self.client._perform_json("POST", "/api-deployer/infras", body=settings)
return self.get_infra(infra_id)
def get_infra(self, infra_id):
"""
Returns a handle to interact with a single deployment infra, as a :class:`DSSAPIDeployerInfra`
:param str infra_id: Identifier of the infra to get
:rtype: :class:`DSSAPIDeployerInfra`
"""
return DSSAPIDeployerInfra(self.client, infra_id)
def list_services(self, as_objects=True):
"""
Lists API services on the API Deployer
:param boolean as_objects: if True, returns a list of :class:`DSSAPIDeployerService`, else returns a list of dict.
Each dict contains at least a field "id" indicating the identifier of this Service
:returns: a list - see as_objects for more information
:rtype: list
"""
l = self.client._perform_json("GET", "/api-deployer/services")
if as_objects:
return [DSSAPIDeployerService(self.client, x["serviceBasicInfo"]["id"]) for x in l]
else:
return l
def create_service(self, service_id):
"""
Creates a new API Service on the API Deployer and returns the handle to interact with it.
:param str service_id: Identifier of the API Service to create
:rtype: :class:`DSSAPIDeployerService`
"""
settings = {
"publishedServiceId" : service_id
}
self.client._perform_json("POST", "/api-deployer/services", body=settings)
return self.get_service(service_id)
def get_service(self, service_id):
"""
Returns a handle to interact with a single service, as a :class:`DSSAPIDeployerService`
:param str service_id: Identifier of the API service to get
:rtype: :class:`DSSAPIDeployerService`
"""
return DSSAPIDeployerService(self.client, service_id)
###############################################
# Infrastructures
###############################################
class DSSAPIDeployerInfra(object):
"""
An API Deployer infrastructure.
Do not create this directly, use :meth:`~dataikuapi.dss.apideployer.DSSAPIDeployer.get_infra`
"""
def __init__(self, client, infra_id):
self.client = client
self.infra_id = infra_id
def id(self):
return self.infra_id
def get_status(self):
"""
Returns status information about this infrastructure
:rtype: a :class:`dataikuapi.dss.apideployer.DSSAPIDeployerInfraStatus`
"""
light = self.client._perform_json("GET", "/api-deployer/infras/%s" % (self.infra_id))
return DSSAPIDeployerInfraStatus(self.client, self.infra_id, light)
def get_settings(self):
"""
Gets the settings of this infra. If you want to modify the settings, you need to
call :meth:`~dataikuapi.dss.apideployer.DSSAPIDeployerInfraSettings.save` on the returned
object
:returns: a :class:`dataikuapi.dss.apideployer.DSSAPIDeployerInfraSettings`
"""
settings = self.client._perform_json(
"GET", "/api-deployer/infras/%s/settings" % (self.infra_id))
return DSSAPIDeployerInfraSettings(self.client, self.infra_id, settings)
def delete(self):
"""
Deletes this infra
You may only delete an infra if it has no deployments on it anymore.
"""
self.client._perform_empty(
"DELETE", "/api-deployer/infras/%s" % (self.infra_id))
class DSSAPIDeployerInfraSettings(object):
"""
The settings of an API Deployer infrastructure
Do not create this directly, use :meth:`~dataikuapi.dss.apideployer.DSSAPIDeployerInfra.get_settings`
"""
def __init__(self, client, infra_id, settings):
self.client = client
self.infra_id = infra_id
self.settings = settings
def get_type(self):
"""Gets the type of this infra, either STATIC or K8S"""
return self.settings["type"]
def add_apinode(self, url, api_key, graphite_prefix=None):
"""Adds an API node to the list of nodes of this infra.
Only applicable to STATIC infrastructures"""
new_node = {
"url": url,
"adminAPIKey" : api_key,
"graphitePrefix" : graphite_prefix
}
self.settings["apiNodes"].append(new_node)
def remove_apinode(self, node_url):
"""
Removes a node from the list of nodes of this infra.
Only applicable to STATIC infrastructures
:param str node_url: URL of the node to remove
"""
api_nodes = list(self.settings["apiNodes"])
for node in api_nodes:
if node.get("url") == node_url:
self.settings["apiNodes"].remove(node)
def get_raw(self):
"""
Gets the raw settings of this infra. This returns a reference to the raw settings, not a copy,
so changes made to the returned object will be reflected when saving.
:rtype: dict
"""
return self.settings
def save(self):
"""Saves back these settings to the infra"""
self.client._perform_empty(
"PUT", "/api-deployer/infras/%s/settings" % (self.infra_id),
body = self.settings)
class DSSAPIDeployerInfraStatus(object):
"""
The status of an API Deployer infrastructure.
Do not create this directly, use :meth:`~dataikuapi.dss.apideployer.DSSAPIDeployerInfra.get_status`
"""
def __init__(self, client, infra_id, light_status):
self.client = client
self.infra_id = infra_id
self.light_status = light_status
def list_deployments(self):
"""
Returns the deployments that are deployed on this infrastructure
:returns: a list of deployments
:rtype: list of :class:`dataikuapi.dss.apideployer.DSSAPIDeployerDeployment`
"""
return [DSSAPIDeployerDeployment(self.client, deployment["id"]) for deployment in self.light_status["deployments"]]
def get_raw(self):
"""
Gets the raw status information. This returns a dictionary with various information about the infrastructure
:rtype: dict
"""
return self.light_status
###############################################
# Deployments
###############################################
class DSSAPIDeployerDeployment(object):
"""
A Deployment on the API Deployer
Do not create this directly, use :meth:`~dataikuapi.dss.apideployer.DSSAPIDeployer.get_deployment`
"""
def __init__(self, client, deployment_id):
self.client = client
self.deployment_id = deployment_id
def id(self):
return self.deployment_id
def get_status(self):
"""Returns status information about this deployment
:rtype: dataikuapi.dss.apideployer.DSSAPIDeployerDeploymentStatus
"""
light = self.client._perform_json("GET", "/api-deployer/deployments/%s" % (self.deployment_id))
heavy = self.client._perform_json("GET", "/api-deployer/deployments/%s/status" % (self.deployment_id))
return DSSAPIDeployerDeploymentStatus(self.client, self.deployment_id, light, heavy)
def get_settings(self):
"""
Gets the settings of this deployment. If you want to modify the settings, you need to
call :meth:`~dataikuapi.dss.apideployer.DSSAPIDeployerDeploymentSettings.save` on the returned
object
:returns: a :class:`dataikuapi.dss.apideployer.DSSAPIDeployerDeploymentSettings`
"""
settings = self.client._perform_json(
"GET", "/api-deployer/deployments/%s/settings" % (self.deployment_id))
return DSSAPIDeployerDeploymentSettings(self.client, self.deployment_id, settings)
def start_update(self):
"""
Starts an asynchronous update of this deployment to try to match the actual state to the current settings
:returns: a :class:`dataikuapi.dss.future.DSSFuture` tracking the progress of the update. Call
:meth:`~dataikuapi.dss.future.DSSFuture.wait_for_result` on the returned object
to wait for completion (or failure)
"""
future_response = self.client._perform_json(
"POST", "/api-deployer/deployments/%s/actions/update" % (self.deployment_id))
return DSSFuture(self.client, future_response.get('jobId', None), future_response)
def delete(self):
"""
Deletes this deployment
You may only delete a deployment if it is disabled and has been updated after disabling it.
"""
return self.client._perform_empty(
"DELETE", "/api-deployer/deployments/%s" % (self.deployment_id))
class DSSAPIDeployerDeploymentSettings(object):
"""The settings of an API Deployer deployment.
Do not create this directly, use :meth:`~dataikuapi.dss.apideployer.DSSAPIDeployerDeployment.get_settings`
"""
def __init__(self, client, deployment_id, settings):
self.client = client
self.deployment_id = deployment_id
self.settings = settings
def get_raw(self):
"""
Gets the raw settings of this deployment. This returns a reference to the raw settings, not a copy,
so changes made to the returned object will be reflected when saving.
:rtype: dict
"""
return self.settings
def set_enabled(self, enabled):
"""Enables or disables this deployment"""
self.settings["enabled"] = enabled
def set_single_version(self, version):
"""
Sets this deployment to target a single version of the API service
:param str version: Identifier of the version to set
"""
self.settings["generationsMapping"] = {
"mode": "SINGLE_GENERATION",
"generation": version
}
def save(self):
"""Saves back these settings to the deployment"""
self.client._perform_empty(
"PUT", "/api-deployer/deployments/%s/settings" % (self.deployment_id),
body = self.settings)
class DSSAPIDeployerDeploymentStatus(object):
"""The status of an API Deployer deployment.
Do not create this directly, use :meth:`~dataikuapi.dss.apideployer.DSSAPIDeployerDeployment.get_status`
"""
def __init__(self, client, deployment_id, light_status, heavy_status):
self.client = client
self.deployment_id = deployment_id
self.light_status = light_status
self.heavy_status = heavy_status
def get_light(self):
"""
Gets the 'light' (summary) status. This returns a dictionary with various information about the deployment,
but not the actual health of the deployment
:rtype: dict
"""
return self.light_status
def get_heavy(self):
"""
Gets the 'heavy' (full) status. This returns a dictionary with various information about the deployment
:rtype: dict
"""
return self.heavy_status
def get_service_urls(self):
"""Returns service-level URLs for this deployment (ie without the enpdoint-specific suffix)"""
if "deployedServiceId" in self.light_status["deploymentBasicInfo"]:
service_id = self.light_status["deploymentBasicInfo"]["deployedServiceId"]
else:
service_id = self.light_status["deploymentBasicInfo"]["publishedServiceId"]
if "apiNodes" in self.heavy_status:
return ["%s/public/api/v1/%s" % (x["url"], service_id) for x in self.heavy_status["apiNodes"]]
elif "publicURL" in self.heavy_status:
return ["%s/public/api/v1/%s" % (self.heavy_status["publicURL"], service_id)]
else:
raise ValueError("PublicURL not available for this deployment. It might still be initializing")
def get_health(self):
"""Returns the health of this deployment as a string
:returns: HEALTHY if the deployment is working properly, various other status otherwise
:rtype: string
"""
return self.heavy_status["health"]
def get_health_messages(self):
"""Returns messages about the health of this deployment"""
return self.heavy_status["healthMessages"]
###############################################
# Published Service
###############################################
class DSSAPIDeployerService(object):
"""
An API service on the API Deployer
Do not create this directly, use :meth:`~dataikuapi.dss.apideployer.DSSAPIDeployer.get_service`
"""
def __init__(self, client, service_id):
self.client = client
self.service_id = service_id
def id(self):
return self.service_id
def get_status(self):
"""
Returns status information about this service. This is used mostly to get information about
which versions are available and which deployments are exposing this service
:rtype: dataikuapi.dss.apideployer.DSSAPIDeployerServiceStatus
"""
light = self.client._perform_json("GET", "/api-deployer/services/%s" % (self.service_id))
return DSSAPIDeployerServiceStatus(self.client, self.service_id, light)
def import_version(self, fp):
"""
Imports a new version for an API service from a file-like object pointing
to a version package Zip file
:param string fp: A file-like object pointing to a version package Zip file
"""
return self.client._perform_empty("POST",
"/api-deployer/services/%s/versions" % (self.service_id), files={"file":fp})
def get_settings(self):
"""
Gets the settings of this service. If you want to modify the settings, you need to
call :meth:`~dataikuapi.dss.apideployer.DSSAPIDeployerServiceSettings.save` on the returned
object.
The main things that can be modified in a service settings are permissions
:returns: a :class:`dataikuapi.dss.apideployer.DSSAPIDeployerServiceSettings`
"""
settings = self.client._perform_json(
"GET", "/api-deployer/services/%s/settings" % (self.service_id))
return DSSAPIDeployerServiceSettings(self.client, self.service_id, settings)
def delete_version(self, version):
"""
Deletes a version from this service
:param string version: The version to delete
"""
self.client._perform_empty(
"DELETE", "/api-deployer/services/%s/versions/%s" % (self.service_id, version))
def delete(self):
"""
Deletes this service
You may only delete a service if it has no deployments on it anymore.
"""
return self.client._perform_empty(
"DELETE", "/api-deployer/services/%s" % (self.service_id))
class DSSAPIDeployerServiceSettings(object):
"""The settings of an API Deployer Service.
Do not create this directly, use :meth:`~dataikuapi.dss.apideployer.DSSAPIDeployerService.get_settings`
"""
def __init__(self, client, service_id, settings):
self.client = client
self.service_id = service_id
self.settings = settings
def get_raw(self):
"""
Gets the raw settings of this deployment. This returns a reference to the raw settings, not a copy,
so changes made to the returned object will be reflected when saving.
:rtype: dict
"""
return self.settings
def save(self):
"""Saves back these settings to the API service"""
self.client._perform_empty(
"PUT", "/api-deployer/services/%s/settings" % (self.service_id),
body = self.settings)
class DSSAPIDeployerServiceStatus(object):
"""The status of an API Deployer Service.
Do not create this directly, use :meth:`~dataikuapi.dss.apideployer.DSSAPIDeployerService.get_status`
"""
def __init__(self, client, service_id, light_status):
self.client = client
self.service_id = service_id
self.light_status = light_status
def get_versions(self):
"""
Returns the versions of this service that have been published on the API Service
Each version is a dict that contains at least a "id" field, which is the version identifier
:returns: a list of versions, each as a dict containing a "id" field
:rtype: list of dicts
"""
return self.light_status["packages"]
def get_raw(self):
"""
Gets the raw status information. This returns a dictionary with various information about the service,
:rtype: dict
"""
return self.light_status