mirror of
https://github.com/mozilla-services/syncstorage-rs.git
synced 2026-04-21 05:21:05 +02:00
Some checks failed
Checks / python-checks (push) Has been cancelled
Checks / rust-checks (push) Has been cancelled
Glean probe-scraper / glean-probe-scraper (push) Has been cancelled
Build, Tag and Push Container Images to GAR Repository / check (push) Has been cancelled
Build, Tag and Push Container Images to GAR Repository / build-and-push-syncstorage-rs (push) Has been cancelled
Build, Tag and Push Container Images to GAR Repository / build-and-push-syncserver-postgres (push) Has been cancelled
Build, Tag and Push Container Images to GAR Repository / build-and-push-syncserver-postgres-enterprise-gar (push) Has been cancelled
Build, Tag and Push Container Images to GAR Repository / build-and-push-syncstorage-rs-spanner-python-utils (push) Has been cancelled
Build, Tag and Push Container Images to GAR Repository / build-and-push-syncserver-postgres-python-utils (push) Has been cancelled
Build, Tag and Push Container Images to GAR Repository / build-and-push-syncserver-postgres-python-utils-enterprise-gar (push) Has been cancelled
Build, Tag and Push Container Images to GAR Repository / build-and-push-syncserver-mysql (push) Has been cancelled
MySQL Build and Test / build-and-test-mysql (push) Has been cancelled
MySQL Build and Test / build-mysql-image (push) Has been cancelled
MySQL Build and Test / mysql-e2e-tests (push) Has been cancelled
Postgres Build and Test / build-and-test-postgres (push) Has been cancelled
Postgres Build and Test / build-postgres-image (push) Has been cancelled
Postgres Build and Test / postgres-e2e-tests (push) Has been cancelled
Publish Sync docs to pages / build-mdbook (push) Has been cancelled
Publish Sync docs to pages / build-openapi (push) Has been cancelled
Publish Sync docs to pages / combine-and-prepare (push) Has been cancelled
Publish Sync docs to pages / deploy (push) Has been cancelled
Spanner Build, Test, and Push / build-and-test-spanner (push) Has been cancelled
Spanner Build, Test, and Push / build-spanner-image (push) Has been cancelled
Spanner Build, Test, and Push / spanner-e2e-tests (push) Has been cancelled
chore: upgrade Python for all utils and refactor
69 lines
2.3 KiB
Python
69 lines
2.3 KiB
Python
# Utility Module for spanner CLI scripts
|
|
#
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
import os
|
|
from enum import auto, Enum
|
|
from urllib import parse
|
|
|
|
DSN_URL = "SYNC_SYNCSTORAGE__DATABASE_URL"
|
|
"""
|
|
Environment variable that stores Sync database URL
|
|
Depending on deployment, can be MySQL or Spanner.
|
|
In this context, should always point to spanner for these scripts.
|
|
"""
|
|
|
|
|
|
class Mode(Enum):
|
|
URL = auto()
|
|
ENV_VAR = auto()
|
|
|
|
|
|
def ids_from_env(dsn: str = DSN_URL, mode: Mode = Mode.ENV_VAR) -> tuple[str, str, str]:
|
|
"""Extract the instance, project, and database IDs from a Spanner DSN url.
|
|
|
|
It is defined as the `SYNC_SYNCSTORAGE__DATABASE_URL` environment variable.
|
|
The defined defaults are in webservices-infra/sync and can be configured there for
|
|
production runs.
|
|
|
|
`dsn` argument is set to default to the `DSN_URL` constant.
|
|
|
|
For reference, an example spanner url passed in is in the following format:
|
|
|
|
`spanner://projects/moz-fx-sync-prod-xxxx/instances/sync/databases/syncdb`
|
|
database_id = `syncdb`, instance_id = `sync`, project_id = `moz-fx-sync-prod-xxxx`
|
|
"""
|
|
# Change these to reflect your Spanner instance install
|
|
instance_id = None
|
|
database_id = None
|
|
project_id = None
|
|
|
|
try:
|
|
if mode == Mode.ENV_VAR:
|
|
url: str | None = os.environ.get(dsn)
|
|
if not url:
|
|
raise Exception(f"No env var found for provided DSN: {dsn}")
|
|
elif mode == Mode.URL:
|
|
url = dsn
|
|
if not url:
|
|
raise Exception(f"No valid url found: {url}")
|
|
parsed_url = parse.urlparse(url)
|
|
if parsed_url.scheme == "spanner":
|
|
path = parsed_url.path.split("/")
|
|
instance_id = path[-3]
|
|
project_id = path[-5]
|
|
database_id = path[-1]
|
|
except Exception as e:
|
|
print(f"Exception parsing url: {e}")
|
|
# Fallbacks if not set
|
|
if not instance_id:
|
|
instance_id = os.environ.get("INSTANCE_ID", "spanner-test")
|
|
if not database_id:
|
|
database_id = os.environ.get("DATABASE_ID", "sync_stage")
|
|
if not project_id:
|
|
project_id = os.environ.get("GOOGLE_CLOUD_PROJECT", "test-project")
|
|
|
|
return (instance_id, database_id, project_id)
|