lamindb_setup 0.77.4__py2.py3-none-any.whl → 0.77.6__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.
Files changed (47) hide show
  1. lamindb_setup/__init__.py +1 -2
  2. lamindb_setup/_cache.py +34 -34
  3. lamindb_setup/_check.py +7 -7
  4. lamindb_setup/_check_setup.py +92 -79
  5. lamindb_setup/_close.py +35 -35
  6. lamindb_setup/_connect_instance.py +425 -444
  7. lamindb_setup/_django.py +41 -41
  8. lamindb_setup/_entry_points.py +22 -22
  9. lamindb_setup/_exportdb.py +68 -68
  10. lamindb_setup/_importdb.py +50 -50
  11. lamindb_setup/_init_instance.py +411 -374
  12. lamindb_setup/_migrate.py +239 -239
  13. lamindb_setup/_register_instance.py +36 -36
  14. lamindb_setup/_schema.py +27 -27
  15. lamindb_setup/_schema_metadata.py +411 -411
  16. lamindb_setup/_set_managed_storage.py +55 -55
  17. lamindb_setup/_setup_user.py +137 -137
  18. lamindb_setup/_silence_loggers.py +44 -44
  19. lamindb_setup/core/__init__.py +21 -21
  20. lamindb_setup/core/_aws_credentials.py +151 -151
  21. lamindb_setup/core/_aws_storage.py +48 -48
  22. lamindb_setup/core/_deprecated.py +55 -55
  23. lamindb_setup/core/_docs.py +14 -14
  24. lamindb_setup/core/_hub_client.py +1 -1
  25. lamindb_setup/core/_hub_core.py +615 -590
  26. lamindb_setup/core/_hub_crud.py +211 -211
  27. lamindb_setup/core/_hub_utils.py +109 -109
  28. lamindb_setup/core/_private_django_api.py +88 -88
  29. lamindb_setup/core/_settings.py +145 -138
  30. lamindb_setup/core/_settings_instance.py +480 -467
  31. lamindb_setup/core/_settings_load.py +105 -105
  32. lamindb_setup/core/_settings_save.py +81 -81
  33. lamindb_setup/core/_settings_storage.py +412 -405
  34. lamindb_setup/core/_settings_store.py +75 -75
  35. lamindb_setup/core/_settings_user.py +55 -53
  36. lamindb_setup/core/_setup_bionty_sources.py +101 -101
  37. lamindb_setup/core/cloud_sqlite_locker.py +237 -232
  38. lamindb_setup/core/django.py +115 -114
  39. lamindb_setup/core/exceptions.py +12 -12
  40. lamindb_setup/core/hashing.py +114 -114
  41. lamindb_setup/core/types.py +19 -19
  42. lamindb_setup/core/upath.py +779 -779
  43. {lamindb_setup-0.77.4.dist-info → lamindb_setup-0.77.6.dist-info}/METADATA +3 -2
  44. lamindb_setup-0.77.6.dist-info/RECORD +47 -0
  45. {lamindb_setup-0.77.4.dist-info → lamindb_setup-0.77.6.dist-info}/WHEEL +1 -1
  46. lamindb_setup-0.77.4.dist-info/RECORD +0 -47
  47. {lamindb_setup-0.77.4.dist-info → lamindb_setup-0.77.6.dist-info}/LICENSE +0 -0
