seldon-sdk 0.1.0.dev0__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.
- seldon_sdk/__init__.py +23 -0
- seldon_sdk/__init__.pyi +30 -0
- seldon_sdk/auth.py +286 -0
- seldon_sdk/cli.py +344 -0
- seldon_sdk/contract.py +200 -0
- seldon_sdk/decorator.py +114 -0
- seldon_sdk/errors.py +37 -0
- seldon_sdk/files.py +350 -0
- seldon_sdk/local.py +69 -0
- seldon_sdk/lockfile.py +42 -0
- seldon_sdk/management.py +532 -0
- seldon_sdk/preparation.py +225 -0
- seldon_sdk/py.typed +0 -0
- seldon_sdk/runtime.py +375 -0
- seldon_sdk/scanner.py +143 -0
- seldon_sdk/skill/SKILL.md +61 -0
- seldon_sdk/testing.py +32 -0
- seldon_sdk/testing.pyi +12 -0
- seldon_sdk-0.1.0.dev0.dist-info/METADATA +263 -0
- seldon_sdk-0.1.0.dev0.dist-info/RECORD +23 -0
- seldon_sdk-0.1.0.dev0.dist-info/WHEEL +4 -0
- seldon_sdk-0.1.0.dev0.dist-info/entry_points.txt +2 -0
- seldon_sdk-0.1.0.dev0.dist-info/licenses/LICENSE +202 -0
seldon_sdk/__init__.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""Explicit Seldon Functions. Imports create no clients and load no application files."""
|
|
2
|
+
|
|
3
|
+
__version__ = "0.1.0.dev0"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def function(**settings):
|
|
7
|
+
from .decorator import function as decorate
|
|
8
|
+
|
|
9
|
+
return decorate(**settings)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def configure(**settings):
|
|
13
|
+
from .runtime import configure as apply
|
|
14
|
+
|
|
15
|
+
apply(**settings)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def __getattr__(name):
|
|
19
|
+
if name in {"CallInfo", "close", "aclose"}:
|
|
20
|
+
from . import runtime
|
|
21
|
+
|
|
22
|
+
return getattr(runtime, name)
|
|
23
|
+
raise AttributeError(name)
|
seldon_sdk/__init__.pyi
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from collections.abc import Awaitable, Callable
|
|
2
|
+
from typing import Any, Generic, ParamSpec, TypeVar, overload
|
|
3
|
+
|
|
4
|
+
from .runtime import CallInfo as CallInfo
|
|
5
|
+
|
|
6
|
+
P = ParamSpec("P")
|
|
7
|
+
R = TypeVar("R")
|
|
8
|
+
T = TypeVar("T")
|
|
9
|
+
|
|
10
|
+
__version__: str
|
|
11
|
+
|
|
12
|
+
class Function(Generic[P, R]):
|
|
13
|
+
key: str
|
|
14
|
+
id: str | None
|
|
15
|
+
digest: str
|
|
16
|
+
contract: dict[str, Any]
|
|
17
|
+
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R: ...
|
|
18
|
+
def export(self) -> dict[str, Any]: ...
|
|
19
|
+
@overload
|
|
20
|
+
def call_with_info(
|
|
21
|
+
self: Function[P, Awaitable[T]], *args: P.args, **kwargs: P.kwargs
|
|
22
|
+
) -> Awaitable[tuple[T, CallInfo]]: ...
|
|
23
|
+
@overload
|
|
24
|
+
def call_with_info(self, *args: P.args, **kwargs: P.kwargs) -> tuple[R, CallInfo]: ...
|
|
25
|
+
async def aclose(self) -> None: ...
|
|
26
|
+
|
|
27
|
+
def function(**settings: Any) -> Callable[[Callable[P, R]], Function[P, R]]: ...
|
|
28
|
+
def configure(**settings: Any) -> None: ...
|
|
29
|
+
def close() -> None: ...
|
|
30
|
+
async def aclose() -> None: ...
|
seldon_sdk/auth.py
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
"""WorkOS public-client login; only refresh credentials persist in a secure keychain."""
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
import hashlib
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
import time
|
|
8
|
+
import webbrowser
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from urllib.parse import urlencode, urlsplit
|
|
11
|
+
|
|
12
|
+
import httpx
|
|
13
|
+
|
|
14
|
+
from .errors import SeldonAuthError, SeldonConfigError
|
|
15
|
+
from .files import atomic_json, read_json
|
|
16
|
+
|
|
17
|
+
WORKOS = "https://api.workos.com"
|
|
18
|
+
DEFAULT_BASE_URL = "https://api.seldon-ai.com"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def base_url(value):
|
|
22
|
+
parsed = urlsplit(value)
|
|
23
|
+
if (
|
|
24
|
+
parsed.scheme != "https"
|
|
25
|
+
and not (parsed.scheme == "http" and parsed.hostname == "localhost")
|
|
26
|
+
) or (
|
|
27
|
+
not parsed.hostname
|
|
28
|
+
or parsed.username
|
|
29
|
+
or parsed.password
|
|
30
|
+
or parsed.query
|
|
31
|
+
or parsed.fragment
|
|
32
|
+
or parsed.path not in ("", "/")
|
|
33
|
+
):
|
|
34
|
+
raise SeldonConfigError("https_origin_required")
|
|
35
|
+
return value.rstrip("/")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class Keychain:
|
|
39
|
+
def __init__(self):
|
|
40
|
+
self.backend = None
|
|
41
|
+
try:
|
|
42
|
+
import keyring
|
|
43
|
+
|
|
44
|
+
backend = keyring.get_keyring()
|
|
45
|
+
candidates = getattr(backend, "backends", [backend])
|
|
46
|
+
for candidate in candidates:
|
|
47
|
+
if type(candidate).__module__ in {
|
|
48
|
+
"keyring.backends.macOS",
|
|
49
|
+
"keyring.backends.Windows",
|
|
50
|
+
"keyring.backends.SecretService",
|
|
51
|
+
}:
|
|
52
|
+
self.backend = candidate
|
|
53
|
+
break
|
|
54
|
+
except Exception:
|
|
55
|
+
# No insecure persistence fallback; callers receive a one-command session.
|
|
56
|
+
self.backend = None
|
|
57
|
+
|
|
58
|
+
def get(self, key):
|
|
59
|
+
if self.backend is None:
|
|
60
|
+
return None
|
|
61
|
+
try:
|
|
62
|
+
return self.backend.get_password("seldon-sdk", key)
|
|
63
|
+
except Exception:
|
|
64
|
+
raise SeldonAuthError("secure_keychain_unavailable") from None
|
|
65
|
+
|
|
66
|
+
def set(self, key, token):
|
|
67
|
+
if self.backend is None:
|
|
68
|
+
return False
|
|
69
|
+
try:
|
|
70
|
+
self.backend.set_password("seldon-sdk", key, token)
|
|
71
|
+
return True
|
|
72
|
+
except Exception:
|
|
73
|
+
raise SeldonAuthError("secure_keychain_unavailable") from None
|
|
74
|
+
|
|
75
|
+
def delete(self, key):
|
|
76
|
+
if self.backend is None:
|
|
77
|
+
return
|
|
78
|
+
try:
|
|
79
|
+
if self.backend.get_password("seldon-sdk", key) is not None:
|
|
80
|
+
self.backend.delete_password("seldon-sdk", key)
|
|
81
|
+
except Exception:
|
|
82
|
+
raise SeldonAuthError("secure_keychain_unavailable") from None
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class Login:
|
|
86
|
+
def __init__(
|
|
87
|
+
self,
|
|
88
|
+
origin=DEFAULT_BASE_URL,
|
|
89
|
+
*,
|
|
90
|
+
profile="default",
|
|
91
|
+
client_id=None,
|
|
92
|
+
store=None,
|
|
93
|
+
config_dir=None,
|
|
94
|
+
http=None,
|
|
95
|
+
sleep=time.sleep,
|
|
96
|
+
clock=time.monotonic,
|
|
97
|
+
):
|
|
98
|
+
self.origin = base_url(origin)
|
|
99
|
+
if not profile or len(profile) > 80:
|
|
100
|
+
raise SeldonConfigError("invalid_profile")
|
|
101
|
+
self.profile = profile
|
|
102
|
+
self.client_id = client_id or os.environ.get("SELDON_WORKOS_CLIENT_ID")
|
|
103
|
+
self.store = store if store is not None else Keychain()
|
|
104
|
+
self.config_dir = Path(
|
|
105
|
+
config_dir or os.environ.get("SELDON_CONFIG_HOME", Path.home() / ".config/seldon")
|
|
106
|
+
)
|
|
107
|
+
self.key = hashlib.sha256((profile + "\n" + self.origin).encode()).hexdigest()
|
|
108
|
+
profiles_path = self.config_dir / "profiles.json"
|
|
109
|
+
if not self.client_id and profiles_path.exists():
|
|
110
|
+
profile_data = read_json(profiles_path).get(self.key, {})
|
|
111
|
+
if (
|
|
112
|
+
profile_data.get("base_url") == self.origin
|
|
113
|
+
and profile_data.get("profile") == profile
|
|
114
|
+
):
|
|
115
|
+
self.client_id = profile_data.get("client_id")
|
|
116
|
+
self.http = http or httpx.Client(timeout=30, follow_redirects=False)
|
|
117
|
+
self.sleep, self.clock = sleep, clock
|
|
118
|
+
self._access = None
|
|
119
|
+
self._claims = None
|
|
120
|
+
|
|
121
|
+
def _post(self, fields):
|
|
122
|
+
if not self.client_id:
|
|
123
|
+
raise SeldonConfigError("workos_client_id_required")
|
|
124
|
+
try:
|
|
125
|
+
response = self.http.post(
|
|
126
|
+
WORKOS + "/user_management/authenticate",
|
|
127
|
+
data={
|
|
128
|
+
"client_id": self.client_id,
|
|
129
|
+
**fields,
|
|
130
|
+
},
|
|
131
|
+
timeout=30,
|
|
132
|
+
follow_redirects=False,
|
|
133
|
+
)
|
|
134
|
+
body = response.json()
|
|
135
|
+
except (httpx.HTTPError, ValueError):
|
|
136
|
+
raise SeldonAuthError("authentication_unavailable") from None
|
|
137
|
+
if response.status_code >= 500:
|
|
138
|
+
raise SeldonAuthError("authentication_unavailable")
|
|
139
|
+
return response.status_code, body
|
|
140
|
+
|
|
141
|
+
def _accept(self, data):
|
|
142
|
+
access, refresh = data.get("access_token"), data.get("refresh_token")
|
|
143
|
+
if not isinstance(access, str) or not isinstance(refresh, str) or not access or not refresh:
|
|
144
|
+
raise SeldonAuthError("invalid_authentication_response")
|
|
145
|
+
# A successful WorkOS refresh can revoke the previous token immediately.
|
|
146
|
+
# Preserve its replacement even when the downstream API is unavailable.
|
|
147
|
+
persisted = self.store.set(self.key, refresh)
|
|
148
|
+
# Verification stays on Seldon's real WorkOS boundary, including organization mapping.
|
|
149
|
+
try:
|
|
150
|
+
response = self.http.get(
|
|
151
|
+
self.origin + "/v1/seldon/functions",
|
|
152
|
+
params={"limit": 1},
|
|
153
|
+
headers={"Authorization": "Bearer " + access},
|
|
154
|
+
timeout=30,
|
|
155
|
+
follow_redirects=False,
|
|
156
|
+
)
|
|
157
|
+
except httpx.HTTPError:
|
|
158
|
+
raise SeldonAuthError("session_verification_unavailable") from None
|
|
159
|
+
if response.status_code != 200:
|
|
160
|
+
raise SeldonAuthError("session_not_accepted")
|
|
161
|
+
try:
|
|
162
|
+
part = access.split(".")[1]
|
|
163
|
+
claims = json.loads(base64.urlsafe_b64decode(part + "=" * (-len(part) % 4)))
|
|
164
|
+
if not claims.get("sub") or not (claims.get("org_id") or claims.get("organization_id")):
|
|
165
|
+
raise ValueError
|
|
166
|
+
except (IndexError, ValueError, TypeError):
|
|
167
|
+
raise SeldonAuthError("invalid_session_claims") from None
|
|
168
|
+
self._access, self._claims = access, claims
|
|
169
|
+
if persisted:
|
|
170
|
+
path = self.config_dir / "profiles.json"
|
|
171
|
+
profiles = read_json(path) if path.exists() else {}
|
|
172
|
+
profiles[self.key] = {
|
|
173
|
+
"profile": self.profile,
|
|
174
|
+
"base_url": self.origin,
|
|
175
|
+
"client_id": self.client_id,
|
|
176
|
+
}
|
|
177
|
+
atomic_json(path, profiles)
|
|
178
|
+
return {"authenticated": True, "persisted": persisted, **self.identity(refresh=False)}
|
|
179
|
+
|
|
180
|
+
def login(self, display):
|
|
181
|
+
if not self.client_id:
|
|
182
|
+
raise SeldonConfigError("workos_client_id_required")
|
|
183
|
+
try:
|
|
184
|
+
response = self.http.post(
|
|
185
|
+
WORKOS + "/user_management/authorize/device",
|
|
186
|
+
data={"client_id": self.client_id},
|
|
187
|
+
timeout=30,
|
|
188
|
+
follow_redirects=False,
|
|
189
|
+
)
|
|
190
|
+
body = response.json()
|
|
191
|
+
uri = urlsplit(body["verification_uri"])
|
|
192
|
+
interval, expires = body["interval"], body["expires_in"]
|
|
193
|
+
if response.status_code != 200 or uri.scheme != "https" or not uri.hostname:
|
|
194
|
+
raise ValueError
|
|
195
|
+
if (
|
|
196
|
+
type(interval) is not int
|
|
197
|
+
or not 1 <= interval <= 60
|
|
198
|
+
or type(expires) is not int
|
|
199
|
+
or not 1 <= expires <= 900
|
|
200
|
+
):
|
|
201
|
+
raise ValueError
|
|
202
|
+
if not all(
|
|
203
|
+
isinstance(body[key], str) and body[key] for key in ("device_code", "user_code")
|
|
204
|
+
):
|
|
205
|
+
raise ValueError
|
|
206
|
+
except (httpx.HTTPError, KeyError, ValueError, TypeError):
|
|
207
|
+
raise SeldonAuthError("device_authorization_unavailable") from None
|
|
208
|
+
display({"verification_uri": body["verification_uri"], "user_code": body["user_code"]})
|
|
209
|
+
deadline = self.clock() + expires
|
|
210
|
+
while self.clock() < deadline:
|
|
211
|
+
self.sleep(min(interval, max(0, deadline - self.clock())))
|
|
212
|
+
if self.clock() >= deadline:
|
|
213
|
+
break
|
|
214
|
+
status, result = self._post(
|
|
215
|
+
{
|
|
216
|
+
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
|
|
217
|
+
"device_code": body["device_code"],
|
|
218
|
+
}
|
|
219
|
+
)
|
|
220
|
+
if status == 200:
|
|
221
|
+
return self._accept(result)
|
|
222
|
+
error = result.get("error")
|
|
223
|
+
if error == "slow_down":
|
|
224
|
+
interval += 5
|
|
225
|
+
elif error != "authorization_pending":
|
|
226
|
+
raise SeldonAuthError("device_authorization_declined")
|
|
227
|
+
raise SeldonAuthError("device_authorization_expired")
|
|
228
|
+
|
|
229
|
+
def access_token(self):
|
|
230
|
+
if self._access is not None and self._claims.get("exp", 0) > time.time() + 30:
|
|
231
|
+
return self._access
|
|
232
|
+
refresh = self.store.get(self.key)
|
|
233
|
+
if not refresh:
|
|
234
|
+
raise SeldonAuthError("login_required")
|
|
235
|
+
status, body = self._post({"grant_type": "refresh_token", "refresh_token": refresh})
|
|
236
|
+
if status != 200:
|
|
237
|
+
if body.get("error") == "invalid_grant":
|
|
238
|
+
self.store.delete(self.key)
|
|
239
|
+
raise SeldonAuthError("login_required")
|
|
240
|
+
self._accept(body)
|
|
241
|
+
return self._access
|
|
242
|
+
|
|
243
|
+
def identity(self, *, refresh=True):
|
|
244
|
+
if refresh or self._claims is None:
|
|
245
|
+
self.access_token()
|
|
246
|
+
claims = self._claims
|
|
247
|
+
return {
|
|
248
|
+
"user_id": claims["sub"],
|
|
249
|
+
"workspace": claims.get("org_id") or claims.get("organization_id"),
|
|
250
|
+
"role": claims.get("role"),
|
|
251
|
+
"expires_at": claims.get("exp"),
|
|
252
|
+
"base_url": self.origin,
|
|
253
|
+
"profile": self.profile,
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
def logout(self, *, open_browser=webbrowser.open):
|
|
257
|
+
remote = "unavailable"
|
|
258
|
+
try:
|
|
259
|
+
self.access_token()
|
|
260
|
+
sid = self._claims.get("sid")
|
|
261
|
+
if isinstance(sid, str) and sid.startswith("session_"):
|
|
262
|
+
url = WORKOS + "/user_management/sessions/logout?" + urlencode({"session_id": sid})
|
|
263
|
+
remote = "browser_confirmation_pending" if open_browser(url) else "unavailable"
|
|
264
|
+
except (SeldonAuthError, SeldonConfigError):
|
|
265
|
+
pass
|
|
266
|
+
finally:
|
|
267
|
+
self.store.delete(self.key)
|
|
268
|
+
self._access = self._claims = None
|
|
269
|
+
return {"local_credentials_deleted": True, "remote_logout": remote}
|
|
270
|
+
|
|
271
|
+
def logout_all(self, *, open_browser=webbrowser.open):
|
|
272
|
+
path = self.config_dir / "profiles.json"
|
|
273
|
+
profiles = read_json(path) if path.exists() else {}
|
|
274
|
+
results = []
|
|
275
|
+
for profile in profiles.values():
|
|
276
|
+
other = Login(
|
|
277
|
+
profile["base_url"],
|
|
278
|
+
profile=profile["profile"],
|
|
279
|
+
client_id=profile["client_id"],
|
|
280
|
+
store=self.store,
|
|
281
|
+
config_dir=self.config_dir,
|
|
282
|
+
http=self.http,
|
|
283
|
+
)
|
|
284
|
+
results.append(other.logout(open_browser=open_browser))
|
|
285
|
+
atomic_json(path, {})
|
|
286
|
+
return results
|
seldon_sdk/cli.py
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
"""One CLI for explicit source preparation, comparison, adoption and local execution."""
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import asyncio
|
|
5
|
+
import importlib.resources
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
import sys
|
|
9
|
+
|
|
10
|
+
import httpx
|
|
11
|
+
|
|
12
|
+
from . import __version__
|
|
13
|
+
from .auth import DEFAULT_BASE_URL, Login, base_url
|
|
14
|
+
from .errors import SeldonError
|
|
15
|
+
from .files import Association, atomic_json, digest, read_json, resolve_function, validate_source
|
|
16
|
+
from .management import Management
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def parser():
|
|
20
|
+
root = argparse.ArgumentParser(prog="seldon")
|
|
21
|
+
root.add_argument("--version", action="version", version=__version__)
|
|
22
|
+
root.add_argument("--base-url", default=os.environ.get("SELDON_BASE_URL", DEFAULT_BASE_URL))
|
|
23
|
+
root.add_argument("--profile", default="default")
|
|
24
|
+
root.add_argument("--association", default=".seldon/operation.json")
|
|
25
|
+
commands = root.add_subparsers(dest="command", required=True)
|
|
26
|
+
source = commands.add_parser(
|
|
27
|
+
"import", help="Validate an explicit source file; import with a login"
|
|
28
|
+
)
|
|
29
|
+
source.add_argument("--source", required=True)
|
|
30
|
+
source.add_argument("--function")
|
|
31
|
+
source.add_argument("--name", default="Selected operation")
|
|
32
|
+
source.add_argument("--offline", action="store_true")
|
|
33
|
+
for name in ("login", "whoami"):
|
|
34
|
+
commands.add_parser(name)
|
|
35
|
+
logout = commands.add_parser("logout")
|
|
36
|
+
logout.add_argument("--all-profiles", action="store_true")
|
|
37
|
+
status = commands.add_parser("status")
|
|
38
|
+
status.add_argument("function", nargs="?")
|
|
39
|
+
status.add_argument("--json", action="store_true")
|
|
40
|
+
report = commands.add_parser("report")
|
|
41
|
+
report.add_argument("function")
|
|
42
|
+
report.add_argument("--run")
|
|
43
|
+
report.add_argument("--json", action="store_true")
|
|
44
|
+
optimize = commands.add_parser("optimize")
|
|
45
|
+
optimize.add_argument("function")
|
|
46
|
+
optimize.add_argument("--cap", required=True)
|
|
47
|
+
optimize.add_argument("--objective", choices=("cost", "latency", "quality"), default="cost")
|
|
48
|
+
optimize.add_argument("--max-p95-ms", type=float)
|
|
49
|
+
optimize.add_argument("--yes", action="store_true")
|
|
50
|
+
optimize.add_argument("--wait", action="store_true")
|
|
51
|
+
optimize.add_argument("--new-run", action="store_true")
|
|
52
|
+
optimize.add_argument("--source-run")
|
|
53
|
+
optimize.add_argument("--timeout", type=float, default=600)
|
|
54
|
+
cancel = commands.add_parser("cancel")
|
|
55
|
+
cancel.add_argument("function")
|
|
56
|
+
cancel.add_argument("--run", required=True)
|
|
57
|
+
ready = commands.add_parser("reference-readiness")
|
|
58
|
+
ready.add_argument("function")
|
|
59
|
+
reference = commands.add_parser("serve-reference")
|
|
60
|
+
reference.add_argument("function")
|
|
61
|
+
reference.add_argument("--acknowledge-deadline-risk", action="store_true")
|
|
62
|
+
for name in ("activate", "publish"):
|
|
63
|
+
action = commands.add_parser(name)
|
|
64
|
+
action.add_argument("function")
|
|
65
|
+
choice = action.add_mutually_exclusive_group(required=True)
|
|
66
|
+
choice.add_argument("--revision")
|
|
67
|
+
if name == "activate":
|
|
68
|
+
choice.add_argument(
|
|
69
|
+
"--alternative", type=int, help="1-based index from the selected report"
|
|
70
|
+
)
|
|
71
|
+
action.add_argument("--approve-managed-fallback", action="store_true")
|
|
72
|
+
examples = commands.add_parser("examples").add_subparsers(dest="example_command", required=True)
|
|
73
|
+
attach = examples.add_parser("import")
|
|
74
|
+
attach.add_argument("function")
|
|
75
|
+
attach.add_argument("--file", required=True)
|
|
76
|
+
attach.add_argument("--replace", action="store_true")
|
|
77
|
+
check = commands.add_parser("check", help="Offline source drift check")
|
|
78
|
+
check.add_argument("modules", nargs="*")
|
|
79
|
+
check.add_argument("--source")
|
|
80
|
+
check.add_argument("--function")
|
|
81
|
+
check.add_argument("--template-file")
|
|
82
|
+
check.add_argument("--syntax", choices=("python_format", "jinja_variables"))
|
|
83
|
+
check.add_argument("--allow-pending", action="store_true")
|
|
84
|
+
check.add_argument("--offline", action="store_true")
|
|
85
|
+
export = commands.add_parser("export")
|
|
86
|
+
export.add_argument("modules", nargs="+")
|
|
87
|
+
export.add_argument("--out", required=True)
|
|
88
|
+
sync = commands.add_parser("sync")
|
|
89
|
+
sync.add_argument("modules", nargs="+")
|
|
90
|
+
sync.add_argument("--json", action="store_true")
|
|
91
|
+
lock = commands.add_parser("lock").add_subparsers(dest="lock_command", required=True)
|
|
92
|
+
set_lock = lock.add_parser("set")
|
|
93
|
+
set_lock.add_argument("key")
|
|
94
|
+
set_lock.add_argument("function")
|
|
95
|
+
set_lock.add_argument("--module", required=True)
|
|
96
|
+
scan = commands.add_parser("scan")
|
|
97
|
+
scan.add_argument("path", nargs="?", default=".")
|
|
98
|
+
scan.add_argument("--json", action="store_true")
|
|
99
|
+
scan.add_argument("--suggest", action="store_true")
|
|
100
|
+
scan.add_argument("--format", choices=("existing", "sdk"), default="existing")
|
|
101
|
+
check.add_argument("--json", action="store_true")
|
|
102
|
+
local = commands.add_parser("function").add_subparsers(dest="local_command", required=True)
|
|
103
|
+
for name in ("pull", "verify", "run"):
|
|
104
|
+
action = local.add_parser(name)
|
|
105
|
+
action.add_argument("function")
|
|
106
|
+
action.add_argument("--out", default=".seldon/function-bundle.json")
|
|
107
|
+
action.add_argument("--input")
|
|
108
|
+
action.add_argument("--fallback", choices=("seldon", "raise"), default="seldon")
|
|
109
|
+
action.add_argument("--no-telemetry", action="store_true")
|
|
110
|
+
skill = commands.add_parser("skill").add_subparsers(dest="skill_command", required=True)
|
|
111
|
+
skill.add_parser("print")
|
|
112
|
+
return root
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def emit(value):
|
|
116
|
+
print(json.dumps(value, ensure_ascii=False, sort_keys=True, indent=2, allow_nan=False))
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _confirmation(summary):
|
|
120
|
+
emit(summary)
|
|
121
|
+
value = summary.get("revision_id") or summary["function_id"]
|
|
122
|
+
return input("Type the displayed revision or Function id to confirm: ").strip() == value
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _local(args):
|
|
126
|
+
identity = resolve_function(args.function)
|
|
127
|
+
key = os.environ.get("SELDON_API_KEY")
|
|
128
|
+
if not key:
|
|
129
|
+
raise SeldonError("inference_api_key_required")
|
|
130
|
+
origin = base_url(args.base_url)
|
|
131
|
+
if args.local_command == "pull":
|
|
132
|
+
try:
|
|
133
|
+
with httpx.Client(timeout=30, follow_redirects=False) as http:
|
|
134
|
+
response = http.get(
|
|
135
|
+
f"{origin}/v1/seldon/functions/{identity}/bundle",
|
|
136
|
+
headers={"Authorization": "Bearer " + key},
|
|
137
|
+
)
|
|
138
|
+
if response.status_code != 200:
|
|
139
|
+
raise SeldonError("bundle_unavailable", status=response.status_code)
|
|
140
|
+
bundle = response.json()
|
|
141
|
+
etag = response.headers.get("etag", "").strip('"')
|
|
142
|
+
if (
|
|
143
|
+
bundle.get("version") != "function-bundle-v1"
|
|
144
|
+
or digest({k: v for k, v in bundle.items() if k != "expires_at"}) != etag
|
|
145
|
+
):
|
|
146
|
+
raise SeldonError("bundle_invalid")
|
|
147
|
+
except (httpx.HTTPError, ValueError):
|
|
148
|
+
raise SeldonError("bundle_unavailable") from None
|
|
149
|
+
atomic_json(args.out, bundle)
|
|
150
|
+
return {
|
|
151
|
+
"bundle_file": args.out,
|
|
152
|
+
"revision_id": bundle["revision_id"],
|
|
153
|
+
"verification": "not_run",
|
|
154
|
+
}
|
|
155
|
+
try:
|
|
156
|
+
from seldon_runtime.runner import Runner
|
|
157
|
+
except ImportError:
|
|
158
|
+
raise SeldonError("install_seldon_runtime_gliner_extra") from None
|
|
159
|
+
|
|
160
|
+
async def execute():
|
|
161
|
+
runner = await Runner.load(
|
|
162
|
+
identity,
|
|
163
|
+
key,
|
|
164
|
+
base_url=origin,
|
|
165
|
+
fallback=args.fallback,
|
|
166
|
+
allow_unverified=args.local_command == "verify",
|
|
167
|
+
telemetry=not args.no_telemetry,
|
|
168
|
+
)
|
|
169
|
+
try:
|
|
170
|
+
if args.local_command == "verify":
|
|
171
|
+
result = await runner.verify()
|
|
172
|
+
return {"verification": result, "status": runner.status()}
|
|
173
|
+
if not args.input:
|
|
174
|
+
raise SeldonError("input_file_required")
|
|
175
|
+
return await runner(read_json(args.input))
|
|
176
|
+
finally:
|
|
177
|
+
await runner.aclose()
|
|
178
|
+
|
|
179
|
+
return asyncio.run(execute())
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def main(argv=None, *, login=None, interactive=None, confirm=None):
|
|
183
|
+
args = parser().parse_args(argv)
|
|
184
|
+
try:
|
|
185
|
+
if args.command == "skill":
|
|
186
|
+
print(importlib.resources.files("seldon_sdk").joinpath("skill/SKILL.md").read_text())
|
|
187
|
+
return 0
|
|
188
|
+
if args.command == "export":
|
|
189
|
+
from .preparation import exported
|
|
190
|
+
|
|
191
|
+
result, code = exported(args.modules, args.out)
|
|
192
|
+
emit(result)
|
|
193
|
+
return code
|
|
194
|
+
if args.command == "lock":
|
|
195
|
+
from .preparation import lock_set
|
|
196
|
+
|
|
197
|
+
emit(lock_set(args.module, args.key, args.function))
|
|
198
|
+
return 0
|
|
199
|
+
if args.command == "scan":
|
|
200
|
+
from .scanner import scan
|
|
201
|
+
|
|
202
|
+
emit(scan(args.path, suggest=args.suggest, format=args.format))
|
|
203
|
+
return 0
|
|
204
|
+
if args.command == "check" and args.modules:
|
|
205
|
+
from .preparation import check_modules
|
|
206
|
+
|
|
207
|
+
session = None if args.offline else login
|
|
208
|
+
if session is None and not args.offline:
|
|
209
|
+
candidate = Login(args.base_url, profile=args.profile)
|
|
210
|
+
if candidate.store.get(candidate.key):
|
|
211
|
+
session = candidate
|
|
212
|
+
api = Management(session, association=args.association) if session else None
|
|
213
|
+
result, code = check_modules(args.modules, api=api, allow_pending=args.allow_pending)
|
|
214
|
+
emit(result)
|
|
215
|
+
return code
|
|
216
|
+
if args.command == "check" and args.function:
|
|
217
|
+
from .preparation import check_template
|
|
218
|
+
|
|
219
|
+
if not args.template_file or not args.syntax:
|
|
220
|
+
raise SeldonError("template_file_and_syntax_required")
|
|
221
|
+
session = login or Login(args.base_url, profile=args.profile)
|
|
222
|
+
result, code = check_template(
|
|
223
|
+
Management(session), args.function, args.template_file, args.syntax
|
|
224
|
+
)
|
|
225
|
+
emit(result)
|
|
226
|
+
return code
|
|
227
|
+
if args.command == "check":
|
|
228
|
+
if not args.source:
|
|
229
|
+
raise SeldonError("source_or_module_required")
|
|
230
|
+
source_digest = digest(validate_source(read_json(args.source)))
|
|
231
|
+
state = Association(args.association).read()
|
|
232
|
+
status = (
|
|
233
|
+
"unchanged"
|
|
234
|
+
if state.get("source_digest") == source_digest
|
|
235
|
+
else "stale_lock"
|
|
236
|
+
if state
|
|
237
|
+
else "unregistered"
|
|
238
|
+
)
|
|
239
|
+
emit({"status": status, "source_digest": source_digest})
|
|
240
|
+
return 0 if status == "unchanged" else 1
|
|
241
|
+
if args.command == "function":
|
|
242
|
+
emit(_local(args))
|
|
243
|
+
return 0
|
|
244
|
+
if args.command == "import" and args.offline:
|
|
245
|
+
source = validate_source(read_json(args.source))
|
|
246
|
+
emit(
|
|
247
|
+
{
|
|
248
|
+
"status": "validated_offline",
|
|
249
|
+
"destination": base_url(args.base_url) + "/v1/seldon/functions",
|
|
250
|
+
"source_file": args.source,
|
|
251
|
+
"source_digest": digest(source),
|
|
252
|
+
"fields": sorted(source) if isinstance(source, dict) else ["calls"],
|
|
253
|
+
"next_action": "dashboard_import_or_login",
|
|
254
|
+
}
|
|
255
|
+
)
|
|
256
|
+
return 0
|
|
257
|
+
session = login or Login(args.base_url, profile=args.profile)
|
|
258
|
+
api = Management(session, association=args.association)
|
|
259
|
+
if args.command == "import":
|
|
260
|
+
emit(
|
|
261
|
+
api.import_source(
|
|
262
|
+
args.source, function=args.function, name=args.name, offline=args.offline
|
|
263
|
+
)
|
|
264
|
+
)
|
|
265
|
+
elif args.command == "sync":
|
|
266
|
+
from .preparation import sync_modules
|
|
267
|
+
|
|
268
|
+
result, code = sync_modules(args.modules, api)
|
|
269
|
+
emit(result)
|
|
270
|
+
return code
|
|
271
|
+
elif args.command == "login":
|
|
272
|
+
emit(session.login(emit))
|
|
273
|
+
elif args.command == "logout":
|
|
274
|
+
emit(session.logout_all() if args.all_profiles else session.logout())
|
|
275
|
+
elif args.command == "whoami":
|
|
276
|
+
emit(session.identity())
|
|
277
|
+
elif args.command == "status":
|
|
278
|
+
emit(api.status(args.function))
|
|
279
|
+
elif args.command == "report":
|
|
280
|
+
emit(api.report(args.function, run_id=args.run))
|
|
281
|
+
elif args.command == "cancel":
|
|
282
|
+
emit(api.cancel(args.function, args.run))
|
|
283
|
+
elif args.command == "optimize":
|
|
284
|
+
result, code = api.optimize(
|
|
285
|
+
args.function,
|
|
286
|
+
cap=args.cap,
|
|
287
|
+
objective=args.objective,
|
|
288
|
+
max_p95_ms=args.max_p95_ms,
|
|
289
|
+
authorize=args.yes,
|
|
290
|
+
wait=args.wait,
|
|
291
|
+
timeout=args.timeout,
|
|
292
|
+
new_run=args.new_run,
|
|
293
|
+
source_run=args.source_run,
|
|
294
|
+
)
|
|
295
|
+
emit(result)
|
|
296
|
+
return code
|
|
297
|
+
elif args.command == "reference-readiness":
|
|
298
|
+
emit(
|
|
299
|
+
api.request(
|
|
300
|
+
"GET",
|
|
301
|
+
"/v1/seldon/functions/"
|
|
302
|
+
+ resolve_function(args.function)
|
|
303
|
+
+ "/reference-readiness",
|
|
304
|
+
)
|
|
305
|
+
)
|
|
306
|
+
elif args.command == "serve-reference":
|
|
307
|
+
result, code = api.serve_reference(
|
|
308
|
+
args.function,
|
|
309
|
+
acknowledge_deadline_risk=args.acknowledge_deadline_risk,
|
|
310
|
+
interactive=sys.stdin.isatty() if interactive is None else interactive,
|
|
311
|
+
confirm=confirm or _confirmation,
|
|
312
|
+
)
|
|
313
|
+
emit(result)
|
|
314
|
+
return code
|
|
315
|
+
elif args.command in ("activate", "publish"):
|
|
316
|
+
result, code = api.activate(
|
|
317
|
+
args.function,
|
|
318
|
+
args.revision,
|
|
319
|
+
alternative=getattr(args, "alternative", None),
|
|
320
|
+
publish=args.command == "publish",
|
|
321
|
+
managed_fallback_approved=args.approve_managed_fallback,
|
|
322
|
+
interactive=sys.stdin.isatty() if interactive is None else interactive,
|
|
323
|
+
confirm=confirm or _confirmation,
|
|
324
|
+
)
|
|
325
|
+
emit(result)
|
|
326
|
+
return code
|
|
327
|
+
elif args.command == "examples":
|
|
328
|
+
result, code = api.import_examples(args.function, args.file, replace=args.replace)
|
|
329
|
+
emit(result)
|
|
330
|
+
return code
|
|
331
|
+
return 0
|
|
332
|
+
except SeldonError as exc:
|
|
333
|
+
print("seldon: " + exc.code, file=sys.stderr)
|
|
334
|
+
return 2
|
|
335
|
+
except (OSError, ValueError, KeyError, StopIteration):
|
|
336
|
+
print("seldon: invalid_input_or_service_response", file=sys.stderr)
|
|
337
|
+
return 2
|
|
338
|
+
except KeyboardInterrupt:
|
|
339
|
+
print("seldon: interrupted; resume the saved run, do not resubmit blindly", file=sys.stderr)
|
|
340
|
+
return 4
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
if __name__ == "__main__":
|
|
344
|
+
raise SystemExit(main())
|