-
Notifications
You must be signed in to change notification settings - Fork 13
/
deployment.py
335 lines (284 loc) · 10.3 KB
/
deployment.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
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
from __future__ import annotations
import asyncio
import json
import os
import posixpath
import tempfile
from abc import abstractmethod
from typing import TYPE_CHECKING, Type, cast
from streamflow.core import utils
from streamflow.core.config import Config
from streamflow.core.context import SchemaEntity
from streamflow.core.persistence import DatabaseLoadingContext, PersistableEntity
if TYPE_CHECKING:
from streamflow.core.data import StreamWrapperContextManager
from streamflow.core.context import StreamFlowContext
from streamflow.core.scheduling import AvailableLocation
from streamflow.core.workflow import Job
from typing import MutableSequence, MutableMapping, Any
LOCAL_LOCATION = "__LOCAL__"
def _init_workdir(deployment_name: str) -> str:
if deployment_name != LOCAL_LOCATION:
return posixpath.join("/tmp", "streamflow") # nosec
else:
return os.path.join(os.path.realpath(tempfile.gettempdir()), "streamflow")
class Location:
__slots__ = ("name", "deployment", "service")
def __init__(self, name: str, deployment: str, service: str | None = None):
self.name: str = name
self.deployment: str = deployment
self.service: str | None = service
def __eq__(self, other):
if not isinstance(other, Location):
return False
else:
return (
self.deployment == other.deployment
and self.service == other.service
and self.name == other.name
)
def __hash__(self):
return hash((self.deployment, self.service, self.name))
def __str__(self) -> str:
if self.service:
return posixpath.join(self.deployment, self.service, self.name)
else:
return posixpath.join(self.deployment, self.name)
class BindingFilter(SchemaEntity):
@abstractmethod
async def get_targets(
self, job: Job, targets: MutableSequence[Target]
) -> MutableSequence[Target]: ...
class Connector(SchemaEntity):
def __init__(self, deployment_name: str, config_dir: str, transferBufferSize: int):
self.deployment_name: str = deployment_name
self.config_dir: str = config_dir
self.transferBufferSize: int = transferBufferSize
@abstractmethod
async def copy_local_to_remote(
self,
src: str,
dst: str,
locations: MutableSequence[Location],
read_only: bool = False,
) -> None: ...
@abstractmethod
async def copy_remote_to_local(
self,
src: str,
dst: str,
locations: MutableSequence[Location],
read_only: bool = False,
) -> None: ...
@abstractmethod
async def copy_remote_to_remote(
self,
src: str,
dst: str,
locations: MutableSequence[Location],
source_location: Location,
source_connector: Connector | None = None,
read_only: bool = False,
) -> None: ...
@abstractmethod
async def deploy(self, external: bool) -> None: ...
@abstractmethod
async def get_available_locations(
self,
service: str | None = None,
input_directory: str | None = None,
output_directory: str | None = None,
tmp_directory: str | None = None,
) -> MutableMapping[str, AvailableLocation]: ...
@abstractmethod
async def run(
self,
location: Location,
command: MutableSequence[str],
environment: MutableMapping[str, str] = None,
workdir: str | None = None,
stdin: int | str | None = None,
stdout: int | str = asyncio.subprocess.STDOUT,
stderr: int | str = asyncio.subprocess.STDOUT,
capture_output: bool = False,
timeout: int | None = None,
job_name: str | None = None,
) -> tuple[Any | None, int] | None: ...
@abstractmethod
async def undeploy(self, external: bool) -> None: ...
@abstractmethod
async def get_stream_reader(
self, location: Location, src: str
) -> StreamWrapperContextManager: ...
class DeploymentManager(SchemaEntity):
def __init__(self, context: StreamFlowContext) -> None:
self.context: StreamFlowContext = context
@abstractmethod
async def close(self) -> None: ...
@abstractmethod
async def deploy(self, deployment_config: DeploymentConfig) -> None: ...
@abstractmethod
def get_connector(self, deployment_name: str) -> Connector | None: ...
@abstractmethod
async def undeploy(self, deployment_name: str) -> None: ...
@abstractmethod
async def undeploy_all(self): ...
class DeploymentConfig(Config, PersistableEntity):
__slots__ = ("name", "type", "config", "external", "lazy", "workdir", "wraps")
def __init__(
self,
name: str,
type: str,
config: MutableMapping[str, Any],
external: bool = False,
lazy: bool = True,
workdir: str | None = None,
wraps: str | None = None,
) -> None:
Config.__init__(self, name, type, config)
PersistableEntity.__init__(self)
self.external: bool = external
self.lazy: bool = lazy
self.workdir: str | None = workdir
self.wraps: str | None = wraps
@classmethod
async def load(
cls,
context: StreamFlowContext,
persistent_id: int,
loading_context: DatabaseLoadingContext,
) -> DeploymentConfig:
row = await context.database.get_deployment(persistent_id)
obj = cls(
name=row["name"],
type=row["type"],
config=json.loads(row["config"]),
external=row["external"],
lazy=row["lazy"],
workdir=row["workdir"],
)
loading_context.add_deployment(persistent_id, obj)
return obj
async def save(self, context: StreamFlowContext) -> None:
async with self.persistence_lock:
if not self.persistent_id:
self.persistent_id = await context.database.add_deployment(
name=self.name,
type=self.type,
config=json.dumps(self.config),
external=self.external,
lazy=self.lazy,
workdir=self.workdir,
)
class Target(PersistableEntity):
def __init__(
self,
deployment: DeploymentConfig,
locations: int = 1,
service: str | None = None,
scheduling_group: str | None = None,
scheduling_policy: Config | None = None,
workdir: str | None = None,
):
super().__init__()
self.deployment: DeploymentConfig = deployment
self.locations: int = locations
self.service: str | None = service
self.scheduling_group: str | None = scheduling_group
self.scheduling_policy: Config | None = scheduling_policy or Config(
name="__DEFAULT__", type="data_locality", config={}
)
self.workdir: str = (
workdir or self.deployment.workdir or _init_workdir(deployment.name)
)
async def _save_additional_params(
self, context: StreamFlowContext
) -> MutableMapping[str, Any]:
return {}
@classmethod
async def _load(
cls,
context: StreamFlowContext,
row: MutableMapping[str, Any],
loading_context: DatabaseLoadingContext,
) -> Target:
return cls(
deployment=await DeploymentConfig.load(
context, row["deployment"], loading_context
),
locations=row["locations"],
service=row["service"],
workdir=row["workdir"],
)
@classmethod
async def load(
cls,
context: StreamFlowContext,
persistent_id: int,
loading_context: DatabaseLoadingContext,
) -> Target:
row = await context.database.get_target(persistent_id)
type = cast(Type[Target], utils.get_class_from_name(row["type"]))
obj = await type._load(context, row, loading_context)
loading_context.add_target(persistent_id, obj)
return obj
async def save(self, context: StreamFlowContext) -> None:
await self.deployment.save(context)
async with self.persistence_lock:
if not self.persistent_id:
self.persistent_id = await context.database.add_target(
deployment=self.deployment.persistent_id,
type=type(self),
params=await self._save_additional_params(context),
locations=self.locations,
service=self.service,
workdir=self.workdir,
)
class LocalTarget(Target):
__deployment_config = None
def __init__(self, workdir: str | None = None):
super().__init__(
deployment=self._get_deployment_config(), locations=1, workdir=workdir
)
@classmethod
def _get_deployment_config(cls):
if not cls.__deployment_config:
cls.__deployment_config = DeploymentConfig(
name=LOCAL_LOCATION, type="local", config={}, external=True, lazy=False
)
return cls.__deployment_config
@classmethod
async def _load(
cls,
context: StreamFlowContext,
row: MutableMapping[str, Any],
loading_context: DatabaseLoadingContext,
) -> LocalTarget:
return cls(workdir=row["workdir"])
class FilterConfig(Config, PersistableEntity):
def __init__(self, name: str, type: str, config: MutableMapping[str, Any]):
Config.__init__(self, name, type, config)
PersistableEntity.__init__(self)
@classmethod
async def load(
cls,
context: StreamFlowContext,
persistent_id: int,
loading_context: DatabaseLoadingContext,
) -> FilterConfig:
row = await context.database.get_filter(persistent_id)
obj = cls(
name=row["name"],
type=row["type"],
config=json.loads(row["config"]),
)
loading_context.add_filter(persistent_id, obj)
return obj
async def save(self, context: StreamFlowContext) -> None:
async with self.persistence_lock:
if not self.persistent_id:
self.persistent_id = await context.database.add_filter(
name=self.name,
type=self.type,
config=json.dumps(self.config),
)