ob-metaflow-extensions 1.1.171rc1__py2.py3-none-any.whl → 1.4.39__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of ob-metaflow-extensions might be problematic. Click here for more details.
- metaflow_extensions/outerbounds/plugins/__init__.py +6 -3
- metaflow_extensions/outerbounds/plugins/apps/app_cli.py +0 -29
- metaflow_extensions/outerbounds/plugins/apps/app_deploy_decorator.py +146 -0
- metaflow_extensions/outerbounds/plugins/apps/core/__init__.py +10 -0
- metaflow_extensions/outerbounds/plugins/apps/core/_state_machine.py +506 -0
- metaflow_extensions/outerbounds/plugins/apps/core/_vendor/__init__.py +0 -0
- metaflow_extensions/outerbounds/plugins/apps/core/_vendor/spinner/__init__.py +4 -0
- metaflow_extensions/outerbounds/plugins/apps/core/_vendor/spinner/spinners.py +478 -0
- metaflow_extensions/outerbounds/plugins/apps/core/app_cli.py +1200 -0
- metaflow_extensions/outerbounds/plugins/apps/core/app_config.py +146 -0
- metaflow_extensions/outerbounds/plugins/apps/core/artifacts.py +0 -0
- metaflow_extensions/outerbounds/plugins/apps/core/capsule.py +958 -0
- metaflow_extensions/outerbounds/plugins/apps/core/click_importer.py +24 -0
- metaflow_extensions/outerbounds/plugins/apps/core/code_package/__init__.py +3 -0
- metaflow_extensions/outerbounds/plugins/apps/core/code_package/code_packager.py +618 -0
- metaflow_extensions/outerbounds/plugins/apps/core/code_package/examples.py +125 -0
- metaflow_extensions/outerbounds/plugins/apps/core/config/__init__.py +12 -0
- metaflow_extensions/outerbounds/plugins/apps/core/config/cli_generator.py +161 -0
- metaflow_extensions/outerbounds/plugins/apps/core/config/config_utils.py +868 -0
- metaflow_extensions/outerbounds/plugins/apps/core/config/schema_export.py +288 -0
- metaflow_extensions/outerbounds/plugins/apps/core/config/typed_configs.py +139 -0
- metaflow_extensions/outerbounds/plugins/apps/core/config/typed_init_generator.py +398 -0
- metaflow_extensions/outerbounds/plugins/apps/core/config/unified_config.py +1088 -0
- metaflow_extensions/outerbounds/plugins/apps/core/config_schema.yaml +337 -0
- metaflow_extensions/outerbounds/plugins/apps/core/dependencies.py +115 -0
- metaflow_extensions/outerbounds/plugins/apps/core/deployer.py +303 -0
- metaflow_extensions/outerbounds/plugins/apps/core/experimental/__init__.py +89 -0
- metaflow_extensions/outerbounds/plugins/apps/core/perimeters.py +87 -0
- metaflow_extensions/outerbounds/plugins/apps/core/secrets.py +164 -0
- metaflow_extensions/outerbounds/plugins/apps/core/utils.py +233 -0
- metaflow_extensions/outerbounds/plugins/apps/core/validations.py +17 -0
- metaflow_extensions/outerbounds/plugins/aws/assume_role_decorator.py +68 -15
- metaflow_extensions/outerbounds/plugins/checkpoint_datastores/coreweave.py +9 -77
- metaflow_extensions/outerbounds/plugins/checkpoint_datastores/external_chckpt.py +85 -0
- metaflow_extensions/outerbounds/plugins/checkpoint_datastores/nebius.py +7 -78
- metaflow_extensions/outerbounds/plugins/fast_bakery/docker_environment.py +6 -2
- metaflow_extensions/outerbounds/plugins/fast_bakery/fast_bakery.py +1 -0
- metaflow_extensions/outerbounds/plugins/nvct/nvct_decorator.py +8 -8
- metaflow_extensions/outerbounds/plugins/optuna/__init__.py +48 -0
- metaflow_extensions/outerbounds/plugins/profilers/simple_card_decorator.py +96 -0
- metaflow_extensions/outerbounds/plugins/s3_proxy/__init__.py +7 -0
- metaflow_extensions/outerbounds/plugins/s3_proxy/binary_caller.py +132 -0
- metaflow_extensions/outerbounds/plugins/s3_proxy/constants.py +11 -0
- metaflow_extensions/outerbounds/plugins/s3_proxy/exceptions.py +13 -0
- metaflow_extensions/outerbounds/plugins/s3_proxy/proxy_bootstrap.py +59 -0
- metaflow_extensions/outerbounds/plugins/s3_proxy/s3_proxy_api.py +93 -0
- metaflow_extensions/outerbounds/plugins/s3_proxy/s3_proxy_decorator.py +250 -0
- metaflow_extensions/outerbounds/plugins/s3_proxy/s3_proxy_manager.py +225 -0
- metaflow_extensions/outerbounds/plugins/snowflake/snowflake.py +37 -7
- metaflow_extensions/outerbounds/plugins/snowpark/snowpark.py +18 -8
- metaflow_extensions/outerbounds/plugins/snowpark/snowpark_cli.py +6 -0
- metaflow_extensions/outerbounds/plugins/snowpark/snowpark_client.py +45 -18
- metaflow_extensions/outerbounds/plugins/snowpark/snowpark_decorator.py +18 -9
- metaflow_extensions/outerbounds/plugins/snowpark/snowpark_job.py +10 -4
- metaflow_extensions/outerbounds/plugins/torchtune/__init__.py +4 -0
- metaflow_extensions/outerbounds/plugins/vllm/__init__.py +173 -95
- metaflow_extensions/outerbounds/plugins/vllm/status_card.py +9 -9
- metaflow_extensions/outerbounds/plugins/vllm/vllm_manager.py +159 -9
- metaflow_extensions/outerbounds/remote_config.py +8 -3
- metaflow_extensions/outerbounds/toplevel/global_aliases_for_metaflow_package.py +62 -1
- metaflow_extensions/outerbounds/toplevel/ob_internal.py +2 -0
- metaflow_extensions/outerbounds/toplevel/plugins/optuna/__init__.py +1 -0
- metaflow_extensions/outerbounds/toplevel/s3_proxy.py +88 -0
- {ob_metaflow_extensions-1.1.171rc1.dist-info → ob_metaflow_extensions-1.4.39.dist-info}/METADATA +2 -2
- {ob_metaflow_extensions-1.1.171rc1.dist-info → ob_metaflow_extensions-1.4.39.dist-info}/RECORD +67 -25
- {ob_metaflow_extensions-1.1.171rc1.dist-info → ob_metaflow_extensions-1.4.39.dist-info}/WHEEL +0 -0
- {ob_metaflow_extensions-1.1.171rc1.dist-info → ob_metaflow_extensions-1.4.39.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
from .config import TypedCoreConfig, TypedDict
|
|
2
|
+
from .perimeters import PerimeterExtractor
|
|
3
|
+
from .capsule import CapsuleApi
|
|
4
|
+
import json
|
|
5
|
+
from ._state_machine import DEPLOYMENT_READY_CONDITIONS, LogLine
|
|
6
|
+
from .app_config import AppConfig, AppConfigError
|
|
7
|
+
from .capsule import CapsuleDeployer, list_and_filter_capsules
|
|
8
|
+
from functools import partial
|
|
9
|
+
import sys
|
|
10
|
+
import uuid
|
|
11
|
+
from typing import Type, Dict, List
|
|
12
|
+
from datetime import datetime
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class AppDeployer(TypedCoreConfig):
|
|
16
|
+
""" """
|
|
17
|
+
|
|
18
|
+
__init__ = TypedCoreConfig.__init__
|
|
19
|
+
|
|
20
|
+
_app_config: AppConfig
|
|
21
|
+
|
|
22
|
+
_state = {}
|
|
23
|
+
|
|
24
|
+
__state_items = [
|
|
25
|
+
"perimeter",
|
|
26
|
+
"api_url",
|
|
27
|
+
"code_package_url",
|
|
28
|
+
"code_package_key",
|
|
29
|
+
"image",
|
|
30
|
+
"project",
|
|
31
|
+
"branch",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def _deploy_config(self) -> AppConfig:
|
|
36
|
+
if not hasattr(self, "_app_config"):
|
|
37
|
+
self._app_config = AppConfig(self._config)
|
|
38
|
+
return self._app_config
|
|
39
|
+
|
|
40
|
+
# Things that need to be set before deploy
|
|
41
|
+
@classmethod
|
|
42
|
+
def _set_state(
|
|
43
|
+
cls,
|
|
44
|
+
perimeter: str,
|
|
45
|
+
api_url: str,
|
|
46
|
+
code_package_url: str = None,
|
|
47
|
+
code_package_key: str = None,
|
|
48
|
+
name_prefix: str = None,
|
|
49
|
+
image: str = None,
|
|
50
|
+
max_entropy: int = 4,
|
|
51
|
+
default_tags: List[Dict[str, str]] = None,
|
|
52
|
+
project: str = None,
|
|
53
|
+
branch: str = None,
|
|
54
|
+
):
|
|
55
|
+
cls._state["perimeter"] = perimeter
|
|
56
|
+
cls._state["api_url"] = api_url
|
|
57
|
+
cls._state["code_package_url"] = code_package_url
|
|
58
|
+
cls._state["code_package_key"] = code_package_key
|
|
59
|
+
cls._state["name_prefix"] = name_prefix
|
|
60
|
+
cls._state["image"] = image
|
|
61
|
+
cls._state["max_entropy"] = max_entropy
|
|
62
|
+
cls._state["default_tags"] = default_tags
|
|
63
|
+
cls._state["project"] = project
|
|
64
|
+
cls._state["branch"] = branch
|
|
65
|
+
|
|
66
|
+
assert (
|
|
67
|
+
max_entropy > 0
|
|
68
|
+
), "max_entropy must be greater than 0. Since AppDeployer's deploy fn can be called many time inside a step itself."
|
|
69
|
+
|
|
70
|
+
def deploy(
|
|
71
|
+
self,
|
|
72
|
+
readiness_condition=DEPLOYMENT_READY_CONDITIONS.ATLEAST_ONE_RUNNING,
|
|
73
|
+
max_wait_time=600,
|
|
74
|
+
readiness_wait_time=10,
|
|
75
|
+
logger_fn=partial(print, file=sys.stderr),
|
|
76
|
+
status_file=None,
|
|
77
|
+
no_loader=False,
|
|
78
|
+
**kwargs,
|
|
79
|
+
) -> "DeployedApp":
|
|
80
|
+
|
|
81
|
+
# Name setting from top level if none is set in the code
|
|
82
|
+
if self._deploy_config._core_config.name is None:
|
|
83
|
+
name = self._state[
|
|
84
|
+
"name_prefix"
|
|
85
|
+
] # for now the name-prefix cannot be very large.
|
|
86
|
+
entropy = uuid.uuid4().hex[: self._state["max_entropy"]]
|
|
87
|
+
self._deploy_config._core_config.name = f"{name}-{entropy}"
|
|
88
|
+
|
|
89
|
+
if len(self._state["default_tags"]) > 0:
|
|
90
|
+
self._deploy_config._core_config.tags = (
|
|
91
|
+
self._deploy_config._core_config.tags or []
|
|
92
|
+
) + self._state["default_tags"]
|
|
93
|
+
|
|
94
|
+
self._deploy_config.commit()
|
|
95
|
+
# Set any state that might have been passed down from the top level
|
|
96
|
+
for k in self.__state_items:
|
|
97
|
+
if self._deploy_config.get_state(k) is None:
|
|
98
|
+
self._deploy_config.set_state(k, self._state[k])
|
|
99
|
+
|
|
100
|
+
capsule = CapsuleDeployer(
|
|
101
|
+
self._deploy_config,
|
|
102
|
+
self._state["api_url"],
|
|
103
|
+
create_timeout=max_wait_time,
|
|
104
|
+
debug_dir=None,
|
|
105
|
+
success_terminal_state_condition=readiness_condition,
|
|
106
|
+
readiness_wait_time=readiness_wait_time,
|
|
107
|
+
logger_fn=logger_fn,
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
currently_present_capsules = list_and_filter_capsules(
|
|
111
|
+
capsule.capsule_api,
|
|
112
|
+
None,
|
|
113
|
+
None,
|
|
114
|
+
capsule.name,
|
|
115
|
+
None,
|
|
116
|
+
None,
|
|
117
|
+
None,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
force_upgrade = self._deploy_config.get_state("force_upgrade", False)
|
|
121
|
+
|
|
122
|
+
if len(currently_present_capsules) > 0:
|
|
123
|
+
# Only update the capsule if there is no upgrade in progress
|
|
124
|
+
# Only update a "already updating" capsule if the `--force-upgrade` flag is provided.
|
|
125
|
+
_curr_cap = currently_present_capsules[0]
|
|
126
|
+
this_capsule_is_being_updated = _curr_cap.get("status", {}).get(
|
|
127
|
+
"updateInProgress", False
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
if this_capsule_is_being_updated and not force_upgrade:
|
|
131
|
+
_upgrader = _curr_cap.get("metadata", {}).get("lastModifiedBy", None)
|
|
132
|
+
message = f"{capsule.capsule_type} is currently being upgraded"
|
|
133
|
+
if _upgrader:
|
|
134
|
+
message = (
|
|
135
|
+
f"{capsule.capsule_type} is currently being upgraded. Upgrade was launched by {_upgrader}. "
|
|
136
|
+
"If you wish to force upgrade, you can do so by providing the `--force-upgrade` flag."
|
|
137
|
+
)
|
|
138
|
+
raise AppConfigError(message)
|
|
139
|
+
|
|
140
|
+
logger_fn(
|
|
141
|
+
f"🚀 {'' if not force_upgrade else 'Force'} Upgrading {capsule.capsule_type.lower()} `{capsule.name}`....",
|
|
142
|
+
)
|
|
143
|
+
else:
|
|
144
|
+
logger_fn(
|
|
145
|
+
f"🚀 Deploying {capsule.capsule_type.lower()} `{capsule.name}`....",
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
capsule.create()
|
|
149
|
+
final_status = capsule.wait_for_terminal_state()
|
|
150
|
+
return DeployedApp(
|
|
151
|
+
final_status["id"],
|
|
152
|
+
final_status["auth_type"],
|
|
153
|
+
final_status["public_url"],
|
|
154
|
+
final_status["name"],
|
|
155
|
+
final_status["deployed_version"],
|
|
156
|
+
final_status["deployed_at"],
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
class DeployedApp:
|
|
161
|
+
def __init__(
|
|
162
|
+
self,
|
|
163
|
+
_id: str,
|
|
164
|
+
capsule_type: str,
|
|
165
|
+
public_url: str,
|
|
166
|
+
name: str,
|
|
167
|
+
deployed_version: str,
|
|
168
|
+
deployed_at: str,
|
|
169
|
+
):
|
|
170
|
+
self._id = _id
|
|
171
|
+
self._capsule_type = capsule_type
|
|
172
|
+
self._public_url = public_url
|
|
173
|
+
self._name = name
|
|
174
|
+
self._deployed_version = deployed_version
|
|
175
|
+
self._deployed_at = deployed_at
|
|
176
|
+
|
|
177
|
+
def _get_capsule_api(self) -> CapsuleApi:
|
|
178
|
+
perimeter, api_server = PerimeterExtractor.during_metaflow_execution()
|
|
179
|
+
return CapsuleApi(api_server, perimeter)
|
|
180
|
+
|
|
181
|
+
def logs(self, previous=False) -> Dict[str, List[LogLine]]:
|
|
182
|
+
"""
|
|
183
|
+
Returns a dictionary of worker_id to logs.
|
|
184
|
+
If `previous` is True, it will return the logs from the previous execution of the workers. Useful when debugging a crashlooping worker.
|
|
185
|
+
"""
|
|
186
|
+
capsule_api = self._get_capsule_api()
|
|
187
|
+
# extract workers from capsule
|
|
188
|
+
workers = capsule_api.get_workers(self._id)
|
|
189
|
+
# get logs from workers
|
|
190
|
+
logs = {
|
|
191
|
+
# worker_id: logs
|
|
192
|
+
}
|
|
193
|
+
for worker in workers:
|
|
194
|
+
# TODO: Handle exceptions better over here.
|
|
195
|
+
logs[worker["workerId"]] = capsule_api.logs(
|
|
196
|
+
self._id, worker["workerId"], previous=previous
|
|
197
|
+
)
|
|
198
|
+
return logs
|
|
199
|
+
|
|
200
|
+
def info(self) -> dict:
|
|
201
|
+
"""
|
|
202
|
+
Returns a dictionary representing the deployed app.
|
|
203
|
+
"""
|
|
204
|
+
capsule_api = self._get_capsule_api()
|
|
205
|
+
capsule = capsule_api.get(self._id)
|
|
206
|
+
return capsule
|
|
207
|
+
|
|
208
|
+
def replicas(self):
|
|
209
|
+
capsule_api = self._get_capsule_api()
|
|
210
|
+
return capsule_api.get_workers(self._id)
|
|
211
|
+
|
|
212
|
+
def scale_to_zero(self):
|
|
213
|
+
"""
|
|
214
|
+
Scales the DeployedApp to 0 replicas.
|
|
215
|
+
"""
|
|
216
|
+
capsule_api = self._get_capsule_api()
|
|
217
|
+
return capsule_api.patch(
|
|
218
|
+
self._id,
|
|
219
|
+
{
|
|
220
|
+
"autoscalingConfig": {
|
|
221
|
+
"minReplicas": 0,
|
|
222
|
+
"maxReplicas": 0,
|
|
223
|
+
}
|
|
224
|
+
},
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
def delete(self):
|
|
228
|
+
"""
|
|
229
|
+
Deletes the DeployedApp.
|
|
230
|
+
"""
|
|
231
|
+
capsule_api = self._get_capsule_api()
|
|
232
|
+
return capsule_api.delete(self._id)
|
|
233
|
+
|
|
234
|
+
@property
|
|
235
|
+
def id(self) -> str:
|
|
236
|
+
return self._id
|
|
237
|
+
|
|
238
|
+
@property
|
|
239
|
+
def auth_style(self) -> str:
|
|
240
|
+
# TODO : Fix naming here.
|
|
241
|
+
return self._capsule_type
|
|
242
|
+
|
|
243
|
+
@property
|
|
244
|
+
def public_url(self) -> str:
|
|
245
|
+
return self._public_url
|
|
246
|
+
|
|
247
|
+
@property
|
|
248
|
+
def name(self) -> str:
|
|
249
|
+
return self._name
|
|
250
|
+
|
|
251
|
+
@property
|
|
252
|
+
def deployed_version(self) -> str:
|
|
253
|
+
return self._deployed_version
|
|
254
|
+
|
|
255
|
+
def to_dict(self) -> dict:
|
|
256
|
+
return {
|
|
257
|
+
"id": self._id,
|
|
258
|
+
"auth_style": self.auth_style, # TODO : Fix naming here.
|
|
259
|
+
"public_url": self._public_url,
|
|
260
|
+
"name": self._name,
|
|
261
|
+
"deployed_version": self._deployed_version,
|
|
262
|
+
"deployed_at": self._deployed_at,
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
@classmethod
|
|
266
|
+
def from_dict(cls, data: dict):
|
|
267
|
+
return cls(
|
|
268
|
+
_id=data["id"],
|
|
269
|
+
capsule_type=data["capsule_type"],
|
|
270
|
+
public_url=data["public_url"],
|
|
271
|
+
name=data["name"],
|
|
272
|
+
deployed_version=data["deployed_version"],
|
|
273
|
+
deployed_at=data["deployed_at"],
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
@property
|
|
277
|
+
def deployed_at(self) -> datetime:
|
|
278
|
+
return datetime.fromisoformat(self._deployed_at)
|
|
279
|
+
|
|
280
|
+
def __repr__(self) -> str:
|
|
281
|
+
return (
|
|
282
|
+
f"DeployedApp(id='{self._id}', "
|
|
283
|
+
f"name='{self._name}', "
|
|
284
|
+
f"public_url='{self._public_url}', "
|
|
285
|
+
f"deployed_version='{self._deployed_version}')"
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
class apps:
|
|
290
|
+
|
|
291
|
+
_name_prefix = None
|
|
292
|
+
|
|
293
|
+
@classmethod
|
|
294
|
+
def set_name_prefix(cls, name_prefix: str):
|
|
295
|
+
cls._name_prefix = name_prefix
|
|
296
|
+
|
|
297
|
+
@property
|
|
298
|
+
def name_prefix(self) -> str:
|
|
299
|
+
return self._name_prefix
|
|
300
|
+
|
|
301
|
+
@property
|
|
302
|
+
def Deployer(self) -> Type[AppDeployer]:
|
|
303
|
+
return AppDeployer
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from typing import TYPE_CHECKING
|
|
3
|
+
|
|
4
|
+
if TYPE_CHECKING:
|
|
5
|
+
from ..app_config import AppConfig
|
|
6
|
+
|
|
7
|
+
DEFAULT_BRANCH = "test"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# Account for project / branch and the capsule input.
|
|
11
|
+
def capsule_input_overrides(app_config: "AppConfig", capsule_input: dict):
|
|
12
|
+
project = app_config.get_state("project", None)
|
|
13
|
+
# Update the project/branch related configurations.
|
|
14
|
+
if project is not None:
|
|
15
|
+
branch = app_config.get_state("branch", DEFAULT_BRANCH)
|
|
16
|
+
capsule_input["tags"].extend(
|
|
17
|
+
[dict(key="project", value=project), dict(key="branch", value=branch)]
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
persistence = app_config.get_state("persistence", None)
|
|
21
|
+
if persistence is not None and persistence != "none":
|
|
22
|
+
capsule_input["persistence"] = persistence
|
|
23
|
+
|
|
24
|
+
capsule_input["generateStaticUrl"] = app_config.get_state(
|
|
25
|
+
"generate_static_url", False
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
model_asset_conf = app_config.get_state("models", None)
|
|
29
|
+
data_asset_conf = app_config.get_state("data", None)
|
|
30
|
+
code_info = _code_info(app_config)
|
|
31
|
+
# todo:fix me
|
|
32
|
+
_objects_key = "associatedObjects"
|
|
33
|
+
if model_asset_conf or data_asset_conf or code_info:
|
|
34
|
+
capsule_input[_objects_key] = {}
|
|
35
|
+
|
|
36
|
+
if model_asset_conf:
|
|
37
|
+
capsule_input[_objects_key]["models"] = [
|
|
38
|
+
{"assetId": x["asset_id"], "assetInstanceId": x["asset_instance_id"]}
|
|
39
|
+
for x in model_asset_conf
|
|
40
|
+
]
|
|
41
|
+
if data_asset_conf:
|
|
42
|
+
capsule_input[_objects_key]["data"] = [
|
|
43
|
+
{"assetId": x["asset_id"], "assetInstanceId": x["asset_instance_id"]}
|
|
44
|
+
for x in data_asset_conf
|
|
45
|
+
]
|
|
46
|
+
if code_info:
|
|
47
|
+
capsule_input[_objects_key]["code"] = code_info
|
|
48
|
+
|
|
49
|
+
return capsule_input
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _code_info(app_config: "AppConfig"):
|
|
53
|
+
from metaflow.metaflow_git import get_repository_info
|
|
54
|
+
|
|
55
|
+
try:
|
|
56
|
+
from metaflow.metaflow_git import _call_git # type: ignore
|
|
57
|
+
except ImportError:
|
|
58
|
+
# Fallback if _call_git is not available
|
|
59
|
+
def _call_git(args, path=None):
|
|
60
|
+
return "", 1, True
|
|
61
|
+
|
|
62
|
+
package_dirs = app_config.get_state("packaging_directories")
|
|
63
|
+
if package_dirs is None:
|
|
64
|
+
return None
|
|
65
|
+
for directory in package_dirs:
|
|
66
|
+
repo_info = get_repository_info(directory)
|
|
67
|
+
if len(repo_info) == 0:
|
|
68
|
+
continue
|
|
69
|
+
git_log_info, returncode, failed = _call_git(
|
|
70
|
+
["log", "-1", "--pretty=%B"],
|
|
71
|
+
path=directory,
|
|
72
|
+
)
|
|
73
|
+
repo_url = repo_info["repo_url"]
|
|
74
|
+
if isinstance(repo_url, str):
|
|
75
|
+
_url = (
|
|
76
|
+
repo_url if not repo_url.endswith(".git") else repo_url.rstrip(".git")
|
|
77
|
+
)
|
|
78
|
+
else:
|
|
79
|
+
_url = str(repo_url)
|
|
80
|
+
_code_info = {
|
|
81
|
+
"commitId": repo_info["commit_sha"],
|
|
82
|
+
"commitLink": os.path.join(_url, "commit", str(repo_info["commit_sha"])),
|
|
83
|
+
}
|
|
84
|
+
if not failed and returncode == 0 and isinstance(git_log_info, str):
|
|
85
|
+
_code_info["commitMessage"] = git_log_info.strip()
|
|
86
|
+
|
|
87
|
+
return _code_info
|
|
88
|
+
|
|
89
|
+
return None
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import json
|
|
3
|
+
from typing import Tuple, Union
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class PerimeterExtractor:
|
|
7
|
+
@classmethod
|
|
8
|
+
def for_ob_cli(
|
|
9
|
+
cls, config_dir: str, profile: str
|
|
10
|
+
) -> Union[Tuple[str, str], Tuple[None, None]]:
|
|
11
|
+
"""
|
|
12
|
+
This function will be called when we are trying to extract the perimeter
|
|
13
|
+
via the ob cli's execution. We will rely on the following logic:
|
|
14
|
+
1. check environment variables like OB_CURRENT_PERIMETER / OBP_PERIMETER
|
|
15
|
+
2. run init config to extract the perimeter related configurations.
|
|
16
|
+
|
|
17
|
+
Returns
|
|
18
|
+
-------
|
|
19
|
+
Tuple[str, str] : Tuple containing perimeter name , API server url.
|
|
20
|
+
"""
|
|
21
|
+
from outerbounds.utils import metaflowconfig
|
|
22
|
+
|
|
23
|
+
perimeter = None
|
|
24
|
+
api_server = None
|
|
25
|
+
if os.environ.get("OB_CURRENT_PERIMETER") or os.environ.get("OBP_PERIMETER"):
|
|
26
|
+
perimeter = os.environ.get("OB_CURRENT_PERIMETER") or os.environ.get(
|
|
27
|
+
"OBP_PERIMETER"
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
if os.environ.get("OBP_API_SERVER"):
|
|
31
|
+
api_server = os.environ.get("OBP_API_SERVER")
|
|
32
|
+
|
|
33
|
+
if perimeter is None or api_server is None:
|
|
34
|
+
metaflow_config = metaflowconfig.init_config(config_dir, profile)
|
|
35
|
+
perimeter = metaflow_config.get("OBP_PERIMETER")
|
|
36
|
+
api_server = metaflowconfig.get_sanitized_url_from_config(
|
|
37
|
+
config_dir, profile, "OBP_API_SERVER"
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
return perimeter, api_server # type: ignore
|
|
41
|
+
|
|
42
|
+
@classmethod
|
|
43
|
+
def during_metaflow_execution(cls) -> Union[Tuple[str, str], Tuple[None, None]]:
|
|
44
|
+
from metaflow.metaflow_config_funcs import init_config
|
|
45
|
+
|
|
46
|
+
clean_url = (
|
|
47
|
+
lambda url: f"https://{url}".rstrip("/")
|
|
48
|
+
if not url.startswith("https://")
|
|
49
|
+
else url
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
config = init_config()
|
|
53
|
+
api_server, perimeter, integrations_url = None, None, None
|
|
54
|
+
perimeter = config.get(
|
|
55
|
+
"OBP_PERIMETER", os.environ.get("OBP_PERIMETER", perimeter)
|
|
56
|
+
)
|
|
57
|
+
if perimeter is None:
|
|
58
|
+
raise RuntimeError(
|
|
59
|
+
"Perimeter not found in metaflow config or environment variables"
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
api_server = config.get(
|
|
63
|
+
"OBP_API_SERVER", os.environ.get("OBP_API_SERVER", api_server)
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
if api_server is not None and not api_server.startswith("https://"):
|
|
67
|
+
api_server = clean_url(api_server)
|
|
68
|
+
|
|
69
|
+
if api_server is not None:
|
|
70
|
+
return perimeter, api_server
|
|
71
|
+
|
|
72
|
+
integrations_url = config.get(
|
|
73
|
+
"OBP_INTEGRATIONS_URL", os.environ.get("OBP_INTEGRATIONS_URL", None)
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
if integrations_url is not None and not integrations_url.startswith("https://"):
|
|
77
|
+
integrations_url = clean_url(integrations_url)
|
|
78
|
+
|
|
79
|
+
if integrations_url is not None:
|
|
80
|
+
api_server = integrations_url.rstrip("/integrations")
|
|
81
|
+
|
|
82
|
+
if api_server is None:
|
|
83
|
+
raise RuntimeError(
|
|
84
|
+
"API server not found in metaflow config or environment variables"
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
return perimeter, api_server
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
from typing import Dict
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
import json
|
|
5
|
+
import requests
|
|
6
|
+
import random
|
|
7
|
+
import time
|
|
8
|
+
import sys
|
|
9
|
+
|
|
10
|
+
from .utils import safe_requests_wrapper, TODOException
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class OuterboundsSecretsException(Exception):
|
|
14
|
+
pass
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class SecretNotFound(OuterboundsSecretsException):
|
|
18
|
+
pass
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class OuterboundsSecretsApiResponse:
|
|
22
|
+
def __init__(self, response):
|
|
23
|
+
self.response = response
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def secret_resource_id(self):
|
|
27
|
+
return self.response["secret_resource_id"]
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def secret_backend_type(self):
|
|
31
|
+
return self.response["secret_backend_type"]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class SecretRetriever:
|
|
35
|
+
def get_secret_as_dict(self, secret_id, options={}, role=None):
|
|
36
|
+
"""
|
|
37
|
+
Supports a special way of specifying secrets sources in outerbounds using the format:
|
|
38
|
+
@secrets(sources=["outerbounds.<integrations_name>"])
|
|
39
|
+
|
|
40
|
+
When invoked it makes a requests to the integrations secrets metadata endpoint on the
|
|
41
|
+
keywest server to get the cloud resource id for a secret. It then uses that to invoke
|
|
42
|
+
secrets manager on the core oss and returns the secrets.
|
|
43
|
+
"""
|
|
44
|
+
headers = {"Content-Type": "application/json", "Connection": "keep-alive"}
|
|
45
|
+
perimeter, integrations_url = self._get_secret_configs()
|
|
46
|
+
integration_name = secret_id
|
|
47
|
+
request_payload = {
|
|
48
|
+
"perimeter_name": perimeter,
|
|
49
|
+
"integration_name": integration_name,
|
|
50
|
+
}
|
|
51
|
+
response = self._make_request(integrations_url, headers, request_payload)
|
|
52
|
+
secret_resource_id = response.secret_resource_id
|
|
53
|
+
secret_backend_type = response.secret_backend_type
|
|
54
|
+
|
|
55
|
+
from metaflow.plugins.secrets.secrets_decorator import (
|
|
56
|
+
get_secrets_backend_provider,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
secrets_provider = get_secrets_backend_provider(secret_backend_type)
|
|
60
|
+
secret_dict = secrets_provider.get_secret_as_dict(
|
|
61
|
+
secret_resource_id, options={}, role=role
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
# Outerbounds stores secrets as binaries. Hence we expect the returned secret to be
|
|
65
|
+
# {<cloud-secret-name>: <base64 encoded full secret>}. We decode the secret here like:
|
|
66
|
+
# 1. decode the base64 encoded full secret
|
|
67
|
+
# 2. load the decoded secret as a json
|
|
68
|
+
# 3. decode the base64 encoded values in the dict
|
|
69
|
+
# 4. return the decoded dict
|
|
70
|
+
binary_secret = next(iter(secret_dict.values()))
|
|
71
|
+
return self._decode_secret(binary_secret)
|
|
72
|
+
|
|
73
|
+
def _is_base64_encoded(self, data):
|
|
74
|
+
try:
|
|
75
|
+
if isinstance(data, str):
|
|
76
|
+
# Check if the string can be base64 decoded
|
|
77
|
+
base64.b64decode(data).decode("utf-8")
|
|
78
|
+
return True
|
|
79
|
+
return False
|
|
80
|
+
except Exception:
|
|
81
|
+
return False
|
|
82
|
+
|
|
83
|
+
def _decode_secret(self, secret):
|
|
84
|
+
try:
|
|
85
|
+
result = {}
|
|
86
|
+
secret_str = secret
|
|
87
|
+
if self._is_base64_encoded(secret):
|
|
88
|
+
# we check if the secret string is base64 encoded because the returned secret from
|
|
89
|
+
# AWS secret manager is base64 encoded while the secret from GCP is not
|
|
90
|
+
secret_str = base64.b64decode(secret).decode("utf-8")
|
|
91
|
+
|
|
92
|
+
secret_dict = json.loads(secret_str)
|
|
93
|
+
for key, value in secret_dict.items():
|
|
94
|
+
result[key] = base64.b64decode(value).decode("utf-8")
|
|
95
|
+
|
|
96
|
+
return result
|
|
97
|
+
except Exception as e:
|
|
98
|
+
raise OuterboundsSecretsException(f"Error decoding secret: {e}")
|
|
99
|
+
|
|
100
|
+
def _get_secret_configs(self):
|
|
101
|
+
from metaflow_extensions.outerbounds.remote_config import init_config # type: ignore
|
|
102
|
+
from os import environ
|
|
103
|
+
|
|
104
|
+
conf = init_config()
|
|
105
|
+
if "OBP_PERIMETER" in conf:
|
|
106
|
+
perimeter = conf["OBP_PERIMETER"]
|
|
107
|
+
else:
|
|
108
|
+
# if the perimeter is not in metaflow config, try to get it from the environment
|
|
109
|
+
perimeter = environ.get("OBP_PERIMETER", "")
|
|
110
|
+
|
|
111
|
+
if "OBP_INTEGRATIONS_URL" in conf:
|
|
112
|
+
integrations_url = conf["OBP_INTEGRATIONS_URL"]
|
|
113
|
+
else:
|
|
114
|
+
# if the integrations is not in metaflow config, try to get it from the environment
|
|
115
|
+
integrations_url = environ.get("OBP_INTEGRATIONS_URL", "")
|
|
116
|
+
|
|
117
|
+
if not perimeter:
|
|
118
|
+
raise OuterboundsSecretsException(
|
|
119
|
+
"No perimeter set. Please make sure to run `outerbounds configure <...>` command which can be found on the Outerbounds UI or reach out to your Outerbounds support team."
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
if not integrations_url:
|
|
123
|
+
raise OuterboundsSecretsException(
|
|
124
|
+
"No integrations url set. Please notify your Outerbounds support team about this issue."
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
integrations_secrets_metadata_url = f"{integrations_url}/secrets/metadata"
|
|
128
|
+
return perimeter, integrations_secrets_metadata_url
|
|
129
|
+
|
|
130
|
+
def _make_request(self, url, headers: Dict, payload: Dict):
|
|
131
|
+
try:
|
|
132
|
+
from metaflow.metaflow_config import SERVICE_HEADERS
|
|
133
|
+
|
|
134
|
+
request_headers = {**headers, **(SERVICE_HEADERS or {})}
|
|
135
|
+
except ImportError:
|
|
136
|
+
raise OuterboundsSecretsException(
|
|
137
|
+
"Failed to create app: No Metaflow service headers found"
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
response = safe_requests_wrapper(
|
|
141
|
+
requests.get,
|
|
142
|
+
url,
|
|
143
|
+
data=json.dumps(payload),
|
|
144
|
+
headers=request_headers,
|
|
145
|
+
conn_error_retries=5,
|
|
146
|
+
retryable_status_codes=[409],
|
|
147
|
+
)
|
|
148
|
+
self._handle_error_response(response)
|
|
149
|
+
return OuterboundsSecretsApiResponse(response.json())
|
|
150
|
+
|
|
151
|
+
@staticmethod
|
|
152
|
+
def _handle_error_response(response: requests.Response):
|
|
153
|
+
if response.status_code >= 500:
|
|
154
|
+
raise OuterboundsSecretsException(
|
|
155
|
+
f"Server error: {response.text}. Please reach out to your Outerbounds support team."
|
|
156
|
+
)
|
|
157
|
+
status_code = response.status_code
|
|
158
|
+
if status_code == 404:
|
|
159
|
+
raise SecretNotFound(f"Secret not found: {response.text}")
|
|
160
|
+
|
|
161
|
+
if status_code >= 400:
|
|
162
|
+
raise OuterboundsSecretsException(
|
|
163
|
+
f"status_code={status_code}\t\n\t\t{response.text}"
|
|
164
|
+
)
|