Skip to content

Commit

Permalink
Fix Python client not respecting CORTEX_CLI_CONFIG_DIR environment va…
Browse files Browse the repository at this point in the history
…riable for client-id.txt (#1953)
  • Loading branch information
jackmpcollins committed Mar 15, 2021
1 parent b3e2fc8 commit 78c253e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
13 changes: 4 additions & 9 deletions pkg/cortex/client/cortex/client.py
Expand Up @@ -33,13 +33,6 @@
from cortex import util


def cli_config_dir() -> Path:
cli_config_dir = os.environ.get("CORTEX_CLI_CONFIG_DIR", "")
if cli_config_dir == "":
return Path.home() / ".cortex"
return Path(cli_config_dir).expanduser().resolve()


class Client:
@sentry_wrapper
def __init__(self, env: dict):
Expand Down Expand Up @@ -129,7 +122,7 @@ def create_api(
if api_spec.get("name") is None:
raise ValueError("`api_spec` must have the `name` key set")

project_dir = cli_config_dir() / "deployments" / api_spec["name"]
project_dir = util.cli_config_dir() / "deployments" / api_spec["name"]

if project_dir.exists():
shutil.rmtree(str(project_dir))
Expand Down Expand Up @@ -367,7 +360,9 @@ def patch(self, api_spec: dict, force: bool = False) -> dict:
force: Override an already in-progress API update.
"""

cortex_yaml_file = cli_config_dir() / "deployments" / f"cortex-{str(uuid.uuid4())}.yaml"
cortex_yaml_file = (
util.cli_config_dir() / "deployments" / f"cortex-{str(uuid.uuid4())}.yaml"
)
with util.open_temporarily(cortex_yaml_file, "w") as f:
yaml.dump([api_spec], f)
args = ["patch", cortex_yaml_file, "--env", self.env_name, "-o", "json"]
Expand Down
3 changes: 2 additions & 1 deletion pkg/cortex/client/cortex/telemetry.py
Expand Up @@ -28,6 +28,7 @@
CORTEX_TELEMETRY_SENTRY_DSN,
CORTEX_TELEMETRY_SENTRY_ENVIRONMENT,
)
from cortex import util


def _sentry_client(
Expand Down Expand Up @@ -81,7 +82,7 @@ def _create_default_scope(optional_tags: dict = {}) -> sentry_sdk.Scope:
scope = sentry_sdk.Scope()

user_id = None
client_id_file_path = pathlib.Path.home() / ".cortex" / "client-id.txt"
client_id_file_path = util.cli_config_dir() / "client-id.txt"
if not client_id_file_path.is_file():
client_id_file_path.parent.mkdir(parents=True, exist_ok=True)
client_id_file_path.write_text(str(uuid4()))
Expand Down
7 changes: 7 additions & 0 deletions pkg/cortex/client/cortex/util.py
Expand Up @@ -18,6 +18,13 @@
import shutil


def cli_config_dir() -> Path:
cli_config_dir = os.environ.get("CORTEX_CLI_CONFIG_DIR", "")
if cli_config_dir == "":
return Path.home() / ".cortex"
return Path(cli_config_dir).expanduser().resolve()


@contextmanager
def open_temporarily(path, mode):
Path(path).parent.mkdir(parents=True, exist_ok=True)
Expand Down

0 comments on commit 78c253e

Please sign in to comment.