@@ -1,590 +1,615 @@
1
- from __future__ import annotations
2
-
3
- import json
4
- import os
5
- import uuid
6
- from importlib import metadata
7
- from typing import TYPE_CHECKING, Literal
8
- from uuid import UUID
9
-
10
- from lamin_utils import logger
11
- from postgrest.exceptions import APIError
12
-
13
- from ._hub_client import (
14
- call_with_fallback,
15
- call_with_fallback_auth,
16
- connect_hub,
17
- )
18
- from ._hub_crud import (
19
- _delete_instance_record,
20
- select_account_by_handle,
21
- select_db_user_by_instance,
22
- select_default_storage_by_instance_id,
23
- select_instance_by_id_with_storage,
24
- select_instance_by_name,
25
- select_instance_by_owner_name,
26
- )
27
- from ._hub_crud import update_instance as _update_instance_record
28
- from ._hub_utils import (
29
- LaminDsn,
30
- LaminDsnModel,
31
- )
32
- from ._settings import settings
33
- from ._settings_storage import StorageSettings, base62
34
-
35
- if TYPE_CHECKING:
36
- from supabase import Client # type: ignore
37
-
38
- from ._settings_instance import InstanceSettings
39
-
40
-
41
- def delete_storage_record(
42
- storage_uuid: UUID,
43
- ) -> None:
44
- return call_with_fallback_auth(
45
- _delete_storage_record,
46
- storage_uuid=storage_uuid,
47
- )
48
-
49
-
50
- def _delete_storage_record(storage_uuid: UUID, client: Client) -> None:
51
- if storage_uuid is None:
52
- return None
53
- response = client.table("storage").delete().eq("id", storage_uuid.hex).execute()
54
- if response.data:
55
- logger.important(f"deleted storage record on hub {storage_uuid.hex}")
56
- else:
57
- raise PermissionError(
58
- f"Deleting of storage with {storage_uuid.hex} was not successful. Probably, you"
59
- " don't have sufficient permissions."
60
- )
61
-
62
-
63
- def update_instance_record(instance_uuid: UUID, fields: dict) -> None:
64
- return call_with_fallback_auth(
65
- _update_instance_record, instance_id=instance_uuid.hex, instance_fields=fields
66
- )
67
-
68
-
69
- def get_storage_records_for_instance(
70
- instance_id: UUID,
71
- ) -> list[dict[str, str | int]]:
72
- return call_with_fallback_auth(
73
- _get_storage_records_for_instance,
74
- instance_id=instance_id,
75
- )
76
-
77
-
78
- def _get_storage_records_for_instance(
79
- instance_id: UUID, client: Client
80
- ) -> list[dict[str, str | int]]:
81
- response = (
82
- client.table("storage").select("*").eq("instance_id", instance_id.hex).execute()
83
- )
84
- return response.data
85
-
86
-
87
- def _select_storage(
88
- ssettings: StorageSettings, update_uid: bool, client: Client
89
- ) -> bool:
90
- root = ssettings.root_as_str
91
- response = client.table("storage").select("*").eq("root", root).execute()
92
- if not response.data:
93
- return False
94
- else:
95
- existing_storage = response.data[0]
96
- if existing_storage["instance_id"] is not None:
97
- if ssettings._instance_id is not None:
98
- # consider storage settings that are meant to be managed by an instance
99
- if UUID(existing_storage["instance_id"]) != ssettings._instance_id:
100
- # everything is alright if the instance_id matches
101
- # we're probably just switching storage locations
102
- # below can be turned into a warning and then delegate the error
103
- # to a unique constraint violation below
104
- raise ValueError(
105
- f"Storage root {root} is already managed by instance {existing_storage['instance_id']}."
106
- )
107
- else:
108
- # if the request is agnostic of the instance, that's alright,
109
- # we'll update the instance_id with what's stored in the hub
110
- ssettings._instance_id = UUID(existing_storage["instance_id"])
111
- ssettings._uuid_ = UUID(existing_storage["id"])
112
- if update_uid:
113
- ssettings._uid = existing_storage["lnid"]
114
- else:
115
- assert ssettings._uid == existing_storage["lnid"]
116
- return True
117
-
118
-
119
- def init_storage(
120
- ssettings: StorageSettings,
121
- auto_populate_instance: bool = True,
122
- ) -> Literal["hub-record-retireved", "hub-record-created"]:
123
- if settings.user.handle != "anonymous":
124
- return call_with_fallback_auth(
125
- _init_storage,
126
- ssettings=ssettings,
127
- auto_populate_instance=auto_populate_instance,
128
- )
129
- else:
130
- storage_exists = call_with_fallback(
131
- _select_storage, ssettings=ssettings, update_uid=True
132
- )
133
- if storage_exists:
134
- return "hub-record-retireved"
135
- else:
136
- raise ValueError("Log in to create a storage location on the hub.")
137
-
138
-
139
- def _init_storage(
140
- ssettings: StorageSettings, auto_populate_instance: bool, client: Client
141
- ) -> Literal["hub-record-retireved", "hub-record-created"]:
142
- from lamindb_setup import settings
143
-
144
- # storage roots are always stored without the trailing slash in the SQL
145
- # database
146
- root = ssettings.root_as_str
147
- if _select_storage(ssettings, update_uid=True, client=client):
148
- return "hub-record-retireved"
149
- if ssettings.type_is_cloud:
150
- id = uuid.uuid5(uuid.NAMESPACE_URL, root)
151
- else:
152
- id = uuid.uuid4()
153
- if (
154
- ssettings._instance_id is None
155
- and settings._instance_exists
156
- and auto_populate_instance
157
- ):
158
- logger.warning(
159
- f"will manage storage location {ssettings.root_as_str} with instance {settings.instance.slug}"
160
- )
161
- ssettings._instance_id = settings.instance._id
162
- instance_id_hex = (
163
- ssettings._instance_id.hex
164
- if (ssettings._instance_id is not None and auto_populate_instance)
165
- else None
166
- )
167
- fields = {
168
- "id": id.hex,
169
- "lnid": ssettings.uid,
170
- "created_by": settings.user._uuid.hex, # type: ignore
171
- "root": root,
172
- "region": ssettings.region,
173
- "type": ssettings.type,
174
- "instance_id": instance_id_hex,
175
- # the empty string is important as we want the user flow to be through LaminHub
176
- # if this errors with unique constraint error, the user has to update
177
- # the description in LaminHub
178
- "description": "",
179
- }
180
- # TODO: add error message for violated unique constraint
181
- # on root & description
182
- client.table("storage").upsert(fields).execute()
183
- ssettings._uuid_ = id
184
- return "hub-record-created"
185
-
186
-
187
- def delete_instance(identifier: UUID | str, require_empty: bool = True) -> str | None:
188
- return call_with_fallback_auth(
189
- _delete_instance, identifier=identifier, require_empty=require_empty
190
- )
191
-
192
-
193
- def _delete_instance(
194
- identifier: UUID | str, require_empty: bool, client: Client
195
- ) -> str | None:
196
- """Fully delete an instance in the hub.
197
-
198
- This function deletes the relevant instance and storage records in the hub,
199
- conditional on the emptiness of the storage location.
200
- """
201
- from ._settings_storage import mark_storage_root
202
- from .upath import check_storage_is_empty, create_path
203
-
204
- # the "/" check is for backward compatibility with the old identifier format
205
- if isinstance(identifier, UUID) or "/" not in identifier:
206
- if isinstance(identifier, UUID):
207
- instance_id_str = identifier.hex
208
- else:
209
- instance_id_str = identifier
210
- instance_with_storage = select_instance_by_id_with_storage(
211
- instance_id=instance_id_str, client=client
212
- )
213
- else:
214
- owner, name = identifier.split("/")
215
- instance_with_storage = select_instance_by_owner_name(
216
- owner=owner, name=name, client=client
217
- )
218
-
219
- if instance_with_storage is None:
220
- logger.important("not deleting instance from hub as instance not found there")
221
- return "instance-not-found"
222
-
223
- storage_records = _get_storage_records_for_instance(
224
- UUID(instance_with_storage["id"]),
225
- client,
226
- )
227
- if require_empty:
228
- for storage_record in storage_records:
229
- account_for_sqlite_file = (
230
- instance_with_storage["db_scheme"] is None
231
- and instance_with_storage["storage"]["root"] == storage_record["root"]
232
- )
233
- root_string = storage_record["root"]
234
- # gate storage and instance deletion on empty storage location for
235
- if client.auth.get_session() is not None:
236
- access_token = client.auth.get_session().access_token
237
- else:
238
- access_token = None
239
- root_path = create_path(root_string, access_token)
240
- mark_storage_root(
241
- root_path,
242
- storage_record["lnid"], # type: ignore
243
- ) # address permission error
244
- check_storage_is_empty(
245
- root_path, account_for_sqlite_file=account_for_sqlite_file
246
- )
247
- _update_instance_record(instance_with_storage["id"], {"storage_id": None}, client)
248
- # first delete the storage records because we will turn instance_id on
249
- # storage into a FK soon
250
- for storage_record in storage_records:
251
- _delete_storage_record(UUID(storage_record["id"]), client) # type: ignore
252
- _delete_instance_record(UUID(instance_with_storage["id"]), client)
253
- return None
254
-
255
-
256
- def delete_instance_record(
257
- instance_id: UUID,
258
- ) -> None:
259
- return call_with_fallback_auth(
260
- _delete_instance_record,
261
- instance_id=instance_id,
262
- )
263
-
264
-
265
- def init_instance(isettings: InstanceSettings) -> None:
266
- return call_with_fallback_auth(_init_instance, isettings=isettings)
267
-
268
-
269
- def _init_instance(isettings: InstanceSettings, client: Client) -> None:
270
- from ._settings import settings
271
-
272
- try:
273
- lamindb_version = metadata.version("lamindb")
274
- except metadata.PackageNotFoundError:
275
- lamindb_version = None
276
- fields = {
277
- "id": isettings._id.hex,
278
- "account_id": settings.user._uuid.hex, # type: ignore
279
- "name": isettings.name,
280
- "lnid": isettings.uid,
281
- "schema_str": isettings._schema_str,
282
- "lamindb_version": lamindb_version,
283
- "public": False,
284
- }
285
- if isettings.dialect != "sqlite":
286
- db_dsn = LaminDsnModel(db=isettings.db)
287
- db_fields = {
288
- "db_scheme": db_dsn.db.scheme,
289
- "db_host": db_dsn.db.host,
290
- "db_port": db_dsn.db.port,
291
- "db_database": db_dsn.db.database,
292
- }
293
- fields.update(db_fields)
294
- # I'd like the following to be an upsert, but this seems to violate RLS
295
- # Similarly, if we don't specify `returning="minimal"`, we'll violate RLS
296
- # we could make this idempotent by catching an error, but this seems dangerous
297
- # as then init_instance is no longer idempotent
298
- try:
299
- client.table("instance").insert(fields, returning="minimal").execute()
300
- except APIError:
301
- logger.warning(
302
- f"instance already existed at: https://lamin.ai/{isettings.owner}/{isettings.name}"
303
- )
304
- return None
305
- client.table("storage").update(
306
- {"instance_id": isettings._id.hex, "is_default": True}
307
- ).eq("id", isettings.storage._uuid.hex).execute() # type: ignore
308
- logger.important(f"go to: https://lamin.ai/{isettings.owner}/{isettings.name}")
309
-
310
-
311
- def connect_instance(
312
- *,
313
- owner: str, # account_handle
314
- name: str, # instance_name
315
- ) -> tuple[dict, dict] | str:
316
- from ._settings import settings
317
-
318
- if settings.user.handle != "anonymous":
319
- return call_with_fallback_auth(_connect_instance, owner=owner, name=name)
320
- else:
321
- return call_with_fallback(_connect_instance, owner=owner, name=name)
322
-
323
-
324
- def _connect_instance(
325
- *,
326
- owner: str, # account_handle
327
- name: str, # instance_name
328
- client: Client,
329
- ) -> tuple[dict, dict] | str:
330
- instance_account_storage = select_instance_by_owner_name(owner, name, client)
331
- if instance_account_storage is None:
332
- # try the via single requests, will take more time
333
- account = select_account_by_handle(owner, client)
334
- if account is None:
335
- return "account-not-exists"
336
- instance = select_instance_by_name(account["id"], name, client)
337
- if instance is None:
338
- return "instance-not-found"
339
- # get default storage
340
- storage = select_default_storage_by_instance_id(instance["id"], client)
341
- if storage is None:
342
- return "default-storage-does-not-exist-on-hub"
343
- else:
344
- account = instance_account_storage.pop("account")
345
- storage = instance_account_storage.pop("storage")
346
- instance = instance_account_storage
347
- # check if is postgres instance
348
- # this used to be a check for `instance["db"] is not None` in earlier versions
349
- # removed this on 2022-10-25 and can remove from the hub probably for lamindb 1.0
350
- if instance["db_scheme"] is not None:
351
- db_user = select_db_user_by_instance(instance["id"], client)
352
- if db_user is None:
353
- name, password = "none", "none"
354
- else:
355
- name, password = db_user["db_user_name"], db_user["db_user_password"]
356
- # construct dsn from instance and db_account fields
357
- db_dsn = LaminDsn.build(
358
- scheme=instance["db_scheme"],
359
- user=name,
360
- password=password,
361
- host=instance["db_host"],
362
- port=instance["db_port"],
363
- database=instance["db_database"],
364
- )
365
- instance["db"] = db_dsn
366
- return instance, storage # type: ignore
367
-
368
-
369
- def _connect_instance_new(
370
- owner: str, # account_handle
371
- name: str, # instance_name
372
- client: Client,
373
- ) -> tuple[dict, dict] | str:
374
- response = client.functions.invoke(
375
- "get-instance-settings", invoke_options={"body": {"owner": owner, "name": name}}
376
- )
377
- # no instance found, check why is that
378
- if response == b"{}":
379
- # try the via single requests, will take more time
380
- account = select_account_by_handle(owner, client)
381
- if account is None:
382
- return "account-not-exists"
383
- instance = select_instance_by_name(account["id"], name, client)
384
- if instance is None:
385
- return "instance-not-found"
386
- # get default storage
387
- storage = select_default_storage_by_instance_id(instance["id"], client)
388
- if storage is None:
389
- return "default-storage-does-not-exist-on-hub"
390
- logger.warning(
391
- "Could not find instance via API, but found directly querying hub."
392
- )
393
- else:
394
- instance = json.loads(response)
395
- storage = instance.pop("storage")
396
-
397
- if instance["db_scheme"] is not None:
398
- db_user_name, db_user_password = None, None
399
- if "db_user_name" in instance and "db_user_password" in instance:
400
- db_user_name, db_user_password = (
401
- instance["db_user_name"],
402
- instance["db_user_password"],
403
- )
404
- else:
405
- db_user = select_db_user_by_instance(instance["id"], client)
406
- if db_user is not None:
407
- db_user_name, db_user_password = (
408
- db_user["db_user_name"],
409
- db_user["db_user_password"],
410
- )
411
- db_dsn = LaminDsn.build(
412
- scheme=instance["db_scheme"],
413
- user=db_user_name if db_user_name is not None else "none",
414
- password=db_user_password if db_user_password is not None else "none",
415
- host=instance["db_host"],
416
- port=instance["db_port"],
417
- database=instance["db_database"],
418
- )
419
- instance["db"] = db_dsn
420
- return instance, storage # type: ignore
421
-
422
-
423
- def connect_instance_new(
424
- *,
425
- owner: str, # account_handle
426
- name: str, # instance_name
427
- ) -> tuple[dict, dict] | str:
428
- from ._settings import settings
429
-
430
- if settings.user.handle != "anonymous":
431
- return call_with_fallback_auth(_connect_instance_new, owner=owner, name=name)
432
- else:
433
- return call_with_fallback(_connect_instance_new, owner=owner, name=name)
434
-
435
-
436
- def access_aws(storage_root: str, access_token: str | None = None) -> dict[str, dict]:
437
- from ._settings import settings
438
-
439
- if settings.user.handle != "anonymous" or access_token is not None:
440
- storage_root_info = call_with_fallback_auth(
441
- _access_aws, storage_root=storage_root, access_token=access_token
442
- )
443
- return storage_root_info
444
- else:
445
- raise RuntimeError("Can only get access to AWS if authenticated.")
446
-
447
-
448
- def _access_aws(*, storage_root: str, client: Client) -> dict[str, dict]:
449
- import lamindb_setup
450
-
451
- storage_root_info: dict[str, dict] = {"credentials": {}, "accessibility": {}}
452
- response = client.functions.invoke(
453
- "access-aws",
454
- invoke_options={"body": {"storage_root": storage_root}},
455
- )
456
- if response is not None and response != b"{}":
457
- data = json.loads(response)
458
-
459
- loaded_credentials = data["Credentials"]
460
- loaded_accessibility = data["StorageAccessibility"]
461
-
462
- credentials = storage_root_info["credentials"]
463
- credentials["key"] = loaded_credentials["AccessKeyId"]
464
- credentials["secret"] = loaded_credentials["SecretAccessKey"]
465
- credentials["token"] = loaded_credentials["SessionToken"]
466
-
467
- accessibility = storage_root_info["accessibility"]
468
- accessibility["storage_root"] = loaded_accessibility["storageRoot"]
469
- accessibility["is_managed"] = loaded_accessibility["isManaged"]
470
- return storage_root_info
471
-
472
-
473
- def get_lamin_site_base_url():
474
- if "LAMIN_ENV" in os.environ:
475
- if os.environ["LAMIN_ENV"] == "local":
476
- return "http://localhost:3000"
477
- elif os.environ["LAMIN_ENV"] == "staging":
478
- return "https://staging.lamin.ai"
479
- return "https://lamin.ai"
480
-
481
-
482
- def sign_up_local_hub(email) -> str | tuple[str, str, str]:
483
- # raises gotrue.errors.AuthApiError: User already registered
484
- password = base62(40) # generate new password
485
- sign_up_kwargs = {"email": email, "password": password}
486
- client = connect_hub()
487
- auth_response = client.auth.sign_up(sign_up_kwargs)
488
- client.auth.sign_out()
489
- return (
490
- password,
491
- auth_response.session.user.id,
492
- auth_response.session.access_token,
493
- )
494
-
495
-
496
- def _sign_in_hub(email: str, password: str, handle: str | None, client: Client):
497
- auth = client.auth.sign_in_with_password(
498
- {
499
- "email": email,
500
- "password": password,
501
- }
502
- )
503
- data = client.table("account").select("*").eq("id", auth.user.id).execute().data
504
- if data: # sync data from hub to local cache in case it was updated on the hub
505
- user = data[0]
506
- user_uuid = UUID(user["id"])
507
- user_id = user["lnid"]
508
- user_handle = user["handle"]
509
- user_name = user["name"]
510
- if handle is not None and handle != user_handle:
511
- logger.warning(
512
- f"using account handle {user_handle} (cached handle was {handle})"
513
- )
514
- else: # user did not complete signup as usermeta has no matching row
515
- logger.error("complete signup on your account page.")
516
- return "complete-signup"
517
- return (
518
- user_uuid,
519
- user_id,
520
- user_handle,
521
- user_name,
522
- auth.session.access_token,
523
- )
524
-
525
-
526
- def sign_in_hub(
527
- email: str, password: str, handle: str | None = None
528
- ) -> Exception | str | tuple[UUID, str, str, str, str]:
529
- try:
530
- result = call_with_fallback(
531
- _sign_in_hub, email=email, password=password, handle=handle
532
- )
533
- except Exception as exception: # this is bad, but I don't find APIError right now
534
- logger.error(exception)
535
- logger.error(
536
- "Could not login. Probably your password is wrong or you didn't complete"
537
- " signup."
538
- )
539
- return exception
540
- return result
541
-
542
-
543
- def _sign_in_hub_api_key(api_key: str, client: Client):
544
- response = client.functions.invoke(
545
- "create-jwt",
546
- invoke_options={"body": {"api_key": api_key}},
547
- )
548
- access_token = json.loads(response)["accessToken"]
549
- # probably need more info here to avoid additional queries
550
- # like handle, uid etc
551
- account_id = client.auth._decode_jwt(access_token)["sub"]
552
- client.postgrest.auth(access_token)
553
- # normally public.account.id is equal to auth.user.id
554
- data = client.table("account").select("*").eq("id", account_id).execute().data
555
- if data:
556
- user = data[0]
557
- user_uuid = UUID(user["id"])
558
- user_id = user["lnid"]
559
- user_handle = user["handle"]
560
- user_name = user["name"]
561
- else:
562
- logger.error("Invalid API key.")
563
- return "invalid-api-key"
564
- return (user_uuid, user_id, user_handle, user_name, access_token)
565
-
566
-
567
- def sign_in_hub_api_key(
568
- api_key: str,
569
- ) -> Exception | str | tuple[UUID, str, str, str, str]:
570
- try:
571
- result = call_with_fallback(_sign_in_hub_api_key, api_key=api_key)
572
- except Exception as exception:
573
- logger.error(exception)
574
- logger.error("Could not login. Probably your API key is wrong.")
575
- return exception
576
- return result
577
-
578
-
579
- def _create_api_key(body: dict, client: Client) -> str:
580
- response = client.functions.invoke(
581
- "create-api-key",
582
- invoke_options={"body": body},
583
- )
584
- api_key = json.loads(response)["apiKey"]
585
- return api_key
586
-
587
-
588
- def create_api_key(body: dict) -> str:
589
- api_key = call_with_fallback_auth(_create_api_key, body=body)
590
- return api_key
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import os
5
+ import uuid
6
+ from importlib import metadata
7
+ from typing import TYPE_CHECKING, Literal
8
+ from uuid import UUID
9
+
10
+ from lamin_utils import logger
11
+ from postgrest.exceptions import APIError
12
+
13
+ from lamindb_setup._migrate import check_whether_migrations_in_sync
14
+
15
+ from ._hub_client import (
16
+ call_with_fallback,
17
+ call_with_fallback_auth,
18
+ connect_hub,
19
+ )
20
+ from ._hub_crud import (
21
+ _delete_instance_record,
22
+ select_account_by_handle,
23
+ select_db_user_by_instance,
24
+ select_default_storage_by_instance_id,
25
+ select_instance_by_id_with_storage,
26
+ select_instance_by_name,
27
+ select_instance_by_owner_name,
28
+ )
29
+ from ._hub_crud import update_instance as _update_instance_record
30
+ from ._hub_utils import (
31
+ LaminDsn,
32
+ LaminDsnModel,
33
+ )
34
+ from ._settings import settings
35
+ from ._settings_storage import StorageSettings, base62
36
+
37
+ if TYPE_CHECKING:
38
+ from supabase import Client # type: ignore
39
+
40
+ from ._settings_instance import InstanceSettings
41
+
42
+
43
+ def delete_storage_record(storage_uuid: UUID, access_token: str | None = None) -> None:
44
+ return call_with_fallback_auth(
45
+ _delete_storage_record, storage_uuid=storage_uuid, access_token=access_token
46
+ )
47
+
48
+
49
+ def _delete_storage_record(storage_uuid: UUID, client: Client) -> None:
50
+ if storage_uuid is None:
51
+ return None
52
+ response = client.table("storage").delete().eq("id", storage_uuid.hex).execute()
53
+ if response.data:
54
+ logger.important(f"deleted storage record on hub {storage_uuid.hex}")
55
+ else:
56
+ raise PermissionError(
57
+ f"Deleting of storage with {storage_uuid.hex} was not successful. Probably, you"
58
+ " don't have sufficient permissions."
59
+ )
60
+
61
+
62
+ def update_instance_record(instance_uuid: UUID, fields: dict) -> None:
63
+ return call_with_fallback_auth(
64
+ _update_instance_record, instance_id=instance_uuid.hex, instance_fields=fields
65
+ )
66
+
67
+
68
+ def get_storage_records_for_instance(
69
+ instance_id: UUID,
70
+ ) -> list[dict[str, str | int]]:
71
+ return call_with_fallback_auth(
72
+ _get_storage_records_for_instance,
73
+ instance_id=instance_id,
74
+ )
75
+
76
+
77
+ def _get_storage_records_for_instance(
78
+ instance_id: UUID, client: Client
79
+ ) -> list[dict[str, str | int]]:
80
+ response = (
81
+ client.table("storage").select("*").eq("instance_id", instance_id.hex).execute()
82
+ )
83
+ return response.data
84
+
85
+
86
+ def _select_storage(
87
+ ssettings: StorageSettings, update_uid: bool, client: Client
88
+ ) -> bool:
89
+ root = ssettings.root_as_str
90
+ response = client.table("storage").select("*").eq("root", root).execute()
91
+ if not response.data:
92
+ return False
93
+ else:
94
+ existing_storage = response.data[0]
95
+ if existing_storage["instance_id"] is not None:
96
+ if ssettings._instance_id is not None:
97
+ # consider storage settings that are meant to be managed by an instance
98
+ if UUID(existing_storage["instance_id"]) != ssettings._instance_id:
99
+ # everything is alright if the instance_id matches
100
+ # we're probably just switching storage locations
101
+ # below can be turned into a warning and then delegate the error
102
+ # to a unique constraint violation below
103
+ raise ValueError(
104
+ f"Storage root {root} is already managed by instance {existing_storage['instance_id']}."
105
+ )
106
+ else:
107
+ # if the request is agnostic of the instance, that's alright,
108
+ # we'll update the instance_id with what's stored in the hub
109
+ ssettings._instance_id = UUID(existing_storage["instance_id"])
110
+ ssettings._uuid_ = UUID(existing_storage["id"])
111
+ if update_uid:
112
+ ssettings._uid = existing_storage["lnid"]
113
+ else:
114
+ assert ssettings._uid == existing_storage["lnid"]
115
+ return True
116
+
117
+
118
+ def init_storage(
119
+ ssettings: StorageSettings,
120
+ auto_populate_instance: bool = True,
121
+ created_by: UUID | None = None,
122
+ access_token: str | None = None,
123
+ ) -> Literal["hub-record-retireved", "hub-record-created"]:
124
+ if settings.user.handle != "anonymous" or access_token is not None:
125
+ return call_with_fallback_auth(
126
+ _init_storage,
127
+ ssettings=ssettings,
128
+ auto_populate_instance=auto_populate_instance,
129
+ created_by=created_by,
130
+ access_token=access_token,
131
+ )
132
+ else:
133
+ storage_exists = call_with_fallback(
134
+ _select_storage, ssettings=ssettings, update_uid=True
135
+ )
136
+ if storage_exists:
137
+ return "hub-record-retireved"
138
+ else:
139
+ raise ValueError("Log in to create a storage location on the hub.")
140
+
141
+
142
+ def _init_storage(
143
+ client: Client,
144
+ ssettings: StorageSettings,
145
+ auto_populate_instance: bool,
146
+ created_by: UUID | None = None,
147
+ ) -> Literal["hub-record-retireved", "hub-record-created"]:
148
+ from lamindb_setup import settings
149
+
150
+ created_by = settings.user._uuid if created_by is None else created_by
151
+ # storage roots are always stored without the trailing slash in the SQL
152
+ # database
153
+ root = ssettings.root_as_str
154
+ if _select_storage(ssettings, update_uid=True, client=client):
155
+ return "hub-record-retireved"
156
+ if ssettings.type_is_cloud:
157
+ id = uuid.uuid5(uuid.NAMESPACE_URL, root)
158
+ else:
159
+ id = uuid.uuid4()
160
+ if (
161
+ ssettings._instance_id is None
162
+ and settings._instance_exists
163
+ and auto_populate_instance
164
+ ):
165
+ logger.warning(
166
+ f"will manage storage location {ssettings.root_as_str} with instance {settings.instance.slug}"
167
+ )
168
+ ssettings._instance_id = settings.instance._id
169
+ instance_id_hex = (
170
+ ssettings._instance_id.hex
171
+ if (ssettings._instance_id is not None and auto_populate_instance)
172
+ else None
173
+ )
174
+ fields = {
175
+ "id": id.hex,
176
+ "lnid": ssettings.uid,
177
+ "created_by": created_by.hex, # type: ignore
178
+ "root": root,
179
+ "region": ssettings.region,
180
+ "type": ssettings.type,
181
+ "instance_id": instance_id_hex,
182
+ # the empty string is important as we want the user flow to be through LaminHub
183
+ # if this errors with unique constraint error, the user has to update
184
+ # the description in LaminHub
185
+ "description": "",
186
+ }
187
+ # TODO: add error message for violated unique constraint
188
+ # on root & description
189
+ client.table("storage").upsert(fields).execute()
190
+ ssettings._uuid_ = id
191
+ return "hub-record-created"
192
+
193
+
194
+ def delete_instance(identifier: UUID | str, require_empty: bool = True) -> str | None:
195
+ return call_with_fallback_auth(
196
+ _delete_instance, identifier=identifier, require_empty=require_empty
197
+ )
198
+
199
+
200
+ def _delete_instance(
201
+ identifier: UUID | str, require_empty: bool, client: Client
202
+ ) -> str | None:
203
+ """Fully delete an instance in the hub.
204
+
205
+ This function deletes the relevant instance and storage records in the hub,
206
+ conditional on the emptiness of the storage location.
207
+ """
208
+ from ._settings_storage import mark_storage_root
209
+ from .upath import check_storage_is_empty, create_path
210
+
211
+ # the "/" check is for backward compatibility with the old identifier format
212
+ if isinstance(identifier, UUID) or "/" not in identifier:
213
+ if isinstance(identifier, UUID):
214
+ instance_id_str = identifier.hex
215
+ else:
216
+ instance_id_str = identifier
217
+ instance_with_storage = select_instance_by_id_with_storage(
218
+ instance_id=instance_id_str, client=client
219
+ )
220
+ else:
221
+ owner, name = identifier.split("/")
222
+ instance_with_storage = select_instance_by_owner_name(
223
+ owner=owner, name=name, client=client
224
+ )
225
+
226
+ if instance_with_storage is None:
227
+ logger.important("not deleting instance from hub as instance not found there")
228
+ return "instance-not-found"
229
+
230
+ storage_records = _get_storage_records_for_instance(
231
+ UUID(instance_with_storage["id"]),
232
+ client,
233
+ )
234
+ if require_empty:
235
+ for storage_record in storage_records:
236
+ account_for_sqlite_file = (
237
+ instance_with_storage["db_scheme"] is None
238
+ and instance_with_storage["storage"]["root"] == storage_record["root"]
239
+ )
240
+ root_string = storage_record["root"]
241
+ # gate storage and instance deletion on empty storage location for
242
+ if client.auth.get_session() is not None:
243
+ access_token = client.auth.get_session().access_token
244
+ else:
245
+ access_token = None
246
+ root_path = create_path(root_string, access_token)
247
+ mark_storage_root(
248
+ root_path,
249
+ storage_record["lnid"], # type: ignore
250
+ ) # address permission error
251
+ check_storage_is_empty(
252
+ root_path, account_for_sqlite_file=account_for_sqlite_file
253
+ )
254
+ _update_instance_record(instance_with_storage["id"], {"storage_id": None}, client)
255
+ # first delete the storage records because we will turn instance_id on
256
+ # storage into a FK soon
257
+ for storage_record in storage_records:
258
+ _delete_storage_record(UUID(storage_record["id"]), client) # type: ignore
259
+ _delete_instance_record(UUID(instance_with_storage["id"]), client)
260
+ return None
261
+
262
+
263
+ def delete_instance_record(instance_id: UUID, access_token: str | None = None) -> None:
264
+ return call_with_fallback_auth(
265
+ _delete_instance_record, instance_id=instance_id, access_token=access_token
266
+ )
267
+
268
+
269
+ def init_instance(
270
+ isettings: InstanceSettings,
271
+ account_id: UUID | None = None,
272
+ access_token: str | None = None,
273
+ ) -> None:
274
+ return call_with_fallback_auth(
275
+ _init_instance,
276
+ isettings=isettings,
277
+ account_id=account_id,
278
+ access_token=access_token,
279
+ )
280
+
281
+
282
+ def _init_instance(
283
+ client: Client, isettings: InstanceSettings, account_id: UUID | None = None
284
+ ) -> None:
285
+ from ._settings import settings
286
+
287
+ account_id = settings.user._uuid if account_id is None else account_id
288
+
289
+ try:
290
+ lamindb_version = metadata.version("lamindb")
291
+ except metadata.PackageNotFoundError:
292
+ lamindb_version = None
293
+ fields = {
294
+ "id": isettings._id.hex,
295
+ "account_id": account_id.hex, # type: ignore
296
+ "name": isettings.name,
297
+ "lnid": isettings.uid,
298
+ "schema_str": isettings._schema_str,
299
+ "lamindb_version": lamindb_version,
300
+ "public": False,
301
+ }
302
+ if isettings.dialect != "sqlite":
303
+ db_dsn = LaminDsnModel(db=isettings.db)
304
+ db_fields = {
305
+ "db_scheme": db_dsn.db.scheme,
306
+ "db_host": db_dsn.db.host,
307
+ "db_port": db_dsn.db.port,
308
+ "db_database": db_dsn.db.database,
309
+ }
310
+ fields.update(db_fields)
311
+ # I'd like the following to be an upsert, but this seems to violate RLS
312
+ # Similarly, if we don't specify `returning="minimal"`, we'll violate RLS
313
+ # we could make this idempotent by catching an error, but this seems dangerous
314
+ # as then init_instance is no longer idempotent
315
+ try:
316
+ client.table("instance").insert(fields, returning="minimal").execute()
317
+ except APIError:
318
+ logger.warning(
319
+ f"instance already existed at: https://lamin.ai/{isettings.owner}/{isettings.name}"
320
+ )
321
+ return None
322
+ client.table("storage").update(
323
+ {"instance_id": isettings._id.hex, "is_default": True}
324
+ ).eq("id", isettings.storage._uuid.hex).execute() # type: ignore
325
+ logger.important(f"go to: https://lamin.ai/{isettings.owner}/{isettings.name}")
326
+
327
+
328
+ def connect_instance(
329
+ *,
330
+ owner: str, # account_handle
331
+ name: str, # instance_name
332
+ access_token: str | None = None,
333
+ ) -> tuple[dict, dict] | str:
334
+ from ._settings import settings
335
+
336
+ if settings.user.handle != "anonymous" or access_token is not None:
337
+ return call_with_fallback_auth(
338
+ _connect_instance, owner=owner, name=name, access_token=access_token
339
+ )
340
+ else:
341
+ return call_with_fallback(_connect_instance, owner=owner, name=name)
342
+
343
+
344
+ def _connect_instance(
345
+ *,
346
+ owner: str, # account_handle
347
+ name: str, # instance_name
348
+ client: Client,
349
+ ) -> tuple[dict, dict] | str:
350
+ instance_account_storage = select_instance_by_owner_name(owner, name, client)
351
+ if instance_account_storage is None:
352
+ # try the via single requests, will take more time
353
+ account = select_account_by_handle(owner, client)
354
+ if account is None:
355
+ return "account-not-exists"
356
+ instance = select_instance_by_name(account["id"], name, client)
357
+ if instance is None:
358
+ return "instance-not-found"
359
+ # get default storage
360
+ storage = select_default_storage_by_instance_id(instance["id"], client)
361
+ if storage is None:
362
+ return "default-storage-does-not-exist-on-hub"
363
+ else:
364
+ account = instance_account_storage.pop("account")
365
+ storage = instance_account_storage.pop("storage")
366
+ instance = instance_account_storage
367
+ # check if is postgres instance
368
+ # this used to be a check for `instance["db"] is not None` in earlier versions
369
+ # removed this on 2022-10-25 and can remove from the hub probably for lamindb 1.0
370
+ if instance["db_scheme"] is not None:
371
+ db_user = select_db_user_by_instance(instance["id"], client)
372
+ if db_user is None:
373
+ name, password = "none", "none"
374
+ else:
375
+ name, password = db_user["db_user_name"], db_user["db_user_password"]
376
+ # construct dsn from instance and db_account fields
377
+ db_dsn = LaminDsn.build(
378
+ scheme=instance["db_scheme"],
379
+ user=name,
380
+ password=password,
381
+ host=instance["db_host"],
382
+ port=instance["db_port"],
383
+ database=instance["db_database"],
384
+ )
385
+ instance["db"] = db_dsn
386
+ check_whether_migrations_in_sync(instance["lamindb_version"])
387
+ return instance, storage # type: ignore
388
+
389
+
390
+ def _connect_instance_new(
391
+ owner: str, # account_handle
392
+ name: str, # instance_name
393
+ client: Client,
394
+ ) -> tuple[dict, dict] | str:
395
+ response = client.functions.invoke(
396
+ "get-instance-settings-v1",
397
+ invoke_options={"body": {"owner": owner, "name": name}},
398
+ )
399
+ # no instance found, check why is that
400
+ if response == b"{}":
401
+ # try the via single requests, will take more time
402
+ account = select_account_by_handle(owner, client)
403
+ if account is None:
404
+ return "account-not-exists"
405
+ instance = select_instance_by_name(account["id"], name, client)
406
+ if instance is None:
407
+ return "instance-not-found"
408
+ # get default storage
409
+ storage = select_default_storage_by_instance_id(instance["id"], client)
410
+ if storage is None:
411
+ return "default-storage-does-not-exist-on-hub"
412
+ logger.warning(
413
+ "Could not find instance via API, but found directly querying hub."
414
+ )
415
+ else:
416
+ instance = json.loads(response)
417
+ storage = instance.pop("storage")
418
+
419
+ if instance["db_scheme"] is not None:
420
+ db_user_name, db_user_password = None, None
421
+ if "db_user_name" in instance and "db_user_password" in instance:
422
+ db_user_name, db_user_password = (
423
+ instance["db_user_name"],
424
+ instance["db_user_password"],
425
+ )
426
+ else:
427
+ db_user = select_db_user_by_instance(instance["id"], client)
428
+ if db_user is not None:
429
+ db_user_name, db_user_password = (
430
+ db_user["db_user_name"],
431
+ db_user["db_user_password"],
432
+ )
433
+ db_dsn = LaminDsn.build(
434
+ scheme=instance["db_scheme"],
435
+ user=db_user_name if db_user_name is not None else "none",
436
+ password=db_user_password if db_user_password is not None else "none",
437
+ host=instance["db_host"],
438
+ port=instance["db_port"],
439
+ database=instance["db_database"],
440
+ )
441
+ instance["db"] = db_dsn
442
+ return instance, storage # type: ignore
443
+
444
+
445
+ def connect_instance_new(
446
+ *,
447
+ owner: str, # account_handle
448
+ name: str, # instance_name
449
+ access_token: str | None = None,
450
+ ) -> tuple[dict, dict] | str:
451
+ from ._settings import settings
452
+
453
+ if settings.user.handle != "anonymous" or access_token is not None:
454
+ return call_with_fallback_auth(
455
+ _connect_instance_new, owner=owner, name=name, access_token=access_token
456
+ )
457
+ else:
458
+ return call_with_fallback(_connect_instance_new, owner=owner, name=name)
459
+
460
+
461
+ def access_aws(storage_root: str, access_token: str | None = None) -> dict[str, dict]:
462
+ from ._settings import settings
463
+
464
+ if settings.user.handle != "anonymous" or access_token is not None:
465
+ storage_root_info = call_with_fallback_auth(
466
+ _access_aws, storage_root=storage_root, access_token=access_token
467
+ )
468
+ return storage_root_info
469
+ else:
470
+ raise RuntimeError("Can only get access to AWS if authenticated.")
471
+
472
+
473
+ def _access_aws(*, storage_root: str, client: Client) -> dict[str, dict]:
474
+ import lamindb_setup
475
+
476
+ storage_root_info: dict[str, dict] = {"credentials": {}, "accessibility": {}}
477
+ response = client.functions.invoke(
478
+ "get-cloud-access-v1",
479
+ invoke_options={"body": {"storage_root": storage_root}},
480
+ )
481
+ if response is not None and response != b"{}":
482
+ data = json.loads(response)
483
+
484
+ loaded_credentials = data["Credentials"]
485
+ loaded_accessibility = data["StorageAccessibility"]
486
+
487
+ credentials = storage_root_info["credentials"]
488
+ credentials["key"] = loaded_credentials["AccessKeyId"]
489
+ credentials["secret"] = loaded_credentials["SecretAccessKey"]
490
+ credentials["token"] = loaded_credentials["SessionToken"]
491
+
492
+ accessibility = storage_root_info["accessibility"]
493
+ accessibility["storage_root"] = loaded_accessibility["storageRoot"]
494
+ accessibility["is_managed"] = loaded_accessibility["isManaged"]
495
+ return storage_root_info
496
+
497
+
498
+ def get_lamin_site_base_url():
499
+ if "LAMIN_ENV" in os.environ:
500
+ if os.environ["LAMIN_ENV"] == "local":
501
+ return "http://localhost:3000"
502
+ elif os.environ["LAMIN_ENV"] == "staging":
503
+ return "https://staging.lamin.ai"
504
+ return "https://lamin.ai"
505
+
506
+
507
+ def sign_up_local_hub(email) -> str | tuple[str, str, str]:
508
+ # raises gotrue.errors.AuthApiError: User already registered
509
+ password = base62(40) # generate new password
510
+ sign_up_kwargs = {"email": email, "password": password}
511
+ client = connect_hub()
512
+ auth_response = client.auth.sign_up(sign_up_kwargs)
513
+ client.auth.sign_out()
514
+ return (
515
+ password,
516
+ auth_response.session.user.id,
517
+ auth_response.session.access_token,
518
+ )
519
+
520
+
521
+ def _sign_in_hub(email: str, password: str, handle: str | None, client: Client):
522
+ auth = client.auth.sign_in_with_password(
523
+ {
524
+ "email": email,
525
+ "password": password,
526
+ }
527
+ )
528
+ data = client.table("account").select("*").eq("id", auth.user.id).execute().data
529
+ if data: # sync data from hub to local cache in case it was updated on the hub
530
+ user = data[0]
531
+ user_uuid = UUID(user["id"])
532
+ user_id = user["lnid"]
533
+ user_handle = user["handle"]
534
+ user_name = user["name"]
535
+ if handle is not None and handle != user_handle:
536
+ logger.warning(
537
+ f"using account handle {user_handle} (cached handle was {handle})"
538
+ )
539
+ else: # user did not complete signup as usermeta has no matching row
540
+ logger.error("complete signup on your account page.")
541
+ return "complete-signup"
542
+ return (
543
+ user_uuid,
544
+ user_id,
545
+ user_handle,
546
+ user_name,
547
+ auth.session.access_token,
548
+ )
549
+
550
+
551
+ def sign_in_hub(
552
+ email: str, password: str, handle: str | None = None
553
+ ) -> Exception | str | tuple[UUID, str, str, str, str]:
554
+ try:
555
+ result = call_with_fallback(
556
+ _sign_in_hub, email=email, password=password, handle=handle
557
+ )
558
+ except Exception as exception: # this is bad, but I don't find APIError right now
559
+ logger.error(exception)
560
+ logger.error(
561
+ "Could not login. Probably your password is wrong or you didn't complete"
562
+ " signup."
563
+ )
564
+ return exception
565
+ return result
566
+
567
+
568
+ def _sign_in_hub_api_key(api_key: str, client: Client):
569
+ response = client.functions.invoke(
570
+ "get-jwt-v1",
571
+ invoke_options={"body": {"api_key": api_key}},
572
+ )
573
+ access_token = json.loads(response)["accessToken"]
574
+ # probably need more info here to avoid additional queries
575
+ # like handle, uid etc
576
+ account_id = client.auth._decode_jwt(access_token)["sub"]
577
+ client.postgrest.auth(access_token)
578
+ # normally public.account.id is equal to auth.user.id
579
+ data = client.table("account").select("*").eq("id", account_id).execute().data
580
+ if data:
581
+ user = data[0]
582
+ user_uuid = UUID(user["id"])
583
+ user_id = user["lnid"]
584
+ user_handle = user["handle"]
585
+ user_name = user["name"]
586
+ else:
587
+ logger.error("Invalid API key.")
588
+ return "invalid-api-key"
589
+ return (user_uuid, user_id, user_handle, user_name, access_token)
590
+
591
+
592
+ def sign_in_hub_api_key(
593
+ api_key: str,
594
+ ) -> Exception | str | tuple[UUID, str, str, str, str]:
595
+ try:
596
+ result = call_with_fallback(_sign_in_hub_api_key, api_key=api_key)
597
+ except Exception as exception:
598
+ logger.error(exception)
599
+ logger.error("Could not login. Probably your API key is wrong.")
600
+ return exception
601
+ return result
602
+
603
+
604
+ def _create_api_key(body: dict, client: Client) -> str:
605
+ response = client.functions.invoke(
606
+ "create-api-key-v1",
607
+ invoke_options={"body": body},
608
+ )
609
+ api_key = json.loads(response)["apiKey"]
610
+ return api_key
611
+
612
+
613
+ def create_api_key(body: dict) -> str:
614
+ api_key = call_with_fallback_auth(_create_api_key, body=body)
615
+ return api_key