outerbounds 0.3.176rc6__py3-none-any.whl → 0.3.178__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.
@@ -1,164 +0,0 @@
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
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 Ourebounds 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 TODOException(
137
- "Failed to create capsule: 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
- )
outerbounds/apps/utils.py DELETED
@@ -1,254 +0,0 @@
1
- import random
2
- import time
3
- import sys
4
- import json
5
- import requests
6
- from outerbounds._vendor import click
7
- from .app_config import CAPSULE_DEBUG
8
-
9
-
10
- class MaximumRetriesExceeded(Exception):
11
- def __init__(self, url, method, status_code, text):
12
- self.url = url
13
- self.method = method
14
- self.status_code = status_code
15
- self.text = text
16
-
17
- def __str__(self):
18
- return f"Maximum retries exceeded for {self.url}[{self.method}] {self.status_code} {self.text}"
19
-
20
-
21
- class KeyValueDictPair(click.ParamType):
22
- name = "KV-DICT-PAIR"
23
-
24
- def convert(self, value, param, ctx):
25
- # Parse a string of the form KEY=VALUE into a dict {KEY: VALUE}
26
- if len(value.split("=", 1)) != 2:
27
- self.fail(
28
- f"Invalid format for {value}. Expected format: KEY=VALUE", param, ctx
29
- )
30
-
31
- key, _value = value.split("=", 1)
32
- try:
33
- return {"key": key, "value": json.loads(_value)}
34
- except json.JSONDecodeError:
35
- return {"key": key, "value": _value}
36
- except Exception as e:
37
- self.fail(f"Invalid value for {value}. Error: {e}", param, ctx)
38
-
39
- def __str__(self):
40
- return repr(self)
41
-
42
- def __repr__(self):
43
- return "KV-PAIR"
44
-
45
-
46
- class KeyValuePair(click.ParamType):
47
- name = "KV-PAIR"
48
-
49
- def convert(self, value, param, ctx):
50
- # Parse a string of the form KEY=VALUE into a dict {KEY: VALUE}
51
- if len(value.split("=", 1)) != 2:
52
- self.fail(
53
- f"Invalid format for {value}. Expected format: KEY=VALUE", param, ctx
54
- )
55
-
56
- key, _value = value.split("=", 1)
57
- try:
58
- return {key: json.loads(_value)}
59
- except json.JSONDecodeError:
60
- return {key: _value}
61
- except Exception as e:
62
- self.fail(f"Invalid value for {value}. Error: {e}", param, ctx)
63
-
64
- def __str__(self):
65
- return repr(self)
66
-
67
- def __repr__(self):
68
- return "KV-PAIR"
69
-
70
-
71
- class MountMetaflowArtifact(click.ParamType):
72
- name = "MOUNT-METAFLOW-ARTIFACT"
73
-
74
- def convert(self, value, param, ctx):
75
- """
76
- Convert a string like "flow=MyFlow,artifact=my_model,path=/tmp/abc" or
77
- "pathspec=MyFlow/123/foo/345/my_model,path=/tmp/abc" to a dict.
78
- """
79
- artifact_dict = {}
80
- parts = value.split(",")
81
-
82
- for part in parts:
83
- if "=" not in part:
84
- self.fail(
85
- f"Invalid format in part '{part}'. Expected 'key=value'", param, ctx
86
- )
87
-
88
- key, val = part.split("=", 1)
89
- artifact_dict[key.strip()] = val.strip()
90
-
91
- # Validate required fields
92
- if "pathspec" in artifact_dict:
93
- if "path" not in artifact_dict:
94
- self.fail(
95
- "When using 'pathspec', you must also specify 'path'", param, ctx
96
- )
97
-
98
- # Return as pathspec format
99
- return {
100
- "pathspec": artifact_dict["pathspec"],
101
- "path": artifact_dict["path"],
102
- }
103
- elif (
104
- "flow" in artifact_dict
105
- and "artifact" in artifact_dict
106
- and "path" in artifact_dict
107
- ):
108
- # Return as flow/artifact format
109
- result = {
110
- "flow": artifact_dict["flow"],
111
- "artifact": artifact_dict["artifact"],
112
- "path": artifact_dict["path"],
113
- }
114
-
115
- # Add optional namespace if provided
116
- if "namespace" in artifact_dict:
117
- result["namespace"] = artifact_dict["namespace"]
118
-
119
- return result
120
- else:
121
- self.fail(
122
- "Invalid format. Must be either 'flow=X,artifact=Y,path=Z' or 'pathspec=X,path=Z'",
123
- param,
124
- ctx,
125
- )
126
-
127
- def __str__(self):
128
- return repr(self)
129
-
130
- def __repr__(self):
131
- return "MOUNT-METAFLOW-ARTIFACT"
132
-
133
-
134
- class MountSecret(click.ParamType):
135
- name = "MOUNT-SECRET"
136
-
137
- def convert(self, value, param, ctx):
138
- """
139
- Convert a string like "id=my_secret,path=/tmp/secret" to a dict.
140
- """
141
- secret_dict = {}
142
- parts = value.split(",")
143
-
144
- for part in parts:
145
- if "=" not in part:
146
- self.fail(
147
- f"Invalid format in part '{part}'. Expected 'key=value'", param, ctx
148
- )
149
-
150
- key, val = part.split("=", 1)
151
- secret_dict[key.strip()] = val.strip()
152
-
153
- # Validate required fields
154
- if "id" in secret_dict and "path" in secret_dict:
155
- return {"id": secret_dict["id"], "path": secret_dict["path"]}
156
- else:
157
- self.fail("Invalid format. Must be 'key=X,path=Y'", param, ctx)
158
-
159
- def __str__(self):
160
- return repr(self)
161
-
162
- def __repr__(self):
163
- return "MOUNT-SECRET"
164
-
165
-
166
- class CommaSeparatedList(click.ParamType):
167
- name = "COMMA-SEPARATED-LIST"
168
-
169
- def convert(self, value, param, ctx):
170
- return value.split(",")
171
-
172
- def __str__(self):
173
- return repr(self)
174
-
175
- def __repr__(self):
176
- return "COMMA-SEPARATED-LIST"
177
-
178
-
179
- KVPairType = KeyValuePair()
180
- MetaflowArtifactType = MountMetaflowArtifact()
181
- SecretMountType = MountSecret()
182
- CommaSeparatedListType = CommaSeparatedList()
183
- KVDictType = KeyValueDictPair()
184
-
185
-
186
- class TODOException(Exception):
187
- pass
188
-
189
-
190
- requests_funcs = [
191
- requests.get,
192
- requests.post,
193
- requests.put,
194
- requests.delete,
195
- requests.patch,
196
- requests.head,
197
- requests.options,
198
- ]
199
-
200
-
201
- def safe_requests_wrapper(
202
- requests_module_fn,
203
- *args,
204
- conn_error_retries=2,
205
- retryable_status_codes=[409],
206
- **kwargs,
207
- ):
208
- """
209
- There are two categories of errors that we need to handle when dealing with any API server.
210
- 1. HTTP errors. These are are errors that are returned from the API server.
211
- - How to handle retries for this case will be application specific.
212
- 2. Errors when the API server may not be reachable (DNS resolution / network issues)
213
- - In this scenario, we know that something external to the API server is going wrong causing the issue.
214
- - Failing pre-maturely in the case might not be the best course of action since critical user jobs might crash on intermittent issues.
215
- - So in this case, we can just planely retry the request.
216
-
217
- This function handles the second case. It's a simple wrapper to handle the retry logic for connection errors.
218
- If this function is provided a `conn_error_retries` of 5, then the last retry will have waited 32 seconds.
219
- Generally this is a safe enough number of retries after which we can assume that something is really broken. Until then,
220
- there can be intermittent issues that would resolve themselves if we retry gracefully.
221
- """
222
- if requests_module_fn not in requests_funcs:
223
- raise TODOException(
224
- f"safe_requests_wrapper doesn't support {requests_module_fn.__name__}. You can only use the following functions: {requests_funcs}"
225
- )
226
-
227
- _num_retries = 0
228
- noise = random.uniform(-0.5, 0.5)
229
- response = None
230
- while _num_retries < conn_error_retries:
231
- try:
232
- response = requests_module_fn(*args, **kwargs)
233
- if response.status_code not in retryable_status_codes:
234
- return response
235
- if CAPSULE_DEBUG:
236
- print(
237
- f"[outerbounds-debug] safe_requests_wrapper: {response.url}[{requests_module_fn.__name__}] {response.status_code} {response.text}",
238
- file=sys.stderr,
239
- )
240
- _num_retries += 1
241
- time.sleep((2 ** (_num_retries + 1)) + noise)
242
- except requests.exceptions.ConnectionError:
243
- if _num_retries <= conn_error_retries - 1:
244
- # Exponential backoff with 2^(_num_retries+1) seconds
245
- time.sleep((2 ** (_num_retries + 1)) + noise)
246
- _num_retries += 1
247
- else:
248
- raise
249
- raise MaximumRetriesExceeded(
250
- response.url,
251
- requests_module_fn.__name__,
252
- response.status_code,
253
- response.text,
254
- )
@@ -1,34 +0,0 @@
1
- import os
2
- from .app_config import AppConfig, AppConfigError
3
- from .secrets import SecretRetriever, SecretNotFound
4
- from .dependencies import bake_deployment_image
5
-
6
-
7
- def deploy_validations(app_config: AppConfig, cache_dir: str, logger):
8
-
9
- # First check if the secrets for the app exist.
10
- app_secrets = app_config.get("secrets", [])
11
- secret_retriever = SecretRetriever()
12
- for secret in app_secrets:
13
- try:
14
- secret_retriever.get_secret_as_dict(secret)
15
- except SecretNotFound:
16
- raise AppConfigError(f"Secret not found: {secret}")
17
-
18
- # TODO: Next check if the compute pool exists.
19
- logger("🍞 Baking Docker Image")
20
- baking_status = bake_deployment_image(
21
- app_config=app_config,
22
- cache_file_path=os.path.join(cache_dir, "image_cache"),
23
- logger=logger,
24
- )
25
- app_config.set_state(
26
- "image",
27
- baking_status.resolved_image,
28
- )
29
- app_config.set_state("python_path", baking_status.python_path)
30
- logger("🐳 Using The Docker Image : %s" % app_config.get_state("image"))
31
-
32
-
33
- def run_validations(app_config: AppConfig):
34
- pass
@@ -1,137 +0,0 @@
1
- import json
2
- import os
3
- import sys
4
- import requests
5
-
6
- from ..utils import metaflowconfig
7
- from outerbounds._vendor import click
8
-
9
-
10
- @click.group()
11
- def cli(**kwargs):
12
- pass
13
-
14
-
15
- @cli.group(help="Commands for pushing Deployments metadata.", hidden=True)
16
- def flowproject(**kwargs):
17
- pass
18
-
19
-
20
- @flowproject.command()
21
- @click.option(
22
- "-d",
23
- "--config-dir",
24
- default=os.path.expanduser(os.environ.get("METAFLOW_HOME", "~/.metaflowconfig")),
25
- help="Path to Metaflow configuration directory",
26
- show_default=True,
27
- )
28
- @click.option(
29
- "-p",
30
- "--profile",
31
- default=os.environ.get("METAFLOW_PROFILE", ""),
32
- help="The named metaflow profile in which your workstation exists",
33
- )
34
- @click.option("--id", help="The ID for this deployment")
35
- def get_metadata(config_dir, profile, id):
36
- api_url = metaflowconfig.get_sanitized_url_from_config(
37
- config_dir, profile, "OBP_API_SERVER"
38
- )
39
- perimeter = _get_perimeter()
40
- headers = _get_request_headers()
41
-
42
- project, branch = _parse_id(id)
43
-
44
- # GET the latest flowproject config in order to modify it
45
- # /v1/perimeters/:perimeter/:project/:branch/flowprojects/latest
46
- response = requests.get(
47
- url=f"{api_url}/v1/perimeters/{perimeter}/projects/{project}/branches/{branch}/latestflowproject",
48
- headers=headers,
49
- )
50
- if response.status_code >= 500:
51
- raise Exception("API request failed.")
52
-
53
- body = response.json()
54
- if response.status_code >= 400:
55
- raise Exception("request failed: %s" % body)
56
-
57
- out = json.dumps(body)
58
-
59
- print(out, file=sys.stdout)
60
-
61
-
62
- @flowproject.command()
63
- @click.option(
64
- "-d",
65
- "--config-dir",
66
- default=os.path.expanduser(os.environ.get("METAFLOW_HOME", "~/.metaflowconfig")),
67
- help="Path to Metaflow configuration directory",
68
- show_default=True,
69
- )
70
- @click.option(
71
- "-p",
72
- "--profile",
73
- default=os.environ.get("METAFLOW_PROFILE", ""),
74
- help="The named metaflow profile in which your workstation exists",
75
- )
76
- @click.argument("json_str")
77
- def set_metadata(config_dir, profile, json_str):
78
- api_url = metaflowconfig.get_sanitized_url_from_config(
79
- config_dir, profile, "OBP_API_SERVER"
80
- )
81
-
82
- perimeter = _get_perimeter()
83
- headers = _get_request_headers()
84
- payload = json.loads(json_str)
85
-
86
- # POST the updated flowproject config
87
- # /v1/perimeters/:perimeter/flowprojects
88
- response = requests.post(
89
- url=f"{api_url}/v1/perimeters/{perimeter}/flowprojects",
90
- json=payload,
91
- headers=headers,
92
- )
93
- if response.status_code >= 500:
94
- raise Exception("API request failed. %s" % response.text)
95
-
96
- if response.status_code >= 400:
97
- raise Exception("request failed: %s" % response.text)
98
- body = response.json()
99
-
100
- print(body, file=sys.stdout)
101
-
102
-
103
- def _get_request_headers():
104
- headers = {"Content-Type": "application/json", "Connection": "keep-alive"}
105
- try:
106
- from metaflow.metaflow_config import SERVICE_HEADERS
107
-
108
- headers = {**headers, **(SERVICE_HEADERS or {})}
109
- except ImportError:
110
- headers = headers
111
-
112
- return headers
113
-
114
-
115
- def _get_perimeter():
116
- # Get current perimeter
117
- from metaflow_extensions.outerbounds.remote_config import init_config # type: ignore
118
-
119
- conf = init_config()
120
- if "OBP_PERIMETER" in conf:
121
- perimeter = conf["OBP_PERIMETER"]
122
- else:
123
- # if the perimeter is not in metaflow config, try to get it from the environment
124
- perimeter = os.environ.get("OBP_PERIMETER", None)
125
- if perimeter is None:
126
- raise Exception("Perimeter not found in config, but is required.")
127
-
128
- return perimeter
129
-
130
-
131
- def _parse_id(id: str):
132
- parts = id.split("/")
133
- if len(parts) != 2:
134
- raise Exception("ID should consist of two parts: project/branch")
135
-
136
- project, branch = parts
137
- return project, branch